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

Commit a70c0cd

Browse files
committed
factory: add UTs
Add UTs to all factory components. Signed-off-by: Peng Tao <bergwolf@gmail.com>
1 parent e02339c commit a70c0cd

File tree

4 files changed

+319
-2
lines changed

4 files changed

+319
-2
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2018 HyperHQ Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
package cache
7+
8+
import (
9+
"io/ioutil"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
14+
vc "github.com/kata-containers/runtime/virtcontainers"
15+
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
16+
)
17+
18+
func TestTemplateFactory(t *testing.T) {
19+
assert := assert.New(t)
20+
21+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
22+
hyperConfig := vc.HypervisorConfig{
23+
KernelPath: testDir,
24+
ImagePath: testDir,
25+
}
26+
vmConfig := vc.VMConfig{
27+
HypervisorType: vc.MockHypervisor,
28+
AgentType: vc.NoopAgentType,
29+
HypervisorConfig: hyperConfig,
30+
}
31+
32+
// New
33+
f := New(2, direct.New(vmConfig))
34+
35+
// Config
36+
assert.Equal(f.Config(), vmConfig)
37+
38+
// GetBaseVM
39+
_, err := f.GetBaseVM()
40+
assert.Nil(err)
41+
42+
// CloseFactory
43+
f.CloseFactory()
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2018 HyperHQ Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
package direct
7+
8+
import (
9+
"io/ioutil"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
14+
vc "github.com/kata-containers/runtime/virtcontainers"
15+
)
16+
17+
func TestTemplateFactory(t *testing.T) {
18+
assert := assert.New(t)
19+
20+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
21+
hyperConfig := vc.HypervisorConfig{
22+
KernelPath: testDir,
23+
ImagePath: testDir,
24+
}
25+
vmConfig := vc.VMConfig{
26+
HypervisorType: vc.MockHypervisor,
27+
AgentType: vc.NoopAgentType,
28+
HypervisorConfig: hyperConfig,
29+
}
30+
31+
// New
32+
f := New(vmConfig)
33+
34+
// Config
35+
assert.Equal(f.Config(), vmConfig)
36+
37+
// GetBaseVM
38+
_, err := f.GetBaseVM()
39+
assert.Nil(err)
40+
41+
// CloseFactory
42+
f.CloseFactory()
43+
}

virtcontainers/factory/factory_test.go

+162-2
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,178 @@ func TestNewFactory(t *testing.T) {
3232
_, err = NewFactory(config, false)
3333
assert.Error(err)
3434

35-
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
36-
assert.Nil(err)
35+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
3736

3837
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
3938
KernelPath: testDir,
4039
ImagePath: testDir,
4140
}
4241

42+
// direct
43+
_, err = NewFactory(config, false)
44+
assert.Nil(err)
45+
_, err = NewFactory(config, true)
46+
assert.Nil(err)
47+
48+
// template
49+
config.Template = true
4350
_, err = NewFactory(config, false)
4451
assert.Nil(err)
52+
_, err = NewFactory(config, true)
53+
assert.Error(err)
4554

55+
// Cache
4656
config.Cache = 10
57+
_, err = NewFactory(config, false)
58+
assert.Nil(err)
59+
_, err = NewFactory(config, true)
60+
assert.Error(err)
61+
62+
config.Template = false
63+
_, err = NewFactory(config, false)
64+
assert.Nil(err)
4765
_, err = NewFactory(config, true)
4866
assert.Error(err)
4967
}
68+
69+
func TestVMConfigValid(t *testing.T) {
70+
assert := assert.New(t)
71+
72+
config := Config{}
73+
74+
err := config.validate()
75+
assert.Error(err)
76+
77+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
78+
79+
config.VMConfig = vc.VMConfig{
80+
HypervisorType: vc.MockHypervisor,
81+
AgentType: vc.NoopAgentType,
82+
HypervisorConfig: vc.HypervisorConfig{
83+
KernelPath: testDir,
84+
ImagePath: testDir,
85+
},
86+
}
87+
88+
err = config.validate()
89+
assert.Nil(err)
90+
}
91+
92+
func TestCheckVMConfig(t *testing.T) {
93+
assert := assert.New(t)
94+
95+
var config1, config2 vc.VMConfig
96+
97+
// default config should equal
98+
err := checkVMConfig(config1, config2)
99+
assert.Nil(err)
100+
101+
config1.HypervisorType = vc.MockHypervisor
102+
err = checkVMConfig(config1, config2)
103+
assert.Error(err)
104+
105+
config2.HypervisorType = vc.MockHypervisor
106+
err = checkVMConfig(config1, config2)
107+
assert.Nil(err)
108+
109+
config1.AgentType = vc.NoopAgentType
110+
err = checkVMConfig(config1, config2)
111+
assert.Error(err)
112+
113+
config2.AgentType = vc.NoopAgentType
114+
err = checkVMConfig(config1, config2)
115+
assert.Nil(err)
116+
117+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
118+
config1.HypervisorConfig = vc.HypervisorConfig{
119+
KernelPath: testDir,
120+
ImagePath: testDir,
121+
}
122+
err = checkVMConfig(config1, config2)
123+
assert.Error(err)
124+
125+
config2.HypervisorConfig = vc.HypervisorConfig{
126+
KernelPath: testDir,
127+
ImagePath: testDir,
128+
}
129+
err = checkVMConfig(config1, config2)
130+
assert.Nil(err)
131+
}
132+
133+
func TestFactoryGetVM(t *testing.T) {
134+
assert := assert.New(t)
135+
136+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
137+
hyperConfig := vc.HypervisorConfig{
138+
KernelPath: testDir,
139+
ImagePath: testDir,
140+
}
141+
vmConfig := vc.VMConfig{
142+
HypervisorType: vc.MockHypervisor,
143+
AgentType: vc.NoopAgentType,
144+
HypervisorConfig: hyperConfig,
145+
}
146+
147+
// direct factory
148+
f, err := NewFactory(Config{VMConfig: vmConfig}, false)
149+
assert.Nil(err)
150+
151+
_, err = f.GetVM(vmConfig)
152+
assert.Nil(err)
153+
154+
f.CloseFactory()
155+
156+
// template factory
157+
f, err = NewFactory(Config{Template: true, VMConfig: vmConfig}, false)
158+
assert.Nil(err)
159+
160+
_, err = f.GetVM(vmConfig)
161+
assert.Nil(err)
162+
163+
f.CloseFactory()
164+
165+
// fetch template factory
166+
f, err = NewFactory(Config{Template: true, VMConfig: vmConfig}, false)
167+
assert.Nil(err)
168+
169+
_, err = NewFactory(Config{Template: true, VMConfig: vmConfig}, true)
170+
assert.Error(err)
171+
172+
_, err = f.GetVM(vmConfig)
173+
assert.Nil(err)
174+
175+
f.CloseFactory()
176+
177+
// cache factory over direct factory
178+
f, err = NewFactory(Config{Cache: 2, VMConfig: vmConfig}, false)
179+
assert.Nil(err)
180+
181+
_, err = f.GetVM(vmConfig)
182+
assert.Nil(err)
183+
184+
f.CloseFactory()
185+
186+
// cache factory over template factory
187+
f, err = NewFactory(Config{Template: true, Cache: 2, VMConfig: vmConfig}, false)
188+
assert.Nil(err)
189+
190+
_, err = f.GetVM(vmConfig)
191+
assert.Nil(err)
192+
193+
// CPU hotplug
194+
vmConfig.HypervisorConfig.DefaultVCPUs++
195+
_, err = f.GetVM(vmConfig)
196+
assert.Nil(err)
197+
198+
// Memory hotplug
199+
vmConfig.HypervisorConfig.DefaultMemSz += 128
200+
_, err = f.GetVM(vmConfig)
201+
assert.Nil(err)
202+
203+
// checkConfig fall back
204+
vmConfig.HypervisorConfig.Mlock = true
205+
_, err = f.GetVM(vmConfig)
206+
assert.Nil(err)
207+
208+
f.CloseFactory()
209+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2018 HyperHQ Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
package template
7+
8+
import (
9+
"io/ioutil"
10+
"os"
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
15+
vc "github.com/kata-containers/runtime/virtcontainers"
16+
)
17+
18+
func TestTemplateFactory(t *testing.T) {
19+
assert := assert.New(t)
20+
21+
testDir, _ := ioutil.TempDir("", "vmfactory-tmp-")
22+
hyperConfig := vc.HypervisorConfig{
23+
KernelPath: testDir,
24+
ImagePath: testDir,
25+
}
26+
vmConfig := vc.VMConfig{
27+
HypervisorType: vc.MockHypervisor,
28+
AgentType: vc.NoopAgentType,
29+
HypervisorConfig: hyperConfig,
30+
}
31+
32+
// New
33+
f := New(vmConfig)
34+
35+
// Config
36+
assert.Equal(f.Config(), vmConfig)
37+
38+
// GetBaseVM
39+
_, err := f.GetBaseVM()
40+
assert.Nil(err)
41+
42+
// Fetch
43+
tt := template{
44+
statePath: testDir,
45+
config: vmConfig,
46+
}
47+
48+
err = tt.checkTemplateVM()
49+
assert.Error(err)
50+
51+
_, err = os.Create(tt.statePath + "/memory")
52+
assert.Nil(err)
53+
err = tt.checkTemplateVM()
54+
assert.Error(err)
55+
56+
_, err = os.Create(tt.statePath + "/state")
57+
assert.Nil(err)
58+
err = tt.checkTemplateVM()
59+
assert.Nil(err)
60+
61+
err = tt.createTemplateVM()
62+
assert.Nil(err)
63+
64+
_, err = tt.GetBaseVM()
65+
assert.Nil(err)
66+
67+
// CloseFactory
68+
f.CloseFactory()
69+
tt.CloseFactory()
70+
}

0 commit comments

Comments
 (0)