Skip to content

Commit ae4a334

Browse files
committed
hack/test-port-forwarding.pl: Add a test using hostSocket
Signed-off-by: Norio Nomura <[email protected]>
1 parent 7636bed commit ae4a334

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

hack/test-port-forwarding.pl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
chomp $ipv4;
3434
}
3535

36+
my $instDir = qx(limactl list "$instance" --yq .dir);
37+
chomp $instDir;
38+
3639
# If $instance is a filename, add our portForwards to it to enable testing
3740
if (-f $instance) {
3841
open(my $fh, "+< $instance") or die "Can't open $instance for read/write: $!";
@@ -94,14 +97,17 @@
9497
s/sshLocalPort/$sshLocalPort/g;
9598
s/ipv4/$ipv4/g;
9699
s/ipv6/$ipv6/g;
100+
s/instDir/$instDir/g;
97101
# forward: 127.0.0.1 899 → 127.0.0.1 799
98102
# ignore: 127.0.0.2 8888
99-
/^(forward|ignore):\s+([0-9.:]+)\s+(\d+)(?:\s+→)?(?:\s+([0-9.:]+)(?:\s+(\d+))?)?/;
103+
/^(forward|ignore):\s+([0-9.:]+)\s+(\d+)(?:\s+→)?(?:\s+(?:([0-9.:]+)(?:\s+(\d+))|(\/\S+))?)?/;
100104
die "Cannot parse test '$_'" unless $1;
101-
my %test; @test{qw(mode guest_ip guest_port host_ip host_port)} = ($1, $2, $3, $4, $5);
105+
my %test; @test{qw(mode guest_ip guest_port host_ip host_port host_socket)} = ($1, $2, $3, $4, $5, $6);
106+
102107
$test{host_ip} ||= "127.0.0.1";
103108
$test{host_port} ||= $test{guest_port};
104-
if ($test{mode} eq "forward" && $test{host_port} < 1024 && $Config{osname} ne "darwin") {
109+
$test{host_socket} ||= "";
110+
if ($test{mode} eq "forward" && $test{host_socket} eq "" && $test{host_port} < 1024 && $Config{osname} ne "darwin") {
105111
printf "🚧 Not supported on $Config{osname}: # $_\n";
106112
next;
107113
}
@@ -115,7 +121,7 @@
115121
}
116122

117123
my $remote = JoinHostPort($test{guest_ip},$test{guest_port});
118-
my $local = JoinHostPort($test{host_ip},$test{host_port});
124+
my $local = $test{host_socket} eq "" ? JoinHostPort($test{host_ip},$test{host_port}) : $test{host_socket};
119125
if ($test{mode} eq "ignore") {
120126
$test{log_msg} = "Not forwarding TCP $remote";
121127
}
@@ -163,7 +169,7 @@
163169
# Try to reach each listener from the host
164170
foreach my $test (@test) {
165171
next if $test->{host_port} == $sshLocalPort;
166-
my $nc = "nc -w 1 $test->{host_ip} $test->{host_port}";
172+
my $nc = $test->{host_socket} eq "" ? "nc -w 1 $test->{host_ip} $test->{host_port}" : "nc -w 1 -U $test->{host_socket}";
167173
open(my $netcat, "| $nc") or die "Can't run '$nc': $!";
168174
print $netcat "$test->{log_msg}\n";
169175
# Don't check for errors on close; macOS nc seems to return non-zero exit code even on success
@@ -175,7 +181,7 @@
175181
seek($log, $ha_log_size, 0) or die "Can't seek $ha_log to $ha_log_size: $!";
176182
my %seen;
177183
while (<$log>) {
178-
$seen{$1}++ if /(Forwarding TCP from .*? to (\d.*?|\[.*?\]):\d+)/;
184+
$seen{$1}++ if /(Forwarding TCP from .*? to ((\d.*?|\[.*?\]):\d+|\/[^"]+))/;
179185
$seen{$1}++ if /(Not forwarding TCP .*?:\d+)/;
180186
}
181187
close $log or die;
@@ -342,3 +348,8 @@ sub JoinHostPort {
342348
- guestIPMustBeZero: true
343349
guestPort: 8888
344350
hostIP: 0.0.0.0
351+
352+
- guestPort: 5000
353+
hostSocket: "{{.Dir}}/sock/port5000.sock"
354+
355+
# forward: 127.0.0.1 5000 → instDir/sock/port5000.sock

0 commit comments

Comments
 (0)