forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm_test.go
135 lines (111 loc) · 2.87 KB
/
vm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
"github.com/stretchr/testify/assert"
)
func TestNewVM(t *testing.T) {
assert := assert.New(t)
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
assert.Nil(err)
defer os.RemoveAll(testDir)
config := VMConfig{
HypervisorType: MockHypervisor,
}
hyperConfig := HypervisorConfig{
KernelPath: testDir,
ImagePath: testDir,
}
ctx := WithNewAgentFunc(context.Background(), newMockAgent)
var vm *VM
_, err = NewVM(ctx, config)
assert.Error(err)
config.HypervisorConfig = hyperConfig
vm, err = NewVM(ctx, config)
assert.Nil(err)
// VM operations
err = vm.Pause(context.Background())
assert.Nil(err)
err = vm.Resume(context.Background())
assert.Nil(err)
err = vm.Start(context.Background())
assert.Nil(err)
err = vm.Disconnect(context.Background())
assert.Nil(err)
err = vm.Save()
assert.Nil(err)
err = vm.Stop(context.Background())
assert.Nil(err)
err = vm.AddCPUs(context.Background(), 2)
assert.Nil(err)
err = vm.AddMemory(context.Background(), 128)
assert.Nil(err)
err = vm.OnlineCPUMemory(context.Background())
assert.Nil(err)
// mock urandom device
savedUrandomDev := urandomDev
defer func() {
urandomDev = savedUrandomDev
}()
tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(tmpdir)
urandomDev = filepath.Join(tmpdir, "urandom")
data := make([]byte, 512)
err = ioutil.WriteFile(urandomDev, data, os.FileMode(0640))
assert.NoError(err)
err = vm.ReseedRNG(context.Background())
assert.Nil(err)
// template VM
config.HypervisorConfig.BootFromTemplate = true
_, err = NewVM(ctx, config)
assert.Error(err)
config.HypervisorConfig.MemoryPath = testDir
_, err = NewVM(ctx, config)
assert.Error(err)
config.HypervisorConfig.DevicesStatePath = testDir
_, err = NewVM(ctx, config)
assert.Nil(err)
}
func TestVMConfigValid(t *testing.T) {
assert := assert.New(t)
config := VMConfig{}
err := config.Valid()
assert.Error(err)
testDir, err := ioutil.TempDir("", "vmfactory-tmp-")
assert.Nil(err)
defer os.RemoveAll(testDir)
config.HypervisorConfig = HypervisorConfig{
KernelPath: testDir,
InitrdPath: testDir,
}
err = config.Valid()
assert.Nil(err)
}
func TestVMConfigGrpc(t *testing.T) {
assert := assert.New(t)
config := VMConfig{
HypervisorType: QemuHypervisor,
HypervisorConfig: newQemuConfig(),
AgentConfig: KataAgentConfig{
LongLiveConn: true,
Debug: false,
Trace: false,
EnableDebugConsole: false,
ContainerPipeSize: 0,
KernelModules: []string{}},
}
p, err := config.ToGrpc()
assert.Nil(err)
config2, err := GrpcToVMConfig(p)
assert.Nil(err)
assert.True(utils.DeepCompare(config, *config2))
}