Merge pull request #2402 from jeanlaurent/os-specific

Add test on os specific code
This commit is contained in:
David Gageot 2015-11-24 18:02:17 +01:00
commit d65e23600e
6 changed files with 167 additions and 25 deletions

View File

@ -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")

View File

@ -10,17 +10,22 @@ 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 {
return detectVBoxManageCmdInPath()
}
func getShareDriveAndName() (string, string) {
return "Users", "/Users"
}

View File

@ -0,0 +1,25 @@
package virtualbox
import (
"testing"
"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()
assert.Equal(t, name, "Users")
assert.Equal(t, dir, "/Users")
}
func TestIsVTXEnabled(t *testing.T) {
assert.False(t, isVTXDisabled(featuresWithVMX))
assert.True(t, isVTXDisabled(featuresNoVMX))
}

View File

@ -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
}
}
@ -34,3 +35,7 @@ func (d *Driver) IsVTXDisabled() bool {
func detectVBoxManageCmd() string {
return detectVBoxManageCmdInPath()
}
func getShareDriveAndName() (string, string) {
return "hosthome", "/home"
}

View File

@ -0,0 +1,114 @@
package virtualbox
import (
"testing"
"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)))
}

View File

@ -89,3 +89,7 @@ func findVBoxInstallDirInRegistry() (string, error) {
return installDir, nil
}
func getShareDriveAndName() (string, string) {
return "c/Users", "c:\\Users"
}