runtime: Let MockFSInit create a mock fs driver at any path

Currently MockFSInit always creates the mockfs at the fixed path
/tmp/vc/mockfs.  This change allows it to be initialized at any path
given as a parameter.  This allows the tests in fs_test.go to be
simplified, because the by using a temporary directory from
t.TempDir(), which is automatically cleaned up, we don't need to
manually trigger initTestDir() (which is misnamed, it's actually a
cleanup function).

For now we still use the fixed path when auto-creating the mockfs in
MockAutoInit(), but we'll change that later.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2022-04-06 15:02:48 +10:00
parent 5d8438e939
commit ef6d54a781
3 changed files with 10 additions and 24 deletions

View File

@ -14,8 +14,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func getFsDriver() (*FS, error) { func getFsDriver(t *testing.T) (*FS, error) {
driver, err := MockFSInit() driver, err := MockFSInit(t.TempDir())
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to init fs driver") return nil, fmt.Errorf("failed to init fs driver")
} }
@ -27,16 +27,8 @@ func getFsDriver() (*FS, error) {
return fs.FS, nil return fs.FS, nil
} }
func initTestDir() func() {
return func() {
os.RemoveAll(MockStorageRootPath())
}
}
func TestFsLockShared(t *testing.T) { func TestFsLockShared(t *testing.T) {
defer initTestDir()() fs, err := getFsDriver(t)
fs, err := getFsDriver()
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, fs) assert.NotNil(t, fs)
@ -61,9 +53,7 @@ func TestFsLockShared(t *testing.T) {
} }
func TestFsLockExclusive(t *testing.T) { func TestFsLockExclusive(t *testing.T) {
defer initTestDir()() fs, err := getFsDriver(t)
fs, err := getFsDriver()
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, fs) assert.NotNil(t, fs)
@ -89,9 +79,7 @@ func TestFsLockExclusive(t *testing.T) {
} }
func TestFsDriver(t *testing.T) { func TestFsDriver(t *testing.T) {
defer initTestDir()() fs, err := getFsDriver(t)
fs, err := getFsDriver()
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, fs) assert.NotNil(t, fs)
@ -162,12 +150,10 @@ func TestFsDriver(t *testing.T) {
} }
func TestGlobalReadWrite(t *testing.T) { func TestGlobalReadWrite(t *testing.T) {
defer initTestDir()()
relPath := "test/123/aaa.json" relPath := "test/123/aaa.json"
data := "hello this is testing global read write" data := "hello this is testing global read write"
fs, err := getFsDriver() fs, err := getFsDriver(t)
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, fs) assert.NotNil(t, fs)

View File

@ -36,7 +36,7 @@ func MockRunVMStoragePath() string {
return filepath.Join(MockStorageRootPath(), vmPathSuffix) return filepath.Join(MockStorageRootPath(), vmPathSuffix)
} }
func MockFSInit() (persistapi.PersistDriver, error) { func MockFSInit(rootPath string) (persistapi.PersistDriver, error) {
driver, err := Init() driver, err := Init()
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not create Mock FS driver: %v", err) return nil, fmt.Errorf("Could not create Mock FS driver: %v", err)
@ -47,7 +47,7 @@ func MockFSInit() (persistapi.PersistDriver, error) {
return nil, fmt.Errorf("Could not create Mock FS driver") return nil, fmt.Errorf("Could not create Mock FS driver")
} }
fsDriver.storageRootPath = MockStorageRootPath() fsDriver.storageRootPath = rootPath
fsDriver.driverName = "mockfs" fsDriver.driverName = "mockfs"
return &MockFS{fsDriver}, nil return &MockFS{fsDriver}, nil
@ -55,7 +55,7 @@ func MockFSInit() (persistapi.PersistDriver, error) {
func MockAutoInit() (persistapi.PersistDriver, error) { func MockAutoInit() (persistapi.PersistDriver, error) {
if mockTesting { if mockTesting {
return MockFSInit() return MockFSInit(MockStorageRootPath())
} }
return nil, nil return nil, nil
} }

View File

@ -28,7 +28,7 @@ func TestMockAutoInit(t *testing.T) {
mockTesting = true mockTesting = true
fsd, err = MockAutoInit() fsd, err = MockAutoInit()
assert.NoError(err) assert.NoError(err)
expectedFS, err := MockFSInit() expectedFS, err := MockFSInit(MockStorageRootPath())
assert.NoError(err) assert.NoError(err)
assert.Equal(expectedFS, fsd) assert.Equal(expectedFS, fsd)
} }