mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-20 00:48:04 +00:00
unit-test: test func for RunningOnVMM should be arch-dependent
original tests for func RunningOnVMM are sort of amd64-specific, since all other archs don't support nested VMM for now. Fixes: #1200 Signed-off-by: Penny Zheng <penny.zheng@arm.com>
This commit is contained in:
parent
0679f6fa59
commit
e93fb0b3a0
84
virtcontainers/hypervisor_amd64_test.go
Normal file
84
virtcontainers/hypervisor_amd64_test.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (c) 2019 ARM Limited
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
|
||||||
|
package virtcontainers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dataFlagsFieldWithoutHypervisor = []byte(`
|
||||||
|
fpu_exception : yes
|
||||||
|
cpuid level : 20
|
||||||
|
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 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt
|
||||||
|
bugs :
|
||||||
|
bogomips : 4589.35
|
||||||
|
`)
|
||||||
|
|
||||||
|
var dataFlagsFieldWithHypervisor = []byte(`
|
||||||
|
fpu_exception : yes
|
||||||
|
cpuid level : 20
|
||||||
|
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 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt
|
||||||
|
bugs :
|
||||||
|
bogomips : 4589.35
|
||||||
|
`)
|
||||||
|
|
||||||
|
var dataWithoutFlagsField = []byte(`
|
||||||
|
fpu_exception : yes
|
||||||
|
cpuid level : 20
|
||||||
|
wp : yes
|
||||||
|
bugs :
|
||||||
|
bogomips : 4589.35
|
||||||
|
`)
|
||||||
|
|
||||||
|
func TestRunningOnVMM(t *testing.T) {
|
||||||
|
var data []testNestedVMMData
|
||||||
|
|
||||||
|
//file cpuinfo doesn't contain 'hypervisor' flag
|
||||||
|
dataNestedVMMFalseSuccessful := testNestedVMMData{
|
||||||
|
content: dataFlagsFieldWithoutHypervisor,
|
||||||
|
expectedErr: false,
|
||||||
|
expected: false,
|
||||||
|
}
|
||||||
|
data = append(data, dataNestedVMMFalseSuccessful)
|
||||||
|
|
||||||
|
//file cpuinfo contains 'hypervisor' flag
|
||||||
|
dataNestedVMMTrueSuccessful := testNestedVMMData{
|
||||||
|
content: dataFlagsFieldWithHypervisor,
|
||||||
|
expectedErr: false,
|
||||||
|
expected: true,
|
||||||
|
}
|
||||||
|
data = append(data, dataNestedVMMTrueSuccessful)
|
||||||
|
|
||||||
|
//file cpuinfo doesn't contain field flags
|
||||||
|
dataNestedVMMWithoutFlagsField := testNestedVMMData{
|
||||||
|
content: dataWithoutFlagsField,
|
||||||
|
expectedErr: true,
|
||||||
|
expected: false,
|
||||||
|
}
|
||||||
|
data = append(data, dataNestedVMMWithoutFlagsField)
|
||||||
|
|
||||||
|
genericTestRunningOnVMM(t, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunningOnVMMNotExistingCPUInfoPathFailure(t *testing.T) {
|
||||||
|
f, err := ioutil.TempFile("", "cpuinfo")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
filePath := f.Name()
|
||||||
|
|
||||||
|
f.Close()
|
||||||
|
os.Remove(filePath)
|
||||||
|
|
||||||
|
if _, err := RunningOnVMM(filePath); err == nil {
|
||||||
|
t.Fatalf("Should fail because %q file path does not exist", filePath)
|
||||||
|
}
|
||||||
|
}
|
30
virtcontainers/hypervisor_arm64_test.go
Normal file
30
virtcontainers/hypervisor_arm64_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (c) 2019 ARM Limited
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
|
||||||
|
package virtcontainers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRunningOnVMM(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
expectedOutput := false
|
||||||
|
|
||||||
|
f, err := ioutil.TempFile("", "cpuinfo")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
running, err := RunningOnVMM(f.Name())
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(expectedOutput, running)
|
||||||
|
}
|
@ -437,84 +437,40 @@ func TestGetHostMemorySizeKb(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataFlagsFieldWithoutHypervisor = []byte(`
|
// nolint: unused
|
||||||
fpu_exception : yes
|
type testNestedVMMData struct {
|
||||||
cpuid level : 20
|
content []byte
|
||||||
wp : yes
|
expectedErr bool
|
||||||
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt
|
expected bool
|
||||||
bugs :
|
|
||||||
bogomips : 4589.35
|
|
||||||
`)
|
|
||||||
|
|
||||||
var dataFlagsFieldWithHypervisor = []byte(`
|
|
||||||
fpu_exception : yes
|
|
||||||
cpuid level : 20
|
|
||||||
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 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt
|
|
||||||
bugs :
|
|
||||||
bogomips : 4589.35
|
|
||||||
`)
|
|
||||||
|
|
||||||
var dataWithoutFlagsField = []byte(`
|
|
||||||
fpu_exception : yes
|
|
||||||
cpuid level : 20
|
|
||||||
wp : yes
|
|
||||||
bugs :
|
|
||||||
bogomips : 4589.35
|
|
||||||
`)
|
|
||||||
|
|
||||||
func testRunningOnVMMSuccessful(t *testing.T, cpuInfoContent []byte, expectedErr bool, expected bool) {
|
|
||||||
f, err := ioutil.TempFile("", "cpuinfo")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.Remove(f.Name())
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
n, err := f.Write(cpuInfoContent)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if n != len(cpuInfoContent) {
|
|
||||||
t.Fatalf("Only %d bytes written out of %d expected", n, len(cpuInfoContent))
|
|
||||||
}
|
|
||||||
|
|
||||||
running, err := RunningOnVMM(f.Name())
|
|
||||||
if !expectedErr && err != nil {
|
|
||||||
t.Fatalf("This test should succeed: %v", err)
|
|
||||||
} else if expectedErr && err == nil {
|
|
||||||
t.Fatalf("This test should fail")
|
|
||||||
}
|
|
||||||
|
|
||||||
if running != expected {
|
|
||||||
t.Fatalf("Expecting running on VMM = %t, Got %t", expected, running)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunningOnVMMFalseSuccessful(t *testing.T) {
|
// nolint: unused
|
||||||
testRunningOnVMMSuccessful(t, dataFlagsFieldWithoutHypervisor, false, false)
|
func genericTestRunningOnVMM(t *testing.T, data []testNestedVMMData) {
|
||||||
}
|
for _, d := range data {
|
||||||
|
f, err := ioutil.TempFile("", "cpuinfo")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
func TestRunningOnVMMTrueSuccessful(t *testing.T) {
|
n, err := f.Write(d.content)
|
||||||
testRunningOnVMMSuccessful(t, dataFlagsFieldWithHypervisor, false, true)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if n != len(d.content) {
|
||||||
|
t.Fatalf("Only %d bytes written out of %d expected", n, len(d.content))
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunningOnVMMNoFlagsFieldFailure(t *testing.T) {
|
running, err := RunningOnVMM(f.Name())
|
||||||
testRunningOnVMMSuccessful(t, dataWithoutFlagsField, true, false)
|
if !d.expectedErr && err != nil {
|
||||||
}
|
t.Fatalf("This test should succeed: %v", err)
|
||||||
|
} else if d.expectedErr && err == nil {
|
||||||
|
t.Fatalf("This test should fail")
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunningOnVMMNotExistingCPUInfoPathFailure(t *testing.T) {
|
if running != d.expected {
|
||||||
f, err := ioutil.TempFile("", "cpuinfo")
|
t.Fatalf("Expecting running on VMM = %t, Got %t", d.expected, running)
|
||||||
if err != nil {
|
}
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath := f.Name()
|
|
||||||
|
|
||||||
f.Close()
|
|
||||||
os.Remove(filePath)
|
|
||||||
|
|
||||||
if _, err := RunningOnVMM(filePath); err == nil {
|
|
||||||
t.Fatalf("Should fail because %q file path does not exist", filePath)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user