mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 23:38:31 +00:00
unit-test: reconstuct TestMain
os.Exit will skip all deferred instructions. So we should reconstruct TestMain to leave all setup-related code in setup(), and all cleanup-related code in shutdown(). Fixes: #2398 Signed-off-by: Penny Zheng <penny.zheng@arm.com>
This commit is contained in:
parent
d11696de9a
commit
aa62781aa7
@ -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)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user