gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/sd_control_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2021 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 builtin_test 21 22 import ( 23 "fmt" 24 25 . "gopkg.in/check.v1" 26 27 "gitee.com/mysnapcore/mysnapd/dirs" 28 "gitee.com/mysnapcore/mysnapd/interfaces" 29 "gitee.com/mysnapcore/mysnapd/interfaces/apparmor" 30 "gitee.com/mysnapcore/mysnapd/interfaces/builtin" 31 "gitee.com/mysnapcore/mysnapd/interfaces/udev" 32 "gitee.com/mysnapcore/mysnapd/snap" 33 "gitee.com/mysnapcore/mysnapd/testutil" 34 ) 35 36 type sdControlSuite struct { 37 iface interfaces.Interface 38 39 dualSDPlugInfo *snap.PlugInfo 40 dualSDPlug *interfaces.ConnectedPlug 41 noFlavorPlugInfo *snap.PlugInfo 42 noFlavorPlug *interfaces.ConnectedPlug 43 slotInfo *snap.SlotInfo 44 slot *interfaces.ConnectedSlot 45 } 46 47 var _ = Suite(&sdControlSuite{ 48 iface: builtin.MustInterface("sd-control"), 49 }) 50 51 const sdControlMockPlugSnapInfoYaml = ` 52 name: my-device 53 version: 1.0 54 plugs: 55 dual-sd: 56 interface: sd-control 57 flavor: dual-sd 58 no-flavor: 59 interface: sd-control 60 apps: 61 svc: 62 command: bin/foo.sh 63 plugs: 64 - dual-sd 65 - no-flavor 66 ` 67 68 const coreSDControlSlotYaml = `name: core 69 version: 0 70 type: os 71 slots: 72 sd-control: 73 ` 74 75 func (s *sdControlSuite) SetUpTest(c *C) { 76 s.dualSDPlug, s.dualSDPlugInfo = MockConnectedPlug(c, sdControlMockPlugSnapInfoYaml, nil, "dual-sd") 77 s.noFlavorPlug, s.noFlavorPlugInfo = MockConnectedPlug(c, sdControlMockPlugSnapInfoYaml, nil, "no-flavor") 78 s.slot, s.slotInfo = MockConnectedSlot(c, coreSDControlSlotYaml, nil, "sd-control") 79 80 } 81 82 func (s *sdControlSuite) TestName(c *C) { 83 c.Assert(s.iface.Name(), Equals, "sd-control") 84 } 85 86 func (s *sdControlSuite) TestSanitizePlug(c *C) { 87 c.Assert(interfaces.BeforePreparePlug(s.iface, s.noFlavorPlugInfo), IsNil) 88 c.Assert(interfaces.BeforePreparePlug(s.iface, s.dualSDPlugInfo), IsNil) 89 } 90 91 func (s *sdControlSuite) TestSanitizeSlot(c *C) { 92 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil) 93 } 94 95 func (s *sdControlSuite) TestApparmorConnectedPlugDualSD(c *C) { 96 spec := &apparmor.Specification{} 97 err := spec.AddConnectedPlug(s.iface, s.dualSDPlug, s.slot) 98 c.Assert(err, IsNil) 99 c.Assert(spec.SnippetForTag("snap.my-device.svc"), testutil.Contains, "/dev/DualSD rw,\n") 100 } 101 102 func (s *sdControlSuite) TestUDevConnectedPlugDualSD(c *C) { 103 spec := &udev.Specification{} 104 err := spec.AddConnectedPlug(s.iface, s.dualSDPlug, s.slot) 105 c.Assert(err, IsNil) 106 c.Assert(spec.Snippets(), HasLen, 2) 107 c.Assert(spec.Snippets(), testutil.Contains, `# sd-control 108 KERNEL=="DualSD", TAG+="snap_my-device_svc"`) 109 c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_my-device_svc", RUN+="%v/snap-device-helper $env{ACTION} snap_my-device_svc $devpath $major:$minor"`, dirs.DistroLibExecDir)) 110 } 111 112 func (s *sdControlSuite) TestUDevConnectedPlugNoFlavor(c *C) { 113 spec := &udev.Specification{} 114 err := spec.AddConnectedPlug(s.iface, s.noFlavorPlug, s.slot) 115 c.Assert(err, IsNil) 116 c.Assert(spec.Snippets(), HasLen, 0) 117 } 118 119 func (s *sdControlSuite) TestApparmorConnectedPlugNoFlavor(c *C) { 120 spec := &apparmor.Specification{} 121 err := spec.AddConnectedPlug(s.iface, s.noFlavorPlug, s.slot) 122 c.Assert(err, IsNil) 123 c.Assert(spec.Snippets(), HasLen, 0) 124 } 125 126 func (s *sdControlSuite) TestInterfaces(c *C) { 127 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 128 }