|
| 1 | +package vz_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/Code-Hex/vz/v3" |
| 8 | +) |
| 9 | + |
| 10 | +func TestVmnetSharedModeAllowsCommunicationBetweenMultipleVMs(t *testing.T) { |
| 11 | + if vz.Available(26.0) { |
| 12 | + t.Skip("VmnetSharedMode is supported from macOS 26") |
| 13 | + } |
| 14 | + config, err := vz.NewVmnetNetworkConfiguration(vz.SharedMode) |
| 15 | + if err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
| 18 | + network, err := vz.NewVmnetNetwork(config) |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + |
| 23 | + container1 := newVirtualizationMachine(t, ConfigureNetworkDeviceWithNetwork(network)) |
| 24 | + container2 := newVirtualizationMachine(t, ConfigureNetworkDeviceWithNetwork(network)) |
| 25 | + t.Cleanup(func() { |
| 26 | + if err := container1.Shutdown(); err != nil { |
| 27 | + log.Println(err) |
| 28 | + } |
| 29 | + if err := container2.Shutdown(); err != nil { |
| 30 | + log.Println(err) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + // Log network information |
| 35 | + ipv4Subnet, err := network.IPv4Subnet() |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + t.Logf("Vmnet network IPv4 subnet: %s", ipv4Subnet.String()) |
| 40 | + prefix, err := network.IPv6Prefix() |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + t.Logf("Vmnet network IPv6 prefix: %s", prefix.String()) |
| 45 | + |
| 46 | + // Detect IP addresses and test communication between VMs |
| 47 | + container1IPv4 := container1.DetectIPv4(t, "eth0") |
| 48 | + t.Logf("Container 1 IPv4: %s", container1IPv4) |
| 49 | + container2IPv4 := container2.DetectIPv4(t, "eth0") |
| 50 | + t.Logf("Container 2 IPv4: %s", container2IPv4) |
| 51 | + container1.exec(t, "ping "+container2IPv4) |
| 52 | + container2.exec(t, "ping "+container1IPv4) |
| 53 | +} |
| 54 | + |
| 55 | +func ConfigureNetworkDeviceWithNetwork(network vz.VmnetNetwork) func(cfg *vz.VirtualMachineConfiguration) error { |
| 56 | + return func(cfg *vz.VirtualMachineConfiguration) error { |
| 57 | + var configurations []*vz.VirtioNetworkDeviceConfiguration |
| 58 | + attachment, err := vz.NewVmnetNetworkDeviceAttachment(network) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + config, err := vz.NewVirtioNetworkDeviceConfiguration(attachment) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + configurations = append(configurations, config) |
| 67 | + cfg.SetNetworkDevicesVirtualMachineConfiguration(configurations) |
| 68 | + return nil |
| 69 | + } |
| 70 | +} |
0 commit comments