github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/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 "github.com/snapcore/snapd/dirs" 26 "github.com/snapcore/snapd/interfaces" 27 "github.com/snapcore/snapd/osutil" 28 "github.com/snapcore/snapd/snap" 29 "github.com/snapcore/snapd/snap/snaptest" 30 "github.com/snapcore/snapd/testutil" 31 "github.com/snapcore/snapd/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 func (s *BackendSuite) SetUpTest(c *C) { 46 // Isolate this test to a temporary directory 47 s.RootDir = c.MkDir() 48 dirs.SetRootDir(s.RootDir) 49 50 restore := osutil.MockMountInfo("") 51 s.AddCleanup(restore) 52 53 // Create a fresh repository for each test 54 s.Repo = interfaces.NewRepository() 55 s.Iface = &TestInterface{InterfaceName: "iface"} 56 err := s.Repo.AddInterface(s.Iface) 57 c.Assert(err, IsNil) 58 59 perf := timings.New(nil) 60 s.meas = perf.StartSpan("", "") 61 62 s.restoreSanitize = snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}) 63 } 64 65 func (s *BackendSuite) TearDownTest(c *C) { 66 dirs.SetRootDir("/") 67 s.restoreSanitize() 68 } 69 70 // Tests for Setup() and Remove() 71 const SambaYamlV1 = ` 72 name: samba 73 version: 1 74 developer: acme 75 apps: 76 smbd: 77 slots: 78 slot: 79 interface: iface 80 ` 81 const SambaYamlV1WithNmbd = ` 82 name: samba 83 version: 1 84 developer: acme 85 apps: 86 smbd: 87 nmbd: 88 slots: 89 slot: 90 interface: iface 91 ` 92 const SambaYamlV1NoSlot = ` 93 name: samba 94 version: 1 95 developer: acme 96 apps: 97 smbd: 98 ` 99 const SambaYamlV1WithNmbdNoSlot = ` 100 name: samba 101 version: 1 102 developer: acme 103 apps: 104 smbd: 105 nmbd: 106 ` 107 const SambaYamlV2 = ` 108 name: samba 109 version: 2 110 developer: acme 111 apps: 112 smbd: 113 slots: 114 slot: 115 interface: iface 116 ` 117 const SambaYamlWithHook = ` 118 name: samba 119 version: 0 120 apps: 121 smbd: 122 nmbd: 123 hooks: 124 configure: 125 plugs: [plug] 126 slots: 127 slot: 128 interface: iface 129 plugs: 130 plug: 131 interface: iface 132 ` 133 const HookYaml = ` 134 name: foo 135 version: 1 136 developer: acme 137 hooks: 138 configure: 139 plugs: 140 plug: 141 interface: iface 142 ` 143 const PlugNoAppsYaml = ` 144 name: foo 145 version: 1 146 developer: acme 147 plugs: 148 plug: 149 interface: iface 150 ` 151 const SlotNoAppsYaml = ` 152 name: foo 153 version: 1 154 developer: acme 155 slots: 156 slots: 157 interface: iface 158 ` 159 160 const SomeSnapYamlV1 = ` 161 name: some-snap 162 version: 1 163 developer: acme 164 apps: 165 someapp: 166 ` 167 168 // Support code for tests 169 170 // InstallSnap "installs" a snap from YAML. 171 func (s *BackendSuite) InstallSnap(c *C, opts interfaces.ConfinementOptions, instanceName, snapYaml string, revision int) *snap.Info { 172 snapInfo := snaptest.MockInfo(c, snapYaml, &snap.SideInfo{ 173 Revision: snap.R(revision), 174 }) 175 176 if instanceName != "" { 177 _, instanceKey := snap.SplitInstanceName(instanceName) 178 snapInfo.InstanceKey = instanceKey 179 c.Assert(snapInfo.InstanceName(), Equals, instanceName) 180 } 181 182 s.addPlugsSlots(c, snapInfo) 183 err := s.Backend.Setup(snapInfo, opts, s.Repo, s.meas) 184 c.Assert(err, IsNil) 185 return snapInfo 186 } 187 188 // UpdateSnap "updates" an existing snap from YAML. 189 func (s *BackendSuite) UpdateSnap(c *C, oldSnapInfo *snap.Info, opts interfaces.ConfinementOptions, snapYaml string, revision int) *snap.Info { 190 newSnapInfo := snaptest.MockInfo(c, snapYaml, &snap.SideInfo{ 191 Revision: snap.R(revision), 192 }) 193 c.Assert(newSnapInfo.InstanceName(), Equals, oldSnapInfo.InstanceName()) 194 s.removePlugsSlots(c, oldSnapInfo) 195 s.addPlugsSlots(c, newSnapInfo) 196 err := s.Backend.Setup(newSnapInfo, opts, s.Repo, s.meas) 197 c.Assert(err, IsNil) 198 return newSnapInfo 199 } 200 201 // RemoveSnap "removes" an "installed" snap. 202 func (s *BackendSuite) RemoveSnap(c *C, snapInfo *snap.Info) { 203 err := s.Backend.Remove(snapInfo.InstanceName()) 204 c.Assert(err, IsNil) 205 s.removePlugsSlots(c, snapInfo) 206 } 207 208 func (s *BackendSuite) addPlugsSlots(c *C, snapInfo *snap.Info) { 209 for _, plugInfo := range snapInfo.Plugs { 210 err := s.Repo.AddPlug(plugInfo) 211 c.Assert(err, IsNil) 212 } 213 for _, slotInfo := range snapInfo.Slots { 214 err := s.Repo.AddSlot(slotInfo) 215 c.Assert(err, IsNil) 216 } 217 } 218 219 func (s *BackendSuite) removePlugsSlots(c *C, snapInfo *snap.Info) { 220 for _, plug := range s.Repo.Plugs(snapInfo.InstanceName()) { 221 err := s.Repo.RemovePlug(plug.Snap.InstanceName(), plug.Name) 222 c.Assert(err, IsNil) 223 } 224 for _, slot := range s.Repo.Slots(snapInfo.InstanceName()) { 225 err := s.Repo.RemoveSlot(slot.Snap.InstanceName(), slot.Name) 226 c.Assert(err, IsNil) 227 } 228 }