github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/interfaces/builtin/block_devices_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/interfaces/udev"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/testutil"
    31  )
    32  
    33  type blockDevicesInterfaceSuite struct {
    34  	testutil.BaseTest
    35  
    36  	iface    interfaces.Interface
    37  	slotInfo *snap.SlotInfo
    38  	slot     *interfaces.ConnectedSlot
    39  	plugInfo *snap.PlugInfo
    40  	plug     *interfaces.ConnectedPlug
    41  }
    42  
    43  var _ = Suite(&blockDevicesInterfaceSuite{
    44  	iface: builtin.MustInterface("block-devices"),
    45  })
    46  
    47  const blockDevicesConsumerYaml = `name: consumer
    48  version: 0
    49  apps:
    50   app:
    51    plugs: [block-devices]
    52  `
    53  
    54  const blockDevicesCoreYaml = `name: core
    55  version: 0
    56  type: os
    57  slots:
    58    block-devices:
    59  `
    60  
    61  func (s *blockDevicesInterfaceSuite) SetUpTest(c *C) {
    62  	s.BaseTest.SetUpTest(c)
    63  
    64  	s.plug, s.plugInfo = MockConnectedPlug(c, blockDevicesConsumerYaml, nil, "block-devices")
    65  	s.slot, s.slotInfo = MockConnectedSlot(c, blockDevicesCoreYaml, nil, "block-devices")
    66  }
    67  
    68  func (s *blockDevicesInterfaceSuite) TestName(c *C) {
    69  	c.Assert(s.iface.Name(), Equals, "block-devices")
    70  }
    71  
    72  func (s *blockDevicesInterfaceSuite) TestSanitizeSlot(c *C) {
    73  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    74  }
    75  
    76  func (s *blockDevicesInterfaceSuite) TestSanitizePlug(c *C) {
    77  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    78  }
    79  
    80  func (s *blockDevicesInterfaceSuite) TestAppArmorSpec(c *C) {
    81  	spec := &apparmor.Specification{}
    82  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    83  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    84  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `# Description: Allow write access to raw disk block devices.`)
    85  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/dev/sd{,[a-h]}[a-z] rw,`)
    86  }
    87  
    88  func (s *blockDevicesInterfaceSuite) TestUDevSpec(c *C) {
    89  	spec := &udev.Specification{}
    90  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    91  	c.Assert(spec.Snippets(), HasLen, 5)
    92  	c.Assert(spec.Snippets()[0], Equals, `# block-devices
    93  KERNEL=="megaraid_sas_ioctl_node", TAG+="snap_consumer_app"`)
    94  	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"`)
    95  }
    96  
    97  func (s *blockDevicesInterfaceSuite) TestStaticInfo(c *C) {
    98  	si := interfaces.StaticInfoOf(s.iface)
    99  	c.Assert(si.ImplicitOnCore, Equals, true)
   100  	c.Assert(si.ImplicitOnClassic, Equals, true)
   101  	c.Assert(si.Summary, Equals, `allows access to disk block devices`)
   102  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "block-devices")
   103  }
   104  
   105  func (s *blockDevicesInterfaceSuite) TestAutoConnect(c *C) {
   106  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   107  }
   108  
   109  func (s *blockDevicesInterfaceSuite) TestInterfaces(c *C) {
   110  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   111  }