From 4b9ab557c8fad2942b473e88f35395913e509722 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Fri, 31 Jan 2020 20:39:34 +0000 Subject: [PATCH] virtcontainers/factory: support new persist API Fix factory implementation and unit tests to support the new persist API Signed-off-by: Julio Montes --- virtcontainers/factory/cache/cache_test.go | 21 +------ virtcontainers/factory/direct/direct_test.go | 24 +------- virtcontainers/factory/factory_test.go | 59 ++++--------------- .../factory/template/template_test.go | 13 +--- 4 files changed, 19 insertions(+), 98 deletions(-) diff --git a/virtcontainers/factory/cache/cache_test.go b/virtcontainers/factory/cache/cache_test.go index 8489fe42d9..0048d77e56 100644 --- a/virtcontainers/factory/cache/cache_test.go +++ b/virtcontainers/factory/cache/cache_test.go @@ -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)) diff --git a/virtcontainers/factory/direct/direct_test.go b/virtcontainers/factory/direct/direct_test.go index 20eec0eb89..6f02b53204 100644 --- a/virtcontainers/factory/direct/direct_test.go +++ b/virtcontainers/factory/direct/direct_test.go @@ -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, diff --git a/virtcontainers/factory/factory_test.go b/virtcontainers/factory/factory_test.go index 91e0a2c32f..ab42423e38 100644 --- a/virtcontainers/factory/factory_test.go +++ b/virtcontainers/factory/factory_test.go @@ -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, diff --git a/virtcontainers/factory/template/template_test.go b/virtcontainers/factory/template/template_test.go index 150d81e2b6..d1c24f75ef 100644 --- a/virtcontainers/factory/template/template_test.go +++ b/virtcontainers/factory/template/template_test.go @@ -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,