mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-12 14:48:13 +00:00
Merge pull request #1987 from marcov/kata-check-quiet
kata-check: reduce default output verbosity
This commit is contained in:
commit
af574851be
11
README.md
11
README.md
@ -60,7 +60,7 @@ technologies:
|
|||||||
### Hardware requirements
|
### Hardware requirements
|
||||||
|
|
||||||
The runtime has a built-in command to determine if your host system is capable
|
The runtime has a built-in command to determine if your host system is capable
|
||||||
of running a Kata Container:
|
of running and creating a Kata Container:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ kata-runtime kata-check
|
$ kata-runtime kata-check
|
||||||
@ -68,8 +68,13 @@ $ kata-runtime kata-check
|
|||||||
|
|
||||||
> **Note:**
|
> **Note:**
|
||||||
>
|
>
|
||||||
> If you run the previous command as the `root` user, further checks will be
|
> - By default, only a brief success / failure message is printed.
|
||||||
> performed (e.g. it will check if another incompatible hypervisor is running).
|
> If more details are needed, the `--verbose` flag can be used to display the
|
||||||
|
> list of all the checks performed.
|
||||||
|
>
|
||||||
|
> - `root` permission is needed to check if the system is capable of running
|
||||||
|
> Kata containers. In this case, additional checks are performed (e.g., if another
|
||||||
|
> incompatible hypervisor is running).
|
||||||
|
|
||||||
## Download and install
|
## Download and install
|
||||||
|
|
||||||
|
@ -304,7 +304,18 @@ func genericHostIsVMContainerCapable(details vmContainerCapableDetails) error {
|
|||||||
var kataCheckCLICommand = cli.Command{
|
var kataCheckCLICommand = cli.Command{
|
||||||
Name: checkCmd,
|
Name: checkCmd,
|
||||||
Usage: "tests if system can run " + project,
|
Usage: "tests if system can run " + project,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "verbose, v",
|
||||||
|
Usage: "display the list of checks performed",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
Action: func(context *cli.Context) error {
|
Action: func(context *cli.Context) error {
|
||||||
|
verbose := context.Bool("verbose")
|
||||||
|
if verbose {
|
||||||
|
kataLog.Logger.SetLevel(logrus.InfoLevel)
|
||||||
|
}
|
||||||
ctx, err := cliContextToContext(context)
|
ctx, err := cliContextToContext(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -335,8 +346,7 @@ var kataCheckCLICommand = cli.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fmt.Println(successMessageCapable)
|
||||||
kataLog.Info(successMessageCapable)
|
|
||||||
|
|
||||||
if os.Geteuid() == 0 {
|
if os.Geteuid() == 0 {
|
||||||
err = archHostCanCreateVMContainer(runtimeConfig.HypervisorType)
|
err = archHostCanCreateVMContainer(runtimeConfig.HypervisorType)
|
||||||
@ -344,7 +354,7 @@ var kataCheckCLICommand = cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
kataLog.Info(successMessageCreate)
|
fmt.Println(successMessageCreate)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
16
cli/main.go
16
cli/main.go
@ -260,11 +260,17 @@ func beforeSubcommands(c *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ignoreLogging := false
|
ignoreConfigLogs := false
|
||||||
var traceRootSpan string
|
var traceRootSpan string
|
||||||
|
|
||||||
subCmdIsCheckCmd := (c.NArg() == 1 && (c.Args()[0] == checkCmd))
|
subCmdIsCheckCmd := (c.NArg() >= 1 && (c.Args()[0] == checkCmd))
|
||||||
if !subCmdIsCheckCmd {
|
if subCmdIsCheckCmd {
|
||||||
|
// checkCmd will use the default logrus logger to stderr
|
||||||
|
// raise the logger default level to warn
|
||||||
|
kataLog.Logger.SetLevel(logrus.WarnLevel)
|
||||||
|
// do not print configuration logs for checkCmd
|
||||||
|
ignoreConfigLogs = true
|
||||||
|
} else {
|
||||||
if path := c.GlobalString("log"); path != "" {
|
if path := c.GlobalString("log"); path != "" {
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0640)
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0640)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -300,11 +306,11 @@ func beforeSubcommands(c *cli.Context) error {
|
|||||||
|
|
||||||
if c.NArg() == 1 && c.Args()[0] == envCmd {
|
if c.NArg() == 1 && c.Args()[0] == envCmd {
|
||||||
// simply report the logging setup
|
// simply report the logging setup
|
||||||
ignoreLogging = true
|
ignoreConfigLogs = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
configFile, runtimeConfig, err = katautils.LoadConfiguration(c.GlobalString(configFilePathOption), ignoreLogging, false)
|
configFile, runtimeConfig, err = katautils.LoadConfiguration(c.GlobalString(configFilePathOption), ignoreConfigLogs, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(err)
|
fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user