Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers/persist: introduce mock fs driver
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent ea8fb96 commit dd2762f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions virtcontainers/persist/fs/mockfs.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit dd2762f

Please sign in to comment.