forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu_arm64_test.go
174 lines (139 loc) · 3.89 KB
/
qemu_arm64_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
// Copyright (c) 2018 IBM
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
govmmQemu "github.com/kata-containers/govmm/qemu"
"github.com/stretchr/testify/assert"
)
func qemuConfig(machineType string) HypervisorConfig {
return HypervisorConfig{
HypervisorMachineType: machineType,
}
}
func newTestQemu(assert *assert.Assertions, machineType string) qemuArch {
config := qemuConfig(machineType)
arch, err := newQemuArch(config)
assert.NoError(err)
return arch
}
func TestQemuArm64CPUModel(t *testing.T) {
assert := assert.New(t)
arm64 := newTestQemu(assert, QemuVirt)
expectedOut := defaultCPUModel
model := arm64.cpuModel()
assert.Equal(expectedOut, model)
}
func TestQemuArm64MemoryTopology(t *testing.T) {
assert := assert.New(t)
arm64 := newTestQemu(assert, QemuVirt)
hostMem := uint64(4096)
mem := uint64(1024)
slots := uint8(3)
expectedMemory := govmmQemu.Memory{
Size: fmt.Sprintf("%dM", mem),
Slots: slots,
MaxMem: fmt.Sprintf("%dM", hostMem),
}
m := arm64.memoryTopology(mem, hostMem, slots)
assert.Equal(expectedMemory, m)
}
func TestMaxQemuVCPUs(t *testing.T) {
assert := assert.New(t)
vCPUs := MaxQemuVCPUs()
assert.Equal(uint32(123), vCPUs)
}
func TestQemuArm64AppendBridges(t *testing.T) {
var devices []govmmQemu.Device
assert := assert.New(t)
arm64 := newTestQemu(assert, QemuVirt)
arm64.bridges(1)
bridges := arm64.getBridges()
assert.Len(bridges, 1)
devices = []govmmQemu.Device{}
devices = arm64.appendBridges(devices)
assert.Len(devices, 1)
expectedOut := []govmmQemu.Device{
govmmQemu.BridgeDevice{
Type: govmmQemu.PCIBridge,
Bus: defaultBridgeBus,
ID: bridges[0].ID,
Chassis: 1,
SHPC: true,
Addr: "2",
},
}
assert.Equal(expectedOut, devices)
}
func TestQemuArm64AppendImage(t *testing.T) {
var devices []govmmQemu.Device
assert := assert.New(t)
f, err := ioutil.TempFile("", "img")
assert.NoError(err)
defer func() { _ = f.Close() }()
defer func() { _ = os.Remove(f.Name()) }()
imageStat, err := f.Stat()
assert.NoError(err)
// save default supportedQemuMachines options
machinesCopy := make([]govmmQemu.Machine, len(supportedQemuMachines))
assert.Equal(len(supportedQemuMachines), copy(machinesCopy, supportedQemuMachines))
cfg := qemuConfig(QemuVirt)
cfg.ImagePath = f.Name()
arm64 := newQemuArch(cfg)
assert.Contains(m.machine().Options, qemuNvdimmOption)
expectedOut := []govmmQemu.Device{
govmmQemu.Object{
Driver: govmmQemu.NVDIMM,
Type: govmmQemu.MemoryBackendFile,
DeviceID: "nv0",
ID: "mem0",
MemPath: f.Name(),
Size: (uint64)(imageStat.Size()),
},
}
devices, err = arm64.appendImage(context.Background(), devices, f.Name())
assert.NoError(err)
assert.Equal(expectedOut, devices)
//restore default supportedQemuMachines options
assert.Equal(len(supportedQemuMachines), copy(supportedQemuMachines, machinesCopy))
}
func TestQemuArm64AppendNvdimmImage(t *testing.T) {
var devices []govmmQemu.Device
assert := assert.New(t)
f, err := ioutil.TempFile("", "img")
assert.NoError(err)
defer func() { _ = f.Close() }()
defer func() { _ = os.Remove(f.Name()) }()
imageStat, err := f.Stat()
assert.NoError(err)
cfg := qemuConfig(QemuVirt)
cfg.ImagePath = f.Name()
arm64, err := newQemuArch(cfg)
assert.NoError(err)
expectedOut := []govmmQemu.Device{
govmmQemu.Object{
Driver: govmmQemu.NVDIMM,
Type: govmmQemu.MemoryBackendFile,
DeviceID: "nv0",
ID: "mem0",
MemPath: f.Name(),
Size: (uint64)(imageStat.Size()),
},
}
devices, err = arm64.appendNvdimmImage(devices, f.Name())
assert.NoError(err)
assert.Equal(expectedOut, devices)
}
func TestQemuArm64WithInitrd(t *testing.T) {
assert := assert.New(t)
cfg := qemuConfig(QemuVirt)
cfg.InitrdPath = "dummy-initrd"
arm64 := newQemuArch(cfg)
assert.NotContains(m.machine().Options, qemuNvdimmOption)
}