mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 15:32:30 +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("", "")
|
||||
return "file://" + dir, dir
|
||||
}()
|
||||
var testDir = ""
|
||||
var ConfigStoragePathSaved = func() string { return "" }
|
||||
var RunStoragePathSaved = func() string { return "" }
|
||||
|
||||
func TestNewStore(t *testing.T) {
|
||||
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
|
||||
// for the store.
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ConfigStoragePathSaved := ConfigStoragePath
|
||||
RunStoragePathSaved := RunStoragePath
|
||||
ConfigStoragePathSaved = ConfigStoragePath
|
||||
RunStoragePathSaved = RunStoragePath
|
||||
// allow the tests to run without affecting the host system.
|
||||
ConfigStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "config") }
|
||||
RunStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "run") }
|
||||
|
||||
defer func() {
|
||||
ConfigStoragePath = ConfigStoragePathSaved
|
||||
RunStoragePath = RunStoragePathSaved
|
||||
}()
|
||||
|
||||
// set now that ConfigStoragePath has been overridden.
|
||||
sandboxDirConfig = filepath.Join(ConfigStoragePath(), testSandboxID)
|
||||
sandboxFileConfig = filepath.Join(ConfigStoragePath(), testSandboxID, ConfigurationFile)
|
||||
@ -134,8 +146,4 @@ func TestMain(m *testing.M) {
|
||||
sandboxDirLock = filepath.Join(RunStoragePath(), testSandboxID)
|
||||
sandboxFileState = filepath.Join(RunStoragePath(), testSandboxID, StateFile)
|
||||
sandboxFileLock = filepath.Join(RunStoragePath(), testSandboxID, LockFile)
|
||||
|
||||
ret := m.Run()
|
||||
|
||||
os.Exit(ret)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user