mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-02 02:02:24 +00:00
factory: add UTs
Add UTs to all factory components. Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
parent
7cdc0fe912
commit
0a11230bfb
44
virtcontainers/factory/cache/cache_test.go
vendored
Normal file
44
virtcontainers/factory/cache/cache_test.go
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2018 HyperHQ Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
|
||||
)
|
||||
|
||||
func TestTemplateFactory(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
hyperConfig := vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
vmConfig := vc.VMConfig{
|
||||
HypervisorType: vc.MockHypervisor,
|
||||
AgentType: vc.NoopAgentType,
|
||||
HypervisorConfig: hyperConfig,
|
||||
}
|
||||
|
||||
// New
|
||||
f := New(2, direct.New(vmConfig))
|
||||
|
||||
// Config
|
||||
assert.Equal(f.Config(), vmConfig)
|
||||
|
||||
// GetBaseVM
|
||||
_, err := f.GetBaseVM()
|
||||
assert.Nil(err)
|
||||
|
||||
// CloseFactory
|
||||
f.CloseFactory()
|
||||
}
|
43
virtcontainers/factory/direct/direct_test.go
Normal file
43
virtcontainers/factory/direct/direct_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright (c) 2018 HyperHQ Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package direct
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
)
|
||||
|
||||
func TestTemplateFactory(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
hyperConfig := vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
vmConfig := vc.VMConfig{
|
||||
HypervisorType: vc.MockHypervisor,
|
||||
AgentType: vc.NoopAgentType,
|
||||
HypervisorConfig: hyperConfig,
|
||||
}
|
||||
|
||||
// New
|
||||
f := New(vmConfig)
|
||||
|
||||
// Config
|
||||
assert.Equal(f.Config(), vmConfig)
|
||||
|
||||
// GetBaseVM
|
||||
_, err := f.GetBaseVM()
|
||||
assert.Nil(err)
|
||||
|
||||
// CloseFactory
|
||||
f.CloseFactory()
|
||||
}
|
@ -32,18 +32,178 @@ func TestNewFactory(t *testing.T) {
|
||||
_, err = NewFactory(config, false)
|
||||
assert.Error(err)
|
||||
|
||||
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
assert.Nil(err)
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
|
||||
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
|
||||
// direct
|
||||
_, err = NewFactory(config, false)
|
||||
assert.Nil(err)
|
||||
_, err = NewFactory(config, true)
|
||||
assert.Nil(err)
|
||||
|
||||
// template
|
||||
config.Template = true
|
||||
_, err = NewFactory(config, false)
|
||||
assert.Nil(err)
|
||||
_, err = NewFactory(config, true)
|
||||
assert.Error(err)
|
||||
|
||||
// Cache
|
||||
config.Cache = 10
|
||||
_, err = NewFactory(config, false)
|
||||
assert.Nil(err)
|
||||
_, err = NewFactory(config, true)
|
||||
assert.Error(err)
|
||||
|
||||
config.Template = false
|
||||
_, err = NewFactory(config, false)
|
||||
assert.Nil(err)
|
||||
_, err = NewFactory(config, true)
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestVMConfigValid(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
config := Config{}
|
||||
|
||||
err := config.validate()
|
||||
assert.Error(err)
|
||||
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
|
||||
config.VMConfig = vc.VMConfig{
|
||||
HypervisorType: vc.MockHypervisor,
|
||||
AgentType: vc.NoopAgentType,
|
||||
HypervisorConfig: vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
},
|
||||
}
|
||||
|
||||
err = config.validate()
|
||||
assert.Nil(err)
|
||||
}
|
||||
|
||||
func TestCheckVMConfig(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
var config1, config2 vc.VMConfig
|
||||
|
||||
// default config should equal
|
||||
err := checkVMConfig(config1, config2)
|
||||
assert.Nil(err)
|
||||
|
||||
config1.HypervisorType = vc.MockHypervisor
|
||||
err = checkVMConfig(config1, config2)
|
||||
assert.Error(err)
|
||||
|
||||
config2.HypervisorType = vc.MockHypervisor
|
||||
err = checkVMConfig(config1, config2)
|
||||
assert.Nil(err)
|
||||
|
||||
config1.AgentType = vc.NoopAgentType
|
||||
err = checkVMConfig(config1, config2)
|
||||
assert.Error(err)
|
||||
|
||||
config2.AgentType = vc.NoopAgentType
|
||||
err = checkVMConfig(config1, config2)
|
||||
assert.Nil(err)
|
||||
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
config1.HypervisorConfig = vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
err = checkVMConfig(config1, config2)
|
||||
assert.Error(err)
|
||||
|
||||
config2.HypervisorConfig = vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
err = checkVMConfig(config1, config2)
|
||||
assert.Nil(err)
|
||||
}
|
||||
|
||||
func TestFactoryGetVM(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
hyperConfig := vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
vmConfig := vc.VMConfig{
|
||||
HypervisorType: vc.MockHypervisor,
|
||||
AgentType: vc.NoopAgentType,
|
||||
HypervisorConfig: hyperConfig,
|
||||
}
|
||||
|
||||
// direct factory
|
||||
f, err := NewFactory(Config{VMConfig: vmConfig}, false)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
f.CloseFactory()
|
||||
|
||||
// template factory
|
||||
f, err = NewFactory(Config{Template: true, VMConfig: vmConfig}, false)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
f.CloseFactory()
|
||||
|
||||
// fetch template factory
|
||||
f, err = NewFactory(Config{Template: true, VMConfig: vmConfig}, false)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = NewFactory(Config{Template: true, VMConfig: vmConfig}, true)
|
||||
assert.Error(err)
|
||||
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
f.CloseFactory()
|
||||
|
||||
// cache factory over direct factory
|
||||
f, err = NewFactory(Config{Cache: 2, VMConfig: vmConfig}, false)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
f.CloseFactory()
|
||||
|
||||
// cache factory over template factory
|
||||
f, err = NewFactory(Config{Template: true, Cache: 2, VMConfig: vmConfig}, false)
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
// CPU hotplug
|
||||
vmConfig.HypervisorConfig.DefaultVCPUs++
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
// Memory hotplug
|
||||
vmConfig.HypervisorConfig.DefaultMemSz += 128
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
// checkConfig fall back
|
||||
vmConfig.HypervisorConfig.Mlock = true
|
||||
_, err = f.GetVM(vmConfig)
|
||||
assert.Nil(err)
|
||||
|
||||
f.CloseFactory()
|
||||
}
|
||||
|
70
virtcontainers/factory/template/template_test.go
Normal file
70
virtcontainers/factory/template/template_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2018 HyperHQ Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
)
|
||||
|
||||
func TestTemplateFactory(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
|
||||
hyperConfig := vc.HypervisorConfig{
|
||||
KernelPath: testDir,
|
||||
ImagePath: testDir,
|
||||
}
|
||||
vmConfig := vc.VMConfig{
|
||||
HypervisorType: vc.MockHypervisor,
|
||||
AgentType: vc.NoopAgentType,
|
||||
HypervisorConfig: hyperConfig,
|
||||
}
|
||||
|
||||
// New
|
||||
f := New(vmConfig)
|
||||
|
||||
// Config
|
||||
assert.Equal(f.Config(), vmConfig)
|
||||
|
||||
// GetBaseVM
|
||||
_, err := f.GetBaseVM()
|
||||
assert.Nil(err)
|
||||
|
||||
// Fetch
|
||||
tt := template{
|
||||
statePath: testDir,
|
||||
config: vmConfig,
|
||||
}
|
||||
|
||||
err = tt.checkTemplateVM()
|
||||
assert.Error(err)
|
||||
|
||||
_, err = os.Create(tt.statePath + "/memory")
|
||||
assert.Nil(err)
|
||||
err = tt.checkTemplateVM()
|
||||
assert.Error(err)
|
||||
|
||||
_, err = os.Create(tt.statePath + "/state")
|
||||
assert.Nil(err)
|
||||
err = tt.checkTemplateVM()
|
||||
assert.Nil(err)
|
||||
|
||||
err = tt.createTemplateVM()
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = tt.GetBaseVM()
|
||||
assert.Nil(err)
|
||||
|
||||
// CloseFactory
|
||||
f.CloseFactory()
|
||||
tt.CloseFactory()
|
||||
}
|
Loading…
Reference in New Issue
Block a user