github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  	"github.com/snapcore/snapd/interfaces"
    26  	"github.com/snapcore/snapd/interfaces/apparmor"
    27  	"github.com/snapcore/snapd/interfaces/builtin"
    28  	"github.com/snapcore/snapd/release"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/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  version: 0
    63  slots:
    64    network-manager-observe:
    65  `
    66  
    67  func (s *NetworkManagerObserveInterfaceSuite) SetUpTest(c *C) {
    68  	s.plug, s.plugInfo = MockConnectedPlug(c, networkManagerObserveConsumerYaml, nil, "network-manager-observe")
    69  	s.appSlot, s.appSlotInfo = MockConnectedSlot(c, networkManagerObserveProducerYaml, nil, "network-manager-observe")
    70  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, networkManagerObserveCoreYaml, nil, "network-manager-observe")
    71  }
    72  
    73  func (s *NetworkManagerObserveInterfaceSuite) TestName(c *C) {
    74  	c.Assert(s.iface.Name(), Equals, "network-manager-observe")
    75  }
    76  
    77  func (s *NetworkManagerObserveInterfaceSuite) TestSanitizeSlot(c *C) {
    78  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil)
    79  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.appSlotInfo), IsNil)
    80  	// network-manager-observe slot can now be used on snap other than core.
    81  	slot := &snap.SlotInfo{
    82  		Snap:      &snap.Info{SuggestedName: "some-snap"},
    83  		Name:      "network-manager-observe",
    84  		Interface: "network-manager-observe",
    85  	}
    86  	c.Assert(interfaces.BeforePrepareSlot(s.iface, slot), IsNil)
    87  }
    88  
    89  func (s *NetworkManagerObserveInterfaceSuite) TestSanitizePlug(c *C) {
    90  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    91  }
    92  
    93  func (s *NetworkManagerObserveInterfaceSuite) TestAppArmorSpec(c *C) {
    94  	// on a core system with NM slot coming from a regular app snap.
    95  	restore := release.MockOnClassic(false)
    96  	defer restore()
    97  
    98  	// connected plug to app slot
    99  	spec := &apparmor.Specification{}
   100  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.appSlot), IsNil)
   101  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   102  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `path="/org/freedesktop/NetworkManager{,/{ActiveConnection,Devices}/*}"`)
   103  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label="snap.producer.app"),`)
   104  
   105  	// connected app slot to plug
   106  	spec = &apparmor.Specification{}
   107  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.appSlot), IsNil)
   108  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"})
   109  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `path="/org/freedesktop/NetworkManager{,/{ActiveConnection,Devices}/*}"`)
   110  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `peer=(label="snap.consumer.app"),`)
   111  
   112  	// on a classic system with NM slot coming from the core snap.
   113  	restore = release.MockOnClassic(true)
   114  	defer restore()
   115  
   116  	// connected plug to core slot
   117  	spec = &apparmor.Specification{}
   118  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   119  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   120  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `path="/org/freedesktop/NetworkManager{,/{ActiveConnection,Devices}/*}"`)
   121  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "peer=(label=unconfined),")
   122  
   123  	// connected core slot to plug
   124  	spec = &apparmor.Specification{}
   125  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
   126  	c.Assert(spec.SecurityTags(), HasLen, 0)
   127  }
   128  
   129  func (s *NetworkManagerObserveInterfaceSuite) TestStaticInfo(c *C) {
   130  	si := interfaces.StaticInfoOf(s.iface)
   131  	c.Assert(si.ImplicitOnCore, Equals, false)
   132  	c.Assert(si.ImplicitOnClassic, Equals, true)
   133  	c.Assert(si.Summary, Equals, `allows observing NetworkManager settings`)
   134  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "network-manager-observe")
   135  }
   136  
   137  func (s *NetworkManagerObserveInterfaceSuite) TestAutoConnect(c *C) {
   138  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.coreSlotInfo), Equals, true)
   139  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.appSlotInfo), Equals, true)
   140  }
   141  
   142  func (s *NetworkManagerObserveInterfaceSuite) TestInterfaces(c *C) {
   143  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   144  }