From dd2762fdadeea6c0e12d71e33ee591f702ffcd8f Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Fri, 31 Jan 2020 19:42:47 +0000 Subject: [PATCH] virtcontainers/persist: introduce mock fs driver Mock FS driver can be used in unit testing to allow Mock fs driver inherits from FS and may overwrite its methods. All files and directories created by this driver are under a path accessible for all users, this path is created under the system temporal directory. Signed-off-by: Julio Montes --- virtcontainers/persist/fs/mockfs.go | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 virtcontainers/persist/fs/mockfs.go diff --git a/virtcontainers/persist/fs/mockfs.go b/virtcontainers/persist/fs/mockfs.go new file mode 100644 index 0000000000..bbbc27d160 --- /dev/null +++ b/virtcontainers/persist/fs/mockfs.go @@ -0,0 +1,52 @@ +// Copyright (c) 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +package fs + +import ( + "fmt" + "os" + "path/filepath" + + persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" +) + +type MockFS struct { + // inherit from FS. Overwrite if needed. + *FS +} + +func MockStorageRootPath() string { + return filepath.Join(os.TempDir(), "vc", "mockfs") +} + +func MockRunStoragePath() string { + return filepath.Join(MockStorageRootPath(), sandboxPathSuffix) +} + +func MockRunVMStoragePath() string { + return filepath.Join(MockStorageRootPath(), vmPathSuffix) +} + +func MockStorageDestroy() { + os.RemoveAll(MockStorageRootPath()) +} + +func MockFSInit() (persistapi.PersistDriver, error) { + driver, err := Init() + if err != nil { + return nil, fmt.Errorf("Could not create Mock FS driver: %v", err) + } + + fsDriver, ok := driver.(*FS) + if !ok { + return nil, fmt.Errorf("Could not create Mock FS driver") + } + + fsDriver.storageRootPath = MockStorageRootPath() + fsDriver.driverName = "mockfs" + + return &MockFS{fsDriver}, nil +}