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