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