github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/interfaces/builtin/snapd_control_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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/snap"
    29  	"github.com/snapcore/snapd/snap/snaptest"
    30  	"github.com/snapcore/snapd/testutil"
    31  )
    32  
    33  type SnapdControlInterfaceSuite struct {
    34  	iface    interfaces.Interface
    35  	slotInfo *snap.SlotInfo
    36  	slot     *interfaces.ConnectedSlot
    37  	plugInfo *snap.PlugInfo
    38  	plug     *interfaces.ConnectedPlug
    39  }
    40  
    41  var _ = Suite(&SnapdControlInterfaceSuite{
    42  	iface: builtin.MustInterface("snapd-control"),
    43  })
    44  
    45  func (s *SnapdControlInterfaceSuite) SetUpTest(c *C) {
    46  	consumingSnapInfo := snaptest.MockInfo(c, `
    47  name: other
    48  version: 0
    49  apps:
    50   app:
    51      command: foo
    52      plugs: [snapd-control]
    53  `, nil)
    54  	s.slotInfo = &snap.SlotInfo{
    55  		Snap:      &snap.Info{SuggestedName: "core", SnapType: snap.TypeOS},
    56  		Name:      "snapd-control",
    57  		Interface: "snapd-control",
    58  	}
    59  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    60  	s.plugInfo = consumingSnapInfo.Plugs["snapd-control"]
    61  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    62  }
    63  
    64  func (s *SnapdControlInterfaceSuite) TestName(c *C) {
    65  	c.Assert(s.iface.Name(), Equals, "snapd-control")
    66  }
    67  
    68  func (s *SnapdControlInterfaceSuite) TestSanitizeSlot(c *C) {
    69  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    70  }
    71  
    72  func (s *SnapdControlInterfaceSuite) TestSanitizePlug(c *C) {
    73  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    74  }
    75  
    76  func (s *SnapdControlInterfaceSuite) TestSanitizePlugWithAttrHappy(c *C) {
    77  	const mockSnapYaml = `name: snapd-manager
    78  version: 1.0
    79  plugs:
    80   snapd-control:
    81    refresh-schedule: managed
    82  `
    83  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
    84  	plug := info.Plugs["snapd-control"]
    85  	c.Assert(interfaces.BeforePreparePlug(s.iface, plug), IsNil)
    86  }
    87  
    88  func (s *SnapdControlInterfaceSuite) TestSanitizePlugWithAttrNotHappy(c *C) {
    89  	const mockSnapYaml = `name: snapd-manager
    90  version: 1.0
    91  plugs:
    92   snapd-control:
    93    refresh-schedule: unsupported-value
    94  `
    95  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
    96  	plug := info.Plugs["snapd-control"]
    97  	c.Assert(interfaces.BeforePreparePlug(s.iface, plug), ErrorMatches, `unsupported refresh-schedule value: "unsupported-value"`)
    98  }
    99  
   100  func (s *SnapdControlInterfaceSuite) TestUsedSecuritySystems(c *C) {
   101  	// connected plugs have a non-nil security snippet for apparmor
   102  	apparmorSpec := &apparmor.Specification{}
   103  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
   104  	c.Assert(err, IsNil)
   105  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   106  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `/run/snapd.socket rw,`)
   107  }
   108  
   109  func (s *SnapdControlInterfaceSuite) TestInterfaces(c *C) {
   110  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   111  }