gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/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  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/interfaces"
    31  	"github.com/snapcore/snapd/interfaces/apparmor"
    32  	"github.com/snapcore/snapd/interfaces/builtin"
    33  	"github.com/snapcore/snapd/interfaces/kmod"
    34  	"github.com/snapcore/snapd/interfaces/udev"
    35  	"github.com/snapcore/snapd/snap"
    36  	"github.com/snapcore/snapd/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  }
   120  
   121  func (s *kvmInterfaceSuite) TestUDevSpec(c *C) {
   122  	spec := &udev.Specification{}
   123  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   124  	c.Assert(spec.Snippets(), HasLen, 2)
   125  	c.Assert(spec.Snippets()[0], Equals, `# kvm
   126  KERNEL=="kvm", TAG+="snap_consumer_app"`)
   127  	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))
   128  }
   129  
   130  func (s *kvmInterfaceSuite) TestStaticInfo(c *C) {
   131  	si := interfaces.StaticInfoOf(s.iface)
   132  	c.Assert(si.ImplicitOnCore, Equals, true)
   133  	c.Assert(si.ImplicitOnClassic, Equals, true)
   134  	c.Assert(si.Summary, Equals, `allows access to the kvm device`)
   135  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "kvm")
   136  }
   137  
   138  func (s *kvmInterfaceSuite) TestAutoConnect(c *C) {
   139  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   140  }
   141  
   142  func (s *kvmInterfaceSuite) TestInterfaces(c *C) {
   143  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   144  }
   145  
   146  func (s *kvmInterfaceSuite) TestKModSpecWithUnknownCpu(c *C) {
   147  	spec := &kmod.Specification{}
   148  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   149  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   150  		"kvm": true,
   151  	})
   152  }
   153  
   154  func (s *kvmInterfaceSuite) TestKModSpecWithIntel(c *C) {
   155  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
   156  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
   157  processor       : 0
   158  flags           : stuff vmx other
   159  `[1:]), 0644), IsNil)
   160  
   161  	spec := &kmod.Specification{}
   162  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   163  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   164  		"kvm_intel": true,
   165  	})
   166  }
   167  
   168  func (s *kvmInterfaceSuite) TestKModSpecWithAMD(c *C) {
   169  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
   170  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
   171  processor       : 0
   172  flags           : stuff svm other
   173  `[1:]), 0644), IsNil)
   174  
   175  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
   176  
   177  	spec := &kmod.Specification{}
   178  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   179  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   180  		"kvm_amd": true,
   181  	})
   182  }
   183  
   184  func (s *kvmInterfaceSuite) TestKModSpecWithEmptyCpuinfo(c *C) {
   185  	mockCpuinfo := filepath.Join(s.tmpdir, "cpuinfo")
   186  	c.Assert(ioutil.WriteFile(mockCpuinfo, []byte(`
   187  `[1:]), 0644), IsNil)
   188  
   189  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
   190  
   191  	spec := &kmod.Specification{}
   192  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   193  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   194  		"kvm": true,
   195  	})
   196  }
   197  
   198  func (s *kvmInterfaceSuite) TestKModSpecWithMissingCpuinfo(c *C) {
   199  	mockCpuinfo := filepath.Join(s.tmpdir, "non-existent-cpuinfo")
   200  
   201  	s.AddCleanup(builtin.MockProcCpuinfo(mockCpuinfo))
   202  
   203  	spec := &kmod.Specification{}
   204  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   205  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   206  		"kvm": true,
   207  	})
   208  }