Repository navigation
Expand file tree
/
Copy pathnetwork_notification_example_test.go
More file actions
73 lines (67 loc) · 1.69 KB
/
Copy pathnetwork_notification_example_test.go
File metadata and controls
73 lines (67 loc) · 1.69 KB
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
//go:build darwin
package vz_test
import (
"context"
"fmt"
"time"
"github.com/Code-Hex/vz/v4"
)
// This example requires macOS 12 or newer. An event reports a framework attachment
// failure, not a guest network outage or an unreachable remote host.
func ExampleVirtualMachine_NetworkDeviceAttachmentWasDisconnectedNotify() {
loader, err := vz.NewLinuxBootLoader("testdata/Image", vz.WithInitrd("testdata/initramfs.cpio.gz"))
if err != nil {
fmt.Println(err)
return
}
config, err := vz.NewVirtualMachineConfiguration(loader, 1, 512*1024*1024)
if err != nil {
fmt.Println(err)
return
}
attachment, err := vz.NewNATNetworkDeviceAttachment()
if err != nil {
fmt.Println(err)
return
}
network, err := vz.NewVirtioNetworkDeviceConfiguration(attachment)
if err != nil {
fmt.Println(err)
return
}
config.SetNetworkDevicesVirtualMachineConfiguration([]*vz.VirtioNetworkDeviceConfiguration{network})
machine, err := vz.NewVirtualMachine(config)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if machine.CanStop() {
if err := machine.Stop(); err != nil {
fmt.Println(err)
}
}
}()
events := make(chan *vz.DisconnectedError, 16)
stop, err := machine.NetworkDeviceAttachmentWasDisconnectedNotify(events)
if err != nil {
fmt.Println(err)
return
}
defer stop()
if err := machine.Start(); err != nil {
fmt.Println(err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
select {
case event := <-events:
fmt.Println("network attachment disconnected:", event.Err)
if event.Config == network {
fmt.Println("the configured NAT device was affected")
}
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}