mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-01 17:52:40 +00:00
test: add a generic function for CLI kata-check command
Add a generic function to run CLI kata-check tests, shared by all the args. Signed-off-by: Marco Vedovati <mvedovati@suse.com>
This commit is contained in:
parent
7019ce5c9b
commit
24fcd1b37d
@ -10,15 +10,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
||||||
@ -47,36 +44,6 @@ func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCCCheckCLIFunction(t *testing.T) {
|
func TestCCCheckCLIFunction(t *testing.T) {
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
_, config, err := makeRuntimeConfig(dir)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
savedSysModuleDir := sysModuleDir
|
|
||||||
savedProcCPUInfo := procCPUInfo
|
|
||||||
|
|
||||||
cpuInfoFile := filepath.Join(dir, "cpuinfo")
|
|
||||||
|
|
||||||
// XXX: override
|
|
||||||
sysModuleDir = filepath.Join(dir, "sys/module")
|
|
||||||
procCPUInfo = cpuInfoFile
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
sysModuleDir = savedSysModuleDir
|
|
||||||
procCPUInfo = savedProcCPUInfo
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = os.MkdirAll(sysModuleDir, testDirMode)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var cpuData []testCPUData
|
var cpuData []testCPUData
|
||||||
var moduleData []testModuleData
|
var moduleData []testModuleData
|
||||||
|
|
||||||
@ -94,50 +61,7 @@ func TestCCCheckCLIFunction(t *testing.T) {
|
|||||||
moduleData = []testModuleData{}
|
moduleData = []testModuleData{}
|
||||||
}
|
}
|
||||||
|
|
||||||
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
|
genericCheckCLIFunction(t, cpuData, moduleData)
|
||||||
assert.NoError(err)
|
|
||||||
defer devNull.Close()
|
|
||||||
|
|
||||||
savedLogOutput := kataLog.Logger.Out
|
|
||||||
|
|
||||||
// discard normal output
|
|
||||||
kataLog.Logger.Out = devNull
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
kataLog.Logger.Out = savedLogOutput
|
|
||||||
}()
|
|
||||||
|
|
||||||
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, cpuData, moduleData)
|
|
||||||
|
|
||||||
ctx := createCLIContext(nil)
|
|
||||||
ctx.App.Name = "foo"
|
|
||||||
ctx.App.Metadata["runtimeConfig"] = config
|
|
||||||
|
|
||||||
// create buffer to save logger output
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
|
|
||||||
// capture output this time
|
|
||||||
kataLog.Logger.Out = buf
|
|
||||||
|
|
||||||
fn, ok := kataCheckCLICommand.Action.(func(context *cli.Context) error)
|
|
||||||
assert.True(ok)
|
|
||||||
|
|
||||||
err = fn(ctx)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
for _, c := range cpuData {
|
|
||||||
assert.True(findAnchoredString(output, c.vendorID))
|
|
||||||
for _, flag := range strings.Fields(c.flags) {
|
|
||||||
assert.True(findAnchoredString(output, flag))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, m := range moduleData {
|
|
||||||
name := path.Base(m.path)
|
|
||||||
assert.True(findAnchoredString(output, name))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckCheckKernelModulesNoNesting(t *testing.T) {
|
func TestCheckCheckKernelModulesNoNesting(t *testing.T) {
|
||||||
|
@ -6,128 +6,34 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, moduleData []testModuleData) {
|
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
||||||
//For now, Arm64 only deal with module check
|
//For now, Arm64 only deal with module check
|
||||||
|
_ = cpuData
|
||||||
|
|
||||||
createModules(assert, cpuInfoFile, moduleData)
|
createModules(assert, cpuInfoFile, moduleData)
|
||||||
|
|
||||||
err := makeCPUInfoFile(cpuInfoFile, "", "")
|
err := makeCPUInfoFile(cpuInfoFile, "", "")
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCCCheckCLIFunction(t *testing.T) {
|
func TestCCCheckCLIFunction(t *testing.T) {
|
||||||
assert := assert.New(t)
|
var cpuData []testCPUData
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
_, config, err := makeRuntimeConfig(dir)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
savedSysModuleDir := sysModuleDir
|
|
||||||
savedProcCPUInfo := procCPUInfo
|
|
||||||
|
|
||||||
cpuInfoFile := filepath.Join(dir, "cpuinfo")
|
|
||||||
|
|
||||||
// XXX: override
|
|
||||||
sysModuleDir = filepath.Join(dir, "sys/module")
|
|
||||||
procCPUInfo = cpuInfoFile
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
sysModuleDir = savedSysModuleDir
|
|
||||||
procCPUInfo = savedProcCPUInfo
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = os.MkdirAll(sysModuleDir, testDirMode)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
moduleData := []testModuleData{
|
moduleData := []testModuleData{
|
||||||
{filepath.Join(sysModuleDir, "kvm"), true, ""},
|
{filepath.Join(sysModuleDir, "kvm"), true, ""},
|
||||||
{filepath.Join(sysModuleDir, "vhost"), true, ""},
|
{filepath.Join(sysModuleDir, "vhost"), true, ""},
|
||||||
{filepath.Join(sysModuleDir, "vhost_net"), true, ""},
|
{filepath.Join(sysModuleDir, "vhost_net"), true, ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
|
genericCheckCLIFunction(t, cpuData, moduleData)
|
||||||
assert.NoError(err)
|
|
||||||
defer devNull.Close()
|
|
||||||
|
|
||||||
savedLogOutput := kataLog.Logger.Out
|
|
||||||
|
|
||||||
// discard normal output
|
|
||||||
kataLog.Logger.Out = devNull
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
kataLog.Logger.Out = savedLogOutput
|
|
||||||
}()
|
|
||||||
|
|
||||||
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, moduleData)
|
|
||||||
|
|
||||||
ctx := createCLIContext(nil)
|
|
||||||
ctx.App.Name = "foo"
|
|
||||||
ctx.App.Metadata["runtimeConfig"] = config
|
|
||||||
|
|
||||||
// create buffer to save logger output
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
|
|
||||||
// capture output this time
|
|
||||||
kataLog.Logger.Out = buf
|
|
||||||
|
|
||||||
fn, ok := kataCheckCLICommand.Action.(func(context *cli.Context) error)
|
|
||||||
assert.True(ok)
|
|
||||||
|
|
||||||
err = fn(ctx)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
for _, m := range moduleData {
|
|
||||||
name := path.Base(m.path)
|
|
||||||
assert.True(findAnchoredString(output, name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKvmIsUsable(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
savedKvmDevice := kvmDevice
|
|
||||||
fakeKVMDevice := filepath.Join(dir, "kvm")
|
|
||||||
kvmDevice = fakeKVMDevice
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
kvmDevice = savedKvmDevice
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = kvmIsUsable()
|
|
||||||
assert.Error(err)
|
|
||||||
|
|
||||||
err = createEmptyFile(fakeKVMDevice)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
err = kvmIsUsable()
|
|
||||||
assert.Error(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCPUDetails(t *testing.T) {
|
func TestGetCPUDetails(t *testing.T) {
|
||||||
|
@ -6,17 +6,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
||||||
@ -45,38 +42,8 @@ func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCCCheckCLIFunction(t *testing.T) {
|
func TestCCCheckCLIFunction(t *testing.T) {
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
_, config, err := makeRuntimeConfig(dir)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
savedSysModuleDir := sysModuleDir
|
|
||||||
savedProcCPUInfo := procCPUInfo
|
|
||||||
|
|
||||||
cpuInfoFile := filepath.Join(dir, "cpuinfo")
|
|
||||||
|
|
||||||
// XXX: override
|
|
||||||
sysModuleDir = filepath.Join(dir, "sys/module")
|
|
||||||
procCPUInfo = cpuInfoFile
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
sysModuleDir = savedSysModuleDir
|
|
||||||
procCPUInfo = savedProcCPUInfo
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = os.MkdirAll(sysModuleDir, testDirMode)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cpuData := []testCPUData{
|
cpuData := []testCPUData{
|
||||||
{"", "", false},
|
fakeCPUData,
|
||||||
}
|
}
|
||||||
|
|
||||||
moduleData := []testModuleData{
|
moduleData := []testModuleData{
|
||||||
@ -84,43 +51,7 @@ func TestCCCheckCLIFunction(t *testing.T) {
|
|||||||
{filepath.Join(sysModuleDir, "kvm_hv"), false, "Y"},
|
{filepath.Join(sysModuleDir, "kvm_hv"), false, "Y"},
|
||||||
}
|
}
|
||||||
|
|
||||||
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
|
genericCheckCLIFunction(t, cpuData, moduleData)
|
||||||
assert.NoError(err)
|
|
||||||
defer devNull.Close()
|
|
||||||
|
|
||||||
savedLogOutput := kataLog.Logger.Out
|
|
||||||
|
|
||||||
// discard normal output
|
|
||||||
kataLog.Logger.Out = devNull
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
kataLog.Logger.Out = savedLogOutput
|
|
||||||
}()
|
|
||||||
|
|
||||||
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, cpuData, moduleData)
|
|
||||||
|
|
||||||
ctx := createCLIContext(nil)
|
|
||||||
ctx.App.Name = "foo"
|
|
||||||
ctx.App.Metadata["runtimeConfig"] = config
|
|
||||||
|
|
||||||
// create buffer to save logger output
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
|
|
||||||
// capture output this time
|
|
||||||
kataLog.Logger.Out = buf
|
|
||||||
|
|
||||||
fn, ok := kataCheckCLICommand.Action.(func(context *cli.Context) error)
|
|
||||||
assert.True(ok)
|
|
||||||
|
|
||||||
err = fn(ctx)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
for _, m := range moduleData {
|
|
||||||
name := path.Base(m.path)
|
|
||||||
assert.True(findAnchoredString(output, name))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArchKernelParamHandler(t *testing.T) {
|
func TestArchKernelParamHandler(t *testing.T) {
|
||||||
|
@ -6,17 +6,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) {
|
||||||
@ -45,81 +42,15 @@ func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCCCheckCLIFunction(t *testing.T) {
|
func TestCCCheckCLIFunction(t *testing.T) {
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
_, config, err := makeRuntimeConfig(dir)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
savedSysModuleDir := sysModuleDir
|
|
||||||
savedProcCPUInfo := procCPUInfo
|
|
||||||
|
|
||||||
cpuInfoFile := filepath.Join(dir, "cpuinfo")
|
|
||||||
|
|
||||||
// XXX: override
|
|
||||||
sysModuleDir = filepath.Join(dir, "sys/module")
|
|
||||||
procCPUInfo = cpuInfoFile
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
sysModuleDir = savedSysModuleDir
|
|
||||||
procCPUInfo = savedProcCPUInfo
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = os.MkdirAll(sysModuleDir, testDirMode)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cpuData := []testCPUData{
|
cpuData := []testCPUData{
|
||||||
{"", "", false},
|
fakeCPUData,
|
||||||
}
|
}
|
||||||
|
|
||||||
moduleData := []testModuleData{
|
moduleData := []testModuleData{
|
||||||
{filepath.Join(sysModuleDir, "kvm"), false, "Y"},
|
{filepath.Join(sysModuleDir, "kvm"), false, "Y"},
|
||||||
}
|
}
|
||||||
|
|
||||||
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
|
genericCheckCLIFunction(t, cpuData, moduleData)
|
||||||
assert.NoError(err)
|
|
||||||
defer devNull.Close()
|
|
||||||
|
|
||||||
savedLogOutput := kataLog.Logger.Out
|
|
||||||
|
|
||||||
// discard normal output
|
|
||||||
kataLog.Logger.Out = devNull
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
kataLog.Logger.Out = savedLogOutput
|
|
||||||
}()
|
|
||||||
|
|
||||||
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, cpuData, moduleData)
|
|
||||||
|
|
||||||
ctx := createCLIContext(nil)
|
|
||||||
ctx.App.Name = "foo"
|
|
||||||
ctx.App.Metadata["runtimeConfig"] = config
|
|
||||||
|
|
||||||
// create buffer to save logger output
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
|
|
||||||
// capture output this time
|
|
||||||
kataLog.Logger.Out = buf
|
|
||||||
|
|
||||||
fn, ok := kataCheckCLICommand.Action.(func(context *cli.Context) error)
|
|
||||||
assert.True(ok)
|
|
||||||
|
|
||||||
err = fn(ctx)
|
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
for _, m := range moduleData {
|
|
||||||
name := path.Base(m.path)
|
|
||||||
assert.True(findAnchoredString(output, name))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArchKernelParamHandler(t *testing.T) {
|
func TestArchKernelParamHandler(t *testing.T) {
|
||||||
|
@ -7,12 +7,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
ktu "github.com/kata-containers/runtime/pkg/katatestutils"
|
ktu "github.com/kata-containers/runtime/pkg/katatestutils"
|
||||||
@ -43,6 +45,8 @@ type testCPUDetail struct {
|
|||||||
expectError bool
|
expectError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fakeCPUData = testCPUData{"", "", false}
|
||||||
|
|
||||||
func createFile(file, contents string) error {
|
func createFile(file, contents string) error {
|
||||||
return ioutil.WriteFile(file, []byte(contents), testFileMode)
|
return ioutil.WriteFile(file, []byte(contents), testFileMode)
|
||||||
}
|
}
|
||||||
@ -189,6 +193,92 @@ func genericTestGetCPUDetails(t *testing.T, validVendor string, validModel strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genericCheckCLIFunction(t *testing.T, cpuData []testCPUData, moduleData []testModuleData) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
dir, err := ioutil.TempDir("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
_, config, err := makeRuntimeConfig(dir)
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
|
savedSysModuleDir := sysModuleDir
|
||||||
|
savedProcCPUInfo := procCPUInfo
|
||||||
|
|
||||||
|
cpuInfoFile := filepath.Join(dir, "cpuinfo")
|
||||||
|
|
||||||
|
// XXX: override
|
||||||
|
sysModuleDir = filepath.Join(dir, "sys/module")
|
||||||
|
procCPUInfo = cpuInfoFile
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
sysModuleDir = savedSysModuleDir
|
||||||
|
procCPUInfo = savedProcCPUInfo
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Replace sysModuleDir in moduleData with the test temp path
|
||||||
|
for i := range moduleData {
|
||||||
|
moduleData[i].path = strings.Replace(moduleData[i].path, savedSysModuleDir, sysModuleDir, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.MkdirAll(sysModuleDir, testDirMode)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
|
||||||
|
assert.NoError(err)
|
||||||
|
defer devNull.Close()
|
||||||
|
|
||||||
|
savedLogOutput := kataLog.Logger.Out
|
||||||
|
|
||||||
|
// discard normal output
|
||||||
|
kataLog.Logger.Out = devNull
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
kataLog.Logger.Out = savedLogOutput
|
||||||
|
}()
|
||||||
|
|
||||||
|
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, cpuData, moduleData)
|
||||||
|
|
||||||
|
flagSet := &flag.FlagSet{}
|
||||||
|
ctx := createCLIContext(flagSet)
|
||||||
|
ctx.App.Name = "foo"
|
||||||
|
ctx.App.Metadata["runtimeConfig"] = config
|
||||||
|
|
||||||
|
// create buffer to save logger output
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
// capture output this time
|
||||||
|
kataLog.Logger.Out = buf
|
||||||
|
|
||||||
|
fn, ok := kataCheckCLICommand.Action.(func(context *cli.Context) error)
|
||||||
|
assert.True(ok)
|
||||||
|
|
||||||
|
err = fn(ctx)
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
|
output := buf.String()
|
||||||
|
|
||||||
|
for _, c := range cpuData {
|
||||||
|
if c == fakeCPUData {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.True(findAnchoredString(output, c.vendorID))
|
||||||
|
for _, flag := range strings.Fields(c.flags) {
|
||||||
|
assert.True(findAnchoredString(output, flag))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, m := range moduleData {
|
||||||
|
name := path.Base(m.path)
|
||||||
|
assert.True(findAnchoredString(output, name))
|
||||||
|
}
|
||||||
|
}
|
||||||
func TestCheckGetCPUInfo(t *testing.T) {
|
func TestCheckGetCPUInfo(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
@ -671,7 +761,8 @@ func TestCheckCLIFunctionFail(t *testing.T) {
|
|||||||
procCPUInfo = oldProcCPUInfo
|
procCPUInfo = oldProcCPUInfo
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := createCLIContext(nil)
|
flagSet := &flag.FlagSet{}
|
||||||
|
ctx := createCLIContext(flagSet)
|
||||||
ctx.App.Name = "foo"
|
ctx.App.Name = "foo"
|
||||||
ctx.App.Metadata["runtimeConfig"] = config
|
ctx.App.Metadata["runtimeConfig"] = config
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user