From ca58bb4ca14250d0c4883b1f1b22c367a21ae2c8 Mon Sep 17 00:00:00 2001 From: Nitesh Konkar Date: Tue, 27 Nov 2018 15:26:33 +0530 Subject: [PATCH] ppc64le: kata-env fails due to missing vendor field There is no vendor field in /proc/cpuinfo contents on ppc64le. Make sure to return "" for vendor field for ppc64le and fix all the corresponding testcases as well. Fixes: #864 Signed-off-by: Nitesh Konkar niteshkonkar@in.ibm.com --- cli/kata-check_amd64_test.go | 31 ++++++++++++++++++++++++++++++- cli/kata-check_ppc64le.go | 12 +++++++++--- cli/kata-check_ppc64le_test.go | 33 ++++++++++++++++++++++++++++++++- cli/kata-check_test.go | 31 +------------------------------ cli/kata-env_amd64_test.go | 4 +++- cli/kata-env_ppc64le_test.go | 4 +++- cli/kata-env_test.go | 6 +++--- cli/utils.go | 2 +- 8 files changed, 82 insertions(+), 41 deletions(-) diff --git a/cli/kata-check_amd64_test.go b/cli/kata-check_amd64_test.go index 2212e89cd2..1721490280 100644 --- a/cli/kata-check_amd64_test.go +++ b/cli/kata-check_amd64_test.go @@ -7,6 +7,7 @@ package main import ( "bytes" + "fmt" "io/ioutil" "os" "path" @@ -488,8 +489,36 @@ func TestKvmIsUsable(t *testing.T) { assert.Error(err) } +type TestDataa struct { + contents string + expectedVendor string + expectedModel string + expectError bool +} + func TestGetCPUDetails(t *testing.T) { - genericTestGetCPUDetails(t) + const validVendorName = "a vendor" + validVendor := fmt.Sprintf(`%s : %s`, archCPUVendorField, validVendorName) + + const validModelName = "some CPU model" + validModel := fmt.Sprintf(`%s : %s`, archCPUModelField, validModelName) + + validContents := fmt.Sprintf(` +a : b +%s +foo : bar +%s +`, validVendor, validModel) + + data := []TestDataa{ + {"", "", "", true}, + {"invalid", "", "", true}, + {archCPUVendorField, "", "", true}, + {validVendor, "", "", true}, + {validModel, "", "", true}, + {validContents, validVendorName, validModelName, false}, + } + genericTestGetCPUDetails(t, validVendor, validModel, validContents, data) } func TestSetCPUtype(t *testing.T) { diff --git a/cli/kata-check_ppc64le.go b/cli/kata-check_ppc64le.go index 627a7e9dde..df5e09b982 100644 --- a/cli/kata-check_ppc64le.go +++ b/cli/kata-check_ppc64le.go @@ -15,13 +15,15 @@ import ( const ( cpuFlagsTag = genericCPUFlagsTag - archCPUVendorField = genericCPUVendorField - archCPUModelField = genericCPUModelField + archCPUVendorField = "" + archCPUModelField = "model" ) var ( ppc64CpuCmd = "ppc64_cpu" smtStatusOption = "--smt" + _ = genericCPUVendorField + _ = genericCPUModelField ) // archRequiredCPUFlags maps a CPU flag value to search for and a @@ -94,7 +96,11 @@ func archKernelParamHandler(onVMM bool, fields logrus.Fields, msg string) bool { } func getCPUDetails() (vendor, model string, err error) { - return genericGetCPUDetails() + vendor, model, err = genericGetCPUDetails() + if err == nil { + model = "POWER8" + } + return vendor, model, err } func isSMTOff() bool { diff --git a/cli/kata-check_ppc64le_test.go b/cli/kata-check_ppc64le_test.go index 6df57e3f4e..5d0f2d9741 100644 --- a/cli/kata-check_ppc64le_test.go +++ b/cli/kata-check_ppc64le_test.go @@ -7,6 +7,7 @@ package main import ( "bytes" + "fmt" "io/ioutil" "os" "path" @@ -207,8 +208,38 @@ func TestKvmIsUsable(t *testing.T) { assert.Error(err) } +type TestDataa struct { + contents string + expectedVendor string + expectedModel string + expectError bool +} + func TestGetCPUDetails(t *testing.T) { - genericTestGetCPUDetails(t) + + const validVendorName = "" + validVendor := fmt.Sprintf(`%s : %s`, archCPUVendorField, validVendorName) + + const validModelName = "POWER8" + validModel := fmt.Sprintf(`%s : %s`, archCPUModelField, validModelName) + + validContents := fmt.Sprintf(` +a : b +%s +foo : bar +%s +`, validVendor, validModel) + + data := []TestDataa{ + {"", "", "", true}, + {"invalid", "", "", true}, + {archCPUVendorField, "", "", true}, + {validVendor, "", "", true}, + {validModel, "", validModelName, false}, + {validContents, validVendorName, validModelName, false}, + } + + genericTestGetCPUDetails(t, validVendor, validModel, validContents, data) } func TestSetCPUtype(t *testing.T) { diff --git a/cli/kata-check_test.go b/cli/kata-check_test.go index 502f280c65..71e065b38f 100644 --- a/cli/kata-check_test.go +++ b/cli/kata-check_test.go @@ -137,36 +137,7 @@ func makeCPUInfoFile(path, vendorID, flags string) error { return ioutil.WriteFile(path, contents.Bytes(), testFileMode) } -func genericTestGetCPUDetails(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) - - const validModelName = "some CPU model" - validModel := fmt.Sprintf(`%s : %s`, archCPUModelField, validModelName) - - validContents := fmt.Sprintf(` -a : b -%s -foo : bar -%s -`, validVendor, validModel) - - data := []testData{ - {"", "", "", true}, - {"invalid", "", "", true}, - {archCPUVendorField, "", "", true}, - {validVendor, "", "", true}, - {validModel, "", "", true}, - {validContents, validVendorName, validModelName, false}, - } - +func genericTestGetCPUDetails(t *testing.T, validVendor string, validModel string, validContents string, data []TestDataa) { tmpdir, err := ioutil.TempDir("", "") if err != nil { panic(err) diff --git a/cli/kata-env_amd64_test.go b/cli/kata-env_amd64_test.go index d515a1526b..7d359ee2a4 100644 --- a/cli/kata-env_amd64_test.go +++ b/cli/kata-env_amd64_test.go @@ -14,7 +14,9 @@ import ( ) func getExpectedHostDetails(tmpdir string) (HostInfo, error) { - return genericGetExpectedHostDetails(tmpdir) + expectedVendor := "moi" + expectedModel := "awesome XI" + return genericGetExpectedHostDetails(tmpdir, expectedVendor, expectedModel) } func TestEnvGetEnvInfoSetsCPUType(t *testing.T) { diff --git a/cli/kata-env_ppc64le_test.go b/cli/kata-env_ppc64le_test.go index e63df6f751..fb0f28dca2 100644 --- a/cli/kata-env_ppc64le_test.go +++ b/cli/kata-env_ppc64le_test.go @@ -8,7 +8,9 @@ package main import "testing" func getExpectedHostDetails(tmpdir string) (HostInfo, error) { - return genericGetExpectedHostDetails(tmpdir) + expectedVendor := "" + expectedModel := "POWER8" + return genericGetExpectedHostDetails(tmpdir, expectedVendor, expectedModel) } func TestEnvGetEnvInfoSetsCPUType(t *testing.T) { diff --git a/cli/kata-env_test.go b/cli/kata-env_test.go index 45a2a08f94..62d5a8eb95 100644 --- a/cli/kata-env_test.go +++ b/cli/kata-env_test.go @@ -179,7 +179,7 @@ func getExpectedAgentDetails(config oci.RuntimeConfig) (AgentInfo, error) { }, nil } -func genericGetExpectedHostDetails(tmpdir string) (HostInfo, error) { +func genericGetExpectedHostDetails(tmpdir string, expectedVendor string, expectedModel string) (HostInfo, error) { type filesToCreate struct { file string contents string @@ -194,8 +194,8 @@ func genericGetExpectedHostDetails(tmpdir string) (HostInfo, error) { } expectedCPU := CPUInfo{ - Vendor: "moi", - Model: "awesome XI", + Vendor: expectedVendor, + Model: expectedModel, } expectedHostDetails := HostInfo{ diff --git a/cli/utils.go b/cli/utils.go index b13dff9338..1cdae0810e 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -150,7 +150,7 @@ func genericGetCPUDetails() (vendor, model string, err error) { } } - if vendor == "" { + if archCPUVendorField != "" && vendor == "" { return "", "", fmt.Errorf("cannot find vendor field in file %v", procCPUInfo) }