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

Commit

Permalink
vc/store: fix cache factory ut
Browse files Browse the repository at this point in the history
In order to run the ut as non-root, we need to add a path prefix
for all store paths.

Fixes: #2113

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Oct 8, 2019
1 parent 4863aa9 commit 6ab89e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
7 changes: 7 additions & 0 deletions virtcontainers/factory/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
"github.com/kata-containers/runtime/virtcontainers/store"
)

func TestTemplateFactory(t *testing.T) {
Expand All @@ -33,6 +34,12 @@ func TestTemplateFactory(t *testing.T) {

ctx := context.Background()

var savedStorePath = store.VCStorePrefix
store.VCStorePrefix = testDir
defer func() {
store.VCStorePrefix = savedStorePath
}()

// New
f := New(ctx, 2, direct.New(ctx, vmConfig))

Expand Down
24 changes: 14 additions & 10 deletions virtcontainers/store/vc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"github.com/kata-containers/runtime/virtcontainers/types"
)

// VCStorePrefix is only used for tests to config a temp store dir
var VCStorePrefix = ""

// VCStore is a virtcontainers specific Store.
// Virtcontainers typically needs a configuration Store for
// storing permanent items across reboots.
Expand Down Expand Up @@ -45,6 +48,7 @@ func (s *VCStore) itemToStore(item Item) *Store {
func NewVCStore(ctx context.Context, configRoot, stateRoot string) (*VCStore, error) {
config, err := New(ctx, configRoot)
if err != nil {
fmt.Printf("config root %s\n", configRoot)
return nil, err
}

Expand Down Expand Up @@ -243,12 +247,12 @@ func (s *VCStore) Unlock(token string) error {
// It should look like file:///var/lib/vc/sbs/<sandboxID>/
// Or for rootless: file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/
func SandboxConfigurationRoot(id string) string {
return filesystemScheme + "://" + filepath.Join(ConfigStoragePath(), id)
return filesystemScheme + "://" + SandboxConfigurationRootPath(id)
}

// SandboxConfigurationRootPath returns a virtcontainers sandbox configuration root path.
func SandboxConfigurationRootPath(id string) string {
return filepath.Join(ConfigStoragePath(), id)
return filepath.Join(VCStorePrefix, ConfigStoragePath(), id)
}

// SandboxConfigurationItemPath returns a virtcontainers sandbox configuration item path.
Expand All @@ -262,12 +266,12 @@ func SandboxConfigurationItemPath(id string, item Item) (string, error) {
return "", err
}

return filepath.Join(ConfigStoragePath(), id, itemFile), nil
return filepath.Join(VCStorePrefix, ConfigStoragePath(), id, itemFile), nil
}

// VCStoreUUIDPath returns a virtcontainers runtime uuid URL.
func VCStoreUUIDPath() string {
return filesystemScheme + "://" + VMUUIDStoragePath
return filesystemScheme + "://" + filepath.Join(VCStorePrefix, VMUUIDStoragePath)
}

// SandboxRuntimeRoot returns a virtcontainers sandbox runtime root URL.
Expand All @@ -276,12 +280,12 @@ func VCStoreUUIDPath() string {
// It should look like file:///run/vc/sbs/<sandboxID>/
// or if rootless: file://<rootlessDir>/run/vc/sbs/<sandboxID>/
func SandboxRuntimeRoot(id string) string {
return filesystemScheme + "://" + filepath.Join(RunStoragePath(), id)
return filesystemScheme + "://" + SandboxRuntimeRootPath(id)
}

// SandboxRuntimeRootPath returns a virtcontainers sandbox runtime root path.
func SandboxRuntimeRootPath(id string) string {
return filepath.Join(RunStoragePath(), id)
return filepath.Join(VCStorePrefix, RunStoragePath(), id)
}

// SandboxRuntimeItemPath returns a virtcontainers sandbox runtime item path.
Expand All @@ -303,12 +307,12 @@ func SandboxRuntimeItemPath(id string, item Item) (string, error) {
// It should look like file:///var/lib/vc/sbs/<sandboxID>/<containerID>
// Or if rootless file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/<containerID>
func ContainerConfigurationRoot(sandboxID, containerID string) string {
return filesystemScheme + "://" + filepath.Join(ConfigStoragePath(), sandboxID, containerID)
return filesystemScheme + "://" + ContainerConfigurationRootPath(sandboxID, containerID)
}

// ContainerConfigurationRootPath returns a virtcontainers container configuration root path.
func ContainerConfigurationRootPath(sandboxID, containerID string) string {
return filepath.Join(ConfigStoragePath(), sandboxID, containerID)
return filepath.Join(VCStorePrefix, ConfigStoragePath(), sandboxID, containerID)
}

// ContainerRuntimeRoot returns a virtcontainers container runtime root URL.
Expand All @@ -317,12 +321,12 @@ func ContainerConfigurationRootPath(sandboxID, containerID string) string {
// It should look like file:///run/vc/sbs/<sandboxID>/<containerID>/
// Or for rootless file://<rootlessDir>/run/vc/sbs/<sandboxID>/<containerID>/
func ContainerRuntimeRoot(sandboxID, containerID string) string {
return filesystemScheme + "://" + filepath.Join(RunStoragePath(), sandboxID, containerID)
return filesystemScheme + "://" + ContainerRuntimeRootPath(sandboxID, containerID)
}

// ContainerRuntimeRootPath returns a virtcontainers container runtime root path.
func ContainerRuntimeRootPath(sandboxID, containerID string) string {
return filepath.Join(RunStoragePath(), sandboxID, containerID)
return filepath.Join(VCStorePrefix, RunStoragePath(), sandboxID, containerID)
}

// VCSandboxStoreExists returns true if a sandbox store already exists.
Expand Down

0 comments on commit 6ab89e4

Please sign in to comment.