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

View File

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

View File

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