gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/network_manager_observe_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 . "gopkg.in/check.v1" 24 25 "gitee.com/mysnapcore/mysnapd/interfaces" 26 "gitee.com/mysnapcore/mysnapd/interfaces/apparmor" 27 "gitee.com/mysnapcore/mysnapd/interfaces/builtin" 28 "gitee.com/mysnapcore/mysnapd/release" 29 "gitee.com/mysnapcore/mysnapd/snap" 30 "gitee.com/mysnapcore/mysnapd/testutil" 31 ) 32 33 type NetworkManagerObserveInterfaceSuite struct { 34 iface interfaces.Interface 35 plug *interfaces.ConnectedPlug 36 plugInfo *snap.PlugInfo 37 appSlot *interfaces.ConnectedSlot 38 appSlotInfo *snap.SlotInfo 39 coreSlot *interfaces.ConnectedSlot 40 coreSlotInfo *snap.SlotInfo 41 } 42 43 var _ = Suite(&NetworkManagerObserveInterfaceSuite{ 44 iface: builtin.MustInterface("network-manager-observe"), 45 }) 46 47 const networkManagerObserveConsumerYaml = `name: consumer 48 version: 0 49 apps: 50 app: 51 plugs: [network-manager-observe] 52 ` 53 54 const networkManagerObserveProducerYaml = `name: producer 55 version: 0 56 apps: 57 app: 58 slots: [network-manager-observe] 59 ` 60 61 const networkManagerObserveCoreYaml = `name: core 62 type: os 63 version: 0 64 slots: 65 network-manager-observe: 66 ` 67 68 func (s *NetworkManagerObserveInterfaceSuite) SetUpTest(c *C) { 69 s.plug, s.plugInfo = MockConnectedPlug(c, networkManagerObserveConsumerYaml, nil, "network-manager-observe") 70 s.appSlot, s.appSlotInfo = MockConnectedSlot(c, networkManagerObserveProducerYaml, nil, "network-manager-observe") 71 s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, networkManagerObserveCoreYaml, nil, "network-manager-observe") 72 } 73 74 func (s *NetworkManagerObserveInterfaceSuite) TestName(c *C) { 75 c.Assert(s.iface.Name(), Equals, "network-manager-observe") 76 } 77 78 func (s *NetworkManagerObserveInterfaceSuite) TestSanitizeSlot(c *C) { 79 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil) 80 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.appSlotInfo), IsNil) 81 // network-manager-observe slot can now be used on snap other than core. 82 slot := &snap.SlotInfo{ 83 Snap: &snap.Info{SuggestedName: "some-snap"}, 84 Name: "network-manager-observe", 85 Interface: "network-manager-observe", 86 } 87 c.Assert(interfaces.BeforePrepareSlot(s.iface, slot), IsNil) 88 } 89 90 func (s *NetworkManagerObserveInterfaceSuite) TestSanitizePlug(c *C) { 91 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 92 } 93 94 func (s *NetworkManagerObserveInterfaceSuite) TestAppArmorSpec(c *C) { 95 // on a core system with NM slot coming from a regular app snap. 96 restore := release.MockOnClassic(false) 97 defer restore() 98 99 // connected plug to app slot 100 spec := &apparmor.Specification{} 101 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.appSlot), IsNil) 102 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 103 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `path="/org/freedesktop/NetworkManager{,/{ActiveConnection,Devices}/*}"`) 104 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label="snap.producer.app"),`) 105 106 // connected app slot to plug 107 spec = &apparmor.Specification{} 108 c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.appSlot), IsNil) 109 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"}) 110 c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `path="/org/freedesktop/NetworkManager{,/{ActiveConnection,Devices}/*}"`) 111 c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `peer=(label="snap.consumer.app"),`) 112 113 // on a classic system with NM slot coming from the core snap. 114 restore = release.MockOnClassic(true) 115 defer restore() 116 117 // connected plug to core slot 118 spec = &apparmor.Specification{} 119 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil) 120 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 121 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `path="/org/freedesktop/NetworkManager{,/{ActiveConnection,Devices}/*}"`) 122 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "peer=(label=unconfined),") 123 124 // connected core slot to plug 125 spec = &apparmor.Specification{} 126 c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil) 127 c.Assert(spec.SecurityTags(), HasLen, 0) 128 } 129 130 func (s *NetworkManagerObserveInterfaceSuite) TestStaticInfo(c *C) { 131 si := interfaces.StaticInfoOf(s.iface) 132 c.Assert(si.ImplicitOnCore, Equals, false) 133 c.Assert(si.ImplicitOnClassic, Equals, true) 134 c.Assert(si.Summary, Equals, `allows observing NetworkManager settings`) 135 c.Assert(si.BaseDeclarationSlots, testutil.Contains, "network-manager-observe") 136 } 137 138 func (s *NetworkManagerObserveInterfaceSuite) TestAutoConnect(c *C) { 139 c.Assert(s.iface.AutoConnect(s.plugInfo, s.coreSlotInfo), Equals, true) 140 c.Assert(s.iface.AutoConnect(s.plugInfo, s.appSlotInfo), Equals, true) 141 } 142 143 func (s *NetworkManagerObserveInterfaceSuite) TestInterfaces(c *C) { 144 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 145 }