-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
virtualization_test.go
475 lines (411 loc) · 11.6 KB
/
virtualization_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
470
471
472
473
474
475
package vz_test
import (
"errors"
"fmt"
"log"
"math"
"net"
"os"
"runtime"
"syscall"
"testing"
"time"
"github.com/Code-Hex/vz/v3"
"github.com/Code-Hex/vz/v3/internal/testhelper"
"golang.org/x/crypto/ssh"
)
func setupConsoleConfig(config *vz.VirtualMachineConfiguration) error {
serialPortAttachment, err := vz.NewFileHandleSerialPortAttachment(os.Stdin, os.Stderr)
if err != nil {
return fmt.Errorf("failed to create file handle serial port attachment: %w", err)
}
consoleConfig, err := vz.NewVirtioConsoleDeviceSerialPortConfiguration(serialPortAttachment)
if err != nil {
return fmt.Errorf("failed to create a console device serial port config: %w", err)
}
config.SetSerialPortsVirtualMachineConfiguration([]*vz.VirtioConsoleDeviceSerialPortConfiguration{
consoleConfig,
})
return nil
}
func setupNetworkConfig(config *vz.VirtualMachineConfiguration) error {
natAttachment, err := vz.NewNATNetworkDeviceAttachment()
if err != nil {
return fmt.Errorf("failed to create NAT network device attachment: %w", err)
}
networkConfig, err := vz.NewVirtioNetworkDeviceConfiguration(natAttachment)
if err != nil {
return fmt.Errorf("failed to create a network device config: %w", err)
}
config.SetNetworkDevicesVirtualMachineConfiguration([]*vz.VirtioNetworkDeviceConfiguration{
networkConfig,
})
mac, err := vz.NewRandomLocallyAdministeredMACAddress()
if err != nil {
return fmt.Errorf("failed to generate random MAC address: %w", err)
}
networkConfig.SetMACAddress(mac)
return nil
}
func setupConfiguration(bootLoader vz.BootLoader) (*vz.VirtualMachineConfiguration, error) {
config, err := vz.NewVirtualMachineConfiguration(
bootLoader,
1,
512*1024*1024,
)
if err != nil {
return nil, fmt.Errorf("failed to create a new virtual machine config: %w", err)
}
// entropy device
entropyConfig, err := vz.NewVirtioEntropyDeviceConfiguration()
if err != nil {
return nil, fmt.Errorf("failed to create entropy device config: %w", err)
}
config.SetEntropyDevicesVirtualMachineConfiguration([]*vz.VirtioEntropyDeviceConfiguration{
entropyConfig,
})
// memory balloon device
memoryBalloonDevice, err := vz.NewVirtioTraditionalMemoryBalloonDeviceConfiguration()
if err != nil {
return nil, fmt.Errorf("failed to create memory balloon device config: %w", err)
}
config.SetMemoryBalloonDevicesVirtualMachineConfiguration([]vz.MemoryBalloonDeviceConfiguration{
memoryBalloonDevice,
})
// vsock device
vsockDevice, err := vz.NewVirtioSocketDeviceConfiguration()
if err != nil {
return nil, fmt.Errorf("failed to create virtio socket device config: %w", err)
}
config.SetSocketDevicesVirtualMachineConfiguration([]vz.SocketDeviceConfiguration{
vsockDevice,
})
if err := setupNetworkConfig(config); err != nil {
return nil, err
}
return config, nil
}
type Container struct {
*vz.VirtualMachine
*ssh.Client
}
func (c *Container) Close() error {
return c.Client.Close()
}
func (c *Container) NewSession(t *testing.T) *ssh.Session {
sshSession, err := c.Client.NewSession()
if err != nil {
t.Fatal(err)
}
testhelper.SetKeepAlive(t, sshSession)
return sshSession
}
func getFreePort() (int, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
func newVirtualizationMachine(
t *testing.T,
configs ...func(*vz.VirtualMachineConfiguration) error,
) *Container {
port, err := getFreePort()
if err != nil {
t.Fatalf("failed to resolve free tcp addr: %v", err)
}
vmlinuz := "./testdata/Image"
initramfs := "./testdata/initramfs.cpio.gz"
cmdline := fmt.Sprintf("console=hvc0 vsock_port=%d", port)
bootLoader, err := vz.NewLinuxBootLoader(
vmlinuz,
vz.WithCommandLine(cmdline),
vz.WithInitrd(initramfs),
)
if err != nil {
t.Fatal(err)
}
config, err := setupConfiguration(bootLoader)
if err != nil {
t.Fatal(err)
}
for _, setConfig := range configs {
if err := setConfig(config); err != nil {
t.Fatal(err)
}
}
validated, err := config.Validate()
if !validated || err != nil {
t.Fatal(validated, err)
}
vm, err := vz.NewVirtualMachine(config)
if err != nil {
t.Fatal(err)
}
socketDevices := vm.SocketDevices()
if len(socketDevices) != 1 {
t.Fatalf("want the number of socket devices is 1 but got %d", len(socketDevices))
}
socketDevice := socketDevices[0]
if canStart := vm.CanStart(); !canStart {
t.Fatal("want CanStart is true")
}
// As it is not possible to intentionally cause errors, only logging is possible.
disconnected, err := vm.NetworkDeviceAttachmentWasDisconnected()
if err == nil {
go func() {
for disconnected := range disconnected {
log.Println(disconnected)
}
}()
}
if err := vm.Start(); err != nil {
t.Fatal(err)
}
waitState(t, 3*time.Second, vm, vz.VirtualMachineStateStarting)
waitState(t, 3*time.Second, vm, vz.VirtualMachineStateRunning)
sshConfig := testhelper.NewSshConfig("root", "passwd")
// Workaround for macOS 11
//
// This is a workaround. This version of the API does not immediately return an error and
// does not seem to have a connection timeout set.
if vz.Available(12) {
time.Sleep(5 * time.Second)
} else {
// Not immediately available in the x86_64 environment
// so wait a little longer for the orbit before testing.
time.Sleep(3 * time.Second)
}
const max = 5
RETRY:
for i := 1; ; i++ {
conn, err := socketDevice.Connect(uint32(port))
if err != nil {
var nserr *vz.NSError
if !errors.As(err, &nserr) || i > max {
t.Fatal(err)
}
if nserr.Code == int(syscall.ECONNRESET) {
t.Logf("retry vsock connect: %d", i)
time.Sleep(backOffDelay(i))
continue RETRY
}
t.Fatalf("failed to connect vsock: %v", err)
}
t.Log("setup ssh client in container")
initialized := make(chan struct{})
retry := make(chan struct{})
go func() {
select {
case <-initialized:
case <-time.After(5 * time.Second):
close(retry)
t.Log("closed", conn.Close())
}
}()
sshClient, err := testhelper.NewSshClient(conn, ":22", sshConfig)
if err != nil {
select {
case <-retry:
t.Log("retry because ssh handshake has been failed")
continue RETRY
default:
}
conn.Close()
t.Fatalf("failed to create a new ssh client: %v", err)
}
close(initialized)
t.Logf("container setup done")
return &Container{
VirtualMachine: vm,
Client: sshClient,
}
}
}
func backOffDelay(retryAttempts int) time.Duration {
factor := 0.5
delay := math.Exp2(float64(retryAttempts)) * factor
return time.Duration(math.Min(delay, 10)) * time.Second
}
func waitState(t *testing.T, wait time.Duration, vm *vz.VirtualMachine, want vz.VirtualMachineState) {
t.Helper()
select {
case got := <-vm.StateChangedNotify():
if want != got {
t.Fatalf("unexpected state want %d but got %d", want, got)
}
case <-time.After(wait):
t.Fatal("failed to wait state changed notification")
}
}
func TestRun(t *testing.T) {
container := newVirtualizationMachine(t,
func(vmc *vz.VirtualMachineConfiguration) error {
return setupConsoleConfig(vmc)
},
)
defer container.Close()
sshSession := container.NewSession(t)
defer sshSession.Close()
vm := container.VirtualMachine
if got := vm.State(); vz.VirtualMachineStateRunning != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateRunning, got)
}
if got := vm.CanPause(); !got {
t.Fatal("want CanPause is true")
}
if err := vm.Pause(); err != nil {
t.Fatal(err)
}
timeout := 5 * time.Second
waitState(t, timeout, vm, vz.VirtualMachineStatePausing)
waitState(t, timeout, vm, vz.VirtualMachineStatePaused)
if got := vm.State(); vz.VirtualMachineStatePaused != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStatePaused, got)
}
if got := vm.CanResume(); !got {
t.Fatal("want CanPause is true")
}
if err := vm.Resume(); err != nil {
t.Fatal(err)
}
waitState(t, timeout, vm, vz.VirtualMachineStateResuming)
waitState(t, timeout, vm, vz.VirtualMachineStateRunning)
if got := vm.CanRequestStop(); !got {
t.Fatal("want CanRequestStop is true")
}
// TODO(codehex): I need to support
// see: https://developer.apple.com/forums/thread/702160
//
// if success, err := vm.RequestStop(); !success || err != nil {
// t.Error(success, err)
// return
// }
// waitState(t, 5*time.Second, vm, vz.VirtualMachineStateStopping)
// waitState(t, 5*time.Second, vm, vz.VirtualMachineStateStopped)
sshSession.Run("poweroff")
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
if got := vm.State(); vz.VirtualMachineStateStopped != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateStopped, got)
}
}
func TestStop(t *testing.T) {
if vz.Available(12) {
t.Skip("Stop is supported from macOS 12")
}
container := newVirtualizationMachine(t)
defer container.Close()
vm := container.VirtualMachine
if got := vm.CanStop(); !got {
t.Fatal("want CanRequestStop is true")
}
if err := vm.Stop(); err != nil {
t.Fatal(err)
}
timeout := 3 * time.Second
waitState(t, timeout, vm, vz.VirtualMachineStateStopping)
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
}
func TestVirtualMachineStateString(t *testing.T) {
cases := []struct {
state vz.VirtualMachineState
want string
}{
{
state: vz.VirtualMachineStateStopped,
want: "VirtualMachineStateStopped",
},
{
state: vz.VirtualMachineStateRunning,
want: "VirtualMachineStateRunning",
},
{
state: vz.VirtualMachineStatePaused,
want: "VirtualMachineStatePaused",
},
{
state: vz.VirtualMachineStateError,
want: "VirtualMachineStateError",
},
{
state: vz.VirtualMachineStateStarting,
want: "VirtualMachineStateStarting",
},
{
state: vz.VirtualMachineStatePausing,
want: "VirtualMachineStatePausing",
},
{
state: vz.VirtualMachineStateResuming,
want: "VirtualMachineStateResuming",
},
{
state: vz.VirtualMachineStateStopping,
want: "VirtualMachineStateStopping",
},
}
for _, tc := range cases {
got := tc.state.String()
if tc.want != got {
t.Fatalf("want %q but got %q", tc.want, got)
}
}
}
func TestRunIssue124(t *testing.T) {
if os.Getenv("TEST_ISSUE_124") != "1" {
t.Skip()
}
container := newVirtualizationMachine(t,
func(vmc *vz.VirtualMachineConfiguration) error {
return setupConsoleConfig(vmc)
},
)
defer container.Close()
sshSession := container.NewSession(t)
defer sshSession.Close()
vm := container.VirtualMachine
if got := vm.State(); vz.VirtualMachineStateRunning != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateRunning, got)
}
if got := vm.CanPause(); !got {
t.Fatal("want CanPause is true")
}
if err := vm.Pause(); err != nil {
t.Fatal(err)
}
timeout := 5 * time.Second
waitState(t, timeout, vm, vz.VirtualMachineStatePausing)
waitState(t, timeout, vm, vz.VirtualMachineStatePaused)
if got := vm.State(); vz.VirtualMachineStatePaused != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStatePaused, got)
}
if got := vm.CanResume(); !got {
t.Fatal("want CanPause is true")
}
if err := vm.Resume(); err != nil {
t.Fatal(err)
}
waitState(t, timeout, vm, vz.VirtualMachineStateResuming)
waitState(t, timeout, vm, vz.VirtualMachineStateRunning)
if got := vm.CanRequestStop(); !got {
t.Fatal("want CanRequestStop is true")
}
ch := make(chan bool)
vm.SetMachineStateFinalizer(func() {
ch <- true
})
runtime.GC()
select {
case <-ch:
t.Errorf("expected finalizer do not run")
case <-time.After(4 * time.Minute):
}
runtime.GC()
sshSession.Run("poweroff")
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
if got := vm.State(); vz.VirtualMachineStateStopped != got {
t.Fatalf("want state %v but got %v", vz.VirtualMachineStateStopped, got)
}
}