diff --git a/xCAT-test/unit/networkutils_neighbour_mac.t b/xCAT-test/unit/networkutils_neighbour_mac.t new file mode 100644 index 0000000000..e64110521c --- /dev/null +++ b/xCAT-test/unit/networkutils_neighbour_mac.t @@ -0,0 +1,137 @@ +#!/usr/bin/env perl +# Sequential discovery resolves the address a findme arrived from to a MAC, and looks that +# MAC up in the mac table. Five plugins carried the same arp(8) parse, which reads the IPv4 +# table and matched the address as a regular expression. The parse now takes the table text, +# so a table from either tool is a fixture here instead of the test host's own cache. +use strict; +use warnings; +## no critic (Modules::RequireFilenameMatchesPackage, TestingAndDebugging::ProhibitNoWarnings) + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; +use Test::More; + +BEGIN { + package xCAT::Table; + sub new { return bless {}, shift; } + sub getAllAttribs { return; } + $INC{'xCAT/Table.pm'} = __FILE__; + + package xCAT::TableUtils; + sub get_site_attribute { return; } + $INC{'xCAT/TableUtils.pm'} = __FILE__; +} + +require xCAT::NetworkUtils; + +my $arp_table = <<'TABLE'; +Address HWtype HWaddress Flags Mask Iface +192.168.201.10 ether 52:54:00:aa:bb:cc C ens7 +192.168.201.11 (incomplete) ens7 +192.168.201.120 ether 52:54:00:11:22:33 C ens7 +TABLE + +my $ip6_table = <<'TABLE'; +2001:db8:177::2 dev ens7 lladdr 52:54:00:aa:bb:cc router REACHABLE +2001:db8:177::3 dev ens7 FAILED +fe80::5054:ff:feaa:bbcc dev ens7 lladdr 52:54:00:aa:bb:cc router STALE +2001:db8:177::4 dev ens7 lladdr 52:54:00:de:ad:be STALE +TABLE + +my $ip4_table = <<'TABLE'; +192.168.201.10 dev ens7 lladdr 52:54:00:aa:bb:cc REACHABLE +192.168.201.11 dev ens7 INCOMPLETE +TABLE + +# IPv4, from arp(8): the format the five plugins read before. +is( + xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.10'), + '52:54:00:aa:bb:cc', + 'the MAC of an IPv4 neighbour comes out of the arp table', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.11'), + undef, + 'an incomplete arp entry has no MAC', +); +# The address used to be interpolated into a regular expression, where a dot matched +# any character, so 192.168.201.1 matched the row of 192.168.201.120 and the wrong +# node was named. +is( + xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.1'), + undef, + 'an address that is a prefix of another row does not match that row', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, '192.168.201.99'), + undef, + 'an address the table does not hold has no MAC', +); + +# IPv6, from ip(8): the case that returned nothing at all. +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:db8:177::2'), + '52:54:00:aa:bb:cc', + 'the MAC of an IPv6 neighbour comes out of the IPv6 neighbour table', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:0db8:0177:0000:0000:0000:0000:0002'), + '52:54:00:aa:bb:cc', + 'the same address written in full matches the same row', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:db8:177::3'), + undef, + 'a failed IPv6 entry carries no lladdr and so no MAC', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, 'fe80::5054:ff:feaa:bbcc%ens7'), + '52:54:00:aa:bb:cc', + 'a scoped link-local address matches its unscoped row', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip6_table, '2001:db8:177::9'), + undef, + 'an IPv6 address the table does not hold has no MAC', +); + +# ip(8) also prints the IPv4 table in that format. +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip4_table, '192.168.201.10'), + '52:54:00:aa:bb:cc', + 'an IPv4 row printed by ip(8) is read as well', +); +is( + xCAT::NetworkUtils->mac_from_neighbour_table($ip4_table, '192.168.201.11'), + undef, + 'an incomplete ip(8) entry has no MAC', +); + +# An upper-case table is one table. +is( + xCAT::NetworkUtils->mac_from_neighbour_table( + "2001:DB8:177::2 dev ens7 lladdr 52:54:00:AA:BB:CC REACHABLE\n", + '2001:db8:177::2', + ), + '52:54:00:aa:bb:cc', + 'the MAC is returned in lower case, as the mac table holds it', +); + +# A twenty-byte InfiniBand address is a link-layer address too. +is( + xCAT::NetworkUtils->mac_from_neighbour_table( + "192.168.201.10 dev ib0 lladdr 80:00:02:08:fe:80:00:00:00:00:00:00:00:02:c9:03:00:1a:2b:3c REACHABLE\n", + '192.168.201.10', + ), + '80:00:02:08:fe:80:00:00:00:00:00:00:00:02:c9:03:00:1a:2b:3c', + 'an InfiniBand link-layer address is read whole', +); + +is(xCAT::NetworkUtils->mac_from_neighbour_table(undef, '192.168.201.10'), undef, + 'no table means no MAC'); +is(xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, undef), undef, + 'no address means no MAC'); +is(xCAT::NetworkUtils->mac_from_neighbour_table($arp_table, 'not-an-address'), undef, + 'an address that does not parse means no MAC'); + +done_testing(); diff --git a/xCAT-test/unit/networkutils_tcp_callback_family.t b/xCAT-test/unit/networkutils_tcp_callback_family.t new file mode 100644 index 0000000000..df6ca49bfe --- /dev/null +++ b/xCAT-test/unit/networkutils_tcp_callback_family.t @@ -0,0 +1,112 @@ +#!/usr/bin/env perl +# After discovery xcatd opens a TCP connection back to the node and sends "restart". +# nodediscover.pm and send_tcp_msg built that socket with IO::Socket::INET, which cannot +# reach an IPv6 address, so an IPv6 node never got the message and exited on the discovery +# timeout. open_tcp_socket chooses the class. Both classes are replaced here, so nothing +# connects to anything. +use strict; +use warnings; +## no critic (Modules::RequireFilenameMatchesPackage, TestingAndDebugging::ProhibitNoWarnings) + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; +use Test::More; + +BEGIN { + package xCAT::Table; + sub new { return bless {}, shift; } + sub getAllAttribs { return; } + $INC{'xCAT/Table.pm'} = __FILE__; + + package xCAT::TableUtils; + sub get_site_attribute { return; } + $INC{'xCAT/TableUtils.pm'} = __FILE__; +} + +require xCAT::NetworkUtils; + +my @attempts; +my %answer; +my $written = ''; + +{ + no warnings 'redefine'; + foreach my $class (qw(IO::Socket::INET IO::Socket::INET6)) { + no strict 'refs'; + *{"${class}::new"} = sub { + my (undef, %args) = @_; + push @attempts, { class => $class, %args }; + return undef unless $answer{$class}; + # An IO::Socket is a filehandle, and send_tcp_msg prints to it. An + # in-memory handle collects what it writes. + open(my $handle, '>', \$written) or die "Unable to open a scratch handle: $!"; + return $handle; + }; + $INC{ join('/', split(/::/, $class)) . '.pm' } = __FILE__; + } +} + +sub attempt { + @attempts = (); + my $sock = xCAT::NetworkUtils->open_tcp_socket(@_); + return ($sock, [@attempts]); +} + +# An IPv6 node: the family-neutral class is asked, and it is the one that can reach it. +local $xCAT::NetworkUtils::inet6support = 1; +%answer = ('IO::Socket::INET6' => 1); +my ($sock, $tried) = attempt('2001:db8:177::2', 3001, 1); +ok($sock, 'a connection to an IPv6 node is opened'); +is(scalar(@{$tried}), 1, 'one class is asked for it'); +is($tried->[0]{class}, 'IO::Socket::INET6', 'and it is the one that carries AF_INET6'); +is($tried->[0]{PeerAddr}, '2001:db8:177::2', 'the node address is passed unbracketed'); +is($tried->[0]{PeerPort}, 3001, 'the port is passed'); +is($tried->[0]{Proto}, 'tcp', 'the connection is TCP'); +is($tried->[0]{Timeout}, 1, 'the connect timeout is passed'); + +# An IPv4 node keeps working through whichever class answers. +%answer = ('IO::Socket::INET6' => 1); +($sock, $tried) = attempt('192.168.201.10', 3001, 1); +ok($sock, 'a connection to an IPv4 node is opened'); +is($tried->[0]{PeerAddr}, '192.168.201.10', 'the IPv4 address is passed'); + +# A management node without the IPv6 modules keeps the original IPv4 path. +{ + local $xCAT::NetworkUtils::inet6support = undef; + %answer = ('IO::Socket::INET' => 1); + ($sock, $tried) = attempt('192.168.201.10', 3001, 1); + ok($sock, 'a connection is opened without the IPv6 modules'); + is_deeply([ map { $_->{class} } @{$tried} ], ['IO::Socket::INET'], + 'only the IPv4 class is asked when the IPv6 modules are absent'); +} + +# The IPv4 class is the fallback when the family-neutral attempt fails. +%answer = ('IO::Socket::INET' => 1); +($sock, $tried) = attempt('192.168.201.10', 3001, 1); +ok($sock, 'the IPv4 class answers after the family-neutral one does not'); +is_deeply([ map { $_->{class} } @{$tried} ], + [ 'IO::Socket::INET6', 'IO::Socket::INET' ], + 'the family-neutral class is asked first and the IPv4 one second'); + +# Nothing answers. +%answer = (); +($sock, $tried) = attempt('2001:db8:177::2', 3001, 1); +ok(!$sock, 'a node that cannot be reached returns no socket'); + +is(xCAT::NetworkUtils->open_tcp_socket(undef, 3001), undef, 'no address opens nothing'); +is(xCAT::NetworkUtils->open_tcp_socket('192.168.201.10', undef), undef, 'no port opens nothing'); + +# send_tcp_msg is the other caller, and it writes the message it was given. +%answer = ('IO::Socket::INET6' => 1); +@attempts = (); +$written = ''; +is(xCAT::NetworkUtils->send_tcp_msg('2001:db8:177::2', 3001, 'restart'), 0, + 'send_tcp_msg reports success for an IPv6 node'); +is($attempts[0]{class}, 'IO::Socket::INET6', 'and it reached that node through the IPv6 class'); +is($written, 'restart', 'the message is written to the socket'); + +%answer = (); +is(xCAT::NetworkUtils->send_tcp_msg('2001:db8:177::2', 3001, 'restart'), 1, + 'send_tcp_msg reports failure when nothing answers'); + +done_testing();