diff --git a/perl-xCAT/xCAT/DSHCore.pm b/perl-xCAT/xCAT/DSHCore.pm index e88ffccae5..18cea18ff0 100644 --- a/perl-xCAT/xCAT/DSHCore.pm +++ b/perl-xCAT/xCAT/DSHCore.pm @@ -14,6 +14,7 @@ use Socket; use xCAT::MsgUtils; use xCAT::NetworkUtils; use xCAT::Utils; +use Text::ParseWords qw(shellwords); =head1 NAME @@ -730,6 +731,99 @@ sub partition_ping_targets } +#--------------------------------------------------------------------------- + +=head3 + nmap_ping_command + + Builds the nmap command pping runs for one address family. + + Arguments: + $family - 4 or 6 + $more_options - the site nmapoptions value, or undef + $targets - reference to the target list + + Returns: + the command as a list + + Comments: + The command is a list, not a string, so pping can exec it + without a shell. nmap needs -6 for an IPv6 target: it reads an + IPv6 literal as a hostname otherwise. + +=cut + +#--------------------------------------------------------------------------- + +sub nmap_ping_command +{ + my ($class, $family, $more_options, $targets) = @_; + + my @command = ('nmap'); + push @command, '-6' if defined($family) && $family == 6; + + # Port 22 is here for the unprivileged case (#4324). + push @command, qw(-PE --system-dns --send-ip -sP --unprivileged -PA80,443,22); + push @command, shellwords($more_options) + if defined($more_options) && length($more_options); + push @command, @{ $targets || [] }; + return @command; +} + + +#--------------------------------------------------------------------------- + +=head3 + fping_ping_command + + Builds the fping command pping runs for one address family. + + Arguments: + $family - 4 or 6 + $binaries - reference to a hash with the fping and fping6 + paths that exist on this host, either one undef + $targets - reference to the target list + + Returns: + the command as a list, or an empty list when no fping on this + host can probe that family + + Comments: + fping6 is a separate program on an older distribution. A + current fping takes -6 instead and ships no fping6. + +=cut + +#--------------------------------------------------------------------------- + +sub fping_ping_command +{ + my ($class, $family, $binaries, $targets) = @_; + $binaries ||= {}; + + my @command; + if (defined($family) && $family == 6) + { + if ($binaries->{fping6}) + { + @command = ($binaries->{fping6}); + } + elsif ($binaries->{fping}) + { + @command = ($binaries->{fping}, '-6'); + } + } + elsif ($binaries->{fping}) + { + @command = ($binaries->{fping}); + } + return () unless @command; + + push @command, '-a', @{ $targets || [] }; + return @command; +} + + #--------------------------------------------------------------------------- =head3 diff --git a/xCAT-client/bin/pping b/xCAT-client/bin/pping index befa24d98c..99c0c390d1 100755 --- a/xCAT-client/bin/pping +++ b/xCAT-client/bin/pping @@ -24,7 +24,6 @@ $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; use IO::Handle; use IO::Select; use Getopt::Long; -use Text::ParseWords qw(shellwords); my $USAGE = "Usage: pping [-i|--interface interfaces][-f|--use_fping] noderange pping -h|--help @@ -166,30 +165,22 @@ sub fping_pping { my ($nodes, $family) = @_; return unless @{$nodes}; - my $fping = -x '/usr/bin/fping' ? '/usr/bin/fping' - : -x '/usr/sbin/fping' ? '/usr/sbin/fping' - : undef; - my @command; - if ($family == 6) { - my $fping6 = -x '/usr/bin/fping6' ? '/usr/bin/fping6' - : -x '/usr/sbin/fping6' ? '/usr/sbin/fping6' - : undef; - if ($fping6) { - @command = ($fping6); - } elsif ($fping) { - @command = ($fping, '-6'); + my %binaries; + foreach my $program (qw(fping fping6)) { + foreach my $directory (qw(/usr/bin /usr/sbin)) { + next unless -x "$directory/$program"; + $binaries{$program} = "$directory/$program"; + last; } - } elsif ($fping) { - @command = ($fping); } + my @command = + xCAT::DSHCore->fping_ping_command($family, \%binaries, $nodes); unless (@command) { warn "fping is not available for IPv$family ping.\n"; print "$_: noping\n" foreach @{$nodes}; return; } - - push @command, '-a', @{$nodes}; my @output; my $pid = open(my $fping_output, '-|'); if (!defined($pid)) { @@ -230,12 +221,8 @@ sub nmap_pping { } $more_options = '' unless defined($more_options); - # Added port 22 for unprivileged case (#4324) - my @command = ('nmap'); - push @command, '-6' if $family == 6; - push @command, qw(-PE --system-dns --send-ip -sP --unprivileged -PA80,443,22); - push @command, shellwords($more_options) if length($more_options); - push @command, @{$nodes}; + my @command = + xCAT::DSHCore->nmap_ping_command($family, $more_options, $nodes); my $pid = open(my $nmap_output, '-|'); die("Cannot open nmap pipe: $!") unless defined($pid); diff --git a/xCAT-server/lib/xcat/plugins/anaconda.pm b/xCAT-server/lib/xcat/plugins/anaconda.pm index c3a5daf083..52c97edc96 100644 --- a/xCAT-server/lib/xcat/plugins/anaconda.pm +++ b/xCAT-server/lib/xcat/plugins/anaconda.pm @@ -71,6 +71,30 @@ sub _syslog_server_arg return xCAT::NetworkUtils->format_uri_host($server); } +# The debug syslog arguments of the stateless kernel command line. An IPv6 +# server has to be bracketed, or the kernel reads the last colon group as the +# port. An unusable server name gives no arguments at all, rather than +# "syslog.server=" with nothing after it. +sub _netboot_syslog_kcmdline_args +{ + my $server = shift; + my $arg = _syslog_server_arg($server); + return '' unless defined($arg); + return " syslog.server=$arg syslog.type=rsyslogd syslog.filter=*.* "; +} + +# The same arguments for a stateful install. The modern installer takes +# inst.syslog and brackets an IPv6 server; the legacy one takes syslog and +# never saw an IPv6 address. +sub _install_syslog_kcmdline_args +{ + my ($server, $modern_installer) = @_; + return " syslog=$server" unless $modern_installer; + my $arg = _syslog_server_arg($server); + return '' unless defined($arg); + return " inst.syslog=$arg"; +} + sub _el10_install_url_args { my ($method, $authority, $httpprefix, $node) = @_; @@ -995,8 +1019,7 @@ sub mknetboot #for use in postscript and postbootscript in xcatdsklspost in the rootimg $kcmdline .= " LOGSERVER=$xcatmasterip "; #for use in syslog dracut module in the initrd - my $syslog_server = _syslog_server_arg($xcatmasterip); - $kcmdline .= " syslog.server=$syslog_server syslog.type=rsyslogd syslog.filter=*.* "; + $kcmdline .= _netboot_syslog_kcmdline_args($xcatmasterip); $kcmdline .= " xcatdebugmode=$::XCATSITEVALS{xcatdebugmode} "; } @@ -1757,13 +1780,12 @@ sub mkinstall $kcmdline .= " inst.loglevel=debug"; #all the logs during installation will be forwarded to xcatmster - my $syslog_server = _syslog_server_arg($instserver); - $kcmdline .= " inst.syslog=$syslog_server"; + $kcmdline .= _install_syslog_kcmdline_args($instserver, 1); } else { if ($::XCATSITEVALS{xcatdebugmode} eq "2") { $kcmdline .= " sshd=1"; } - $kcmdline .= " syslog=$instserver"; + $kcmdline .= _install_syslog_kcmdline_args($instserver, 0); } } diff --git a/xCAT-test/bats/ipv6_allowcred_awk.bats b/xCAT-test/bats/ipv6_allowcred_awk.bats new file mode 100644 index 0000000000..4a28a0f1fb --- /dev/null +++ b/xCAT-test/bats/ipv6_allowcred_awk.bats @@ -0,0 +1,94 @@ +#!/usr/bin/env bats +# +# allowcred.awk runs on the management node and answers the credential request +# a node sends during discovery. It listens on a gawk network special file, and +# the family of that file comes from the master address xCAT exports. An IPv6 +# master needs /inet6/, because a /inet/ listener on this gawk accepts no IPv6 +# connection, so an IPv6-only node never gets its credentials. +# +# Each test runs the real program and connects to it over the family under +# test. Everything is on the loopback address; nothing reaches the lab. + +load 'helpers/shell_source' + +setup() +{ + SCRIPT="$(require_repo_file 'xCAT/postscripts/allowcred.awk')" + command -v gawk >/dev/null || skip 'gawk is required' + command -v socat >/dev/null || skip 'socat is required' + command -v ss >/dev/null || skip 'ss is required' + PORT=$(( 20000 + RANDOM % 20000 )) + SERVER_PID= +} + +teardown() +{ + [ -n "$SERVER_PID" ] && kill "$SERVER_PID" 2>/dev/null + return 0 +} + +# listen -- start allowcred.awk with the environment already in the +# caller's export list, then wait for the port. Its stdio is detached, because +# a child holding the test's pipes open makes a failure hang instead of go red. +listen() +{ + local port="$1" i + setsid gawk -f "$SCRIPT" /dev/null 2>&1 & + SERVER_PID=$! + for i in $(seq 1 100); do + ss -Hltn "sport = :${port}" 2>/dev/null | grep -q . && return 0 + sleep 0.1 + done + return 1 +} + +# ask -- send one credential request and print the answer. +ask() +{ + printf 'CREDOKBYYOU\n' | socat -T2 - "$1" 2>/dev/null +} + +@test "an IPv6 master address makes the credential callback answer over IPv6" { + export MASTER_IP='2001:db8::1' + export XCAT_CREDENTIAL_CALLBACK_PORT="$PORT" + listen "$PORT" + run ask "TCP6:[::1]:${PORT}" + [ "$status" -eq 0 ] + [ "$output" = 'CREDOKBYME' ] +} + +@test "an IPv4 master address makes the credential callback answer over IPv4" { + export MASTER_IP='192.0.2.1' + export XCAT_CREDENTIAL_CALLBACK_PORT="$PORT" + listen "$PORT" + run ask "TCP4:127.0.0.1:${PORT}" + [ "$status" -eq 0 ] + [ "$output" = 'CREDOKBYME' ] +} + +@test "the master address is read from MASTER when MASTER_IP is unset" { + export MASTER='2001:db8::1' + export XCAT_CREDENTIAL_CALLBACK_PORT="$PORT" + listen "$PORT" + run ask "TCP6:[::1]:${PORT}" + [ "$status" -eq 0 ] + [ "$output" = 'CREDOKBYME' ] +} + +@test "an IPv4 listener refuses the IPv6 loopback" { + export MASTER_IP='192.0.2.1' + export XCAT_CREDENTIAL_CALLBACK_PORT="$PORT" + listen "$PORT" + run ask "TCP6:[::1]:${PORT}" + [ "$output" != 'CREDOKBYME' ] +} + +@test "a port outside the valid range falls back to the default port 300" { + [ "$(id -u)" -eq 0 ] || skip 'binding port 300 needs root' + export MASTER_IP='2001:db8::1' + export XCAT_CREDENTIAL_CALLBACK_PORT='99999' + listen 300 + run ask 'TCP6:[::1]:300' + [ "$status" -eq 0 ] + [ "$output" = 'CREDOKBYME' ] +} diff --git a/xCAT-test/unit/anaconda_ipv6_rendering.t b/xCAT-test/unit/anaconda_ipv6_rendering.t index 76be979f3b..993d8c5d8e 100644 --- a/xCAT-test/unit/anaconda_ipv6_rendering.t +++ b/xCAT-test/unit/anaconda_ipv6_rendering.t @@ -18,9 +18,6 @@ my $anaconda = File::Spec->catfile( 'xCAT-server/lib/xcat/plugins/anaconda.pm' ); do $anaconda or die $@ || "Unable to load $anaconda: $!"; -open(my $anaconda_fh, '<', $anaconda) or die "Unable to read $anaconda: $!"; -my $anaconda_source = do { local $/; <$anaconda_fh> }; -close($anaconda_fh); my %addresses = ( nodev4 => { 4 => '192.0.2.50' }, @@ -118,15 +115,36 @@ ok( !defined(xCAT_plugin::anaconda::_syslog_server_arg('bad host')), 'malformed syslog server arguments remain rejected' ); -like( - $anaconda_source, - qr/syslog\.server=\$syslog_server syslog\.type=rsyslogd/, - 'stateless debug boot uses the family-safe syslog argument' +# The two kernel command lines that carry the debug syslog server, rendered. +is( + xCAT_plugin::anaconda::_netboot_syslog_kcmdline_args('2001:db8::1'), + ' syslog.server=[2001:db8::1] syslog.type=rsyslogd syslog.filter=*.* ', + 'stateless debug boot brackets an IPv6 syslog server' ); -like( - $anaconda_source, - qr/inst\.syslog=\$syslog_server/, - 'stateful EL10 debug install uses the family-safe syslog argument' +is( + xCAT_plugin::anaconda::_netboot_syslog_kcmdline_args('192.0.2.1'), + ' syslog.server=192.0.2.1 syslog.type=rsyslogd syslog.filter=*.* ', + 'stateless debug boot leaves an IPv4 syslog server unbracketed' +); +is( + xCAT_plugin::anaconda::_netboot_syslog_kcmdline_args('bad host'), + '', + 'stateless debug boot omits the syslog arguments for an unusable server' +); +is( + xCAT_plugin::anaconda::_install_syslog_kcmdline_args( '2001:db8::1', 1 ), + ' inst.syslog=[2001:db8::1]', + 'stateful EL10 debug install brackets an IPv6 syslog server' +); +is( + xCAT_plugin::anaconda::_install_syslog_kcmdline_args( '192.0.2.1', 1 ), + ' inst.syslog=192.0.2.1', + 'stateful EL10 debug install leaves an IPv4 syslog server unbracketed' +); +is( + xCAT_plugin::anaconda::_install_syslog_kcmdline_args( '192.0.2.1', 0 ), + ' syslog=192.0.2.1', + 'the legacy installer keeps its own syslog argument' ); is( diff --git a/xCAT-test/unit/credentials_ipv6_callback.t b/xCAT-test/unit/credentials_ipv6_callback.t index f9c05c7c97..6dac20a411 100644 --- a/xCAT-test/unit/credentials_ipv6_callback.t +++ b/xCAT-test/unit/credentials_ipv6_callback.t @@ -4,32 +4,29 @@ use warnings; use FindBin; BEGIN { $ENV{XCATROOT} = "$FindBin::Bin/../../xCAT-server"; } -use POSIX (); -use Time::HiRes qw(usleep); use Test::More; use lib "$FindBin::Bin/../../perl-xCAT"; use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; use lib "$FindBin::Bin/../../xCAT-server/lib/xcat"; +# credentials.pm loads Socket6 and IO::Socket::INET6 with an eval and records +# the result in a file-scoped lexical at load time, so the IPv6 branch of +# _open_callback_socket is unreachable on a host without them - which is the +# host the unit suite runs on. These stubs satisfy that eval, so the branch is +# measured everywhere, and each test replaces the constructor it needs. +BEGIN { + package Socket6; + $INC{'Socket6.pm'} = __FILE__; + + package IO::Socket::INET6; + sub new { return; } + $INC{'IO/Socket/INET6.pm'} = __FILE__; +} + my $source = "$FindBin::Bin/../../xCAT-server/lib/xcat/plugins/credentials.pm"; require $source; pass('credentials plugin loads successfully'); -my $allowcred = "$FindBin::Bin/../../xCAT/postscripts/allowcred.awk"; -open(my $allowcred_fh, '<', $allowcred) or die "Unable to read $allowcred: $!"; -my $allowcred_source = do { local $/; <$allowcred_fh> }; -close($allowcred_fh); -like( - $allowcred_source, - qr/"MASTER_IP" in ENVIRON/, - 'shipped credential callback selects its listener family from the resolved master' -); -like( - $allowcred_source, - qr{listener = "/inet6/tcp/" port "/0/0"}, - 'shipped credential callback has an IPv6 listener path' -); - { package CredentialsCallbackTest::Socket; } @@ -96,14 +93,7 @@ ok( 'malformed callback endpoint is rejected' ); -my $has_inet6 = eval { - require Socket6; - require IO::Socket::INET6; - 1; -}; -SKIP: { - skip 'IO::Socket::INET6 and Socket6 are unavailable', 4 unless $has_inet6; - +{ my ( %inet6_args, $inet4_calls ); no warnings 'redefine'; local *IO::Socket::INET6::new = sub { @@ -148,44 +138,6 @@ SKIP: { ); } -SKIP: { - skip 'IPv6 loopback sockets are unavailable', 2 unless $has_inet6; - my $listener = IO::Socket::INET6->new( - LocalAddr => '::1', - LocalPort => 0, - Listen => 1, - Proto => 'tcp', - ReuseAddr => 1, - ); - skip 'IPv6 loopback listener could not be created', 2 unless $listener; - - my $port = $listener->sockport(); - my $pid = fork(); - skip 'fork is unavailable', 2 unless defined($pid); - if ($pid == 0) { - local $SIG{ALRM} = sub { POSIX::_exit(2); }; - alarm(8); - my $peer = $listener->accept(); - POSIX::_exit(3) unless $peer; - my $challenge = <$peer>; - if (defined($challenge) && $challenge eq "CREDOKBYYOU?\n") { - print {$peer} "CREDOKBYME\n"; - close($peer); - POSIX::_exit(0); - } - POSIX::_exit(4); - } - - close($listener); - is( - xCAT_plugin::credentials::ok_with_node( '::1', [ 0, $port ] ), - 1, - 'plain callback completes over a real IPv6 loopback socket' - ); - waitpid( $pid, 0 ); - is( $?, 0, 'IPv6 callback server received the unchanged challenge' ); -} - { no warnings qw(once redefine); local *CALLBACK_SOCKET; @@ -236,68 +188,4 @@ SKIP: { ); } -my $gawk; -for my $directory (split(/:/, $ENV{PATH} || '')) { - my $candidate = "$directory/gawk"; - if (-x $candidate) { - $gawk = $candidate; - last; - } -} - -SKIP: { - skip 'gawk IPv6 networking is unavailable', 2 unless $has_inet6 && $gawk; - - my $reservation = IO::Socket::INET6->new( - LocalAddr => '::1', - LocalPort => 0, - Listen => 1, - Proto => 'tcp', - ReuseAddr => 1, - ); - skip 'IPv6 loopback port reservation failed', 2 unless $reservation; - my $port = $reservation->sockport(); - close($reservation); - - my $pid = fork(); - skip 'fork is unavailable', 2 unless defined($pid); - if ($pid == 0) { - $ENV{MASTER_IP} = '::1'; - $ENV{XCAT_CREDENTIAL_CALLBACK_PORT} = $port; - open(STDOUT, '>', '/dev/null'); - open(STDERR, '>', '/dev/null'); - exec($gawk, '-f', $allowcred) or POSIX::_exit(127); - } - - my $socket; - for (1 .. 40) { - $socket = IO::Socket::INET6->new( - PeerAddr => '::1', - PeerPort => $port, - Proto => 'tcp', - Timeout => 1, - ); - last if $socket; - usleep(100_000); - } - ok($socket, 'actual shipped callback accepts an IPv6 loopback connection'); - - my $response; - if ($socket) { - print {$socket} "CREDOKBYYOU?\n"; - local $SIG{ALRM} = sub { die "credential callback read timed out\n"; }; - eval { - alarm(5); - $response = <$socket>; - alarm(0); - }; - alarm(0); - close($socket); - } - is($response, "CREDOKBYME\n", 'actual shipped callback completes the credential challenge over IPv6'); - - kill('TERM', $pid); - waitpid($pid, 0); -} - done_testing(); diff --git a/xCAT-test/unit/pping_ipv6.t b/xCAT-test/unit/pping_ipv6.t index 107bfc3b8e..f4b9f88aa9 100644 --- a/xCAT-test/unit/pping_ipv6.t +++ b/xCAT-test/unit/pping_ipv6.t @@ -235,50 +235,82 @@ is_deeply( 'pping result parser ignores non-result diagnostics' ); -my $pping_source = "$FindBin::Bin/../../xCAT-client/bin/pping"; -open(my $fh, '<', $pping_source) or die "Unable to read $pping_source: $!"; -my $content = do { local $/; <$fh> }; -close($fh); +# pping runs nmap or fping as a list, never through a shell, so the command it +# builds is the thing to measure. The two builders below carry that decision. +is_deeply( + [ xCAT::DSHCore->nmap_ping_command( 6, '', [ '2001:db8::20', 'v6only' ] ) + ], + [ 'nmap', '-6', '-PE', '--system-dns', '--send-ip', '-sP', + '--unprivileged', '-PA80,443,22', '2001:db8::20', 'v6only' + ], + 'the nmap command for an IPv6 partition selects nmap -6' +); +is_deeply( + [ xCAT::DSHCore->nmap_ping_command( 4, '', ['192.0.2.10'] ) ], + [ 'nmap', '-PE', '--system-dns', '--send-ip', '-sP', + '--unprivileged', '-PA80,443,22', '192.0.2.10' + ], + 'the nmap command for an IPv4 partition does not select nmap -6' +); +is_deeply( + [ xCAT::DSHCore->nmap_ping_command( + 4, '--max-retries 2 -T4', ['192.0.2.10'] + ) + ], + [ 'nmap', '-PE', '--system-dns', '--send-ip', '-sP', + '--unprivileged', '-PA80,443,22', '--max-retries', '2', '-T4', + '192.0.2.10' + ], + 'the site nmap options become separate command arguments' +); +is_deeply( + [ xCAT::DSHCore->nmap_ping_command( + 4, q{--script 'a b'}, ['192.0.2.10'] + ) + ], + [ 'nmap', '-PE', '--system-dns', '--send-ip', '-sP', + '--unprivileged', '-PA80,443,22', '--script', 'a b', '192.0.2.10' + ], + 'a quoted site nmap option stays one argument' +); +is_deeply( + [ xCAT::DSHCore->nmap_ping_command( 4, undef, ['192.0.2.10'] ) ], + [ 'nmap', '-PE', '--system-dns', '--send-ip', '-sP', + '--unprivileged', '-PA80,443,22', '192.0.2.10' + ], + 'no site nmap options adds no empty argument' +); -like( - $content, - qr/nmap_pping\(\$ipv6_nodes,\s*6\)/, - 'pping sends its IPv6 target partition through the IPv6 nmap path' +is_deeply( + [ xCAT::DSHCore->fping_ping_command( + 6, { fping => '/usr/bin/fping', fping6 => '/usr/sbin/fping6' }, + ['2001:db8::20'] + ) + ], + [ '/usr/sbin/fping6', '-a', '2001:db8::20' ], + 'fping6 probes an IPv6 partition where the host has it' ); -like( - $content, - qr/push \@command, '-6' if \$family == 6;/, - 'the IPv6 nmap path explicitly selects nmap -6' +is_deeply( + [ xCAT::DSHCore->fping_ping_command( + 6, { fping => '/usr/bin/fping' }, ['2001:db8::20'] + ) + ], + [ '/usr/bin/fping', '-6', '-a', '2001:db8::20' ], + 'fping -6 probes an IPv6 partition on a host that ships no fping6' ); -like( - $content, - qr/push \@command, shellwords\(\$more_options\)/, - 'site nmap options are parsed into list-form command arguments' +is_deeply( + [ xCAT::DSHCore->fping_ping_command( + 4, { fping => '/usr/bin/fping', fping6 => '/usr/sbin/fping6' }, + ['192.0.2.10'] + ) + ], + [ '/usr/bin/fping', '-a', '192.0.2.10' ], + 'fping probes an IPv4 partition without a family option' ); -like( - $content, - qr/open\(my \$nmap_output, '-\|'\)/, - 'nmap output uses a lexical pipe' -); -like( - $content, - qr/exec \{ \$command\[0\] \} \@command;/, - 'nmap executes without an interpolated shell command' -); -like( - $content, - qr/xCAT::NetworkUtils->nmap_alive_targets/, - 'pping reuses the shared NetworkUtils nmap parser' -); -like( - $content, - qr/xCAT::NetworkUtils->match_ping_targets/, - 'pping reuses one shared address index for an fping result batch' -); -unlike( - $content, - qr/open\(NMAP,/, - 'pping no longer uses the legacy bareword two-argument nmap pipe' +is_deeply( + [ xCAT::DSHCore->fping_ping_command( 6, {}, ['2001:db8::20'] ) ], + [], + 'no fping on the host gives no command, so pping reports noping' ); done_testing(); diff --git a/xCAT-test/unit/updatenode_ipv6_endpoint.t b/xCAT-test/unit/updatenode_ipv6_endpoint.t index 790819d26d..8506f39b43 100644 --- a/xCAT-test/unit/updatenode_ipv6_endpoint.t +++ b/xCAT-test/unit/updatenode_ipv6_endpoint.t @@ -1,39 +1,117 @@ #!/usr/bin/env perl +# updatenode tells xcatdsklspost on the node where to fetch from, as an +# authority it builds from the service node name and the httpport site +# attribute. An IPv6 service node has to be bracketed there, or the node reads +# the last colon group of the address as the port. +# +# Each case runs the real dispatch path and reads the xdsh command updatenode +# would have run. use strict; use warnings; +no warnings 'once'; use FindBin; +use lib "$FindBin::Bin/../../xCAT-server/lib"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../perl-xCAT"; + use Test::More; -my $source = "$FindBin::Bin/../../xCAT-server/lib/xcat/plugins/updatenode.pm"; -open(my $fh, '<', $source) or die "Unable to read $source: $!"; -my $content = do { local $/; <$fh> }; -close($fh); +BEGIN { + $ENV{XCATROOT} = "$FindBin::Bin/../../xCAT-server"; -my @formatted = $content =~ /my \$httpendpoint = xCAT::NetworkUtils->format_host_port\(\$snkey, \$httpport\);/g; -is( - scalar(@formatted), - 2, - 'postscript and software-update paths format the HTTP endpoint centrally' + # monitorctrl pulls in the notification plugin and the whole monitoring + # tree, which updatenode never calls on this path. + package xCAT_monitoring::monitorctrl; + sub setNodeStatusAttributes { return; } + $INC{'xCAT_monitoring/monitorctrl.pm'} = __FILE__; +} + +require "$FindBin::Bin/../../xCAT-server/lib/xcat/plugins/updatenode.pm"; + +our $server_nodes; + +# Runs one updatenode entry point and answers the commands it dispatched and +# the responses it sent to the callback. +sub dispatch { + my ( $entrypoint, $server, @extra ) = @_; + $server_nodes = { $server => ['node01'] }; + my ( @commands, @responses ); + + no warnings qw(once redefine); + local *xCAT::TableUtils::getInstallDir = sub { return '/install'; }; + local *xCAT::TableUtils::getTftpDir = sub { return '/tftpboot'; }; + local *xCAT::TableUtils::get_site_attribute = sub { + my ( $class, $name ) = @_; + return ('80') if $name eq 'httpport'; + return (); + }; + local *xCAT::InstUtils::get_server_nodes = sub { return $server_nodes; }; + local *xCAT::InstUtils::getOSnodes = + sub { return ( 0, [], ['node01'] ); }; + local *xCAT::MsgUtils::message = sub { return; }; + + my $subreq = sub { + my $request = shift; + push @commands, $request->{arg}->[-1]; + return; + }; + my $callback = sub { push @responses, $_[0]; return; }; + + my $run = \&{"xCAT_plugin::updatenode::$entrypoint"}; + $run->( { node => ['node01'], postscripts => ['syslog'] }, + $subreq, $callback, @extra ); + + return ( \@commands, \@responses ); +} + +# updatenode -P, without --setservers. +my ( $commands, $responses ) = dispatch( 'updatenoderunps', '2001:db8::1' ); +is( scalar( @{$commands} ), 1, 'updatenode -P dispatches one command' ); +like( + $commands->[0], + qr{\Qxcatdsklspost 1 -m '[2001:db8::1]:80'\E}, + 'updatenode -P brackets an IPv6 service node in the HTTP endpoint' ); -my @commands = $content =~ /xcatdsklspost [^\n]+ -[mM] '\$httpendpoint' /g; -is( - scalar(@commands), - 3, - 'all updatenode xcatdsklspost commands quote the IPv6-safe endpoint' +( $commands, $responses ) = dispatch( 'updatenoderunps', '192.0.2.10' ); +like( + $commands->[0], + qr{\Qxcatdsklspost 1 -m '192.0.2.10:80'\E}, + 'updatenode -P leaves an IPv4 service node unbracketed' ); -unlike( - $content, - qr/xcatdsklspost [^\n]+ -[mM] \$snkey:\$httpport /, - 'updatenode does not concatenate an unbracketed IPv6 host and HTTP port' +# updatenode -P --setservers takes the -M form of the same endpoint. +{ + local $::SETSERVER = 1; + ( $commands, $responses ) = dispatch( 'updatenoderunps', '2001:db8::1' ); + like( + $commands->[0], + qr{\Qxcatdsklspost 1 -M '[2001:db8::1]:80'\E}, + 'updatenode --setservers brackets an IPv6 service node too' + ); +} + +# updatenode -S. +( $commands, $responses ) = dispatch( 'updatenodesoftware', '2001:db8::1' ); +is( scalar( @{$commands} ), 1, 'updatenode -S dispatches one command' ); +like( + $commands->[0], + qr{\Qxcatdsklspost 2 -m '[2001:db8::1]:80'\E}, + 'updatenode -S brackets an IPv6 service node in the HTTP endpoint' ); -is( - scalar(() = $content =~ /unless \(defined\(\$httpendpoint\)\)/g), - 2, - 'both endpoint-building paths reject an invalid server authority' -); +# A service node name that is not a usable URI authority has no endpoint, and +# the nodes behind it are skipped rather than sent a malformed command. +foreach my $entrypoint (qw(updatenoderunps updatenodesoftware)) { + ( $commands, $responses ) = dispatch( $entrypoint, 'bad host name' ); + is_deeply( $commands, [], + "$entrypoint dispatches nothing for an unusable service node name" ); + is_deeply( + [ map { $_->{errorcode}->[0] } grep { $_->{errorcode} } @{$responses} ], + [1], + "$entrypoint reports an error for an unusable service node name" + ); +} done_testing();