runtime: Allock mockfs storage to be placed in any directory

Currently EnableMockTesting() takes no arguments and will always place the
mock storage in the fixed location /tmp/vc/mockfs.  This means that one
test run can interfere with the next one if anything isn't cleaned up
(and there are other bugs which means that happens).  If if those were
fixed this would allow developers testing on the same machine to interfere
with each other.

So, allow the mockfs to be placed at an arbitrary place given as a
parameter to EnableMockTesting().  In TestMain() we place it under our
existing temporary directory, so we don't need any additional cleanup just
for the mockfs.

fixes #4140

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2022-04-06 15:12:49 +10:00
parent ef6d54a781
commit 1b931f4203
3 changed files with 14 additions and 14 deletions

View File

@ -7,25 +7,27 @@ package fs
import (
"fmt"
"os"
"path/filepath"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
)
var mockTesting = false
var mockRootPath = ""
type MockFS struct {
// inherit from FS. Overwrite if needed.
*FS
}
func EnableMockTesting() {
mockTesting = true
func EnableMockTesting(rootPath string) {
mockRootPath = rootPath
}
func MockStorageRootPath() string {
return filepath.Join(os.TempDir(), "vc", "mockfs")
if mockRootPath == "" {
panic("Using uninitialized mock storage root path")
}
return mockRootPath
}
func MockRunStoragePath() string {
@ -54,7 +56,7 @@ func MockFSInit(rootPath string) (persistapi.PersistDriver, error) {
}
func MockAutoInit() (persistapi.PersistDriver, error) {
if mockTesting {
if mockRootPath != "" {
return MockFSInit(MockStorageRootPath())
}
return nil, nil

View File

@ -13,19 +13,19 @@ import (
func TestMockAutoInit(t *testing.T) {
assert := assert.New(t)
orgMockTesting := mockTesting
orgMockRootPath := mockRootPath
defer func() {
mockTesting = orgMockTesting
mockRootPath = orgMockRootPath
}()
mockTesting = false
mockRootPath = ""
fsd, err := MockAutoInit()
assert.Nil(fsd)
assert.NoError(err)
// Testing mock driver
mockTesting = true
mockRootPath = t.TempDir()
fsd, err = MockAutoInit()
assert.NoError(err)
expectedFS, err := MockFSInit(MockStorageRootPath())

View File

@ -57,8 +57,6 @@ var testHyperstartTtySocket = ""
// cleanUp Removes any stale sandbox/container state that can affect
// the next test to run.
func cleanUp() {
os.RemoveAll(fs.MockRunStoragePath())
os.RemoveAll(fs.MockRunVMStoragePath())
syscall.Unmount(GetSharePath(testSandboxID), syscall.MNT_DETACH|UmountNoFollow)
os.RemoveAll(testDir)
os.MkdirAll(testDir, DirMode)
@ -107,8 +105,6 @@ func setupClh() {
func TestMain(m *testing.M) {
var err error
fs.EnableMockTesting()
flag.Parse()
logger := logrus.NewEntry(logrus.New())
@ -125,6 +121,8 @@ func TestMain(m *testing.M) {
panic(err)
}
fs.EnableMockTesting(filepath.Join(testDir, "mockfs"))
fmt.Printf("INFO: Creating virtcontainers test directory %s\n", testDir)
err = os.MkdirAll(testDir, DirMode)
if err != nil {