gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/ifacetest/backendtest.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 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 ifacetest
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"gitee.com/mysnapcore/mysnapd/dirs"
    26  	"gitee.com/mysnapcore/mysnapd/interfaces"
    27  	"gitee.com/mysnapcore/mysnapd/osutil"
    28  	"gitee.com/mysnapcore/mysnapd/snap"
    29  	"gitee.com/mysnapcore/mysnapd/snap/snaptest"
    30  	"gitee.com/mysnapcore/mysnapd/testutil"
    31  	"gitee.com/mysnapcore/mysnapd/timings"
    32  )
    33  
    34  type BackendSuite struct {
    35  	Backend         interfaces.SecurityBackend
    36  	Repo            *interfaces.Repository
    37  	Iface           *TestInterface
    38  	RootDir         string
    39  	restoreSanitize func()
    40  
    41  	meas *timings.Span
    42  	testutil.BaseTest
    43  }
    44  
    45  // CoreSnapInfo is set in SetupSuite
    46  var DefaultInitializeOpts = &interfaces.SecurityBackendOptions{}
    47  
    48  func (s *BackendSuite) SetUpTest(c *C) {
    49  	coreSnapPlaceInfo := snap.MinimalPlaceInfo("core", snap.Revision{N: 123})
    50  	snInfo, ok := coreSnapPlaceInfo.(*snap.Info)
    51  	c.Assert(ok, Equals, true)
    52  	DefaultInitializeOpts.CoreSnapInfo = snInfo
    53  
    54  	snapdSnapPlaceInfo := snap.MinimalPlaceInfo("snapd", snap.Revision{N: 321})
    55  	snInfo, ok = snapdSnapPlaceInfo.(*snap.Info)
    56  	c.Assert(ok, Equals, true)
    57  	DefaultInitializeOpts.SnapdSnapInfo = snInfo
    58  
    59  	// Isolate this test to a temporary directory
    60  	s.RootDir = c.MkDir()
    61  	dirs.SetRootDir(s.RootDir)
    62  
    63  	restore := osutil.MockMountInfo("")
    64  	s.AddCleanup(restore)
    65  
    66  	// Create a fresh repository for each test
    67  	s.Repo = interfaces.NewRepository()
    68  	s.Iface = &TestInterface{InterfaceName: "iface"}
    69  	err := s.Repo.AddInterface(s.Iface)
    70  	c.Assert(err, IsNil)
    71  
    72  	perf := timings.New(nil)
    73  	s.meas = perf.StartSpan("", "")
    74  
    75  	s.restoreSanitize = snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {})
    76  }
    77  
    78  func (s *BackendSuite) TearDownTest(c *C) {
    79  	dirs.SetRootDir("/")
    80  	s.restoreSanitize()
    81  }
    82  
    83  // Tests for Setup() and Remove()
    84  const SambaYamlV1 = `
    85  name: samba
    86  version: 1
    87  developer: acme
    88  apps:
    89      smbd:
    90  slots:
    91      slot:
    92          interface: iface
    93  `
    94  const SambaYamlV1Core20Base = `
    95  name: samba
    96  base: core20
    97  version: 1
    98  developer: acme
    99  apps:
   100      smbd:
   101  slots:
   102      slot:
   103          interface: iface
   104  `
   105  const SambaYamlV1WithNmbd = `
   106  name: samba
   107  version: 1
   108  developer: acme
   109  apps:
   110      smbd:
   111      nmbd:
   112  slots:
   113      slot:
   114          interface: iface
   115  `
   116  const SambaYamlV1NoSlot = `
   117  name: samba
   118  version: 1
   119  developer: acme
   120  apps:
   121      smbd:
   122  `
   123  const SambaYamlV1WithNmbdNoSlot = `
   124  name: samba
   125  version: 1
   126  developer: acme
   127  apps:
   128      smbd:
   129      nmbd:
   130  `
   131  const SambaYamlV2 = `
   132  name: samba
   133  version: 2
   134  developer: acme
   135  apps:
   136      smbd:
   137  slots:
   138      slot:
   139          interface: iface
   140  `
   141  const SambaYamlWithHook = `
   142  name: samba
   143  version: 0
   144  apps:
   145      smbd:
   146      nmbd:
   147  hooks:
   148      configure:
   149          plugs: [plug]
   150  slots:
   151      slot:
   152          interface: iface
   153  plugs:
   154      plug:
   155          interface: iface
   156  `
   157  const HookYaml = `
   158  name: foo
   159  version: 1
   160  developer: acme
   161  hooks:
   162      configure:
   163  plugs:
   164      plug:
   165          interface: iface
   166  `
   167  const PlugNoAppsYaml = `
   168  name: foo
   169  version: 1
   170  developer: acme
   171  plugs:
   172      plug:
   173          interface: iface
   174  `
   175  const SlotNoAppsYaml = `
   176  name: foo
   177  version: 1
   178  developer: acme
   179  slots:
   180      slots:
   181          interface: iface
   182  `
   183  
   184  const SomeSnapYamlV1 = `
   185  name: some-snap
   186  version: 1
   187  developer: acme
   188  apps:
   189      someapp:
   190  `
   191  
   192  // Support code for tests
   193  
   194  // InstallSnap "installs" a snap from YAML.
   195  func (s *BackendSuite) InstallSnap(c *C, opts interfaces.ConfinementOptions, instanceName, snapYaml string, revision int) *snap.Info {
   196  	snapInfo := snaptest.MockInfo(c, snapYaml, &snap.SideInfo{
   197  		Revision: snap.R(revision),
   198  	})
   199  
   200  	if instanceName != "" {
   201  		_, instanceKey := snap.SplitInstanceName(instanceName)
   202  		snapInfo.InstanceKey = instanceKey
   203  		c.Assert(snapInfo.InstanceName(), Equals, instanceName)
   204  	}
   205  
   206  	s.addPlugsSlots(c, snapInfo)
   207  	err := s.Backend.Setup(snapInfo, opts, s.Repo, s.meas)
   208  	c.Assert(err, IsNil)
   209  	return snapInfo
   210  }
   211  
   212  // UpdateSnap "updates" an existing snap from YAML.
   213  func (s *BackendSuite) UpdateSnap(c *C, oldSnapInfo *snap.Info, opts interfaces.ConfinementOptions, snapYaml string, revision int) *snap.Info {
   214  	newSnapInfo := snaptest.MockInfo(c, snapYaml, &snap.SideInfo{
   215  		Revision: snap.R(revision),
   216  	})
   217  	c.Assert(newSnapInfo.InstanceName(), Equals, oldSnapInfo.InstanceName())
   218  	s.removePlugsSlots(c, oldSnapInfo)
   219  	s.addPlugsSlots(c, newSnapInfo)
   220  	err := s.Backend.Setup(newSnapInfo, opts, s.Repo, s.meas)
   221  	c.Assert(err, IsNil)
   222  	return newSnapInfo
   223  }
   224  
   225  // RemoveSnap "removes" an "installed" snap.
   226  func (s *BackendSuite) RemoveSnap(c *C, snapInfo *snap.Info) {
   227  	err := s.Backend.Remove(snapInfo.InstanceName())
   228  	c.Assert(err, IsNil)
   229  	s.removePlugsSlots(c, snapInfo)
   230  }
   231  
   232  func (s *BackendSuite) addPlugsSlots(c *C, snapInfo *snap.Info) {
   233  	for _, plugInfo := range snapInfo.Plugs {
   234  		err := s.Repo.AddPlug(plugInfo)
   235  		c.Assert(err, IsNil)
   236  	}
   237  	for _, slotInfo := range snapInfo.Slots {
   238  		err := s.Repo.AddSlot(slotInfo)
   239  		c.Assert(err, IsNil)
   240  	}
   241  }
   242  
   243  func (s *BackendSuite) removePlugsSlots(c *C, snapInfo *snap.Info) {
   244  	for _, plug := range s.Repo.Plugs(snapInfo.InstanceName()) {
   245  		err := s.Repo.RemovePlug(plug.Snap.InstanceName(), plug.Name)
   246  		c.Assert(err, IsNil)
   247  	}
   248  	for _, slot := range s.Repo.Slots(snapInfo.InstanceName()) {
   249  		err := s.Repo.RemoveSlot(slot.Snap.InstanceName(), slot.Name)
   250  		c.Assert(err, IsNil)
   251  	}
   252  }