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

Commit

Permalink
Merge pull request #2371 from bergwolf/ut
Browse files Browse the repository at this point in the history
ut: fix make test failures
  • Loading branch information
lifupan authored Dec 27, 2019
2 parents b9120b2 + 3ed472d commit 3ea3d32
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion virtcontainers/factory/direct/direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions virtcontainers/persist/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package fs

import (
"fmt"
"io/ioutil"
"os"
"testing"

Expand All @@ -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)
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions virtcontainers/pkg/nsenter/nsenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion virtcontainers/store/filesystem_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions virtcontainers/store/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions virtcontainers/virtcontainers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3ea3d32

Please sign in to comment.