github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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/snap"
    28  	"github.com/snapcore/snapd/snap/snaptest"
    29  	"github.com/snapcore/snapd/snap/squashfs"
    30  
    31  	"github.com/snapcore/snapd/overlord/snapstate/backend"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  func TestBackend(t *testing.T) { TestingT(t) }
    36  
    37  func makeTestSnap(c *C, snapYamlContent string) string {
    38  	info := snaptest.MockInfo(c, snapYamlContent, nil)
    39  	var files [][]string
    40  	for _, app := range info.Apps {
    41  		// files is a list of (filename, content)
    42  		files = append(files, []string{app.Command, ""})
    43  	}
    44  	return snaptest.MakeTestSnapWithFiles(c, snapYamlContent, files)
    45  }
    46  
    47  type backendSuite struct {
    48  	testutil.BaseTest
    49  }
    50  
    51  var _ = Suite(&backendSuite{})
    52  
    53  func (s *backendSuite) SetUpTest(c *C) {
    54  	s.BaseTest.SetUpTest(c)
    55  	s.BaseTest.AddCleanup(snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}))
    56  }
    57  
    58  func (s *backendSuite) TearDownTest(c *C) {
    59  	s.BaseTest.TearDownTest(c)
    60  }
    61  
    62  func (s *backendSuite) TestOpenSnapFile(c *C) {
    63  	const yaml = `name: hello
    64  version: 1.0
    65  apps:
    66   bin:
    67     command: bin
    68  `
    69  
    70  	snapPath := makeTestSnap(c, yaml)
    71  	info, snapf, err := backend.OpenSnapFile(snapPath, nil)
    72  	c.Assert(err, IsNil)
    73  
    74  	c.Assert(snapf, FitsTypeOf, &squashfs.Snap{})
    75  	c.Check(info.InstanceName(), Equals, "hello")
    76  }
    77  
    78  func (s *backendSuite) TestOpenSnapFilebSideInfo(c *C) {
    79  	const yaml = `name: foo
    80  version: 0
    81  apps:
    82   bar:
    83    command: bin/bar
    84  plugs:
    85    plug:
    86  slots:
    87   slot:
    88  `
    89  
    90  	snapPath := makeTestSnap(c, yaml)
    91  	si := snap.SideInfo{RealName: "blessed", Revision: snap.R(42)}
    92  	info, _, err := backend.OpenSnapFile(snapPath, &si)
    93  	c.Assert(err, IsNil)
    94  
    95  	// check side info
    96  	c.Check(info.InstanceName(), Equals, "blessed")
    97  	c.Check(info.Revision, Equals, snap.R(42))
    98  
    99  	c.Check(info.SideInfo, DeepEquals, si)
   100  
   101  	// ensure that all leaf objects link back to the same snap.Info
   102  	// and not to some copy.
   103  	// (we had a bug around this)
   104  	c.Check(info.Apps["bar"].Snap, Equals, info)
   105  	c.Check(info.Plugs["plug"].Snap, Equals, info)
   106  	c.Check(info.Slots["slot"].Snap, Equals, info)
   107  
   108  }