github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/builtin/network_control_test.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 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/interfaces/mount"
    29  	"github.com/snapcore/snapd/interfaces/seccomp"
    30  	"github.com/snapcore/snapd/interfaces/udev"
    31  	"github.com/snapcore/snapd/osutil"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/testutil"
    34  )
    35  
    36  type NetworkControlInterfaceSuite struct {
    37  	iface    interfaces.Interface
    38  	slotInfo *snap.SlotInfo
    39  	slot     *interfaces.ConnectedSlot
    40  	plugInfo *snap.PlugInfo
    41  	plug     *interfaces.ConnectedPlug
    42  }
    43  
    44  var _ = Suite(&NetworkControlInterfaceSuite{
    45  	iface: builtin.MustInterface("network-control"),
    46  })
    47  
    48  const networkControlConsumerYaml = `name: consumer
    49  version: 0
    50  apps:
    51   app:
    52    plugs: [network-control]
    53  `
    54  
    55  const networkControlCoreYaml = `name: core
    56  version: 0
    57  type: os
    58  slots:
    59    network-control:
    60  `
    61  
    62  func (s *NetworkControlInterfaceSuite) SetUpTest(c *C) {
    63  	s.plug, s.plugInfo = MockConnectedPlug(c, networkControlConsumerYaml, nil, "network-control")
    64  	s.slot, s.slotInfo = MockConnectedSlot(c, networkControlCoreYaml, nil, "network-control")
    65  }
    66  
    67  func (s *NetworkControlInterfaceSuite) TestName(c *C) {
    68  	c.Assert(s.iface.Name(), Equals, "network-control")
    69  }
    70  
    71  func (s *NetworkControlInterfaceSuite) TestSanitizeSlot(c *C) {
    72  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    73  }
    74  
    75  func (s *NetworkControlInterfaceSuite) TestSanitizePlug(c *C) {
    76  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    77  }
    78  
    79  func (s *NetworkControlInterfaceSuite) TestAppArmorSpec(c *C) {
    80  	spec := &apparmor.Specification{}
    81  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    82  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    83  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/run/netns/* rw,\n")
    84  	c.Assert(spec.UpdateNS(), DeepEquals, []string{`
    85  /var/ r,
    86  /var/lib/ r,
    87  /var/lib/snapd/ r,
    88  /var/lib/snapd/hostfs/ r,
    89  /var/lib/snapd/hostfs/var/ r,
    90  /var/lib/snapd/hostfs/var/lib/ r,
    91  /var/lib/snapd/hostfs/var/lib/dhcp/ r,
    92  /var/lib/dhcp/ r,
    93  mount options=(rw bind) /var/lib/snapd/hostfs/var/lib/dhcp/ -> /var/lib/dhcp/,
    94  umount /var/lib/dhcp/,
    95  `})
    96  }
    97  
    98  func (s *NetworkControlInterfaceSuite) TestSecCompSpec(c *C) {
    99  	spec := &seccomp.Specification{}
   100  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   101  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   102  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "setns - CLONE_NEWNET\n")
   103  }
   104  
   105  func (s *NetworkControlInterfaceSuite) TestUDevSpec(c *C) {
   106  	spec := &udev.Specification{}
   107  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   108  	c.Assert(spec.Snippets(), HasLen, 3)
   109  	c.Assert(spec.Snippets(), testutil.Contains, `# network-control
   110  KERNEL=="tun", TAG+="snap_consumer_app"`)
   111  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_consumer_app", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`)
   112  }
   113  
   114  func (s *NetworkControlInterfaceSuite) TestMountSpec(c *C) {
   115  	spec := &mount.Specification{}
   116  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   117  	c.Assert(spec.MountEntries(), HasLen, 1)
   118  	c.Assert(spec.MountEntries(), DeepEquals, []osutil.MountEntry{{
   119  		Name:    "/var/lib/snapd/hostfs/var/lib/dhcp",
   120  		Dir:     "/var/lib/dhcp",
   121  		Options: []string{"bind", "rw", "x-snapd.ignore-missing"},
   122  	}})
   123  }
   124  
   125  func (s *NetworkControlInterfaceSuite) TestStaticInfo(c *C) {
   126  	si := interfaces.StaticInfoOf(s.iface)
   127  	c.Assert(si.ImplicitOnCore, Equals, true)
   128  	c.Assert(si.ImplicitOnClassic, Equals, true)
   129  	c.Assert(si.Summary, Equals, `allows configuring networking and network namespaces`)
   130  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "network-control")
   131  }
   132  
   133  func (s *NetworkControlInterfaceSuite) TestAutoConnect(c *C) {
   134  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   135  }
   136  func (s *NetworkControlInterfaceSuite) TestInterfaces(c *C) {
   137  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   138  }