mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-01 07:47:15 +00:00
Merge pull request #2399 from Pennyzct/cleanup_dir_temp
unit-test: cleaning up stale files under /tmp
This commit is contained in:
commit
df802cc359
@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -128,6 +129,7 @@ func TestAcrnArchBaseAppendImage(t *testing.T) {
|
|||||||
|
|
||||||
image, err := ioutil.TempFile("", "img")
|
image, err := ioutil.TempFile("", "img")
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
defer os.Remove(image.Name())
|
||||||
err = image.Close()
|
err = image.Close()
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
|
@ -636,6 +636,7 @@ func TestAgentConfigure(t *testing.T) {
|
|||||||
|
|
||||||
dir, err := ioutil.TempDir("", "kata-agent-test")
|
dir, err := ioutil.TempDir("", "kata-agent-test")
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
k := &kataAgent{}
|
k := &kataAgent{}
|
||||||
h := &mockHypervisor{}
|
h := &mockHypervisor{}
|
||||||
@ -758,6 +759,7 @@ func TestAgentCreateContainer(t *testing.T) {
|
|||||||
|
|
||||||
dir, err := ioutil.TempDir("", "kata-agent-test")
|
dir, err := ioutil.TempDir("", "kata-agent-test")
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
err = k.configure(&mockHypervisor{}, sandbox.id, dir, true, KataAgentConfig{})
|
err = k.configure(&mockHypervisor{}, sandbox.id, dir, true, KataAgentConfig{})
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
@ -904,8 +906,10 @@ func TestKataCleanupSandbox(t *testing.T) {
|
|||||||
s := Sandbox{
|
s := Sandbox{
|
||||||
id: "testFoo",
|
id: "testFoo",
|
||||||
}
|
}
|
||||||
dir := path.Join(kataHostSharedDir(), s.id)
|
|
||||||
err := os.MkdirAll(dir, 0777)
|
dir := kataHostSharedDir()
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
err := os.MkdirAll(path.Join(dir, s.id), 0777)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
k := &kataAgent{ctx: context.Background()}
|
k := &kataAgent{ctx: context.Background()}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -287,6 +288,7 @@ func TestQemuArchBaseAppendImage(t *testing.T) {
|
|||||||
|
|
||||||
image, err := ioutil.TempFile("", "img")
|
image, err := ioutil.TempFile("", "img")
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
defer os.Remove(image.Name())
|
||||||
err = image.Close()
|
err = image.Close()
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
|
@ -27,6 +27,9 @@ var storeRoot, storeRootDir = func() (string, string) {
|
|||||||
dir, _ := ioutil.TempDir("", "")
|
dir, _ := ioutil.TempDir("", "")
|
||||||
return "file://" + dir, dir
|
return "file://" + dir, dir
|
||||||
}()
|
}()
|
||||||
|
var testDir = ""
|
||||||
|
var ConfigStoragePathSaved = func() string { return "" }
|
||||||
|
var RunStoragePathSaved = func() string { return "" }
|
||||||
|
|
||||||
func TestNewStore(t *testing.T) {
|
func TestNewStore(t *testing.T) {
|
||||||
s, err := New(context.Background(), storeRoot)
|
s, err := New(context.Background(), storeRoot)
|
||||||
@ -111,22 +114,31 @@ func TestManagerFindStore(t *testing.T) {
|
|||||||
// TestMain is the common main function used by ALL the test functions
|
// TestMain is the common main function used by ALL the test functions
|
||||||
// for the store.
|
// for the store.
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
testDir, err := ioutil.TempDir("", "store-tmp-")
|
setup()
|
||||||
|
rt := m.Run()
|
||||||
|
shutdown()
|
||||||
|
os.Exit(rt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func shutdown() {
|
||||||
|
os.RemoveAll(testDir)
|
||||||
|
ConfigStoragePath = ConfigStoragePathSaved
|
||||||
|
RunStoragePath = RunStoragePathSaved
|
||||||
|
}
|
||||||
|
|
||||||
|
func setup() {
|
||||||
|
var err error
|
||||||
|
testDir, err = ioutil.TempDir("", "store-tmp-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigStoragePathSaved := ConfigStoragePath
|
ConfigStoragePathSaved = ConfigStoragePath
|
||||||
RunStoragePathSaved := RunStoragePath
|
RunStoragePathSaved = RunStoragePath
|
||||||
// allow the tests to run without affecting the host system.
|
// allow the tests to run without affecting the host system.
|
||||||
ConfigStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "config") }
|
ConfigStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "config") }
|
||||||
RunStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "run") }
|
RunStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "run") }
|
||||||
|
|
||||||
defer func() {
|
|
||||||
ConfigStoragePath = ConfigStoragePathSaved
|
|
||||||
RunStoragePath = RunStoragePathSaved
|
|
||||||
}()
|
|
||||||
|
|
||||||
// set now that ConfigStoragePath has been overridden.
|
// set now that ConfigStoragePath has been overridden.
|
||||||
sandboxDirConfig = filepath.Join(ConfigStoragePath(), testSandboxID)
|
sandboxDirConfig = filepath.Join(ConfigStoragePath(), testSandboxID)
|
||||||
sandboxFileConfig = filepath.Join(ConfigStoragePath(), testSandboxID, ConfigurationFile)
|
sandboxFileConfig = filepath.Join(ConfigStoragePath(), testSandboxID, ConfigurationFile)
|
||||||
@ -134,8 +146,4 @@ func TestMain(m *testing.M) {
|
|||||||
sandboxDirLock = filepath.Join(RunStoragePath(), testSandboxID)
|
sandboxDirLock = filepath.Join(RunStoragePath(), testSandboxID)
|
||||||
sandboxFileState = filepath.Join(RunStoragePath(), testSandboxID, StateFile)
|
sandboxFileState = filepath.Join(RunStoragePath(), testSandboxID, StateFile)
|
||||||
sandboxFileLock = filepath.Join(RunStoragePath(), testSandboxID, LockFile)
|
sandboxFileLock = filepath.Join(RunStoragePath(), testSandboxID, LockFile)
|
||||||
|
|
||||||
ret := m.Run()
|
|
||||||
|
|
||||||
os.Exit(ret)
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ package virtcontainers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kata-containers/runtime/virtcontainers/utils"
|
"github.com/kata-containers/runtime/virtcontainers/utils"
|
||||||
@ -17,7 +18,10 @@ import (
|
|||||||
func TestNewVM(t *testing.T) {
|
func TestNewVM(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
|
||||||
|
assert.Nil(err)
|
||||||
|
defer os.RemoveAll(testDir)
|
||||||
|
|
||||||
config := VMConfig{
|
config := VMConfig{
|
||||||
HypervisorType: MockHypervisor,
|
HypervisorType: MockHypervisor,
|
||||||
AgentType: NoopAgentType,
|
AgentType: NoopAgentType,
|
||||||
@ -31,7 +35,7 @@ func TestNewVM(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
var vm *VM
|
var vm *VM
|
||||||
_, err := NewVM(ctx, config)
|
_, err = NewVM(ctx, config)
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
|
|
||||||
config.HypervisorConfig = hyperConfig
|
config.HypervisorConfig = hyperConfig
|
||||||
@ -82,7 +86,10 @@ func TestVMConfigValid(t *testing.T) {
|
|||||||
err := config.Valid()
|
err := config.Valid()
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
|
|
||||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
|
||||||
|
assert.Nil(err)
|
||||||
|
defer os.RemoveAll(testDir)
|
||||||
|
|
||||||
config.HypervisorConfig = HypervisorConfig{
|
config.HypervisorConfig = HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
InitrdPath: testDir,
|
InitrdPath: testDir,
|
||||||
|
Loading…
Reference in New Issue
Block a user