2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 01:12:18 +00:00
Files
xcat-core/xCAT-test/unit/networkutils_linklocal_preference.t
T
Daniel Hilst d9763bd2e3 fix(ipv6): the link-local order is applied to one resolver path of two
getipaddr reaches the resolver through Socket6 when that module is present and
through core Socket otherwise, and only the core path ordered its results. A
management node with Socket6 installed therefore still answered
fe80::4054:ff:fec9:9ad2%mgmt0 for its own name, which is what xcatconfig wrote
into site.master. Measured on an AlmaLinux 10.2 node with perl-Socket6: the
previous commit was installed and getipaddr returned the link-local address
unchanged.

_socket6_addrinfo_routable_first reorders the flat five-element records
Socket6::getaddrinfo answers, by the same rule the core path uses. A list whose
length is not a whole number of records is returned untouched.

networkutils_linklocal_preference.t covers the record reorder with a stubbed
Socket6::getnameinfo. Making the link-local test constant false fails 2 of its
assertions. The call site itself has no unit test; the cluster run is what
shows site.master routable.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-10-21 18:59:07 -03:00

111 lines
4.1 KiB
Perl

#!/usr/bin/env perl
# A management node resolves its own name to every address on it, and a
# link-local address is usually first. xcatconfig writes that answer into
# site.master and site.nameservers, which reach every node through DHCP, DNS
# and the kernel command line, so a link-local address there is unusable: a
# node on another link cannot reach it, and the %zone form is not a literal any
# consumer accepts.
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../perl-xCAT";
use Test::More;
use xCAT::NetworkUtils;
my @cases = (
[ 'a global IPv6 address outranks a link-local one',
[ 'fe80::4054:ff:fec9:9ad2', '2001:db8::50' ],
'2001:db8::50',
],
[ 'an IPv4 address outranks a link-local IPv6 address',
[ 'fe80::4054:ff:fec9:9ad2', '192.0.2.154' ],
'192.0.2.154',
],
[ 'the zone identifier is removed from the address that is returned',
[ 'fe80::4054:ff:fec9:9ad2%mgmt0' ],
'fe80::4054:ff:fec9:9ad2',
],
[ 'a link-local address is still returned when it is the only one',
[ 'fe80::1%eth0' ],
'fe80::1',
],
[ 'an IPv4 link-local address outranks nothing but another link-local',
[ '169.254.1.1', '192.0.2.154' ],
'192.0.2.154',
],
[ 'the first address wins when none is link-local',
[ '2001:db8::50', '192.0.2.154' ],
'2001:db8::50',
],
[ 'order is kept among link-local addresses',
[ 'fe80::2', 'fe80::3' ],
'fe80::2',
],
);
foreach my $case (@cases) {
my ( $why, $addresses, $expected ) = @{$case};
my @ordered = xCAT::NetworkUtils->prefer_routable_addresses( @{$addresses} );
is( $ordered[0], $expected, $why );
}
is_deeply(
[ xCAT::NetworkUtils->prefer_routable_addresses(
'fe80::1%eth0', '192.0.2.154', 'fe80::2', '2001:db8::50' ) ],
[ '192.0.2.154', '2001:db8::50', 'fe80::1', 'fe80::2' ],
'every routable address comes before every link-local one, order kept inside each group'
);
is_deeply( [ xCAT::NetworkUtils->prefer_routable_addresses() ],
[], 'an empty list stays empty' );
# getipaddr reaches the resolver through one of two code paths, and only the
# Socket6 one runs on a management node that has Socket6 installed. Both order
# their results, so both are covered here. Socket6::getaddrinfo answers a flat
# list of five-element records, so the records move and their elements do not.
{
no warnings 'once', 'redefine';
local *Socket6::NI_NUMERICHOST = sub { 1 };
local *Socket6::getnameinfo = sub {
my %address_of = (
PACKED_LINKLOCAL => 'fe80::4054:ff:fec9:9ad2',
PACKED_GLOBAL => '2001:db8::50',
PACKED_V4 => '192.0.2.154',
PACKED_BROKEN => undef,
);
return ( $address_of{ $_[0] } );
};
my @flat = (
10, 1, 6, 'PACKED_LINKLOCAL', 'linklocal.example',
2, 1, 6, 'PACKED_V4', 'v4.example',
10, 1, 6, 'PACKED_GLOBAL', 'global.example',
);
my @ordered = xCAT::NetworkUtils::_socket6_addrinfo_routable_first(@flat);
is( scalar(@ordered), 15, 'every record survives the reorder' );
is_deeply( [ @ordered[ 3, 8, 13 ] ],
[ 'PACKED_V4', 'PACKED_GLOBAL', 'PACKED_LINKLOCAL' ],
'the link-local record moves behind both routable records' );
is_deeply( [ @ordered[ 0 .. 4 ] ],
[ 2, 1, 6, 'PACKED_V4', 'v4.example' ],
'the five elements of a record stay with that record' );
my @with_broken = xCAT::NetworkUtils::_socket6_addrinfo_routable_first(
10, 1, 6, 'PACKED_BROKEN', 'broken.example',
10, 1, 6, 'PACKED_GLOBAL', 'global.example',
);
is( $with_broken[3], 'PACKED_GLOBAL',
'a record whose address cannot be read goes last, not first' );
is_deeply( [ xCAT::NetworkUtils::_socket6_addrinfo_routable_first( 1, 2, 3 ) ],
[ 1, 2, 3 ],
'a list that is not a whole number of records is returned untouched' );
}
is_deeply(
[ xCAT::NetworkUtils->prefer_routable_addresses( undef, '', '192.0.2.154' ) ],
['192.0.2.154'], 'undef and empty entries are dropped' );
done_testing();