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

Commit

Permalink
unit-test: refine unit tests
Browse files Browse the repository at this point in the history
we need to refine unit tests due to previous two commits and
add new test for new func checkVersionConsistencyInComponents.

Fixes: #2375

Signed-off-by: Penny Zheng <[email protected]>
  • Loading branch information
Pennyzct committed Feb 17, 2020
1 parent 1ad927d commit 1c1e7cc
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 21 deletions.
85 changes: 85 additions & 0 deletions cli/kata-check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

ktu "github.com/kata-containers/runtime/pkg/katatestutils"
"github.com/kata-containers/runtime/pkg/katautils"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
Expand Down Expand Up @@ -905,3 +906,87 @@ func TestArchRequiredKernelModules(t *testing.T) {

assert.EqualValues(count, expectedCount)
}

func TestCheckVersionConsistencyInComponents(t *testing.T) {
type testData struct {
proxyExist bool
expectError bool
shimVersion string
proxyVersion string
runtimeVersion string
}

data := []testData{
{
true,
true,
"kata-shim version 0.2.0-rc0-xxxxxxxxxxxxx",
"kata-proxy version 0.1.0-rc0-xxxxxxxxxxxxx",
"0.2.0-rc0",
},
{
true,
true,
"kata-shim version 0.1.0-rc0-xxxxxxxxxxxxx",
"kata-proxy version 0.2.0-rc0-xxxxxxxxxxxxx",
"0.2.0-rc0",
},
{
true,
true,
"kata-shim version 0.1.0-rc0-xxxxxxxxxxxxx",
"kata-proxy version 0.1.0-rc0-xxxxxxxxxxxxx",
"0.2.0-rc0",
},
{
true,
false,
"kata-shim version 0.2.0-rc0-xxxxxxxxxxxxx",
"kata-proxy version 0.2.0-rc0-xxxxxxxxxxxxx",
"0.2.0-rc0",
},
{
false,
true,
"kata-shim version 0.1.0-rc0-xxxxxxxxxxxxx",
"",
"0.2.0-rc0",
},
{
false,
false,
"kata-shim version 0.2.0-rc0-xxxxxxxxxxxxx",
"",
"0.2.0-rc0",
},
}

origVersion := version
for _, d := range data {
tmpdir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)

testShimVersion = d.shimVersion
if d.proxyExist {
testProxyVersion = d.proxyVersion
}
_, config, err := makeRuntimeConfig(tmpdir)
assert.NoError(t, err)
if !d.proxyExist {
config.ProxyType = vc.NoProxyType
}
version = d.runtimeVersion
defer func() {
version = origVersion
}()

err = checkVersionConsistencyInComponents(config)
if d.expectError {
assert.Error(t, err, fmt.Sprintf("%+v", d))
continue
} else {
assert.NoError(t, err, fmt.Sprintf("%+v", d))
}
}
}
45 changes: 24 additions & 21 deletions cli/kata-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import (
"github.com/stretchr/testify/assert"
)

const testProxyVersion = "proxy version 0.1"
const testShimVersion = "shim version 0.1"
const testNetmonVersion = "netmon version 0.1"
const testHypervisorVersion = "QEMU emulator version 2.7.0+git.741f430a96-6.1, Copyright (c) 2003-2016 Fabrice Bellard and the QEMU Project developers"
var (
testProxyVersion = "proxy version 0.1"
testShimVersion = "shim version 0.1"
testNetmonVersion = "netmon version 0.1"
testHypervisorVersion = "QEMU emulator version 2.7.0+git.741f430a96-6.1, Copyright (c) 2003-2016 Fabrice Bellard and the QEMU Project developers"
)

var (
hypervisorDebug = false
Expand Down Expand Up @@ -187,15 +189,15 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC
func getExpectedProxyDetails(config oci.RuntimeConfig) (ProxyInfo, error) {
return ProxyInfo{
Type: string(config.ProxyType),
Version: testProxyVersion,
Version: constructVersionInfo(testProxyVersion),
Path: config.ProxyConfig.Path,
Debug: config.ProxyConfig.Debug,
}, nil
}

func getExpectedNetmonDetails(config oci.RuntimeConfig) (NetmonInfo, error) {
return NetmonInfo{
Version: testNetmonVersion,
Version: constructVersionInfo(testNetmonVersion),
Path: config.NetmonConfig.Path,
Debug: config.NetmonConfig.Debug,
Enable: config.NetmonConfig.Enable,
Expand All @@ -212,7 +214,7 @@ func getExpectedShimDetails(config oci.RuntimeConfig) (ShimInfo, error) {

return ShimInfo{
Type: string(config.ShimType),
Version: testShimVersion,
Version: constructVersionInfo(testShimVersion),
Path: shimPath,
Debug: shimConfig.Debug,
}, nil
Expand Down Expand Up @@ -353,11 +355,12 @@ func getExpectedKernel(config oci.RuntimeConfig) KernelInfo {
func getExpectedRuntimeDetails(config oci.RuntimeConfig, configFile string) RuntimeInfo {
runtimePath, _ := os.Executable()

runtimeVersionInfo := constructVersionInfo(version)
runtimeVersionInfo.Commit = commit
return RuntimeInfo{
Version: RuntimeVersionInfo{
Semver: version,
Commit: commit,
OCI: specs.Version,
Version: runtimeVersionInfo,
OCI: specs.Version,
},
Config: RuntimeConfigInfo{
Path: configFile,
Expand Down Expand Up @@ -678,7 +681,7 @@ func TestEnvGetProxyInfo(t *testing.T) {
expectedProxy, err := getExpectedProxyDetails(config)
assert.NoError(t, err)

proxy, err := getProxyInfo(config)
proxy := getProxyInfo(config)
assert.NoError(t, err)

assert.Equal(t, expectedProxy, proxy)
Expand All @@ -701,9 +704,9 @@ func TestEnvGetProxyInfoNoVersion(t *testing.T) {
err = os.Remove(config.ProxyConfig.Path)
assert.NoError(t, err)

expectedProxy.Version = unknown
expectedProxy.Version = unknownVersionInfo

proxy, err := getProxyInfo(config)
proxy := getProxyInfo(config)
assert.NoError(t, err)

assert.Equal(t, expectedProxy, proxy)
Expand All @@ -722,7 +725,7 @@ func TestEnvGetNetmonInfo(t *testing.T) {
expectedNetmon, err := getExpectedNetmonDetails(config)
assert.NoError(t, err)

netmon, err := getNetmonInfo(config)
netmon := getNetmonInfo(config)
assert.NoError(t, err)

assert.Equal(t, expectedNetmon, netmon)
Expand All @@ -745,9 +748,9 @@ func TestEnvGetNetmonInfoNoVersion(t *testing.T) {
err = os.Remove(config.NetmonConfig.Path)
assert.NoError(t, err)

expectedNetmon.Version = unknown
expectedNetmon.Version = unknownVersionInfo

netmon, err := getNetmonInfo(config)
netmon := getNetmonInfo(config)
assert.NoError(t, err)

assert.Equal(t, expectedNetmon, netmon)
Expand Down Expand Up @@ -792,7 +795,7 @@ func TestEnvGetShimInfoNoVersion(t *testing.T) {
exit 1`)
assert.NoError(t, err)

expectedShim.Version = unknown
expectedShim.Version = unknownVersionInfo

shim, err := getShimInfo(config)
assert.NoError(t, err)
Expand Down Expand Up @@ -880,14 +883,14 @@ func testEnvShowTOMLSettings(t *testing.T, tmpdir string, tmpfile *os.File) erro

proxy := ProxyInfo{
Type: "proxy-type",
Version: "proxy-version",
Version: constructVersionInfo(testProxyVersion),
Path: "file:///proxy-url",
Debug: false,
}

shim := ShimInfo{
Type: "shim-type",
Version: "shim-version",
Version: constructVersionInfo(testShimVersion),
Path: "/resolved/shim/path",
}

Expand Down Expand Up @@ -949,14 +952,14 @@ func testEnvShowJSONSettings(t *testing.T, tmpdir string, tmpfile *os.File) erro

proxy := ProxyInfo{
Type: "proxy-type",
Version: "proxy-version",
Version: constructVersionInfo(testProxyVersion),
Path: "file:///proxy-url",
Debug: false,
}

shim := ShimInfo{
Type: "shim-type",
Version: "shim-version",
Version: constructVersionInfo(testShimVersion),
Path: "/resolved/shim/path",
}

Expand Down

0 comments on commit 1c1e7cc

Please sign in to comment.