Skip to content

Commit 6617c8f

Browse files
committed
feat(vmnet_test.go): add unit tests
Add: - `TestVmnetSharedModeAllowsCommunicationBetweenMultipleVMs()` - `Container.DetectIPv4()`
1 parent b01d9df commit 6617c8f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

virtualization_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88
"net"
99
"os"
10+
"regexp"
1011
"runtime"
1112
"syscall"
1213
"testing"
@@ -110,6 +111,27 @@ func (c *Container) NewSession(t *testing.T) *ssh.Session {
110111
return sshSession
111112
}
112113

114+
func (c *Container) DetectIPv4(t *testing.T, ifname string) string {
115+
sshSession, err := c.Client.NewSession()
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
defer sshSession.Close()
120+
121+
output, err := sshSession.Output(fmt.Sprintf("ip address show dev %s scope global", ifname))
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
re := regexp.MustCompile(`(?ms)^\s+inet\s+([0-9.]+)/`)
126+
matches := re.FindStringSubmatch(string(output))
127+
if len(matches) == 2 {
128+
return matches[1]
129+
}
130+
t.Fatalf("failed to parse IP address from output: %s", output)
131+
132+
return ""
133+
}
134+
113135
func (c *Container) Shutdown() error {
114136
defer func() {
115137
log.Println("shutdown done")

vmnet_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)