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

Commit

Permalink
kata-env: kata-env error out when there is no VERSION_ID.
Browse files Browse the repository at this point in the history
For example, under debian buster/sid. Those information should be
provide with best effort instead of error out. Set name and version
to "<<unknown>>" if they are not defined.

Fixes: #1177

Signed-off-by: Yang Bo <[email protected]>
  • Loading branch information
yyyeerbo committed Jan 31, 2019
1 parent 91c454d commit da4d4c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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 da4d4c9

Please sign in to comment.