Merge pull request #2371 from bergwolf/ut

ut: fix make test failures
This commit is contained in:
Fupan Li 2019-12-27 08:40:18 +08:00 committed by GitHub
commit 3ea3d3201b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 7 deletions

View File

@ -160,7 +160,9 @@ func TestMain(m *testing.M) {
// Make sure we have the opportunity to flush the coverage report to disk when
// terminating the process.
atexit(cover.FlushProfiles)
defer func() {
cover.FlushProfiles()
}()
// If the test binary name is kata-runtime.coverage, we've are being asked to
// run the coverage-instrumented kata-runtime.

View File

@ -8,17 +8,26 @@ package direct
import (
"context"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/store"
)
func TestTemplateFactory(t *testing.T) {
assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
assert.Nil(err)
store.VCStorePrefix = testDir
defer func() {
os.RemoveAll(testDir)
store.VCStorePrefix = ""
}()
hyperConfig := vc.HypervisorConfig{
KernelPath: testDir,
ImagePath: testDir,

View File

@ -7,6 +7,7 @@ package fs
import (
"fmt"
"io/ioutil"
"os"
"testing"
@ -32,6 +33,13 @@ func TestFsLock(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, fs)
testDir, err := ioutil.TempDir("", "fs-tmp-")
assert.Nil(t, err)
TestSetRunStoragePath(testDir)
defer func() {
os.RemoveAll(testDir)
}()
fs.sandboxState.SandboxContainer = "test-fs-driver"
sandboxDir, err := fs.sandboxDir()
assert.Nil(t, err)
@ -51,6 +59,13 @@ func TestFsDriver(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, fs)
testDir, err := ioutil.TempDir("", "fs-tmp-")
assert.Nil(t, err)
TestSetRunStoragePath(testDir)
defer func() {
os.RemoveAll(testDir)
}()
ss := persistapi.SandboxState{}
cs := make(map[string]persistapi.ContainerState)
// missing sandbox container id

View File

@ -18,10 +18,14 @@ import (
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
ktu "github.com/kata-containers/runtime/pkg/katatestutils"
)
const testPID = 12345
var tu = ktu.NewTestConstraint(true)
func TestGetNSPathFromPID(t *testing.T) {
for nsType := range CloneFlagsTable {
expectedPath := fmt.Sprintf("/proc/%d/ns/%s", testPID, nsType)
@ -165,6 +169,9 @@ func TestNsEnterEmptyNamespaceListSuccess(t *testing.T) {
}
func TestNsEnterSuccessful(t *testing.T) {
if tu.NotValid(ktu.NeedRoot()) {
t.Skip(ktu.TestDisabledNeedRoot)
}
nsList := supportedNamespaces()
sleepDuration := 60

View File

@ -20,7 +20,11 @@ type TestNoopStructure struct {
Field2 string
}
var rootPath = "/tmp/root1/"
var rootPath = func() string {
dir, _ := ioutil.TempDir("", "")
return dir
}()
var expectedFilesystemData = "{\"Field1\":\"value1\",\"Field2\":\"value2\"}"
func TestStoreFilesystemStore(t *testing.T) {

View File

@ -23,14 +23,17 @@ var sandboxDirState = ""
var sandboxDirLock = ""
var sandboxFileState = ""
var sandboxFileLock = ""
var storeRoot = "file:///tmp/root1/"
var storeRoot, storeRootDir = func() (string, string) {
dir, _ := ioutil.TempDir("", "")
return "file://" + dir, dir
}()
func TestNewStore(t *testing.T) {
s, err := New(context.Background(), storeRoot)
assert.Nil(t, err)
assert.Equal(t, s.scheme, "file")
assert.Equal(t, s.host, "")
assert.Equal(t, s.path, "/tmp/root1/")
assert.Equal(t, s.path, storeRootDir)
}
func TestDeleteStore(t *testing.T) {

View File

@ -57,19 +57,28 @@ var testVirtiofsdPath = ""
var testHyperstartCtlSocket = ""
var testHyperstartTtySocket = ""
var savedRunVMStoragePathFunc func() string
// cleanUp Removes any stale sandbox/container state that can affect
// the next test to run.
func cleanUp() {
globalSandboxList.removeSandbox(testSandboxID)
store.DeleteAll()
os.RemoveAll(testDir)
os.MkdirAll(testDir, store.DirMode)
store.VCStorePrefix = ""
store.RunVMStoragePath = savedRunVMStoragePathFunc
setup()
}
func setup() {
os.Mkdir(filepath.Join(testDir, testBundle), store.DirMode)
store.VCStorePrefix = testDir
savedRunVMStoragePathFunc = store.RunVMStoragePath
store.RunVMStoragePath = func() string {
return filepath.Join("testDir", "vm")
}
os.MkdirAll(store.RunVMStoragePath(), store.DirMode)
os.MkdirAll(filepath.Join(testDir, testBundle), store.DirMode)
for _, filename := range []string{testQemuKernelPath, testQemuInitrdPath, testQemuImagePath, testQemuPath} {
_, err := os.Create(filename)