From b7745d7bc493601700c58b8a9973de459f5a715a Mon Sep 17 00:00:00 2001 From: Jean-Laurent de Morlhon Date: Tue, 24 Nov 2015 16:00:43 +0100 Subject: [PATCH] Add Test on VTX Detection Signed-off-by: Jean-Laurent de Morlhon --- drivers/virtualbox/virtualbox_darwin.go | 9 +- drivers/virtualbox/virtualbox_darwin_test.go | 11 ++ drivers/virtualbox/virtualbox_linux.go | 19 ++-- drivers/virtualbox/virtualbox_linux_test.go | 101 +++++++++++++++++++ 4 files changed, 127 insertions(+), 13 deletions(-) diff --git a/drivers/virtualbox/virtualbox_darwin.go b/drivers/virtualbox/virtualbox_darwin.go index a1dcfd2521..2c411dd3af 100644 --- a/drivers/virtualbox/virtualbox_darwin.go +++ b/drivers/virtualbox/virtualbox_darwin.go @@ -10,15 +10,16 @@ import ( // IsVTXDisabled checks if VT-X is disabled in the BIOS. If it is, the vm will fail to start. // If we can't be sure it is disabled, we carry on and will check the vm logs after it's started. func (d *Driver) IsVTXDisabled() bool { - errmsg := "Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v" features, err := syscall.Sysctl("machdep.cpu.features") if err != nil { - log.Debugf(errmsg, err) + log.Debugf("Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v", err) return false } + return isVTXDisabled(features) +} - disabled := !strings.Contains(features, "VMX") - return disabled +func isVTXDisabled(features string) bool { + return !strings.Contains(features, "VMX") } func detectVBoxManageCmd() string { diff --git a/drivers/virtualbox/virtualbox_darwin_test.go b/drivers/virtualbox/virtualbox_darwin_test.go index 0bc1a1fee7..b1e840e819 100644 --- a/drivers/virtualbox/virtualbox_darwin_test.go +++ b/drivers/virtualbox/virtualbox_darwin_test.go @@ -5,6 +5,12 @@ import ( "github.com/stretchr/testify/assert" ) + +const ( + featuresWithVMX = "FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 VMX PBE SSE3 PCLMULQDQ DTES64 AVX1.0 RDRAND F16C" + featuresNoVMX = "FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 PBE SSE3 PCLMULQDQ DTES64 AVX1.0 RDRAND F16C" +) + func TestShareName(t *testing.T) { name, dir := getShareDriveAndName() @@ -12,3 +18,8 @@ func TestShareName(t *testing.T) { assert.Equal(t, dir, "/Users") } + +func TestIsVTXEnabled(t *testing.T) { + assert.False(t, isVTXDisabled(featuresWithVMX)) + assert.True(t, isVTXDisabled(featuresNoVMX)) +} diff --git a/drivers/virtualbox/virtualbox_linux.go b/drivers/virtualbox/virtualbox_linux.go index 773bacce6f..734c8ab98c 100644 --- a/drivers/virtualbox/virtualbox_linux.go +++ b/drivers/virtualbox/virtualbox_linux.go @@ -11,20 +11,21 @@ import ( // If we can't be sure it is disabled, we carry on and will check the vm logs after it's started. // We want to check that either vmx or smd flags are present in /proc/cpuinfo. func (d *Driver) IsVTXDisabled() bool { + cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo") + if err != nil { + log.Debugf("Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v", err) + return false + } + return isVTXDisabled(cpuinfo) +} + +func isVTXDisabled(cpuinfo []byte) bool { features := [2][]byte{ {'v', 'm', 'x'}, {'s', 'm', 'd'}, } - - errmsg := "Couldn't check that VT-X/AMD-v is enabled. Will check that the vm is properly created: %v" - content, err := ioutil.ReadFile("/proc/cpuinfo") - if err != nil { - log.Debugf(errmsg, err) - return false - } - for _, v := range features { - if bytes.Contains(content, v) { + if bytes.Contains(cpuinfo, v) { return false } } diff --git a/drivers/virtualbox/virtualbox_linux_test.go b/drivers/virtualbox/virtualbox_linux_test.go index 42b186304f..25081885b6 100644 --- a/drivers/virtualbox/virtualbox_linux_test.go +++ b/drivers/virtualbox/virtualbox_linux_test.go @@ -5,9 +5,110 @@ import ( "github.com/stretchr/testify/assert" ) + +const ( + amdCPUInfo = ` +processor : 0 +vendor_id : GenuineIntel +cpu family : 6 +model : 70 +model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz +stepping : 1 +microcode : 0x19 +cpu MHz : 2294.688 +cache size : 6144 KB +physical id : 0 +siblings : 1 +core id : 0 +cpu cores : 1 +apicid : 0 +initial apicid : 0 +fpu : yes +fpu_exception : yes +cpuid level : 13 +wp : yes +flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 smd clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm +bugs : +bogomips : 4589.37 +clflush size : 64 +cache_alignment : 64 +address sizes : 39 bits physical, 48 bits virtual +power management: +` + intelCPUInfo = ` +processor : 0 +vendor_id : GenuineIntel +cpu family : 6 +model : 70 +model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz +stepping : 1 +microcode : 0x19 +cpu MHz : 2294.688 +cache size : 6144 KB +physical id : 0 +siblings : 1 +core id : 0 +cpu cores : 1 +apicid : 0 +initial apicid : 0 +fpu : yes +fpu_exception : yes +cpuid level : 13 +wp : yes +flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr vmx pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm +bugs : +bogomips : 4589.37 +clflush size : 64 +cache_alignment : 64 +address sizes : 39 bits physical, 48 bits virtual +power management: +` + + faultyCPUInfo = ` +processor : 0 +vendor_id : GenuineIntel +cpu family : 6 +model : 70 +model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz +stepping : 1 +microcode : 0x19 +cpu MHz : 2294.688 +cache size : 6144 KB +physical id : 0 +siblings : 1 +core id : 0 +cpu cores : 1 +apicid : 0 +initial apicid : 0 +fpu : yes +fpu_exception : yes +cpuid level : 13 +wp : yes +flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm +bugs : +bogomips : 4589.37 +clflush size : 64 +cache_alignment : 64 +address sizes : 39 bits physical, 48 bits virtual +power management: +` +) + func TestShareName(t *testing.T) { name, dir := getShareDriveAndName() assert.Equal(t, name, "hosthome") assert.Equal(t, dir, "/home") } + +func TestCpuInfoOnAMD(t *testing.T) { + assert.False(t, isVTXDisabled([]byte(amdCPUInfo))) +} + +func TestCpuInfoOnIntel(t *testing.T) { + assert.False(t, isVTXDisabled([]byte(intelCPUInfo))) +} + +func TestCpuInfoOnNone(t *testing.T) { + assert.True(t, isVTXDisabled([]byte(faultyCPUInfo))) +}