github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/snapstate/backend/backend_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package backend_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/overlord/snapstate/backend"
    28  	"github.com/snapcore/snapd/snap"
    29  	"github.com/snapcore/snapd/snap/snaptest"
    30  	"github.com/snapcore/snapd/snap/squashfs"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  func TestBackend(t *testing.T) { TestingT(t) }
    35  
    36  func makeTestSnap(c *C, snapYamlContent string) string {
    37  	info := snaptest.MockInfo(c, snapYamlContent, nil)
    38  	var files [][]string
    39  	for _, app := range info.Apps {
    40  		// files is a list of (filename, content)
    41  		files = append(files, []string{app.Command, ""})
    42  	}
    43  	return snaptest.MakeTestSnapWithFiles(c, snapYamlContent, files)
    44  }
    45  
    46  type backendSuite struct {
    47  	testutil.BaseTest
    48  }
    49  
    50  var _ = Suite(&backendSuite{})
    51  
    52  func (s *backendSuite) SetUpTest(c *C) {
    53  	s.BaseTest.SetUpTest(c)
    54  	s.BaseTest.AddCleanup(snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}))
    55  }
    56  
    57  func (s *backendSuite) TearDownTest(c *C) {
    58  	s.BaseTest.TearDownTest(c)
    59  }
    60  
    61  func (s *backendSuite) TestOpenSnapFile(c *C) {
    62  	const yaml = `name: hello
    63  version: 1.0
    64  apps:
    65   bin:
    66     command: bin
    67  `
    68  
    69  	snapPath := makeTestSnap(c, yaml)
    70  	info, snapf, err := backend.OpenSnapFile(snapPath, nil)
    71  	c.Assert(err, IsNil)
    72  
    73  	c.Assert(snapf, FitsTypeOf, &squashfs.Snap{})
    74  	c.Check(info.InstanceName(), Equals, "hello")
    75  }
    76  
    77  func (s *backendSuite) TestOpenSnapFilebSideInfo(c *C) {
    78  	const yaml = `name: foo
    79  version: 0
    80  apps:
    81   bar:
    82    command: bin/bar
    83  plugs:
    84    plug:
    85  slots:
    86   slot:
    87  `
    88  
    89  	snapPath := makeTestSnap(c, yaml)
    90  	si := snap.SideInfo{RealName: "blessed", Revision: snap.R(42)}
    91  	info, _, err := backend.OpenSnapFile(snapPath, &si)
    92  	c.Assert(err, IsNil)
    93  
    94  	// check side info
    95  	c.Check(info.InstanceName(), Equals, "blessed")
    96  	c.Check(info.Revision, Equals, snap.R(42))
    97  
    98  	c.Check(info.SideInfo, DeepEquals, si)
    99  
   100  	// ensure that all leaf objects link back to the same snap.Info
   101  	// and not to some copy.
   102  	// (we had a bug around this)
   103  	c.Check(info.Apps["bar"].Snap, Equals, info)
   104  	c.Check(info.Plugs["plug"].Snap, Equals, info)
   105  	c.Check(info.Slots["slot"].Snap, Equals, info)
   106  
   107  }