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

Commit

Permalink
virtcontainers/factory: support new persist API
Browse files Browse the repository at this point in the history
Fix factory implementation and unit tests to support the new persist API

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent 71f48a3 commit 4b9ab55
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 98 deletions.
21 changes: 2 additions & 19 deletions virtcontainers/factory/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ package cache

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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,
Expand All @@ -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))

Expand Down
24 changes: 2 additions & 22 deletions virtcontainers/factory/direct/direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ package direct

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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,
Expand Down
59 changes: 13 additions & 46 deletions virtcontainers/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ package factory

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

vc "github.com/kata-containers/runtime/virtcontainers"
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
},
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 2 additions & 11 deletions virtcontainers/factory/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ package template

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 4b9ab55

Please sign in to comment.