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

Commit

Permalink
virtcontainers: support new persist API
Browse files Browse the repository at this point in the history
Fix API, container and kata implementations and unit tests to support
the new persist API

Signed-off-by: Julio Montes <julio.montes@intel.com>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent 9585bc9 commit 11bd456
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 65 deletions.
9 changes: 6 additions & 3 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
98 changes: 62 additions & 36 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1682,6 +1705,7 @@ func TestNetworkOperation(t *testing.T) {

func TestCleanupContainer(t *testing.T) {
config := newTestSandboxConfigNoop()
assert := assert.New(t)

ctx := context.Background()

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/kata_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions virtcontainers/kata_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

package virtcontainers

import (
"testing"
)
import "testing"

func TestKataProxyStart(t *testing.T) {
agent := &kataAgent{}
Expand Down
8 changes: 6 additions & 2 deletions virtcontainers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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{
Expand Down
Loading

0 comments on commit 11bd456

Please sign in to comment.