From 4f51687550628a6627f39c27726cbd9d793387f1 Mon Sep 17 00:00:00 2001 From: Yang Bo Date: Mon, 28 Jan 2019 09:27:21 +0800 Subject: [PATCH] kata-env: kata-env error out when there is no VERSION_ID. For example, under debian buster/sid. Those information should be provide with best effort instead of error out. Set name and version to "<>" if they are not defined. Fixes: #1177 Signed-off-by: Yang Bo --- cli/kata-env_test.go | 4 ++-- cli/utils.go | 16 +++++++++++++--- cli/utils_test.go | 8 +++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cli/kata-env_test.go b/cli/kata-env_test.go index 966b818bc6..a3b6a5970a 100644 --- a/cli/kata-env_test.go +++ b/cli/kata-env_test.go @@ -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) { @@ -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) { diff --git a/cli/utils.go b/cli/utils.go index 7252d20e3c..cc996ca3a1 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -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) @@ -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], `"`) } @@ -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. diff --git a/cli/utils_test.go b/cli/utils_test.go index 65c720f85d..7fa028b57e 100644 --- a/cli/utils_test.go +++ b/cli/utils_test.go @@ -101,6 +101,8 @@ func TestGetDistroDetails(t *testing.T) { expectError bool } + const unknown = "<>" + tmpdir, err := ioutil.TempDir("", "") if err != nil { panic(err) @@ -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},