Skip to content

Commit

Permalink
Merge pull request kata-containers#1178 from yyyeerbo/wip
Browse files Browse the repository at this point in the history
kata-env: kata-env error out when there is no VERSION_ID.
  • Loading branch information
Julio Montes authored Jan 31, 2019
2 parents 530360d + 4f51687 commit 5f1ca16
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cli/kata-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestEnvGetHostInfoNoOSRelease(t *testing.T) {
assert.NoError(t, err)

_, err = getHostInfo()
assert.Error(t, err)
assert.NoError(t, err)
}

func TestEnvGetHostInfoNoProcVersion(t *testing.T) {
Expand Down Expand Up @@ -600,7 +600,7 @@ func TestEnvGetEnvInfoNoOSRelease(t *testing.T) {
assert.NoError(t, err)

_, err = getEnvInfo(configFile, config)
assert.Error(t, err)
assert.NoError(t, err)
}

func TestEnvGetEnvInfoNoProcCPUInfo(t *testing.T) {
Expand Down
16 changes: 13 additions & 3 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func getKernelVersion() (string, error) {
// returned.
func getDistroDetails() (name, version string, err error) {
files := []string{osRelease, osReleaseClr}
name = ""
version = ""

for _, file := range files {
contents, err := katautils.GetFileContents(file)
Expand All @@ -62,10 +64,10 @@ func getDistroDetails() (name, version string, err error) {
lines := strings.Split(contents, "\n")

for _, line := range lines {
if strings.HasPrefix(line, "NAME=") {
if strings.HasPrefix(line, "NAME=") && name == "" {
fields := strings.Split(line, "=")
name = strings.Trim(fields[1], `"`)
} else if strings.HasPrefix(line, "VERSION_ID=") {
} else if strings.HasPrefix(line, "VERSION_ID=") && version == "" {
fields := strings.Split(line, "=")
version = strings.Trim(fields[1], `"`)
}
Expand All @@ -76,7 +78,15 @@ func getDistroDetails() (name, version string, err error) {
}
}

return "", "", fmt.Errorf("failed to find expected fields in one of %v", files)
if name == "" {
name = unknown
}

if version == "" {
version = unknown
}

return name, version, nil
}

// genericGetCPUDetails returns the vendor and model of the CPU.
Expand Down
8 changes: 5 additions & 3 deletions cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func TestGetDistroDetails(t *testing.T) {
expectError bool
}

const unknown = "<<unknown>>"

tmpdir, err := ioutil.TempDir("", "")
if err != nil {
panic(err)
Expand Down Expand Up @@ -144,11 +146,11 @@ VERSION_ID="%s"

_, _, err = getDistroDetails()
// ENOENT
assert.Error(t, err)
assert.NoError(t, err)

data := []testData{
{"", "", "", "", true},
{"invalid", "", "", "", true},
{"", "", unknown, unknown, false},
{"invalid", "", unknown, unknown, false},
{clrContents, "", clrExpectedName, clrExpectedVersion, false},
{"", nonClrContents, nonClrExpectedName, nonClrExpectedVersion, false},
{clrContents, nonClrContents, nonClrExpectedName, nonClrExpectedVersion, false},
Expand Down

0 comments on commit 5f1ca16

Please sign in to comment.