mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-27 20:18:57 +00:00
commit
c26788c329
7
virtcontainers/factory/cache/cache_test.go
vendored
7
virtcontainers/factory/cache/cache_test.go
vendored
@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
|
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
|
||||||
|
"github.com/kata-containers/runtime/virtcontainers/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTemplateFactory(t *testing.T) {
|
func TestTemplateFactory(t *testing.T) {
|
||||||
@ -33,6 +34,12 @@ func TestTemplateFactory(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
var savedStorePath = store.VCStorePrefix
|
||||||
|
store.VCStorePrefix = testDir
|
||||||
|
defer func() {
|
||||||
|
store.VCStorePrefix = savedStorePath
|
||||||
|
}()
|
||||||
|
|
||||||
// New
|
// New
|
||||||
f := New(ctx, 2, direct.New(ctx, vmConfig))
|
f := New(ctx, 2, direct.New(ctx, vmConfig))
|
||||||
|
|
||||||
|
@ -100,25 +100,23 @@ type manager struct {
|
|||||||
|
|
||||||
var stores = &manager{stores: make(map[string]*Store)}
|
var stores = &manager{stores: make(map[string]*Store)}
|
||||||
|
|
||||||
func (m *manager) addStore(s *Store) (err error) {
|
func (m *manager) addStore(s *Store) (rs *Store, err error) {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return fmt.Errorf("Store can not be nil")
|
return nil, fmt.Errorf("Store can not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.url == "" {
|
if s.url == "" {
|
||||||
return fmt.Errorf("Store URL can not be nil")
|
return nil, fmt.Errorf("Store URL can not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
if m.stores[s.url] != nil {
|
if m.stores[s.url] == nil {
|
||||||
return fmt.Errorf("Store %s already added", s.url)
|
m.stores[s.url] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
m.stores[s.url] = s
|
return m.stores[s.url], nil
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *manager) removeStore(url string) {
|
func (m *manager) removeStore(url string) {
|
||||||
@ -165,11 +163,11 @@ func New(ctx context.Context, storeURL string) (*Store, error) {
|
|||||||
s.backend = backend
|
s.backend = backend
|
||||||
|
|
||||||
// Create new backend
|
// Create new backend
|
||||||
if err := s.backend.new(ctx, s.path, s.host); err != nil {
|
if err = s.backend.new(ctx, s.path, s.host); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := stores.addStore(s); err != nil {
|
if s, err = stores.addStore(s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,14 +55,15 @@ func TestManagerAddStore(t *testing.T) {
|
|||||||
assert.NotNil(t, newStore, "findStore failed")
|
assert.NotNil(t, newStore, "findStore failed")
|
||||||
|
|
||||||
// Duplicate, should fail
|
// Duplicate, should fail
|
||||||
err = stores.addStore(s)
|
ns, err := stores.addStore(s)
|
||||||
assert.NotNil(t, err, "addStore should have failed")
|
assert.Nil(t, err, "addStore should not failed")
|
||||||
|
assert.Equal(t, s, ns)
|
||||||
|
|
||||||
// Try with an empty URL
|
// Try with an empty URL
|
||||||
sEmpty, err := New(context.Background(), storeRoot)
|
sEmpty, err := New(context.Background(), storeRoot)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
sEmpty.url = ""
|
sEmpty.url = ""
|
||||||
err = stores.addStore(sEmpty)
|
_, err = stores.addStore(sEmpty)
|
||||||
assert.NotNil(t, err, "addStore should have failed on an empty store URL")
|
assert.NotNil(t, err, "addStore should have failed on an empty store URL")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ import (
|
|||||||
"github.com/kata-containers/runtime/virtcontainers/types"
|
"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.
|
// VCStore is a virtcontainers specific Store.
|
||||||
// Virtcontainers typically needs a configuration Store for
|
// Virtcontainers typically needs a configuration Store for
|
||||||
// storing permanent items across reboots.
|
// storing permanent items across reboots.
|
||||||
@ -45,6 +48,7 @@ func (s *VCStore) itemToStore(item Item) *Store {
|
|||||||
func NewVCStore(ctx context.Context, configRoot, stateRoot string) (*VCStore, error) {
|
func NewVCStore(ctx context.Context, configRoot, stateRoot string) (*VCStore, error) {
|
||||||
config, err := New(ctx, configRoot)
|
config, err := New(ctx, configRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("config root %s\n", configRoot)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,12 +247,12 @@ func (s *VCStore) Unlock(token string) error {
|
|||||||
// It should look like file:///var/lib/vc/sbs/<sandboxID>/
|
// It should look like file:///var/lib/vc/sbs/<sandboxID>/
|
||||||
// Or for rootless: file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/
|
// Or for rootless: file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/
|
||||||
func SandboxConfigurationRoot(id string) string {
|
func SandboxConfigurationRoot(id string) string {
|
||||||
return filesystemScheme + "://" + filepath.Join(ConfigStoragePath(), id)
|
return filesystemScheme + "://" + SandboxConfigurationRootPath(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SandboxConfigurationRootPath returns a virtcontainers sandbox configuration root path.
|
// SandboxConfigurationRootPath returns a virtcontainers sandbox configuration root path.
|
||||||
func SandboxConfigurationRootPath(id string) string {
|
func SandboxConfigurationRootPath(id string) string {
|
||||||
return filepath.Join(ConfigStoragePath(), id)
|
return filepath.Join(VCStorePrefix, ConfigStoragePath(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SandboxConfigurationItemPath returns a virtcontainers sandbox configuration item path.
|
// SandboxConfigurationItemPath returns a virtcontainers sandbox configuration item path.
|
||||||
@ -262,12 +266,12 @@ func SandboxConfigurationItemPath(id string, item Item) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(ConfigStoragePath(), id, itemFile), nil
|
return filepath.Join(VCStorePrefix, ConfigStoragePath(), id, itemFile), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// VCStoreUUIDPath returns a virtcontainers runtime uuid URL.
|
// VCStoreUUIDPath returns a virtcontainers runtime uuid URL.
|
||||||
func VCStoreUUIDPath() string {
|
func VCStoreUUIDPath() string {
|
||||||
return filesystemScheme + "://" + VMUUIDStoragePath
|
return filesystemScheme + "://" + filepath.Join(VCStorePrefix, VMUUIDStoragePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SandboxRuntimeRoot returns a virtcontainers sandbox runtime root URL.
|
// SandboxRuntimeRoot returns a virtcontainers sandbox runtime root URL.
|
||||||
@ -276,12 +280,12 @@ func VCStoreUUIDPath() string {
|
|||||||
// It should look like file:///run/vc/sbs/<sandboxID>/
|
// It should look like file:///run/vc/sbs/<sandboxID>/
|
||||||
// or if rootless: file://<rootlessDir>/run/vc/sbs/<sandboxID>/
|
// or if rootless: file://<rootlessDir>/run/vc/sbs/<sandboxID>/
|
||||||
func SandboxRuntimeRoot(id string) string {
|
func SandboxRuntimeRoot(id string) string {
|
||||||
return filesystemScheme + "://" + filepath.Join(RunStoragePath(), id)
|
return filesystemScheme + "://" + SandboxRuntimeRootPath(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SandboxRuntimeRootPath returns a virtcontainers sandbox runtime root path.
|
// SandboxRuntimeRootPath returns a virtcontainers sandbox runtime root path.
|
||||||
func SandboxRuntimeRootPath(id string) string {
|
func SandboxRuntimeRootPath(id string) string {
|
||||||
return filepath.Join(RunStoragePath(), id)
|
return filepath.Join(VCStorePrefix, RunStoragePath(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SandboxRuntimeItemPath returns a virtcontainers sandbox runtime item path.
|
// SandboxRuntimeItemPath returns a virtcontainers sandbox runtime item path.
|
||||||
@ -303,12 +307,12 @@ func SandboxRuntimeItemPath(id string, item Item) (string, error) {
|
|||||||
// It should look like file:///var/lib/vc/sbs/<sandboxID>/<containerID>
|
// It should look like file:///var/lib/vc/sbs/<sandboxID>/<containerID>
|
||||||
// Or if rootless file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/<containerID>
|
// Or if rootless file://<rootlessDir>/var/lib/vc/sbs/<sandboxID>/<containerID>
|
||||||
func ContainerConfigurationRoot(sandboxID, containerID string) string {
|
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.
|
// ContainerConfigurationRootPath returns a virtcontainers container configuration root path.
|
||||||
func ContainerConfigurationRootPath(sandboxID, containerID string) string {
|
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.
|
// ContainerRuntimeRoot returns a virtcontainers container runtime root URL.
|
||||||
@ -317,12 +321,12 @@ func ContainerConfigurationRootPath(sandboxID, containerID string) string {
|
|||||||
// It should look like file:///run/vc/sbs/<sandboxID>/<containerID>/
|
// It should look like file:///run/vc/sbs/<sandboxID>/<containerID>/
|
||||||
// Or for rootless file://<rootlessDir>/run/vc/sbs/<sandboxID>/<containerID>/
|
// Or for rootless file://<rootlessDir>/run/vc/sbs/<sandboxID>/<containerID>/
|
||||||
func ContainerRuntimeRoot(sandboxID, containerID string) string {
|
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.
|
// ContainerRuntimeRootPath returns a virtcontainers container runtime root path.
|
||||||
func ContainerRuntimeRootPath(sandboxID, containerID string) string {
|
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.
|
// VCSandboxStoreExists returns true if a sandbox store already exists.
|
||||||
|
@ -8,6 +8,8 @@ package store
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -99,6 +101,17 @@ func TestStoreVCSandboxFileNegative(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreVCNewVCSandboxStore(t *testing.T) {
|
func TestStoreVCNewVCSandboxStore(t *testing.T) {
|
||||||
|
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||||
|
defer func() {
|
||||||
|
os.RemoveAll(testDir)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var savedStorePath = VCStorePrefix
|
||||||
|
VCStorePrefix = testDir
|
||||||
|
defer func() {
|
||||||
|
VCStorePrefix = savedStorePath
|
||||||
|
}()
|
||||||
|
|
||||||
_, err := NewVCSandboxStore(context.Background(), testSandboxID)
|
_, err := NewVCSandboxStore(context.Background(), testSandboxID)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
@ -107,6 +120,17 @@ func TestStoreVCNewVCSandboxStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreVCNewVCContainerStore(t *testing.T) {
|
func TestStoreVCNewVCContainerStore(t *testing.T) {
|
||||||
|
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||||
|
defer func() {
|
||||||
|
os.RemoveAll(testDir)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var savedStorePath = VCStorePrefix
|
||||||
|
VCStorePrefix = testDir
|
||||||
|
defer func() {
|
||||||
|
VCStorePrefix = savedStorePath
|
||||||
|
}()
|
||||||
|
|
||||||
_, err := NewVCContainerStore(context.Background(), testSandboxID, "foobar")
|
_, err := NewVCContainerStore(context.Background(), testSandboxID, "foobar")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user