gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/optical_drive_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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  	"fmt"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"gitee.com/mysnapcore/mysnapd/dirs"
    28  	"gitee.com/mysnapcore/mysnapd/interfaces"
    29  	"gitee.com/mysnapcore/mysnapd/interfaces/apparmor"
    30  	"gitee.com/mysnapcore/mysnapd/interfaces/builtin"
    31  	"gitee.com/mysnapcore/mysnapd/interfaces/udev"
    32  	"gitee.com/mysnapcore/mysnapd/snap"
    33  	"gitee.com/mysnapcore/mysnapd/snap/snaptest"
    34  	"gitee.com/mysnapcore/mysnapd/testutil"
    35  )
    36  
    37  type OpticalDriveInterfaceSuite struct {
    38  	iface    interfaces.Interface
    39  	slotInfo *snap.SlotInfo
    40  	slot     *interfaces.ConnectedSlot
    41  
    42  	// Consuming Snap
    43  	testPlugReadonly     *interfaces.ConnectedPlug
    44  	testPlugReadonlyInfo *snap.PlugInfo
    45  	testPlugWritable     *interfaces.ConnectedPlug
    46  	testPlugWritableInfo *snap.PlugInfo
    47  	testPlugDefault      *interfaces.ConnectedPlug
    48  	testPlugDefaultInfo  *snap.PlugInfo
    49  }
    50  
    51  var _ = Suite(&OpticalDriveInterfaceSuite{
    52  	iface: builtin.MustInterface("optical-drive"),
    53  })
    54  
    55  const opticalDriveConsumerYaml = `name: consumer
    56  version: 0
    57  plugs:
    58   plug-for-readonly:
    59    interface: optical-drive
    60    write: false
    61   plug-for-writable:
    62    interface: optical-drive
    63    write: true
    64  apps:
    65   app:
    66    plugs: [optical-drive]
    67   app-readonly:
    68    plugs: [plug-for-readonly]
    69   app-writable:
    70    plugs: [plug-for-writable]
    71  `
    72  
    73  const opticalDriveCoreYaml = `name: core
    74  version: 0
    75  type: os
    76  slots:
    77    optical-drive:
    78  `
    79  
    80  func (s *OpticalDriveInterfaceSuite) SetUpTest(c *C) {
    81  	consumingSnapInfo := snaptest.MockInfo(c, opticalDriveConsumerYaml, nil)
    82  
    83  	s.testPlugDefaultInfo = consumingSnapInfo.Plugs["optical-drive"]
    84  	s.testPlugDefault = interfaces.NewConnectedPlug(s.testPlugDefaultInfo, nil, nil)
    85  	s.testPlugReadonlyInfo = consumingSnapInfo.Plugs["plug-for-readonly"]
    86  	s.testPlugReadonly = interfaces.NewConnectedPlug(s.testPlugReadonlyInfo, nil, nil)
    87  	s.testPlugWritableInfo = consumingSnapInfo.Plugs["plug-for-writable"]
    88  	s.testPlugWritable = interfaces.NewConnectedPlug(s.testPlugWritableInfo, nil, nil)
    89  
    90  	s.slot, s.slotInfo = MockConnectedSlot(c, opticalDriveCoreYaml, nil, "optical-drive")
    91  }
    92  
    93  func (s *OpticalDriveInterfaceSuite) TestName(c *C) {
    94  	c.Assert(s.iface.Name(), Equals, "optical-drive")
    95  }
    96  
    97  func (s *OpticalDriveInterfaceSuite) TestSanitizeSlot(c *C) {
    98  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    99  }
   100  
   101  func (s *OpticalDriveInterfaceSuite) TestSanitizePlug(c *C) {
   102  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.testPlugDefaultInfo), IsNil)
   103  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.testPlugReadonlyInfo), IsNil)
   104  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.testPlugWritableInfo), IsNil)
   105  }
   106  
   107  func (s *OpticalDriveInterfaceSuite) TestAppArmorSpec(c *C) {
   108  	type options struct {
   109  		appName         string
   110  		includeSnippets []string
   111  		excludeSnippets []string
   112  	}
   113  	checkConnectedPlugSnippet := func(plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot, opts *options) {
   114  		apparmorSpec := &apparmor.Specification{}
   115  		err := apparmorSpec.AddConnectedPlug(s.iface, plug, slot)
   116  		c.Assert(err, IsNil)
   117  		c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{opts.appName})
   118  		for _, expectedSnippet := range opts.includeSnippets {
   119  			c.Assert(apparmorSpec.SnippetForTag(opts.appName), testutil.Contains, expectedSnippet)
   120  		}
   121  		for _, unexpectedSnippet := range opts.excludeSnippets {
   122  			c.Assert(apparmorSpec.SnippetForTag(opts.appName), Not(testutil.Contains), unexpectedSnippet)
   123  		}
   124  	}
   125  
   126  	expectedSnippet1 := `/dev/scd[0-9]* r,`
   127  	expectedSnippet2 := `/dev/scd[0-9]* w,`
   128  
   129  	checkConnectedPlugSnippet(s.testPlugDefault, s.slot, &options{
   130  		appName:         "snap.consumer.app",
   131  		includeSnippets: []string{expectedSnippet1},
   132  		excludeSnippets: []string{expectedSnippet2},
   133  	})
   134  	checkConnectedPlugSnippet(s.testPlugReadonly, s.slot, &options{
   135  		appName:         "snap.consumer.app-readonly",
   136  		includeSnippets: []string{expectedSnippet1},
   137  		excludeSnippets: []string{expectedSnippet2},
   138  	})
   139  	checkConnectedPlugSnippet(s.testPlugWritable, s.slot, &options{
   140  		appName:         "snap.consumer.app-writable",
   141  		includeSnippets: []string{expectedSnippet1, expectedSnippet2},
   142  		excludeSnippets: []string{},
   143  	})
   144  }
   145  
   146  func (s *OpticalDriveInterfaceSuite) TestUDevSpec(c *C) {
   147  	spec := &udev.Specification{}
   148  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugDefault, s.slot), IsNil)
   149  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugReadonly, s.slot), IsNil)
   150  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugWritable, s.slot), IsNil)
   151  	c.Assert(spec.Snippets(), HasLen, 12) // four rules multiplied by three apps
   152  	c.Assert(spec.Snippets(), testutil.Contains, `# optical-drive
   153  KERNEL=="sr[0-9]*", TAG+="snap_consumer_app"`)
   154  	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))
   155  }
   156  
   157  func (s *OpticalDriveInterfaceSuite) TestStaticInfo(c *C) {
   158  	si := interfaces.StaticInfoOf(s.iface)
   159  	c.Assert(si.ImplicitOnCore, Equals, false)
   160  	c.Assert(si.ImplicitOnClassic, Equals, true)
   161  	c.Assert(si.Summary, Equals, `allows access to optical drives`)
   162  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "optical-drive")
   163  }
   164  
   165  func (s *OpticalDriveInterfaceSuite) TestInterfaces(c *C) {
   166  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   167  }