Add Test on VTX Detection

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-11-24 16:00:43 +01:00
parent 84a40bcc06
commit b7745d7bc4
4 changed files with 127 additions and 13 deletions

View File

@ -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 {

View File

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

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
}
}

View File

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