Skip to content

Commit ef88218

Browse files
committed
hack/test-port-forwarding.pl: Add a test using hostSocket
Signed-off-by: Norio Nomura <[email protected]> hack/test-port-forwarding.pl: use platform-independent path on hostSocket Signed-off-by: Norio Nomura <[email protected]> hack/test-port-forwarding.pl: Skip hostSocket test on Windows host Signed-off-by: Norio Nomura <[email protected]>
1 parent 8108012 commit ef88218

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

hack/test-port-forwarding.pl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use warnings;
1717

1818
use Config qw(%Config);
19+
use File::Spec::Functions qw(catfile);
1920
use IO::Handle qw();
2021
use Socket qw(inet_ntoa);
2122
use Sys::Hostname qw(hostname);
@@ -33,6 +34,11 @@
3334
chomp $ipv4;
3435
}
3536

37+
my $instDir = qx(limactl list "$instance" --yq .dir);
38+
chomp $instDir;
39+
# platform independent way to add trailing path separator
40+
my $sockDir = catfile($instDir, "sock", "");
41+
3642
# If $instance is a filename, add our portForwards to it to enable testing
3743
if (-f $instance) {
3844
open(my $fh, "+< $instance") or die "Can't open $instance for read/write: $!";
@@ -94,14 +100,17 @@
94100
s/sshLocalPort/$sshLocalPort/g;
95101
s/ipv4/$ipv4/g;
96102
s/ipv6/$ipv6/g;
103+
s/sockDir\//$sockDir/g;
97104
# forward: 127.0.0.1 899 → 127.0.0.1 799
98105
# ignore: 127.0.0.2 8888
99-
/^(forward|ignore):\s+([0-9.:]+)\s+(\d+)(?:\s+→)?(?:\s+([0-9.:]+)(?:\s+(\d+))?)?/;
106+
/^(forward|ignore):\s+([0-9.:]+)\s+(\d+)(?:\s+→)?(?:\s+(?:([0-9.:]+)(?:\s+(\d+))|(\S+))?)?/;
100107
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);
108+
my %test; @test{qw(mode guest_ip guest_port host_ip host_port host_socket)} = ($1, $2, $3, $4, $5, $6);
109+
102110
$test{host_ip} ||= "127.0.0.1";
103111
$test{host_port} ||= $test{guest_port};
104-
if ($test{mode} eq "forward" && $test{host_port} < 1024 && $Config{osname} ne "darwin") {
112+
$test{host_socket} ||= "";
113+
if ($test{mode} eq "forward" && $test{host_socket} eq "" && $test{host_port} < 1024 && $Config{osname} ne "darwin") {
105114
printf "🚧 Not supported on $Config{osname}: # $_\n";
106115
next;
107116
}
@@ -113,9 +122,13 @@
113122
printf "🚧 Not supported for $instanceType machines: # $_\n";
114123
next;
115124
}
125+
if ($test{host_socket} ne "" && $Config{osname} eq "cygwin") {
126+
printf "🚧 Not supported on $Config{osname}: # $_\n";
127+
next;
128+
}
116129

117130
my $remote = JoinHostPort($test{guest_ip},$test{guest_port});
118-
my $local = JoinHostPort($test{host_ip},$test{host_port});
131+
my $local = $test{host_socket} eq "" ? JoinHostPort($test{host_ip},$test{host_port}) : $test{host_socket};
119132
if ($test{mode} eq "ignore") {
120133
$test{log_msg} = "Not forwarding TCP $remote";
121134
}
@@ -163,7 +176,7 @@
163176
# Try to reach each listener from the host
164177
foreach my $test (@test) {
165178
next if $test->{host_port} == $sshLocalPort;
166-
my $nc = "nc -w 1 $test->{host_ip} $test->{host_port}";
179+
my $nc = $test->{host_socket} eq "" ? "nc -w 1 $test->{host_ip} $test->{host_port}" : "nc -w 1 -U $test->{host_socket}";
167180
open(my $netcat, "| $nc") or die "Can't run '$nc': $!";
168181
print $netcat "$test->{log_msg}\n";
169182
# Don't check for errors on close; macOS nc seems to return non-zero exit code even on success
@@ -175,7 +188,7 @@
175188
seek($log, $ha_log_size, 0) or die "Can't seek $ha_log to $ha_log_size: $!";
176189
my %seen;
177190
while (<$log>) {
178-
$seen{$1}++ if /(Forwarding TCP from .*? to (\d.*?|\[.*?\]):\d+)/;
191+
$seen{$1}++ if /(Forwarding TCP from .*? to ((\d.*?|\[.*?\]):\d+|\/[^"]+))/;
179192
$seen{$1}++ if /(Not forwarding TCP .*?:\d+)/;
180193
}
181194
close $log or die;
@@ -342,3 +355,8 @@ sub JoinHostPort {
342355
- guestIPMustBeZero: true
343356
guestPort: 8888
344357
hostIP: 0.0.0.0
358+
359+
- guestPort: 5000
360+
hostSocket: port5000.sock
361+
362+
# forward: 127.0.0.1 5000 → sockDir/port5000.sock

0 commit comments

Comments
 (0)