virtcontainers/factory: support new persist API

Fix factory implementation and unit tests to support the new persist API

Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2020-01-31 20:39:34 +00:00
parent 71f48a3364
commit 4b9ab557c8
4 changed files with 19 additions and 98 deletions

View File

@ -7,9 +7,6 @@ package cache
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
@ -19,18 +16,11 @@ import (
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
)
var rootPathSave = fs.StorageRootPath()
func TestTemplateFactory(t *testing.T) {
assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
testDir := fs.MockStorageRootPath()
defer fs.MockStorageDestroy()
hyperConfig := vc.HypervisorConfig{
KernelPath: testDir,
@ -45,13 +35,6 @@ func TestTemplateFactory(t *testing.T) {
ctx := context.Background()
runPathSave := fs.RunStoragePath()
fs.TestSetRunStoragePath(filepath.Join(testDir, "vc", "run"))
// allow the tests to run without affecting the host system.
defer func() {
fs.TestSetRunStoragePath(runPathSave)
}()
// New
f := New(ctx, 2, direct.New(ctx, vmConfig))

View File

@ -7,9 +7,6 @@ package direct
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
@ -18,28 +15,11 @@ import (
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
)
var rootPathSave = fs.StorageRootPath()
func TestTemplateFactory(t *testing.T) {
assert := assert.New(t)
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
assert.Nil(err)
runPathSave := fs.RunStoragePath()
fs.TestSetRunStoragePath(filepath.Join(testDir, "vc", "run"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetRunStoragePath(runPathSave)
}()
testDir := fs.MockStorageRootPath()
defer fs.MockStorageDestroy()
hyperConfig := vc.HypervisorConfig{
KernelPath: testDir,

View File

@ -7,9 +7,7 @@ package factory
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
vc "github.com/kata-containers/runtime/virtcontainers"
@ -22,8 +20,6 @@ import (
const testDisabledAsNonRoot = "Test disabled as requires root privileges"
var rootPathSave = fs.StorageRootPath()
func TestNewFactory(t *testing.T) {
var config Config
@ -44,17 +40,10 @@ func TestNewFactory(t *testing.T) {
_, err = NewFactory(ctx, config, false)
assert.Error(err)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
defer fs.MockStorageDestroy()
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
KernelPath: testDir,
ImagePath: testDir,
KernelPath: fs.MockStorageRootPath(),
ImagePath: fs.MockStorageRootPath(),
}
// direct
@ -71,7 +60,7 @@ func TestNewFactory(t *testing.T) {
}
config.Template = true
config.TemplatePath = testDir
config.TemplatePath = fs.MockStorageRootPath()
f, err = NewFactory(ctx, config, false)
assert.Nil(err)
f.CloseFactory(ctx)
@ -119,19 +108,12 @@ func TestFactorySetLogger(t *testing.T) {
func TestVMConfigValid(t *testing.T) {
assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
defer fs.MockStorageDestroy()
config := vc.VMConfig{
HypervisorType: vc.MockHypervisor,
HypervisorConfig: vc.HypervisorConfig{
KernelPath: testDir,
ImagePath: testDir,
KernelPath: fs.MockStorageRootPath(),
ImagePath: fs.MockStorageRootPath(),
},
}
@ -174,13 +156,8 @@ func TestCheckVMConfig(t *testing.T) {
err = checkVMConfig(config1, config2)
assert.Nil(err)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
testDir := fs.MockStorageRootPath()
defer fs.MockStorageDestroy()
config1.HypervisorConfig = vc.HypervisorConfig{
KernelPath: testDir,
@ -200,13 +177,8 @@ func TestCheckVMConfig(t *testing.T) {
func TestFactoryGetVM(t *testing.T) {
assert := assert.New(t)
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
testDir := fs.MockStorageRootPath()
defer fs.MockStorageDestroy()
hyperConfig := vc.HypervisorConfig{
KernelPath: testDir,
@ -366,13 +338,8 @@ func TestDeepCompare(t *testing.T) {
AgentType: vc.NoopAgentType,
ProxyType: vc.NoopProxyType,
}
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
testDir := fs.MockStorageRootPath()
defer fs.MockStorageDestroy()
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
KernelPath: testDir,

View File

@ -7,9 +7,7 @@ package template
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
@ -21,8 +19,6 @@ import (
const testDisabledAsNonRoot = "Test disabled as requires root privileges"
var rootPathSave = fs.StorageRootPath()
func TestTemplateFactory(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
@ -32,13 +28,8 @@ func TestTemplateFactory(t *testing.T) {
templateWaitForAgent = 1 * time.Microsecond
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
fs.TestSetStorageRootPath(filepath.Join(testDir, "vc"))
defer func() {
os.RemoveAll(testDir)
fs.TestSetStorageRootPath(rootPathSave)
}()
testDir := fs.MockStorageRootPath()
defer fs.MockStorageDestroy()
hyperConfig := vc.HypervisorConfig{
KernelPath: testDir,