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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2022 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/testutil"
    34  )
    35  
    36  type acrnSupportInterfaceSuite 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(&acrnSupportInterfaceSuite{
    47  	iface: builtin.MustInterface("acrn-support"),
    48  })
    49  
    50  const acrnSupportConsumerYaml = `name: consumer
    51  version: 0
    52  apps:
    53    app:
    54     plugs: [acrn-support]
    55  `
    56  
    57  const acrnSupportCoreYaml = `name: core
    58  version: 0
    59  type: os
    60  slots:
    61    acrn-support:
    62  `
    63  
    64  func (s *acrnSupportInterfaceSuite) SetUpTest(c *C) {
    65  	s.BaseTest.SetUpTest(c)
    66  
    67  	s.plug, s.plugInfo = MockConnectedPlug(c, acrnSupportConsumerYaml, nil, "acrn-support")
    68  	s.slot, s.slotInfo = MockConnectedSlot(c, acrnSupportCoreYaml, nil, "acrn-support")
    69  }
    70  
    71  func (s *acrnSupportInterfaceSuite) TestName(c *C) {
    72  	c.Assert(s.iface.Name(), Equals, "acrn-support")
    73  }
    74  
    75  func (s *acrnSupportInterfaceSuite) TestSanitizeSlot(c *C) {
    76  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    77  }
    78  
    79  func (s *acrnSupportInterfaceSuite) TestSanitizePlug(c *C) {
    80  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    81  }
    82  
    83  func (s *acrnSupportInterfaceSuite) 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"), Equals, `
    88  # Description: Allow write access to acrn_hsm.
    89  /dev/acrn_hsm rw,
    90  # Allow offlining CPU cores
    91  /sys/devices/system/cpu/cpu[0-9]*/online rw,
    92  
    93  `)
    94  }
    95  
    96  func (s *acrnSupportInterfaceSuite) TestUDevSpec(c *C) {
    97  	spec := &udev.Specification{}
    98  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    99  	c.Assert(spec.Snippets(), HasLen, 2)
   100  	c.Assert(spec.Snippets()[0], Equals, `# acrn-support
   101  KERNEL=="acrn_hsm", TAG+="snap_consumer_app"`)
   102  	c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%s/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
   103  }
   104  
   105  func (s *acrnSupportInterfaceSuite) TestStaticInfo(c *C) {
   106  	si := interfaces.StaticInfoOf(s.iface)
   107  	c.Assert(si.ImplicitOnCore, Equals, true)
   108  	c.Assert(si.ImplicitOnClassic, Equals, true)
   109  	c.Assert(si.Summary, Equals, `allows operating managing the ACRN hypervisor`)
   110  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "acrn-support")
   111  }
   112  
   113  func (s *acrnSupportInterfaceSuite) TestAutoConnect(c *C) {
   114  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   115  }
   116  
   117  func (s *acrnSupportInterfaceSuite) TestInterfaces(c *C) {
   118  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   119  }