diff --git a/xCAT-test/unit/mknb_exported_genesis.t b/xCAT-test/unit/mknb_exported_genesis.t index 9c6959dee8..fe4d97622f 100644 --- a/xCAT-test/unit/mknb_exported_genesis.t +++ b/xCAT-test/unit/mknb_exported_genesis.t @@ -23,12 +23,16 @@ BEGIN { } $INC{'xCAT/TableUtils.pm'} = __FILE__; + # The real module is loaded so mknb's address formatting is the real one. + # Only the probes that read the running host's interfaces are replaced. + require xCAT::NetworkUtils; package xCAT::NetworkUtils; our ($normnet_addresses, $hexnet_addresses, @master_addresses); - sub my_nets { return $normnet_addresses; } - sub my_hexnets { return $hexnet_addresses; } - sub getipaddr { return @master_addresses; } - $INC{'xCAT/NetworkUtils.pm'} = __FILE__; + no warnings 'redefine'; ## no critic (TestingAndDebugging::ProhibitNoWarnings) + *my_nets = sub { return $normnet_addresses; }; + *my_nets6 = sub { return {}; }; + *my_hexnets = sub { return $hexnet_addresses; }; + *getipaddr = sub { return @master_addresses; }; package xCAT::NodeRange; sub import { diff --git a/xCAT-test/unit/mknb_xcatd_address.t b/xCAT-test/unit/mknb_xcatd_address.t index 5cbf80c6f2..4dd81988ab 100644 --- a/xCAT-test/unit/mknb_xcatd_address.t +++ b/xCAT-test/unit/mknb_xcatd_address.t @@ -29,22 +29,36 @@ BEGIN { } $INC{'xCAT/TableUtils.pm'} = __FILE__; + # The real xCAT::NetworkUtils is loaded, so format_uri_host and + # xcatd_cmdline_address are the implementations under test. Only the host + # probes are replaced: they read the running host's interfaces. + require xCAT::NetworkUtils; package xCAT::NetworkUtils; - our ($normnet_addresses, $hexnet_addresses, @master_addresses); - sub my_nets { + our ($normnet_addresses, $normnet6_addresses, $hexnet_addresses, + @master_addresses, @master6_addresses); + no warnings 'redefine'; ## no critic (TestingAndDebugging::ProhibitNoWarnings) + *my_nets = sub { die "mknb did not request all normalized-network addresses" unless $_[-1] eq 'all'; return $normnet_addresses; - } - sub my_hexnets { + }; + *my_nets6 = sub { + die "mknb did not request all IPv6-network addresses" + unless $_[-1] eq 'all'; + return $normnet6_addresses; + }; + *my_hexnets = sub { die "mknb did not request all hexadecimal-network addresses" unless $_[-1] eq 'all'; return $hexnet_addresses; - } - sub getipaddr { return @master_addresses; } + }; + *getipaddr = sub { + my %options = @_[ 2 .. $#_ ]; + return @master6_addresses if $options{OnlyV6}; + return @master_addresses; + }; our $nic_ips; - sub get_nic_ip { return $nic_ips || {}; } - $INC{'xCAT/NetworkUtils.pm'} = __FILE__; + *get_nic_ip = sub { return $nic_ips || {}; }; package xCAT::NodeRange; sub import { @@ -159,6 +173,11 @@ sub read_config { return $content; } +sub read_config_if { + my ($path) = @_; + return -s $path ? read_config($path) : ''; +} + sub generation_succeeded { my ($responses, $description) = @_; ok( @@ -168,6 +187,8 @@ sub generation_succeeded { } sub use_reporter_address_maps { + $xCAT::NetworkUtils::normnet6_addresses = {}; + @xCAT::NetworkUtils::master6_addresses = (); $xCAT::NetworkUtils::normnet_addresses = { '192.168.144.0/20' => [ '192.168.148.10', @@ -720,4 +741,115 @@ for my $case ([undef, ''], ['', ''], [0, ''], ['0', ''], ['80', ''], ['080', ':0 'GRUB discovery preserves HTTP port defaulting'); } + +$xCAT::TableUtils::site_httpport = undef; + +# An IPv6 provisioning network: mknb writes the same nets/ scripts for it, named after the +# network and its prefix length the way DHCP names them, and the xcatd= endpoint is bracketed. +# "xcatd=2001:db8:177::1:3001" is a valid IPv6 literal with no port, so an unbracketed value +# sends the node to a different address and nothing reports a problem. +use_reporter_address_maps(); +$xCAT::NetworkUtils::normnet_addresses = {}; +$xCAT::NetworkUtils::hexnet_addresses = {}; +$xCAT::NetworkUtils::normnet6_addresses = { + '2001:db8:177::/64' => ['2001:db8:177::1'], +}; +prepare_tftpdir($tmpdir, 'tftpboot-x86-v6', 'x86_64'); +$responses = run_mknb('x86_64'); +generation_succeeded($responses, 'x86 configuration generation succeeds for an IPv6 network'); + +foreach my $loader (qw(xnba ipxe)) { + my $path = "$xCAT::TableUtils::tftpdir/xcat/$loader/nets/2001:db8:177::_64"; + ok(-s $path, "mknb writes xcat/$loader/nets for an IPv6 network"); + my $script = read_config_if($path); + like( + $script, + qr/\Qxcatd=[2001:db8:177::1]:3001\E/, + "the $loader script brackets the IPv6 xcatd endpoint", + ); + unlike( + $script, + qr/xcatd=2001:db8:177::1:3001/, + "the $loader script does not write an IPv6 endpoint that parses as an address", + ); + my $uefi = "$xCAT::TableUtils::tftpdir/xcat/$loader/nets/2001:db8:177::_64.uefi"; + like( + read_config_if($uefi), + qr/\Qimgargs kernel xcatd=[2001:db8:177::1]:3001\E/, + "the $loader UEFI script brackets the IPv6 xcatd endpoint", + ); + my $elilo = "$xCAT::TableUtils::tftpdir/xcat/$loader/nets/2001:db8:177::_64.elilo"; + like( + read_config_if($elilo), + qr/\Qappend="xcatd=[2001:db8:177::1]:3001 destiny=discover\E/, + "the $loader elilo entry brackets the IPv6 xcatd endpoint", + ); +} + +# site.master resolves to an IPv6 address on an IPv6-only management node, and that address +# is the one the node has to reach. +$xCAT::NetworkUtils::normnet6_addresses = { + '2001:db8:177::/64' => ['2001:db8:177::1', '2001:db8:177::9'], +}; +@xCAT::NetworkUtils::master6_addresses = ('2001:db8:177::9'); +prepare_tftpdir($tmpdir, 'tftpboot-x86-v6-master', 'x86_64'); +generation_succeeded(run_mknb('x86_64'), 'x86 generation succeeds with an IPv6 site.master'); +like( + read_config_if("$xCAT::TableUtils::tftpdir/xcat/ipxe/nets/2001:db8:177::_64"), + qr/\Qxcatd=[2001:db8:177::9]:3001\E/, + 'an IPv6 site.master address is selected as the xcatd endpoint', +); +@xCAT::NetworkUtils::master6_addresses = (); + +# A POWER network puts the address in an HTTP URL as well, where an unbracketed literal +# makes the last group of the address read as the port. +use_reporter_address_maps(); +$xCAT::NetworkUtils::normnet_addresses = {}; +$xCAT::NetworkUtils::hexnet_addresses = {}; +$xCAT::NetworkUtils::normnet6_addresses = { + '2001:db8:177::/64' => ['2001:db8:177::1'], +}; +prepare_tftpdir($tmpdir, 'tftpboot-power-v6', 'ppc64'); +generation_succeeded(run_mknb('ppc64'), 'POWER configuration generation succeeds for an IPv6 network'); +my $power_v6 = read_config_if("$xCAT::TableUtils::tftpdir/pxelinux.cfg/p/2001:db8:177::_64"); +like($power_v6, qr{\Qkernel http://[2001:db8:177::1]/\E}, 'the POWER kernel URL brackets the IPv6 address'); +like($power_v6, qr{\Qinitrd http://[2001:db8:177::1]/\E}, 'the POWER initrd URL brackets the IPv6 address'); +like($power_v6, qr/\Qappend "xcatd=[2001:db8:177::1]:3001\E/, 'the POWER append line brackets the IPv6 xcatd endpoint'); +unlike($power_v6, qr{http://2001:db8:177::1/}, 'no POWER URL leaves the IPv6 address unbracketed'); + +# s390x and grub2 build their command line in their own helper, so each is asked directly. +my $s390x_v6_root = "$tmpdir/tftpboot-s390x-v6"; +make_path("$s390x_v6_root/pxelinux.cfg/s390x", "$s390x_v6_root/xcat"); +my ($s390x_v6_path, $s390x_v6_error) = xCAT_plugin::mknb::_write_s390x_discovery_config( + tftpdir => $s390x_v6_root, + network => '2001:db8:177::_64', + xcatd_address => '2001:db8:177::1', + xcatdport => 3001, + kernel => 'xcat/genesis.kernel.s390x', + initrd => "$s390x_v6_root/xcat/genesis.fs.s390x.gz", +); +is($s390x_v6_error, undef, 's390x writes a configuration for an IPv6 network'); +like( + read_config($s390x_v6_path), + qr/^ APPEND \Qxcatd=[2001:db8:177::1]:3001\E /m, + 's390x brackets the IPv6 xcatd endpoint', +); + +my $grub_v6_root = "$tmpdir/tftpboot-grub-v6"; +make_path("$grub_v6_root/boot/grub2", "$grub_v6_root/xcat"); +foreach my $artifact ('genesis.kernel.riscv64', 'genesis.fs.riscv64.gz') { + write_text("$grub_v6_root/xcat/$artifact", ''); +} +my $grub_v6_path = xCAT_plugin::mknb::_write_grub2_discovery_config( + tftpdir => $grub_v6_root, + hexnet => 'c0a89', + xcatd_address => '2001:db8:177::1', + xcatdport => 3001, +); +ok(defined($grub_v6_path), 'grub2 writes a discovery configuration for an IPv6 xcatd address'); +my $grub_v6 = read_config($grub_v6_path); +like($grub_v6, qr/\Qxcatd=[2001:db8:177::1]:3001\E/, 'grub2 brackets the IPv6 xcatd endpoint'); +like($grub_v6, qr/^ set root=\Qhttp,[2001:db8:177::1]\E$/m, 'the grub2 HTTP root brackets the IPv6 address'); +like($grub_v6, qr/^ set root=\Qtftp,[2001:db8:177::1]\E$/m, 'the grub2 TFTP root brackets the IPv6 address'); + done_testing(); diff --git a/xCAT-test/unit/networkutils_my_nets6.t b/xCAT-test/unit/networkutils_my_nets6.t new file mode 100644 index 0000000000..d9df6a2c02 --- /dev/null +++ b/xCAT-test/unit/networkutils_my_nets6.t @@ -0,0 +1,100 @@ +#!/usr/bin/env perl +# my_nets6 answers the question my_nets cannot: which IPv6 networks does this host serve. +# my_nets stays IPv4-only because lsslp.pm and PPCdb.pm read its keys with dotted-quad +# arithmetic, so an IPv6 key there is a wrong answer and not a new one. +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 @prefixes; +{ + no warnings 'redefine'; + # The address probe reads the running host's interfaces. The networks under test are + # the ones this list declares. + *xCAT::NetworkUtils::_local_ip_prefixes = sub { + my ($family) = @_; + return $family == 6 ? @prefixes : (); + }; +} + +# ipv6_network_address masks an address down to its prefix. +is(xCAT::NetworkUtils->ipv6_network_address('2001:db8:177::1', 64), '2001:db8:177::', + 'a /64 address is masked to its network'); +is(xCAT::NetworkUtils->ipv6_network_address('2001:db8:177:abcd::5', 60), '2001:db8:177:abc0::', + 'a prefix that is not a byte boundary masks the partial byte'); +is(xCAT::NetworkUtils->ipv6_network_address('2001:db8:177::1', 128), '2001:db8:177::1', + 'a /128 address is its own network'); +is(xCAT::NetworkUtils->ipv6_network_address('2001:db8:177::1', 0), '::', + 'a /0 address masks to the unspecified address'); +is(xCAT::NetworkUtils->ipv6_network_address('fe80::1%eth0', 10), 'fe80::', + 'a scoped literal is masked after the scope is removed'); +is(xCAT::NetworkUtils->ipv6_network_address('192.0.2.1', 24), undef, + 'an IPv4 address is not an IPv6 network'); +is(xCAT::NetworkUtils->ipv6_network_address('2001:db8::1', 129), undef, + 'a prefix longer than 128 bits is refused'); +is(xCAT::NetworkUtils->ipv6_network_address('2001:db8::1', 'sixty-four'), undef, + 'a prefix that is not a number is refused'); +is(xCAT::NetworkUtils->ipv6_network_address(undef, 64), undef, + 'a missing address is refused'); + +# my_nets6 keys its entries the way my_nets keys its IPv4 entries, so mknb and DHCP +# name the same network boot script. +@prefixes = ( [ '2001:db8:177::1', 64 ] ); +is_deeply( + xCAT::NetworkUtils->my_nets6(), + { '2001:db8:177::/64' => '2001:db8:177::1' }, + 'one global address becomes one network keyed /', +); + +@prefixes = ( + [ '2001:db8:177::1', 64 ], + [ '2001:db8:177::9', 64 ], + [ '2001:db8:178::1', 64 ], +); +is_deeply( + xCAT::NetworkUtils->my_nets6(), + { + '2001:db8:177::/64' => '2001:db8:177::9', + '2001:db8:178::/64' => '2001:db8:178::1', + }, + 'the default behavior keeps the last address of each network, as my_nets does', +); +is_deeply( + xCAT::NetworkUtils->my_nets6('all'), + { + '2001:db8:177::/64' => [ '2001:db8:177::1', '2001:db8:177::9' ], + '2001:db8:178::/64' => ['2001:db8:178::1'], + }, + 'all mode keeps every address of each network in operating-system order', +); + +# A malformed entry is left out instead of becoming a network named after nothing. +@prefixes = ( [ '2001:db8:177::1', 64 ], [ 'not-an-address', 64 ] ); +is_deeply( + xCAT::NetworkUtils->my_nets6('all'), + { '2001:db8:177::/64' => ['2001:db8:177::1'] }, + 'an address that does not parse is skipped', +); + +@prefixes = (); +is(xCAT::NetworkUtils->my_nets6(), undef, 'a host with no global IPv6 address returns no networks'); +is(xCAT::NetworkUtils->my_nets6('all'), undef, 'all mode returns no networks either'); + +done_testing(); diff --git a/xCAT-test/unit/secureboot_genesis_shim.t b/xCAT-test/unit/secureboot_genesis_shim.t index aad0cc20a2..b84e93e004 100644 --- a/xCAT-test/unit/secureboot_genesis_shim.t +++ b/xCAT-test/unit/secureboot_genesis_shim.t @@ -11,6 +11,7 @@ use File::Path qw(make_path); use File::Slurper qw(read_text write_binary); use File::Temp qw(tempdir); use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; use Test::More; BEGIN { @@ -23,12 +24,16 @@ BEGIN { sub get_site_attribute { return $_[-1] eq 'master' ? ('master.example.com') : () } $INC{'xCAT/TableUtils.pm'} = __FILE__; + # The real module is loaded so mknb's address formatting is the real one. + # Only the probes that read the running host's interfaces are replaced. + require xCAT::NetworkUtils; package xCAT::NetworkUtils; - sub my_nets { return { '192.168.144.0/20' => ['192.168.148.10'] } } - sub my_hexnets { return { c0a89 => ['192.168.148.10'] } } - sub getipaddr { return ('203.0.113.10') } - sub get_nic_ip { return {} } - $INC{'xCAT/NetworkUtils.pm'} = __FILE__; + no warnings 'redefine'; ## no critic (TestingAndDebugging::ProhibitNoWarnings) + *my_nets = sub { return { '192.168.144.0/20' => ['192.168.148.10'] } }; + *my_nets6 = sub { return {} }; + *my_hexnets = sub { return { c0a89 => ['192.168.148.10'] } }; + *getipaddr = sub { return ('203.0.113.10') }; + *get_nic_ip = sub { return {} }; package xCAT::MsgUtils; sub trace { return }