forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fc_test.go
257 lines (193 loc) · 5 KB
/
fc_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//go:build linux
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
"strings"
"testing"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
"github.com/stretchr/testify/assert"
)
func TestFCGenerateSocket(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
i, err := fc.GenerateSocket("a")
assert.NoError(err)
assert.NotNil(i)
hvsock, ok := i.(types.HybridVSock)
assert.True(ok)
assert.NotEmpty(hvsock.UdsPath)
// Path must be absolute
assert.True(strings.HasPrefix(hvsock.UdsPath, "/"))
assert.NotZero(hvsock.Port)
}
func TestFCTruncateID(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
testLongID := "3ef98eb7c6416be11e0accfed2f4e6560e07f8e33fa8d31922fd4d61747d7ead"
expectedID := "3ef98eb7c6416be11e0accfed2f4e656"
id := fc.truncateID(testLongID)
assert.Equal(expectedID, id)
testShortID := "3ef98eb7c6416be11"
expectedID = "3ef98eb7c6416be11"
id = fc.truncateID(testShortID)
assert.Equal(expectedID, id)
}
func TestFCParseVersion(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
// correct versions
for rawVersion, v := range map[string]string{"Firecracker v0.23.1": "0.23.1", "Firecracker v0.25.0\nSupported snapshot data format versions: 0.23.0": "0.25.0"} {
parsedVersion, err := fc.parseVersion(rawVersion)
assert.NoError(err)
assert.Equal(parsedVersion, v)
}
// wrong version str
rawVersion := "Firecracker_v0.23.0"
parsedVersion, err := fc.parseVersion(rawVersion)
assert.Error(err)
assert.Equal(parsedVersion, "")
}
func TestFCCheckVersion(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
// correct version
v := "0.23.0"
err := fc.checkVersion(v)
assert.NoError(err)
// version too low
v = "0.1.1"
err = fc.checkVersion(v)
assert.Error(err)
b := err.Error()
assert.True(strings.Contains(b, "version 0.1.1 is not supported")) // sanity
// version is malformed
v = "Firecracker v0.23.0"
err = fc.checkVersion(v)
assert.Error(err)
b = err.Error()
assert.True(strings.Contains(b, "Malformed firecracker version:")) // sanity
}
func TestFCGetVersionNumber(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
_, err := fc.getVersionNumber()
assert.Error(err)
}
func TestFCDriveIndexToID(t *testing.T) {
assert := assert.New(t)
d := fcDriveIndexToID(5)
assert.Equal(d, "drive_5")
}
func TestFCPauseVM(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
err := fc.PauseVM(ctx)
assert.NoError(err)
}
func TestFCSaveVM(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
err := fc.SaveVM()
assert.NoError(err)
}
func TestFCResumeVM(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
err := fc.ResumeVM(ctx)
assert.NoError(err)
}
func TestFCGetVirtioFsPid(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
pid := fc.GetVirtioFsPid()
assert.Nil(pid)
}
func TestFCIsRateLimiterBuiltin(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
rl := fc.IsRateLimiterBuiltin()
assert.True(rl)
}
func TestFCCheck(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
err := fc.Check()
assert.NoError(err)
}
func TestFCGetPids(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
pids := fc.GetPids()
assert.Equal(len(pids), 1)
}
func TestFCCleanup(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
err := fc.Cleanup(ctx)
assert.NoError(err)
}
func TestFCToGrpc(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
_, err := fc.toGrpc(ctx)
assert.Error(err)
}
func TestFCHypervisorConfig(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
config := fc.HypervisorConfig()
assert.Equal(fc.config, config)
}
func TestFCGetTotalMemoryMB(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
var initialMemSize uint32 = 1024
fc.config.MemorySize = 1024
memSize := fc.GetTotalMemoryMB(ctx)
assert.Equal(memSize, initialMemSize)
}
func TestFCClient(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
conn := fc.client(ctx)
assert.Equal(conn, fc.connection)
}
func TestFCVmRunning(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
ctx := context.Background()
sr := fc.vmRunning(ctx)
assert.False(sr)
}
func TestFCCreateJailedDrive(t *testing.T) {
assert := assert.New(t)
fc := firecracker{}
driveID := fcDriveIndexToID(0)
_, err := fc.createJailedDrive(driveID)
assert.NoError(err)
}
func TestFcSetConfig(t *testing.T) {
assert := assert.New(t)
config := HypervisorConfig{
HypervisorPath: "/some/where/firecracker",
KernelPath: "/some/where/kernel",
ImagePath: "/some/where/image",
JailerPath: "/some/where/jailer",
Debug: true,
}
fc := firecracker{}
assert.Equal(fc.config, HypervisorConfig{})
err := fc.setConfig(&config)
assert.NoError(err)
assert.Equal(fc.config, config)
}