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