persist: address comments

Address some comments.

Signed-off-by: Wei Zhang <weizhang555@gmail.com>
This commit is contained in:
Wei Zhang 2019-12-01 20:39:02 +08:00 committed by Wei Zhang
parent d33b154dd7
commit 4a298cb9b7
10 changed files with 117 additions and 42 deletions

View File

@ -805,7 +805,7 @@ func (a *Acrn) storeInfo() error {
jsonOut, err := json.Marshal(a.info) jsonOut, err := json.Marshal(a.info)
if err != nil { if err != nil {
return fmt.Errorf("Could not marshall data: %s", err) return fmt.Errorf("Could not marshal data: %s", err)
} }
if err := store.GlobalWrite(relPath, jsonOut); err != nil { if err := store.GlobalWrite(relPath, jsonOut); err != nil {

View File

@ -13,8 +13,8 @@ import (
"testing" "testing"
"github.com/kata-containers/runtime/virtcontainers/device/config" "github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
chclient "github.com/kata-containers/runtime/virtcontainers/pkg/cloud-hypervisor/client" chclient "github.com/kata-containers/runtime/virtcontainers/pkg/cloud-hypervisor/client"
"github.com/kata-containers/runtime/virtcontainers/store"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -184,7 +184,7 @@ func TestCloudHypervisorCleanupVM(t *testing.T) {
t.Errorf("cloudHypervisor.cleanupVM() expected error != %v", err) t.Errorf("cloudHypervisor.cleanupVM() expected error != %v", err)
} }
dir := filepath.Join(store.RunVMStoragePath(), clh.id) dir := filepath.Join(fs.RunVMStoragePath(), clh.id)
os.MkdirAll(dir, os.ModePerm) os.MkdirAll(dir, os.ModePerm)
if err := clh.cleanupVM(false); err != nil { if err := clh.cleanupVM(false); err != nil {

View File

@ -8,6 +8,7 @@ package cache
import ( import (
"context" "context"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -18,10 +19,19 @@ import (
"github.com/kata-containers/runtime/virtcontainers/persist/fs" "github.com/kata-containers/runtime/virtcontainers/persist/fs"
) )
var rootPathSave = fs.StorageRootPath()
func TestTemplateFactory(t *testing.T) { func TestTemplateFactory(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
hyperConfig := vc.HypervisorConfig{ hyperConfig := vc.HypervisorConfig{
KernelPath: testDir, KernelPath: testDir,
ImagePath: testDir, ImagePath: testDir,

View File

@ -9,23 +9,36 @@ import (
"context" "context"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
vc "github.com/kata-containers/runtime/virtcontainers" vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/store" "github.com/kata-containers/runtime/virtcontainers/persist/fs"
) )
var rootPathSave = fs.StorageRootPath()
func TestTemplateFactory(t *testing.T) { func TestTemplateFactory(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
testDir, err := ioutil.TempDir("", "vmfactory-tmp-") testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
assert.Nil(err) fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
store.VCStorePrefix = testDir
defer func() { defer func() {
os.RemoveAll(testDir) os.RemoveAll(testDir)
store.VCStorePrefix = "" fs.TestSetStorageRootPath(rootPathSave)
}()
assert.Nil(err)
runPathSave := fs.RunStoragePath()
fs.TestSetRunStoragePath(filepath.Join(testDir, "vc", "run"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetRunStoragePath(runPathSave)
}() }()
hyperConfig := vc.HypervisorConfig{ hyperConfig := vc.HypervisorConfig{

View File

@ -9,10 +9,12 @@ import (
"context" "context"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
vc "github.com/kata-containers/runtime/virtcontainers" vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory/base" "github.com/kata-containers/runtime/virtcontainers/factory/base"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/utils" "github.com/kata-containers/runtime/virtcontainers/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -20,6 +22,8 @@ import (
const testDisabledAsNonRoot = "Test disabled as requires root privileges" const testDisabledAsNonRoot = "Test disabled as requires root privileges"
var rootPathSave = fs.StorageRootPath()
func TestNewFactory(t *testing.T) { func TestNewFactory(t *testing.T) {
var config Config var config Config
@ -41,6 +45,12 @@ func TestNewFactory(t *testing.T) {
assert.Error(err) assert.Error(err)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{ config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
KernelPath: testDir, KernelPath: testDir,
@ -110,6 +120,12 @@ func TestVMConfigValid(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
config := vc.VMConfig{ config := vc.VMConfig{
HypervisorType: vc.MockHypervisor, HypervisorType: vc.MockHypervisor,
@ -159,6 +175,13 @@ func TestCheckVMConfig(t *testing.T) {
assert.Nil(err) assert.Nil(err)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
config1.HypervisorConfig = vc.HypervisorConfig{ config1.HypervisorConfig = vc.HypervisorConfig{
KernelPath: testDir, KernelPath: testDir,
ImagePath: testDir, ImagePath: testDir,
@ -178,6 +201,13 @@ func TestFactoryGetVM(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
hyperConfig := vc.HypervisorConfig{ hyperConfig := vc.HypervisorConfig{
KernelPath: testDir, KernelPath: testDir,
ImagePath: testDir, ImagePath: testDir,
@ -337,6 +367,13 @@ func TestDeepCompare(t *testing.T) {
ProxyType: vc.NoopProxyType, ProxyType: vc.NoopProxyType,
} }
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{ config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
KernelPath: testDir, KernelPath: testDir,
ImagePath: testDir, ImagePath: testDir,

View File

@ -9,16 +9,20 @@ import (
"context" "context"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
vc "github.com/kata-containers/runtime/virtcontainers" vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
) )
const testDisabledAsNonRoot = "Test disabled as requires root privileges" const testDisabledAsNonRoot = "Test disabled as requires root privileges"
var rootPathSave = fs.StorageRootPath()
func TestTemplateFactory(t *testing.T) { func TestTemplateFactory(t *testing.T) {
if os.Geteuid() != 0 { if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot) t.Skip(testDisabledAsNonRoot)
@ -29,6 +33,13 @@ func TestTemplateFactory(t *testing.T) {
templateWaitForAgent = 1 * time.Microsecond templateWaitForAgent = 1 * time.Microsecond
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-") testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
hyperConfig := vc.HypervisorConfig{ hyperConfig := vc.HypervisorConfig{
KernelPath: testDir, KernelPath: testDir,
ImagePath: testDir, ImagePath: testDir,

View File

@ -69,6 +69,12 @@ func TestSetRunStoragePath(path string) {
} }
} }
func TestSetStorageRootPath(path string) {
StorageRootPath = func() string {
return path
}
}
// FS storage driver implementation // FS storage driver implementation
type FS struct { type FS struct {
sandboxState *persistapi.SandboxState sandboxState *persistapi.SandboxState
@ -319,7 +325,7 @@ func (fs *FS) GlobalWrite(relativePath string, data []byte) error {
_, err = os.Stat(dir) _, err = os.Stat(dir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
if err := os.MkdirAll(dir, dirMode); err != nil { if err := os.MkdirAll(dir, dirMode); err != nil {
fs.Logger().WithError(err).Errorf("failed to create dir %q", dir) fs.Logger().WithError(err).WithField("directory", dir).Error("failed to create dir")
return err return err
} }
} else if err != nil { } else if err != nil {
@ -328,13 +334,13 @@ func (fs *FS) GlobalWrite(relativePath string, data []byte) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, fileMode) f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, fileMode)
if err != nil { if err != nil {
fs.Logger().WithError(err).Errorf("failed to open file %q for writting", path) fs.Logger().WithError(err).WithField("file", path).Error("failed to open file for writting")
return err return err
} }
defer f.Close() defer f.Close()
if _, err := f.Write(data); err != nil { if _, err := f.Write(data); err != nil {
fs.Logger().WithError(err).Errorf("failed to write file %q: %v ", path, err) fs.Logger().WithError(err).WithField("file", path).Error("failed to write file")
return err return err
} }
return nil return nil
@ -349,14 +355,14 @@ func (fs *FS) GlobalRead(relativePath string) ([]byte, error) {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
fs.Logger().WithError(err).Errorf("failed to open file %q for reading", path) fs.Logger().WithError(err).WithField("file", path).Error("failed to open file for reading")
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
data, err := ioutil.ReadAll(f) data, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
fs.Logger().WithError(err).Errorf("failed to read file %q: %v ", path, err) fs.Logger().WithError(err).WithField("file", path).Error("failed to read file")
return nil, err return nil, err
} }
return data, nil return data, nil

View File

@ -33,16 +33,12 @@ func initTestDir() func() {
testDir, _ := ioutil.TempDir("", "vc-tmp-") testDir, _ := ioutil.TempDir("", "vc-tmp-")
// allow the tests to run without affecting the host system. // allow the tests to run without affecting the host system.
rootSave := StorageRootPath() rootSave := StorageRootPath()
StorageRootPath = func() string { TestSetStorageRootPath(filepath.Join(testDir, "vc"))
return filepath.Join(testDir, "vc")
}
os.MkdirAll(testDir, dirMode) os.MkdirAll(testDir, dirMode)
return func() { return func() {
StorageRootPath = func() string { TestSetStorageRootPath(rootSave)
return rootSave
}
os.RemoveAll(testDir) os.RemoveAll(testDir)
} }
} }
@ -54,13 +50,6 @@ func TestFsLockShared(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, fs) assert.NotNil(t, fs)
testDir, err := ioutil.TempDir("", "fs-tmp-")
assert.Nil(t, err)
TestSetRunStoragePath(testDir)
defer func() {
os.RemoveAll(testDir)
}()
sid := "test-fs-driver" sid := "test-fs-driver"
fs.sandboxState.SandboxContainer = sid fs.sandboxState.SandboxContainer = sid
sandboxDir, err := fs.sandboxDir(sid) sandboxDir, err := fs.sandboxDir(sid)
@ -116,13 +105,6 @@ func TestFsDriver(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, fs) assert.NotNil(t, fs)
testDir, err := ioutil.TempDir("", "fs-tmp-")
assert.Nil(t, err)
TestSetRunStoragePath(testDir)
defer func() {
os.RemoveAll(testDir)
}()
ss := persistapi.SandboxState{} ss := persistapi.SandboxState{}
cs := make(map[string]persistapi.ContainerState) cs := make(map[string]persistapi.ContainerState)
// missing sandbox container id // missing sandbox container id

View File

@ -29,11 +29,15 @@ import (
) )
const ( const (
tempBundlePath = "/tmp/virtc/ocibundle/" containerID = "virtc-oci-test"
containerID = "virtc-oci-test" fileMode = os.FileMode(0640)
consolePath = "/tmp/virtc/console" dirMode = os.FileMode(0750)
fileMode = os.FileMode(0640) )
dirMode = os.FileMode(0750)
var (
tempRoot = ""
tempBundlePath = ""
consolePath = ""
) )
func createConfig(fileName string, fileData string) (string, error) { func createConfig(fileName string, fileData string) (string, error) {
@ -633,16 +637,27 @@ func TestGetShmSizeBindMounted(t *testing.T) {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
var err error
tempRoot, err = ioutil.TempDir("", "virtc-")
if err != nil {
panic(err)
}
tempBundlePath = filepath.Join(tempRoot, "ocibundle")
consolePath = filepath.Join(tempRoot, "console")
/* Create temp bundle directory if necessary */ /* Create temp bundle directory if necessary */
err := os.MkdirAll(tempBundlePath, dirMode) err = os.MkdirAll(tempBundlePath, dirMode)
if err != nil { if err != nil {
fmt.Printf("Unable to create %s %v\n", tempBundlePath, err) fmt.Printf("Unable to create %s %v\n", tempBundlePath, err)
os.Exit(1) os.Exit(1)
} }
defer os.RemoveAll(tempBundlePath) ret := m.Run()
os.Exit(m.Run()) os.RemoveAll(tempRoot)
os.Exit(ret)
} }
func TestAddAssetAnnotations(t *testing.T) { func TestAddAssetAnnotations(t *testing.T) {

View File

@ -51,8 +51,6 @@ var testVirtiofsdPath = ""
var testHyperstartCtlSocket = "" var testHyperstartCtlSocket = ""
var testHyperstartTtySocket = "" var testHyperstartTtySocket = ""
var savedRunVMStoragePathFunc func() string
// cleanUp Removes any stale sandbox/container state that can affect // cleanUp Removes any stale sandbox/container state that can affect
// the next test to run. // the next test to run.
func cleanUp() { func cleanUp() {
@ -160,10 +158,13 @@ func TestMain(m *testing.M) {
// allow the tests to run without affecting the host system. // allow the tests to run without affecting the host system.
runPathSave := fs.RunStoragePath() runPathSave := fs.RunStoragePath()
rootPathSave := fs.StorageRootPath()
fs.TestSetRunStoragePath(filepath.Join(testDir, "vc", "run")) fs.TestSetRunStoragePath(filepath.Join(testDir, "vc", "run"))
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() { defer func() {
fs.TestSetRunStoragePath(runPathSave) fs.TestSetRunStoragePath(runPathSave)
fs.TestSetStorageRootPath(rootPathSave)
}() }()
// set now that configStoragePath has been overridden. // set now that configStoragePath has been overridden.