-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
issues_test.go
381 lines (334 loc) · 8.02 KB
/
issues_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
package vz
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Code-Hex/vz/v3/internal/objc"
)
func newTestConfig(t *testing.T) *VirtualMachineConfiguration {
f, err := os.CreateTemp("", "vmlinuz")
if err != nil {
t.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
bootloader, err := NewLinuxBootLoader(f.Name())
if err != nil {
t.Fatal(err)
}
config, err := NewVirtualMachineConfiguration(bootloader, 1, 1024*1024*1024)
if err != nil {
t.Fatal(err)
}
return config
}
func TestIssue50(t *testing.T) {
config := newTestConfig(t)
ok, err := config.Validate()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("failed to validate config")
}
m, err := NewVirtualMachine(config)
if err != nil {
t.Fatal(err)
}
t.Run("check for segmentation faults", func(t *testing.T) {
cases := []struct {
name string
run func() error
}{
{
name: "start handler",
run: func() error { return m.Start() },
},
{
name: "pause handler",
run: m.Pause,
},
{
name: "resume handler",
run: m.Resume,
},
{
name: "stop handler",
run: m.Stop,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_ = tc.run()
})
}
})
}
func TestIssue43(t *testing.T) {
const doesNotExists = "/non/existing/path"
t.Run("does not throw NSInvalidArgumentException", func(t *testing.T) {
t.Run("NewLinuxBootLoader", func(t *testing.T) {
_, err := NewLinuxBootLoader(doesNotExists)
if err == nil {
t.Fatal("expected returns error")
}
if !strings.HasPrefix(err.Error(), "invalid linux kernel") {
t.Error(err)
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}
f, err := os.CreateTemp("", "vmlinuz")
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
_, err = NewLinuxBootLoader(f.Name(), WithInitrd(doesNotExists))
if err == nil {
t.Fatal("expected returns error")
}
if !strings.HasPrefix(err.Error(), "invalid initial RAM disk") {
t.Error(err)
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}
})
cases := map[string]func() error{
"NewSharedDirectory": func() error {
_, err := NewSharedDirectory(doesNotExists, false)
return err
},
"NewDiskImageStorageDeviceAttachment": func() error {
_, err := NewDiskImageStorageDeviceAttachment(doesNotExists, false)
return err
},
}
for name, run := range cases {
t.Run(name, func(t *testing.T) {
err := run()
if err == nil {
t.Fatal("expected returns error")
}
if !errors.Is(err, ErrUnsupportedOSVersion) && !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}
})
}
})
}
func TestIssue81(t *testing.T) {
config := newTestConfig(t)
ok, err := config.Validate()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("failed to validate config")
}
t.Run("SocketDevices", func(t *testing.T) {
vsockDevs := config.SocketDevices()
if len(vsockDevs) != 0 {
t.Errorf("unexpected number of virtio-vsock devices: got %d, expected 0", len(vsockDevs))
}
vsockDev, err := NewVirtioSocketDeviceConfiguration()
if err != nil {
t.Fatal(err)
}
config.SetSocketDevicesVirtualMachineConfiguration([]SocketDeviceConfiguration{vsockDev})
vsockDevs = config.SocketDevices()
if len(vsockDevs) != 1 {
t.Errorf("unexpected number of virtio-vsock devices: got %d, expected 1", len(vsockDevs))
}
})
t.Run("NetworkDevices", func(t *testing.T) {
networkDevs := config.NetworkDevices()
if len(networkDevs) != 0 {
t.Errorf("unexpected number of virtio-net devices: got %d, expected 0", len(networkDevs))
}
nat, err := NewNATNetworkDeviceAttachment()
if err != nil {
t.Fatal(err)
}
networkDev, err := NewVirtioNetworkDeviceConfiguration(nat)
if err != nil {
t.Fatal(err)
}
config.SetNetworkDevicesVirtualMachineConfiguration([]*VirtioNetworkDeviceConfiguration{networkDev})
networkDevs = config.NetworkDevices()
if len(networkDevs) != 1 {
t.Errorf("unexpected number of virtio-vsock devices: got %d, expected 1", len(networkDevs))
}
})
}
func TestIssue96(t *testing.T) {
t.Run("non-network fd", func(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", "*")
if err != nil {
t.Fatal(err)
}
_, err = NewFileHandleNetworkDeviceAttachment(f)
if err == nil {
t.Fatal("want error")
}
})
t.Run("unix socket", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "issue96.sock")
ln, err := net.ListenUnix("unix", &net.UnixAddr{
Name: path,
Net: "unix",
})
if err != nil {
t.Fatal(err)
}
defer ln.Close()
f, err := ln.File()
if err != nil {
t.Fatal(err)
}
_, err = NewFileHandleNetworkDeviceAttachment(f)
if err == nil {
t.Fatal("want error")
}
})
t.Run("TCP socket", func(t *testing.T) {
t.Parallel()
ln, err := net.ListenTCP("tcp", &net.TCPAddr{
Port: 0,
})
if err != nil {
t.Fatal(err)
}
defer ln.Close()
f, err := ln.File()
if err != nil {
t.Fatal(err)
}
_, err = NewFileHandleNetworkDeviceAttachment(f)
if err == nil {
t.Fatal("want error")
}
})
}
// unixgram must be supported
func TestIssue98(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "issue98.sock")
ln, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: path,
Net: "unix",
})
if err != nil {
t.Fatal(err)
}
defer ln.Close()
f, err := ln.File()
if err != nil {
t.Fatal(err)
}
_, err = NewFileHandleNetworkDeviceAttachment(f)
if err != nil {
t.Fatal(err)
}
}
func TestIssue119(t *testing.T) {
vmlinuz := "./testdata/Image"
initramfs := "./testdata/initramfs.cpio.gz"
bootLoader, err := NewLinuxBootLoader(
vmlinuz,
WithCommandLine("console=hvc0"),
WithInitrd(initramfs),
)
if err != nil {
t.Fatal(err)
}
config, err := setupIssue119Config(bootLoader)
if err != nil {
t.Fatal(err)
}
vm, err := NewVirtualMachine(config)
if err != nil {
t.Fatal(err)
}
if canStart := vm.CanStart(); !canStart {
t.Fatal("want CanStart is true")
}
if err := vm.Start(); err != nil {
t.Fatal(err)
}
if got := vm.State(); VirtualMachineStateRunning != got {
t.Fatalf("want state %v but got %v", VirtualMachineStateRunning, got)
}
// Simulates Go's VirtualMachine struct has been destructured but
// Objective-C VZVirtualMachine object has not been destructured.
objc.Retain(vm.pointer)
vm.finalize()
sendStop := false
if vm.CanStop() {
if err := vm.Stop(); err != nil {
t.Error(err)
}
sendStop = true
}
if vm.CanRequestStop() {
if _, err := vm.RequestStop(); err != nil {
t.Error(err)
}
sendStop = true
}
if !sendStop {
t.Fatal("unexpected failed to send stop signal")
}
timer := time.After(3 * time.Second)
for {
select {
case state := <-vm.StateChangedNotify():
if VirtualMachineStateStopped == state {
return
}
case <-timer:
t.Fatal("failed to shutdown vm")
}
}
}
func setupIssue119Config(bootLoader *LinuxBootLoader) (*VirtualMachineConfiguration, error) {
config, err := NewVirtualMachineConfiguration(
bootLoader,
1,
256*1024*1024,
)
if err != nil {
return nil, fmt.Errorf("failed to create a new virtual machine config: %w", err)
}
// entropy device
entropyConfig, err := NewVirtioEntropyDeviceConfiguration()
if err != nil {
return nil, fmt.Errorf("failed to create entropy device config: %w", err)
}
config.SetEntropyDevicesVirtualMachineConfiguration([]*VirtioEntropyDeviceConfiguration{
entropyConfig,
})
// memory balloon device
memoryBalloonDevice, err := NewVirtioTraditionalMemoryBalloonDeviceConfiguration()
if err != nil {
return nil, fmt.Errorf("failed to create memory balloon device config: %w", err)
}
config.SetMemoryBalloonDevicesVirtualMachineConfiguration([]MemoryBalloonDeviceConfiguration{
memoryBalloonDevice,
})
if _, err := config.Validate(); err != nil {
return nil, err
}
return config, nil
}