forked from containers/virtcontainers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qemu_test.go
469 lines (392 loc) · 10.5 KB
/
qemu_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package virtcontainers
import (
"fmt"
"os"
"reflect"
"strings"
"testing"
ciaoQemu "github.com/01org/ciao/qemu"
)
func newQemuConfig() HypervisorConfig {
return HypervisorConfig{
KernelPath: testQemuKernelPath,
ImagePath: testQemuImagePath,
HypervisorPath: testQemuPath,
}
}
func testQemuBuildKernelParams(t *testing.T, kernelParams []Param, expected string, debug bool) {
qemuConfig := newQemuConfig()
qemuConfig.KernelParams = kernelParams
if debug == true {
qemuConfig.Debug = true
}
q := &qemu{}
err := q.buildKernelParams(qemuConfig)
if err != nil {
t.Fatal(err)
}
if strings.Join(q.kernelParams, " ") != expected {
t.Fatal()
}
}
var testQemuKernelParamsBase = "root=/dev/pmem0p1 rootflags=dax,data=ordered,errors=remount-ro rw rootfstype=ext4 tsc=reliable no_timer_check rcupdate.rcu_expedited=1 i8042.direct=1 i8042.dumbkbd=1 i8042.nopnp=1 i8042.noaux=1 noreplace-smp reboot=k panic=1 console=hvc0 console=hvc1 initcall_debug init=/usr/lib/systemd/systemd systemd.unit=container.target iommu=off systemd.mask=systemd-networkd.service systemd.mask=systemd-networkd.socket cryptomgr.notests"
var testQemuKernelParamsNonDebug = "quiet systemd.show_status=false"
var testQemuKernelParamsDebug = "debug systemd.show_status=true systemd.log_level=debug"
func TestQemuBuildKernelParamsFoo(t *testing.T) {
// two representations of the same kernel parameters
suffixStr := "foo=foo bar=bar"
suffixParams := []Param{
{
parameter: "foo",
value: "foo",
},
{
parameter: "bar",
value: "bar",
},
}
type testData struct {
debugParams string
debugValue bool
}
data := []testData{
{testQemuKernelParamsNonDebug, false},
{testQemuKernelParamsDebug, true},
}
for _, d := range data {
// kernel params consist of a default set of params,
// followed by a set of params that depend on whether
// debug mode is enabled and end with any user-supplied
// params.
expected := []string{testQemuKernelParamsBase, d.debugParams, suffixStr}
expectedOut := strings.Join(expected, " ")
testQemuBuildKernelParams(t, suffixParams, expectedOut, d.debugValue)
}
}
func testQemuAppend(t *testing.T, structure interface{}, expected []ciaoQemu.Device, devType deviceType) {
var devices []ciaoQemu.Device
q := &qemu{}
switch s := structure.(type) {
case Volume:
devices = q.appendVolume(devices, s)
case Socket:
devices = q.appendSocket(devices, s)
case PodConfig:
switch devType {
case fsDev:
devices = q.appendFSDevices(devices, s)
case consoleDev:
devices = q.appendConsoles(devices, s)
}
}
if reflect.DeepEqual(devices, expected) == false {
t.Fatal()
}
}
func TestQemuAppendVolume(t *testing.T) {
mountTag := "testMountTag"
hostPath := "testHostPath"
expectedOut := []ciaoQemu.Device{
ciaoQemu.FSDevice{
Driver: ciaoQemu.Virtio9P,
FSDriver: ciaoQemu.Local,
ID: fmt.Sprintf("extra-%s-9p", mountTag),
Path: hostPath,
MountTag: mountTag,
SecurityModel: ciaoQemu.None,
},
}
volume := Volume{
MountTag: mountTag,
HostPath: hostPath,
}
testQemuAppend(t, volume, expectedOut, -1)
}
func TestQemuAppendSocket(t *testing.T) {
deviceID := "channelTest"
id := "charchTest"
hostPath := "/tmp/hyper_test.sock"
name := "sh.hyper.channel.test"
expectedOut := []ciaoQemu.Device{
ciaoQemu.CharDevice{
Driver: ciaoQemu.VirtioSerialPort,
Backend: ciaoQemu.Socket,
DeviceID: deviceID,
ID: id,
Path: hostPath,
Name: name,
},
}
socket := Socket{
DeviceID: deviceID,
ID: id,
HostPath: hostPath,
Name: name,
}
testQemuAppend(t, socket, expectedOut, -1)
}
func TestQemuAppendFSDevices(t *testing.T) {
podID := "testPodID"
contID := "testContID"
contRootFs := "testContRootFs"
volMountTag := "testVolMountTag"
volHostPath := "testVolHostPath"
expectedOut := []ciaoQemu.Device{
ciaoQemu.FSDevice{
Driver: ciaoQemu.Virtio9P,
FSDriver: ciaoQemu.Local,
ID: fmt.Sprintf("ctr-%s-9p", fmt.Sprintf("%s.1", contID)),
Path: fmt.Sprintf("%s.1", contRootFs),
MountTag: fmt.Sprintf("ctr-rootfs-%s.1", contID),
SecurityModel: ciaoQemu.None,
},
ciaoQemu.FSDevice{
Driver: ciaoQemu.Virtio9P,
FSDriver: ciaoQemu.Local,
ID: fmt.Sprintf("ctr-%s-9p", fmt.Sprintf("%s.2", contID)),
Path: fmt.Sprintf("%s.2", contRootFs),
MountTag: fmt.Sprintf("ctr-rootfs-%s.2", contID),
SecurityModel: ciaoQemu.None,
},
ciaoQemu.FSDevice{
Driver: ciaoQemu.Virtio9P,
FSDriver: ciaoQemu.Local,
ID: fmt.Sprintf("extra-%s-9p", fmt.Sprintf("%s.1", volMountTag)),
Path: fmt.Sprintf("%s.1", volHostPath),
MountTag: fmt.Sprintf("%s.1", volMountTag),
SecurityModel: ciaoQemu.None,
},
ciaoQemu.FSDevice{
Driver: ciaoQemu.Virtio9P,
FSDriver: ciaoQemu.Local,
ID: fmt.Sprintf("extra-%s-9p", fmt.Sprintf("%s.2", volMountTag)),
Path: fmt.Sprintf("%s.2", volHostPath),
MountTag: fmt.Sprintf("%s.2", volMountTag),
SecurityModel: ciaoQemu.None,
},
}
volumes := []Volume{
{
MountTag: fmt.Sprintf("%s.1", volMountTag),
HostPath: fmt.Sprintf("%s.1", volHostPath),
},
{
MountTag: fmt.Sprintf("%s.2", volMountTag),
HostPath: fmt.Sprintf("%s.2", volHostPath),
},
}
containers := []ContainerConfig{
{
ID: fmt.Sprintf("%s.1", contID),
RootFs: fmt.Sprintf("%s.1", contRootFs),
},
{
ID: fmt.Sprintf("%s.2", contID),
RootFs: fmt.Sprintf("%s.2", contRootFs),
},
}
podConfig := PodConfig{
ID: podID,
Volumes: volumes,
Containers: containers,
}
testQemuAppend(t, podConfig, expectedOut, fsDev)
}
func TestQemuAppendConsoles(t *testing.T) {
podID := "testPodID"
contConsolePath := "testContConsolePath"
expectedOut := []ciaoQemu.Device{
ciaoQemu.SerialDevice{
Driver: ciaoQemu.VirtioSerial,
ID: "serial0",
},
ciaoQemu.CharDevice{
Driver: ciaoQemu.Console,
Backend: ciaoQemu.Serial,
DeviceID: "console0",
ID: "charconsole0",
Path: contConsolePath,
},
ciaoQemu.CharDevice{
Driver: ciaoQemu.Console,
Backend: ciaoQemu.Socket,
DeviceID: "console1",
ID: "charconsole1",
Path: fmt.Sprintf("%s/%s/console.sock", runStoragePath, podID),
},
}
containers := []ContainerConfig{
{
Interactive: true,
Console: contConsolePath,
},
{
Interactive: false,
Console: "",
},
}
podConfig := PodConfig{
ID: podID,
Containers: containers,
}
testQemuAppend(t, podConfig, expectedOut, consoleDev)
}
func TestQemuAppendImage(t *testing.T) {
var devices []ciaoQemu.Device
qemuConfig := newQemuConfig()
q := &qemu{
config: qemuConfig,
}
imageFile, err := os.Open(q.config.ImagePath)
if err != nil {
t.Fatal(err)
}
defer imageFile.Close()
imageStat, err := imageFile.Stat()
if err != nil {
t.Fatal(err)
}
expectedOut := []ciaoQemu.Device{
ciaoQemu.Object{
Driver: ciaoQemu.NVDIMM,
Type: ciaoQemu.MemoryBackendFile,
DeviceID: "nv0",
ID: "mem0",
MemPath: q.config.ImagePath,
Size: (uint64)(imageStat.Size()),
},
}
podConfig := PodConfig{}
devices, err = q.appendImage(devices, podConfig)
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(devices, expectedOut) == false {
t.Fatal()
}
}
func TestQemuInit(t *testing.T) {
qemuConfig := newQemuConfig()
q := &qemu{}
err := q.init(qemuConfig)
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(qemuConfig, q.config) == false {
t.Fatal()
}
if reflect.DeepEqual(qemuConfig.HypervisorPath, q.path) == false {
t.Fatal()
}
// non-debug is the default
var testQemuKernelParamsDefault = testQemuKernelParamsBase + " " + testQemuKernelParamsNonDebug
if strings.Join(q.kernelParams, " ") != testQemuKernelParamsDefault {
t.Fatal()
}
}
func TestQemuSetCPUResources(t *testing.T) {
vcpus := 1
q := &qemu{}
expectedOut := ciaoQemu.SMP{
CPUs: uint32(vcpus),
Cores: uint32(vcpus),
Sockets: uint32(1),
Threads: uint32(1),
}
vmConfig := Resources{
VCPUs: uint(vcpus),
}
podConfig := PodConfig{
VMConfig: vmConfig,
}
smp := q.setCPUResources(podConfig)
if reflect.DeepEqual(smp, expectedOut) == false {
t.Fatal()
}
}
func TestQemuSetMemoryResources(t *testing.T) {
mem := 1000
q := &qemu{}
expectedOut := ciaoQemu.Memory{
Size: "1000M",
Slots: uint8(2),
MaxMem: "1500M",
}
vmConfig := Resources{
Memory: uint(mem),
}
podConfig := PodConfig{
VMConfig: vmConfig,
}
memory := q.setMemoryResources(podConfig)
if reflect.DeepEqual(memory, expectedOut) == false {
t.Fatal()
}
}
func testQemuAddDevice(t *testing.T, devInfo interface{}, devType deviceType, expected []ciaoQemu.Device) {
q := &qemu{}
err := q.addDevice(devInfo, devType)
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(q.qemuConfig.Devices, expected) == false {
t.Fatal()
}
}
func TestQemuAddDeviceFsDev(t *testing.T) {
mountTag := "testMountTag"
hostPath := "testHostPath"
expectedOut := []ciaoQemu.Device{
ciaoQemu.FSDevice{
Driver: ciaoQemu.Virtio9P,
FSDriver: ciaoQemu.Local,
ID: fmt.Sprintf("extra-%s-9p", mountTag),
Path: hostPath,
MountTag: mountTag,
SecurityModel: ciaoQemu.None,
},
}
volume := Volume{
MountTag: mountTag,
HostPath: hostPath,
}
testQemuAddDevice(t, volume, fsDev, expectedOut)
}
func TestQemuAddDeviceSerialPordDev(t *testing.T) {
deviceID := "channelTest"
id := "charchTest"
hostPath := "/tmp/hyper_test.sock"
name := "sh.hyper.channel.test"
expectedOut := []ciaoQemu.Device{
ciaoQemu.CharDevice{
Driver: ciaoQemu.VirtioSerialPort,
Backend: ciaoQemu.Socket,
DeviceID: deviceID,
ID: id,
Path: hostPath,
Name: name,
},
}
socket := Socket{
DeviceID: deviceID,
ID: id,
HostPath: hostPath,
Name: name,
}
testQemuAddDevice(t, socket, serialPortDev, expectedOut)
}