From 3f5c6e7182b798504df1e2cacf4f7d7d813f5655 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 6 Apr 2022 15:12:49 +1000 Subject: [PATCH] runtime: Allock mockfs storage to be placed in any directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (cherry picked from commit 1b931f420339f8849e98517b38ba609a7fa7c9d3) Signed-off-by: Fabiano FidĂȘncio --- src/runtime/virtcontainers/persist/fs/mockfs.go | 14 ++++++++------ .../virtcontainers/persist/fs/mockfs_test.go | 8 ++++---- src/runtime/virtcontainers/virtcontainers_test.go | 6 ++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/runtime/virtcontainers/persist/fs/mockfs.go b/src/runtime/virtcontainers/persist/fs/mockfs.go index dca4acad42..18045f1ee5 100644 --- a/src/runtime/virtcontainers/persist/fs/mockfs.go +++ b/src/runtime/virtcontainers/persist/fs/mockfs.go @@ -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 diff --git a/src/runtime/virtcontainers/persist/fs/mockfs_test.go b/src/runtime/virtcontainers/persist/fs/mockfs_test.go index 9cd5832895..f2c1abd766 100644 --- a/src/runtime/virtcontainers/persist/fs/mockfs_test.go +++ b/src/runtime/virtcontainers/persist/fs/mockfs_test.go @@ -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()) diff --git a/src/runtime/virtcontainers/virtcontainers_test.go b/src/runtime/virtcontainers/virtcontainers_test.go index 6a3d7fa580..b7117000ce 100644 --- a/src/runtime/virtcontainers/virtcontainers_test.go +++ b/src/runtime/virtcontainers/virtcontainers_test.go @@ -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 {