From 84a40bcc06b00ed846b212da2614105e9431a501 Mon Sep 17 00:00:00 2001 From: Jean-Laurent de Morlhon Date: Tue, 24 Nov 2015 16:00:19 +0100 Subject: [PATCH 1/2] Refactor OS Specific code And introduce Per-OS testing Signed-off-by: Jean-Laurent de Morlhon --- drivers/virtualbox/virtualbox.go | 13 +------------ drivers/virtualbox/virtualbox_darwin.go | 4 ++++ drivers/virtualbox/virtualbox_darwin_test.go | 14 ++++++++++++++ drivers/virtualbox/virtualbox_linux.go | 4 ++++ drivers/virtualbox/virtualbox_linux_test.go | 13 +++++++++++++ drivers/virtualbox/virtualbox_windows.go | 4 ++++ 6 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 drivers/virtualbox/virtualbox_darwin_test.go create mode 100644 drivers/virtualbox/virtualbox_linux_test.go diff --git a/drivers/virtualbox/virtualbox.go b/drivers/virtualbox/virtualbox.go index cb07891b8a..6ddcfb7b0d 100644 --- a/drivers/virtualbox/virtualbox.go +++ b/drivers/virtualbox/virtualbox.go @@ -372,18 +372,7 @@ func (d *Driver) Create() error { return err } - var shareName, shareDir string // TODO configurable at some point - switch runtime.GOOS { - case "windows": - shareName = "c/Users" - shareDir = "c:\\Users" - case "darwin": - shareName = "Users" - shareDir = "/Users" - case "linux": - shareName = "hosthome" - shareDir = "/home" - } + shareName, shareDir := getShareDriveAndName() if shareDir != "" && !d.NoShare { log.Debugf("setting up shareDir") diff --git a/drivers/virtualbox/virtualbox_darwin.go b/drivers/virtualbox/virtualbox_darwin.go index a8379115c5..a1dcfd2521 100644 --- a/drivers/virtualbox/virtualbox_darwin.go +++ b/drivers/virtualbox/virtualbox_darwin.go @@ -24,3 +24,7 @@ func (d *Driver) IsVTXDisabled() bool { func detectVBoxManageCmd() string { return detectVBoxManageCmdInPath() } + +func getShareDriveAndName() (string, string) { + return "Users", "/Users" +} diff --git a/drivers/virtualbox/virtualbox_darwin_test.go b/drivers/virtualbox/virtualbox_darwin_test.go new file mode 100644 index 0000000000..0bc1a1fee7 --- /dev/null +++ b/drivers/virtualbox/virtualbox_darwin_test.go @@ -0,0 +1,14 @@ +package virtualbox + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) +func TestShareName(t *testing.T) { + name, dir := getShareDriveAndName() + + assert.Equal(t, name, "Users") + assert.Equal(t, dir, "/Users") + +} diff --git a/drivers/virtualbox/virtualbox_linux.go b/drivers/virtualbox/virtualbox_linux.go index 0c6bf9eaa2..773bacce6f 100644 --- a/drivers/virtualbox/virtualbox_linux.go +++ b/drivers/virtualbox/virtualbox_linux.go @@ -34,3 +34,7 @@ func (d *Driver) IsVTXDisabled() bool { func detectVBoxManageCmd() string { return detectVBoxManageCmdInPath() } + +func getShareDriveAndName() (string, string) { + return "hosthome", "/home" +} diff --git a/drivers/virtualbox/virtualbox_linux_test.go b/drivers/virtualbox/virtualbox_linux_test.go new file mode 100644 index 0000000000..42b186304f --- /dev/null +++ b/drivers/virtualbox/virtualbox_linux_test.go @@ -0,0 +1,13 @@ +package virtualbox + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) +func TestShareName(t *testing.T) { + name, dir := getShareDriveAndName() + + assert.Equal(t, name, "hosthome") + assert.Equal(t, dir, "/home") +} diff --git a/drivers/virtualbox/virtualbox_windows.go b/drivers/virtualbox/virtualbox_windows.go index 7c56685547..3cb395d235 100644 --- a/drivers/virtualbox/virtualbox_windows.go +++ b/drivers/virtualbox/virtualbox_windows.go @@ -89,3 +89,7 @@ func findVBoxInstallDirInRegistry() (string, error) { return installDir, nil } + +func getShareDriveAndName() (string, string) { + return "c/Users", "c:\\Users" +} 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 2/2] 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))) +}