Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1202 from Pennyzct/unit-test
Browse files Browse the repository at this point in the history
Unit test: fix bugs on a few unit tests on aarch64
  • Loading branch information
grahamwhaley authored Feb 15, 2019
2 parents 7f2b2da + 23c554e commit 816ea42
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 154 deletions.
6 changes: 3 additions & 3 deletions cli/kata-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const (
kernelPropertyCorrect = "Kernel property value correct"

// these refer to fields in the procCPUINFO file
genericCPUFlagsTag = "flags"
genericCPUVendorField = "vendor_id"
genericCPUModelField = "model name"
genericCPUFlagsTag = "flags" // nolint: varcheck, unused
genericCPUVendorField = "vendor_id" // nolint: varcheck, unused
genericCPUModelField = "model name" // nolint: varcheck, unused
)

// variables rather than consts to allow tests to modify them
Expand Down
9 changes: 1 addition & 8 deletions cli/kata-check_amd64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,6 @@ func TestKvmIsUsable(t *testing.T) {
assert.Error(err)
}

type TestDataa struct {
contents string
expectedVendor string
expectedModel string
expectError bool
}

func TestGetCPUDetails(t *testing.T) {
const validVendorName = "a vendor"
validVendor := fmt.Sprintf(`%s : %s`, archCPUVendorField, validVendorName)
Expand All @@ -505,7 +498,7 @@ foo : bar
%s
`, validVendor, validModel)

data := []TestDataa{
data := []testCPUDetail{
{"", "", "", true},
{"invalid", "", "", true},
{archCPUVendorField, "", "", true},
Expand Down
10 changes: 5 additions & 5 deletions cli/kata-check_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func normalizeArmModel(model string) string {
return model
}

func getCPUDetails() (vendor, model string, err error) {
if vendor, model, err := genericGetCPUDetails(); err == nil {
func getCPUDetails() (string, string, error) {
vendor, model, err := genericGetCPUDetails()
if err == nil {
vendor = normalizeArmVendor(vendor)
model = normalizeArmModel(model)
return vendor, model, err
} else {
return vendor, model, err
}

return vendor, model, err
}
22 changes: 10 additions & 12 deletions cli/kata-check_arm64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,17 @@ func TestKvmIsUsable(t *testing.T) {
func TestGetCPUDetails(t *testing.T) {
type testData struct {
contents string
expectedVendor string
expectedModel string
expectedNormalizeVendor string
expectedNormalizeModel string
expectError bool
}

const validVendorName = "0x41"
const validNormalizeVendorName = "ARM Limited"
validVendorName := "0x41"
validNormalizeVendorName := "ARM Limited"
validVendor := fmt.Sprintf(`%s : %s`, archCPUVendorField, validVendorName)

const validModelName = "8"
const validNormalizeModelName = "v8"
validModelName := "8"
validNormalizeModelName := "v8"
validModel := fmt.Sprintf(`%s : %s`, archCPUModelField, validModelName)

validContents := fmt.Sprintf(`
Expand All @@ -152,12 +150,12 @@ foo : bar
`, validVendor, validModel)

data := []testData{
{"", "", "", "", "", true},
{"invalid", "", "", "", "", true},
{archCPUVendorField, "", "", "", "", true},
{validVendor, "", "", "", "", true},
{validModel, "", "", "", "", true},
{validContents, validVendorName, validModelName, validNormalizeVendorName, validNormalizeModelName, false},
{"", "", "", true},
{"invalid", "", "", true},
{archCPUVendorField, "", "", true},
{validVendor, "", "", true},
{validModel, "", "", true},
{validContents, validNormalizeVendorName, validNormalizeModelName, false},
}

tmpdir, err := ioutil.TempDir("", "")
Expand Down
9 changes: 1 addition & 8 deletions cli/kata-check_ppc64le_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ func TestKvmIsUsable(t *testing.T) {
assert.Error(err)
}

type TestDataa struct {
contents string
expectedVendor string
expectedModel string
expectError bool
}

func TestGetCPUDetails(t *testing.T) {

const validVendorName = ""
Expand All @@ -230,7 +223,7 @@ foo : bar
%s
`, validVendor, validModel)

data := []TestDataa{
data := []testCPUDetail{
{"", "", "", true},
{"invalid", "", "", true},
{archCPUVendorField, "", "", true},
Expand Down
16 changes: 1 addition & 15 deletions cli/kata-check_s390x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,7 @@ func TestKvmIsUsable(t *testing.T) {
assert.Error(err)
}

type TestDataa struct {
contents string
expectedVendor string
expectedModel string
expectError bool
}

func TestGetCPUDetails(t *testing.T) {
type testData struct {
contents string
expectedVendor string
expectedModel string
expectError bool
}

const validVendorName = "a vendor"
validVendor := fmt.Sprintf(`%s : %s`, archCPUVendorField, validVendorName)

Expand All @@ -235,7 +221,7 @@ foo : bar
%s
`, validVendor, validModel)

data := []TestDataa{
data := []testCPUDetail{
{"", "", "", true},
{"invalid", "", "", true},
{archCPUVendorField, "", "", true},
Expand Down
12 changes: 11 additions & 1 deletion cli/kata-check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ type testModuleData struct {
contents string
}

// nolint: structcheck, unused
type testCPUData struct {
vendorID string
flags string
expectError bool
}

// nolint: structcheck, unused
type testCPUDetail struct {
contents string
expectedVendor string
expectedModel string
expectError bool
}

func createFile(file, contents string) error {
return ioutil.WriteFile(file, []byte(contents), testFileMode)
}
Expand Down Expand Up @@ -138,7 +147,8 @@ func makeCPUInfoFile(path, vendorID, flags string) error {
return ioutil.WriteFile(path, contents.Bytes(), testFileMode)
}

func genericTestGetCPUDetails(t *testing.T, validVendor string, validModel string, validContents string, data []TestDataa) {
// nolint: unused
func genericTestGetCPUDetails(t *testing.T, validVendor string, validModel string, validContents string, data []testCPUDetail) {
tmpdir, err := ioutil.TempDir("", "")
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions cli/kata-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func getExpectedAgentDetails(config oci.RuntimeConfig) (AgentInfo, error) {
}, nil
}

// nolint: unused
func genericGetExpectedHostDetails(tmpdir string, expectedVendor string, expectedModel string) (HostInfo, error) {
type filesToCreate struct {
file string
Expand Down
84 changes: 84 additions & 0 deletions virtcontainers/hypervisor_amd64_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions virtcontainers/hypervisor_arm64_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 816ea42

Please sign in to comment.