From d24865965e9b2ba2088f33a8e9b19bf3fc13d107 Mon Sep 17 00:00:00 2001 From: Nitesh Konkar Date: Fri, 23 Nov 2018 20:33:45 +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 the check is only for arm and amd64. Fixes: #864 Signed-off-by: Nitesh Konkar niteshkonkar@in.ibm.com --- cli/kata-check.go | 9 +--- cli/kata-check_ppc64le.go | 6 ++- cli/kata-check_ppc64le_test.go | 67 ++++++++++++++++++++++++++- cli/kata-env_ppc64le_test.go | 84 +++++++++++++++++++++++++++++++++- cli/utils.go | 2 +- 5 files changed, 154 insertions(+), 14 deletions(-) diff --git a/cli/kata-check.go b/cli/kata-check.go index 8f6b1057c1..6bae3e1307 100644 --- a/cli/kata-check.go +++ b/cli/kata-check.go @@ -75,14 +75,7 @@ func getCPUInfo(cpuInfoFile string) (string, error) { return "", err } - cpus := strings.SplitAfter(text, "\n\n") - - trimmed := strings.TrimSpace(cpus[0]) - if trimmed == "" { - return "", fmt.Errorf("Cannot determine CPU details") - } - - return trimmed, nil + return text, nil } // findAnchoredString searches haystack for needle and returns true if found diff --git a/cli/kata-check_ppc64le.go b/cli/kata-check_ppc64le.go index 627a7e9dde..a90a0ff046 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 diff --git a/cli/kata-check_ppc64le_test.go b/cli/kata-check_ppc64le_test.go index 6df57e3f4e..d37a6a7306 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" @@ -208,7 +209,71 @@ func TestKvmIsUsable(t *testing.T) { } func TestGetCPUDetails(t *testing.T) { - genericTestGetCPUDetails(t) + type testData struct { + contents string + expectedVendor string + expectedModel string + expectError bool + } + + const validVendorName = "" + 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}, + {validContents, validVendorName, validModelName, false}, + } + + tmpdir, err := ioutil.TempDir("", "") + if err != nil { + panic(err) + } + defer os.RemoveAll(tmpdir) + + savedProcCPUInfo := procCPUInfo + + testProcCPUInfo := filepath.Join(tmpdir, "cpuinfo") + + // override + procCPUInfo = testProcCPUInfo + + defer func() { + procCPUInfo = savedProcCPUInfo + }() + + _, _, err = getCPUDetails() + // ENOENT + assert.Error(t, err) + assert.True(t, os.IsNotExist(err)) + + for _, d := range data { + err := createFile(procCPUInfo, d.contents) + assert.NoError(t, err) + + vendor, model, err := getCPUDetails() + if d.expectError { + assert.Error(t, err, fmt.Sprintf("%+v", d)) + continue + } else { + assert.NoError(t, err, fmt.Sprintf("%+v", d)) + assert.Equal(t, d.expectedVendor, vendor) + assert.Equal(t, d.expectedModel, model) + } + } + } func TestSetCPUtype(t *testing.T) { diff --git a/cli/kata-env_ppc64le_test.go b/cli/kata-env_ppc64le_test.go index e63df6f751..12ee456ab3 100644 --- a/cli/kata-env_ppc64le_test.go +++ b/cli/kata-env_ppc64le_test.go @@ -5,10 +5,90 @@ package main -import "testing" +import ( + "fmt" + vcUtils "github.com/kata-containers/runtime/virtcontainers/utils" + "path/filepath" + goruntime "runtime" + "testing" +) func getExpectedHostDetails(tmpdir string) (HostInfo, error) { - return genericGetExpectedHostDetails(tmpdir) + type filesToCreate struct { + file string + contents string + } + + const expectedKernelVersion = "99.1" + const expectedArch = goruntime.GOARCH + + expectedDistro := DistroInfo{ + Name: "Foo", + Version: "42", + } + + expectedCPU := CPUInfo{ + Vendor: "", + Model: "awesome XI", + } + + expectedHostDetails := HostInfo{ + Kernel: expectedKernelVersion, + Architecture: expectedArch, + Distro: expectedDistro, + CPU: expectedCPU, + VMContainerCapable: false, + SupportVSocks: vcUtils.SupportsVsocks(), + } + + testProcCPUInfo := filepath.Join(tmpdir, "cpuinfo") + testOSRelease := filepath.Join(tmpdir, "os-release") + + // XXX: This file is *NOT* created by this function on purpose + // (to ensure the only file checked by the tests is + // testOSRelease). osReleaseClr handling is tested in + // utils_test.go. + testOSReleaseClr := filepath.Join(tmpdir, "os-release-clr") + + testProcVersion := filepath.Join(tmpdir, "proc-version") + + // override + procVersion = testProcVersion + osRelease = testOSRelease + osReleaseClr = testOSReleaseClr + procCPUInfo = testProcCPUInfo + + procVersionContents := fmt.Sprintf("Linux version %s a b c", + expectedKernelVersion) + + osReleaseContents := fmt.Sprintf(` +NAME="%s" +VERSION_ID="%s" +`, expectedDistro.Name, expectedDistro.Version) + + procCPUInfoContents := fmt.Sprintf(` +%s : %s +%s : %s +`, + archCPUVendorField, + expectedCPU.Vendor, + archCPUModelField, + expectedCPU.Model) + + data := []filesToCreate{ + {procVersion, procVersionContents}, + {osRelease, osReleaseContents}, + {procCPUInfo, procCPUInfoContents}, + } + + for _, d := range data { + err := createFile(d.file, d.contents) + if err != nil { + return HostInfo{}, err + } + } + + return expectedHostDetails, nil } func TestEnvGetEnvInfoSetsCPUType(t *testing.T) { 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) }