Skip to content

Commit

Permalink
ppc64le: kata-env fails due to missing vendor field
Browse files Browse the repository at this point in the history
There is no vendor field in /proc/cpuinfo contents
on ppc64le. Make sure the check is only for
arm and amd64.

Fixes: kata-containers#864

Signed-off-by: Nitesh Konkar niteshkonkar@in.ibm.com
  • Loading branch information
nitkon committed Nov 23, 2018
1 parent 6d17e27 commit d248659
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 14 deletions.
9 changes: 1 addition & 8 deletions cli/kata-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions cli/kata-check_ppc64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 66 additions & 1 deletion cli/kata-check_ppc64le_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -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) {
Expand Down
84 changes: 82 additions & 2 deletions cli/kata-env_ppc64le_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit d248659

Please sign in to comment.