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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017-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  	"io/ioutil"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"gitee.com/mysnapcore/mysnapd/dirs"
    30  	"gitee.com/mysnapcore/mysnapd/interfaces"
    31  	"gitee.com/mysnapcore/mysnapd/interfaces/apparmor"
    32  	"gitee.com/mysnapcore/mysnapd/interfaces/builtin"
    33  	"gitee.com/mysnapcore/mysnapd/interfaces/kmod"
    34  	"gitee.com/mysnapcore/mysnapd/interfaces/udev"
    35  	"gitee.com/mysnapcore/mysnapd/snap"
    36  	"gitee.com/mysnapcore/mysnapd/testutil"
    37  )
    38  
    39  type kvmInterfaceSuite struct {
    40  	testutil.BaseTest
    41  
    42  	iface    interfaces.Interface
    43  	slotInfo *snap.SlotInfo
    44  	slot     *interfaces.ConnectedSlot
    45  	plugInfo *snap.PlugInfo
    46  	plug     *interfaces.ConnectedPlug
    47  
    48  	tmpdir string
    49  }
    50  
    51  var _ = Suite(&kvmInterfaceSuite{
    52  	iface: builtin.MustInterface("kvm"),
    53  })
    54  
    55  const kvmConsumerYaml = `name: consumer
    56  version: 0
    57  apps:
    58   app:
    59    plugs: [kvm]
    60  `
    61  
    62  const kvmCoreYaml = `name: core
    63  version: 0
    64  type: os
    65  slots:
    66    kvm:
    67  `
    68  
    69  func (s *kvmInterfaceSuite) SetUpTest(c *C) {
    70  	s.BaseTest.SetUpTest(c)
    71  
    72  	s.plug, s.plugInfo = MockConnectedPlug(c, kvmConsumerYaml, nil, "kvm")
    73  	s.slot, s.slotInfo = MockConnectedSlot(c, kvmCoreYaml, nil, "kvm")
    74  
    75  	// Need to Mock output of /proc/cpuinfo
    76  	s.tmpdir = c.MkDir()
    77  	dirs.SetRootDir(s.tmpdir)
    78  	s.AddCleanup(func() { dirs.SetRootDir("/") })
    79  
    80  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
    81  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
    82  processor       : 0
    83  flags		: cpuflags without kvm support
    84  
    85  processor	: 42
    86  flags		: another cpu also without kvm support
    87  `[1:]), 0644), IsNil)
    88  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
    89  }
    90  
    91  func (s *kvmInterfaceSuite) TestName(c *C) {
    92  	c.Assert(s.iface.Name(), Equals, "kvm")
    93  }
    94  
    95  func (s *kvmInterfaceSuite) TestSanitizeSlot(c *C) {
    96  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    97  }
    98  
    99  func (s *kvmInterfaceSuite) TestSanitizePlug(c *C) {
   100  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
   101  }
   102  
   103  func (s *kvmInterfaceSuite) TestAppArmorSpec(c *C) {
   104  	spec := &apparmor.Specification{}
   105  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   106  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   107  	c.Assert(spec.SnippetForTag("snap.consumer.app"), Equals, `
   108  # Description: Allow write access to kvm.
   109  # See 'man kvm' for details.
   110  
   111  /dev/kvm rw,
   112  
   113  # Allow nested virtualization checks for different CPU models and architectures (where it is supported).
   114  /sys/module/kvm_intel/parameters/nested r,
   115  /sys/module/kvm_amd/parameters/nested r,
   116  /sys/module/kvm_hv/parameters/nested r, # PPC64.
   117  /sys/module/kvm/parameters/nested r, # S390.
   118  
   119  # Allow AMD SEV checks for AMD CPU's.
   120  /sys/module/kvm_amd/parameters/sev r,
   121  `)
   122  }
   123  
   124  func (s *kvmInterfaceSuite) TestUDevSpec(c *C) {
   125  	spec := &udev.Specification{}
   126  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   127  	c.Assert(spec.Snippets(), HasLen, 2)
   128  	c.Assert(spec.Snippets()[0], Equals, `# kvm
   129  KERNEL=="kvm", TAG+="snap_consumer_app"`)
   130  	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))
   131  }
   132  
   133  func (s *kvmInterfaceSuite) TestStaticInfo(c *C) {
   134  	si := interfaces.StaticInfoOf(s.iface)
   135  	c.Assert(si.ImplicitOnCore, Equals, true)
   136  	c.Assert(si.ImplicitOnClassic, Equals, true)
   137  	c.Assert(si.Summary, Equals, `allows access to the kvm device`)
   138  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "kvm")
   139  }
   140  
   141  func (s *kvmInterfaceSuite) TestAutoConnect(c *C) {
   142  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   143  }
   144  
   145  func (s *kvmInterfaceSuite) TestInterfaces(c *C) {
   146  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   147  }
   148  
   149  func (s *kvmInterfaceSuite) TestKModSpecWithUnknownCpu(c *C) {
   150  	spec := &kmod.Specification{}
   151  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   152  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   153  		"kvm": true,
   154  	})
   155  }
   156  
   157  func (s *kvmInterfaceSuite) TestKModSpecWithIntel(c *C) {
   158  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
   159  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
   160  processor       : 0
   161  flags           : stuff vmx other
   162  `[1:]), 0644), IsNil)
   163  
   164  	spec := &kmod.Specification{}
   165  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   166  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   167  		"kvm_intel": true,
   168  	})
   169  }
   170  
   171  func (s *kvmInterfaceSuite) TestKModSpecWithAMD(c *C) {
   172  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
   173  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
   174  processor       : 0
   175  flags           : stuff svm other
   176  `[1:]), 0644), IsNil)
   177  
   178  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
   179  
   180  	spec := &kmod.Specification{}
   181  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   182  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   183  		"kvm_amd": true,
   184  	})
   185  }
   186  
   187  func (s *kvmInterfaceSuite) TestKModSpecWithEmptyCpuinfo(c *C) {
   188  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
   189  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
   190  `[1:]), 0644), IsNil)
   191  
   192  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
   193  
   194  	spec := &kmod.Specification{}
   195  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   196  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   197  		"kvm": true,
   198  	})
   199  }
   200  
   201  func (s *kvmInterfaceSuite) TestKModSpecWithMissingCpuinfo(c *C) {
   202  	mockCpuinfo := filepath.Join(s.tmpdir, "non-existent-cpuinfo")
   203  
   204  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
   205  
   206  	spec := &kmod.Specification{}
   207  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   208  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   209  		"kvm": true,
   210  	})
   211  }