diff --git a/virtcontainers/api.go b/virtcontainers/api.go index 1024d28e44..f54214c925 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -13,7 +13,7 @@ import ( deviceApi "github.com/kata-containers/runtime/virtcontainers/device/api" deviceConfig "github.com/kata-containers/runtime/virtcontainers/device/config" - "github.com/kata-containers/runtime/virtcontainers/persist/fs" + "github.com/kata-containers/runtime/virtcontainers/persist" "github.com/kata-containers/runtime/virtcontainers/pkg/compatoci" vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/types" @@ -308,9 +308,12 @@ func ListSandbox(ctx context.Context) ([]SandboxStatus, error) { span, ctx := trace(ctx, "ListSandbox") defer span.Finish() - sbsdir := fs.RunStoragePath() + store, err := persist.GetDriver() + if err != nil { + return []SandboxStatus{}, err + } - dir, err := os.Open(sbsdir) + dir, err := os.Open(store.RunStoragePath()) if err != nil { if os.IsNotExist(err) { // No sandbox directory is not an error diff --git a/virtcontainers/api_test.go b/virtcontainers/api_test.go index 7461ef130a..6d7061614b 100644 --- a/virtcontainers/api_test.go +++ b/virtcontainers/api_test.go @@ -17,7 +17,6 @@ import ( ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/virtcontainers/persist" - "github.com/kata-containers/runtime/virtcontainers/persist/fs" "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" "github.com/kata-containers/runtime/virtcontainers/pkg/mock" "github.com/kata-containers/runtime/virtcontainers/pkg/rootless" @@ -75,7 +74,7 @@ func newBasicTestCmd() types.Cmd { } func rmSandboxDir(sid string) error { - store, err := persist.GetDriver("fs") + store, err := persist.GetDriver() if err != nil { return fmt.Errorf("failed to get fs persist driver: %v", err) } @@ -154,7 +153,9 @@ func TestCreateSandboxNoopAgentSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) } @@ -191,7 +192,9 @@ func TestCreateSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) } @@ -218,7 +221,9 @@ func TestDeleteSandboxNoopAgentSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -263,7 +268,9 @@ func TestDeleteSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -279,9 +286,6 @@ func TestDeleteSandboxFailing(t *testing.T) { defer cleanUp() assert := assert.New(t) - sandboxDir := filepath.Join(fs.RunStoragePath(), testSandboxID) - os.Remove(sandboxDir) - p, err := DeleteSandbox(context.Background(), testSandboxID) assert.Error(err) assert.Nil(p) @@ -343,9 +347,6 @@ func TestStartSandboxFailing(t *testing.T) { defer cleanUp() assert := assert.New(t) - sandboxDir := filepath.Join(fs.RunStoragePath(), testSandboxID) - os.Remove(sandboxDir) - p, err := StartSandbox(context.Background(), testSandboxID) assert.Error(err) assert.Nil(p) @@ -410,9 +411,6 @@ func TestStopSandboxKataAgentSuccessful(t *testing.T) { func TestStopSandboxFailing(t *testing.T) { defer cleanUp() - sandboxDir := filepath.Join(fs.RunStoragePath(), testSandboxID) - os.Remove(sandboxDir) - p, err := StopSandbox(context.Background(), testSandboxID, false) assert.Error(t, err) assert.Nil(t, p) @@ -428,7 +426,9 @@ func TestRunSandboxNoopAgentSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) } @@ -466,13 +466,13 @@ func TestRunSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) - _, err = os.Stat(sandboxDir) - assert.NoError(err) - pImpl, ok := p.(*Sandbox) assert.True(ok) + sandboxDir := filepath.Join(pImpl.newStore.RunStoragePath(), p.ID()) + _, err = os.Stat(sandboxDir) + assert.NoError(err) + err = bindUnmountAllRootfs(ctx, testDir, pImpl) assert.NoError(err) } @@ -693,7 +693,9 @@ func TestCreateContainerSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -724,7 +726,9 @@ func TestCreateContainerFailingNoSandbox(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.Error(err) @@ -747,7 +751,9 @@ func TestDeleteContainerSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -791,7 +797,9 @@ func TestDeleteContainerFailingNoContainer(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -848,7 +856,9 @@ func TestStartContainerFailingNoContainer(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -869,7 +879,9 @@ func TestStartContainerFailingSandboxNotStarted(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -949,7 +961,9 @@ func TestStopContainerFailingNoContainer(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -1053,7 +1067,9 @@ func TestEnterContainerFailingNoContainer(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -1106,7 +1122,9 @@ func TestStatusContainerSuccessful(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + pImpl, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(pImpl.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -1123,9 +1141,6 @@ func TestStatusContainerSuccessful(t *testing.T) { status, err := StatusContainer(ctx, p.ID(), contID) assert.NoError(err) - pImpl, ok := p.(*Sandbox) - assert.True(ok) - cImpl, ok := c.(*Container) assert.True(ok) @@ -1149,7 +1164,9 @@ func TestStatusContainerStateReady(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -1212,7 +1229,9 @@ func TestStatusContainerStateRunning(t *testing.T) { assert.NoError(err) assert.NotNil(p) - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -1424,7 +1443,11 @@ func createAndStartSandbox(ctx context.Context, config SandboxConfig) (sandbox V return nil, "", err } - sandboxDir = filepath.Join(fs.RunStoragePath(), sandbox.ID()) + s, ok := sandbox.(*Sandbox) + if !ok { + return nil, "", fmt.Errorf("Could not get Sandbox") + } + sandboxDir = filepath.Join(s.newStore.RunStoragePath(), sandbox.ID()) _, err = os.Stat(sandboxDir) if err != nil { return nil, "", err @@ -1682,6 +1705,7 @@ func TestNetworkOperation(t *testing.T) { func TestCleanupContainer(t *testing.T) { config := newTestSandboxConfigNoop() + assert := assert.New(t) ctx := context.Background() @@ -1709,7 +1733,9 @@ func TestCleanupContainer(t *testing.T) { CleanupContainer(ctx, p.ID(), c.ID(), true) } - sandboxDir := filepath.Join(fs.RunStoragePath(), p.ID()) + s, ok := p.(*Sandbox) + assert.True(ok) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) if err == nil { diff --git a/virtcontainers/container_test.go b/virtcontainers/container_test.go index 9b003b66eb..b11e83aff3 100644 --- a/virtcontainers/container_test.go +++ b/virtcontainers/container_test.go @@ -316,7 +316,7 @@ func TestContainerAddDriveDir(t *testing.T) { }, } - sandbox.newStore, err = persist.GetDriver("fs") + sandbox.newStore, err = persist.GetDriver() assert.NoError(err) assert.NotNil(sandbox.newStore) diff --git a/virtcontainers/kata_agent_test.go b/virtcontainers/kata_agent_test.go index e0d5d0d69d..6d40286659 100644 --- a/virtcontainers/kata_agent_test.go +++ b/virtcontainers/kata_agent_test.go @@ -621,8 +621,8 @@ func TestAgentPathAPI(t *testing.T) { id := "foobar" // getSharePath - path1 = k1.getSharePath(id) - path2 = k2.getSharePath(id) + path1 := k1.getSharePath(id) + path2 := k2.getSharePath(id) assert.Equal(path1, path2) } @@ -710,7 +710,7 @@ func TestAgentCreateContainer(t *testing.T) { hypervisor: &mockHypervisor{}, } - newStore, err := persist.GetDriver("fs") + newStore, err := persist.GetDriver() assert.NoError(err) assert.NotNil(newStore) sandbox.newStore = newStore diff --git a/virtcontainers/kata_proxy_test.go b/virtcontainers/kata_proxy_test.go index d14fc7bcb5..b4a889b49e 100644 --- a/virtcontainers/kata_proxy_test.go +++ b/virtcontainers/kata_proxy_test.go @@ -5,9 +5,7 @@ package virtcontainers -import ( - "testing" -) +import "testing" func TestKataProxyStart(t *testing.T) { agent := &kataAgent{} diff --git a/virtcontainers/proxy.go b/virtcontainers/proxy.go index 86de050d92..4614eaf44d 100644 --- a/virtcontainers/proxy.go +++ b/virtcontainers/proxy.go @@ -14,7 +14,7 @@ import ( "strings" kataclient "github.com/kata-containers/agent/protocols/client" - "github.com/kata-containers/runtime/virtcontainers/persist/fs" + "github.com/kata-containers/runtime/virtcontainers/persist" "github.com/sirupsen/logrus" ) @@ -146,7 +146,11 @@ func validateProxyConfig(proxyConfig ProxyConfig) error { func defaultProxyURL(id, socketType string) (string, error) { switch socketType { case SocketTypeUNIX: - socketPath := filepath.Join(filepath.Join(fs.RunStoragePath(), id), "proxy.sock") + store, err := persist.GetDriver() + if err != nil { + return "", err + } + socketPath := filepath.Join(filepath.Join(store.RunStoragePath(), id), "proxy.sock") return fmt.Sprintf("unix://%s", socketPath), nil case SocketTypeVSOCK: // TODO Build the VSOCK default URL diff --git a/virtcontainers/proxy_test.go b/virtcontainers/proxy_test.go index af251154c9..b1899d2ca2 100644 --- a/virtcontainers/proxy_test.go +++ b/virtcontainers/proxy_test.go @@ -173,7 +173,7 @@ func testDefaultProxyURL(expectedURL string, socketType string, sandboxID string } func TestDefaultProxyURLUnix(t *testing.T) { - path := filepath.Join(filepath.Join(fs.RunStoragePath(), sandboxID), "proxy.sock") + path := filepath.Join(filepath.Join(fs.MockRunStoragePath(), sandboxID), "proxy.sock") socketPath := fmt.Sprintf("unix://%s", path) assert.NoError(t, testDefaultProxyURL(socketPath, SocketTypeUNIX, sandboxID)) } @@ -183,7 +183,7 @@ func TestDefaultProxyURLVSock(t *testing.T) { } func TestDefaultProxyURLUnknown(t *testing.T) { - path := filepath.Join(filepath.Join(fs.RunStoragePath(), sandboxID), "proxy.sock") + path := filepath.Join(filepath.Join(fs.MockRunStoragePath(), sandboxID), "proxy.sock") socketPath := fmt.Sprintf("unix://%s", path) assert.Error(t, testDefaultProxyURL(socketPath, "foobar", sandboxID)) } @@ -204,7 +204,7 @@ func testProxyStart(t *testing.T, agent agent, proxy proxy) { } invalidPath := filepath.Join(tmpdir, "enoent") - expectedSocketPath := filepath.Join(filepath.Join(fs.RunStoragePath(), testSandboxID), "proxy.sock") + expectedSocketPath := filepath.Join(filepath.Join(fs.MockRunStoragePath(), testSandboxID), "proxy.sock") expectedURI := fmt.Sprintf("unix://%s", expectedSocketPath) data := []testData{ diff --git a/virtcontainers/virtcontainers_test.go b/virtcontainers/virtcontainers_test.go index f7a17bdf34..383310fbc7 100644 --- a/virtcontainers/virtcontainers_test.go +++ b/virtcontainers/virtcontainers_test.go @@ -15,6 +15,7 @@ import ( "path/filepath" "testing" + "github.com/kata-containers/runtime/virtcontainers/persist" "github.com/kata-containers/runtime/virtcontainers/persist/fs" "github.com/kata-containers/runtime/virtcontainers/store" "github.com/kata-containers/runtime/virtcontainers/utils" @@ -56,8 +57,8 @@ var testHyperstartTtySocket = "" // the next test to run. func cleanUp() { globalSandboxList.removeSandbox(testSandboxID) - os.RemoveAll(fs.RunStoragePath()) - os.RemoveAll(fs.RunVMStoragePath()) + os.RemoveAll(fs.MockRunStoragePath()) + os.RemoveAll(fs.MockRunVMStoragePath()) os.RemoveAll(testDir) os.MkdirAll(testDir, DirMode) @@ -109,6 +110,8 @@ func setupClh() { func TestMain(m *testing.M) { var err error + persist.EnableMockTesting() + flag.Parse() logger := logrus.NewEntry(logrus.New()) @@ -161,19 +164,8 @@ func TestMain(m *testing.M) { setupClh() - // allow the tests to run without affecting the host system. - runPathSave := fs.RunStoragePath() - rootPathSave := fs.StorageRootPath() - fs.TestSetRunStoragePath(filepath.Join(testDir, "vc", "run")) - fs.TestSetStorageRootPath(filepath.Join(testDir, "vc")) - - defer func() { - fs.TestSetRunStoragePath(runPathSave) - fs.TestSetStorageRootPath(rootPathSave) - }() - // set now that configStoragePath has been overridden. - sandboxDirState = filepath.Join(fs.RunStoragePath(), testSandboxID) + sandboxDirState = filepath.Join(fs.MockRunStoragePath(), testSandboxID) testHyperstartCtlSocket = filepath.Join(testDir, "test_hyper.sock") testHyperstartTtySocket = filepath.Join(testDir, "test_tty.sock")