From 5c249f499e50d78e3e5fa59fef784c6cc0ebc51f Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:39:03 -0300 Subject: [PATCH] test(ipv6): three makehosts IPv6 address tests are missing makehosts reads the address test in four places. The branch widened all four from isIpv4addr to isValidIp so an IPv6 hosts.ip reaches /etc/hosts, and only addotherinterfaces had a test. Narrowing add_hosts_content, delotherinterfaces or the no-noderange path back to isIpv4addr left the whole unit suite passing, so three of the four changes measured nothing. The no-noderange path had no sub to call: its loop sat inside process_request, which opens /etc/hosts and takes a lock. Move that loop to add_all_hosts_content, which takes the hosts table rows and answers the node names, and leave the table read, donics and writeout in process_request. The domain carry-over between rows is kept as it was. makehosts_ipv6.t now drives each of the three remaining places with an IPv6 address and reads the host entries it produced. Each one fails on its own assertion when its address test is narrowed back to isIpv4addr. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-server/lib/xcat/plugins/hosts.pm | 98 ++++++++++++++++++--------- xCAT-test/unit/makehosts_ipv6.t | 92 +++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 31 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/hosts.pm b/xCAT-server/lib/xcat/plugins/hosts.pm index 2adcaadba4..f64a627831 100644 --- a/xCAT-server/lib/xcat/plugins/hosts.pm +++ b/xCAT-server/lib/xcat/plugins/hosts.pm @@ -442,6 +442,68 @@ sub add_hosts_content { } } +#------------------------------------------------------------------------------- + +=head3 add_all_hosts_content + + Adds a host entry for every row of the hosts table. + + Arguments: + hostents - reference to the list of hosts table rows + callback - the command callback + domain - the domain to use when a row resolves to none + Returns: + the node name of every row, in order + Comments: + makehosts with no noderange takes this path, so an address the entry + test rejects reaches no host entry at all. + +=cut + +#------------------------------------------------------------------------------- +sub add_all_hosts_content +{ + my %args = @_; + my $hostents = $args{hostents}; + my $callback = $args{callback}; + my $domain = $args{domain}; + + my @allnodes; + foreach my $hostent (@{$hostents}) + { + push @allnodes, $hostent->{node}; + + my $netn; + ($domain, $netn) = &getIPdomain($hostent->{ip}); + if (!$domain) { + $domain = $::sitedomain; + } + if (!$domain) { + $domain = $::XCATSITEVALS{domain}; + } + + if (xCAT::NetworkUtils->isValidIp($hostent->{ip})) + { + addnode $callback, $hostent->{node}, $hostent->{ip}, + $hostent->{hostnames}, $domain; + } + else + { + my $rsp; + push @{ $rsp->{data} }, + "Invalid IP Addr \'$hostent->{ip}\' for node \'$hostent->{node}\'."; + xCAT::MsgUtils->message("E", $rsp, $callback); + } + + if (defined($hostent->{otherinterfaces})) + { + addotherinterfaces $callback, $hostent->{node}, + $hostent->{otherinterfaces}, $domain; + } + } + return @allnodes; +} + sub process_request { Getopt::Long::Configure("bundling"); @@ -637,37 +699,11 @@ sub process_request $hoststab->getAllNodeAttribs( [ 'ip', 'node', 'hostnames', 'otherinterfaces' ]); - my @allnodes; - foreach (@hostents) - { - - push @allnodes, $_->{node}; - - my $netn; - ($domain, $netn) = &getIPdomain($_->{ip}); - if (!$domain) { - $domain = $::sitedomain; - } - if (!$domain) { - $domain = $::XCATSITEVALS{domain}; - } - - if (xCAT::NetworkUtils->isValidIp($_->{ip})) - { - addnode $callback, $_->{node}, $_->{ip}, $_->{hostnames}, $domain; - } - else - { - my $rsp; - push @{ $rsp->{data} }, "Invalid IP Addr \'$_->{ip}\' for node \'$_->{node}\'."; - xCAT::MsgUtils->message("E", $rsp, $callback); - } - - if (defined($_->{otherinterfaces})) - { - addotherinterfaces $callback, $_->{node}, $_->{otherinterfaces}, $domain; - } - } + my @allnodes = add_all_hosts_content( + hostents => \@hostents, + callback => $callback, + domain => $domain + ); # also do nics table &donics(nodes => \@allnodes, callback => $callback, delnode => $DELNODE); diff --git a/xCAT-test/unit/makehosts_ipv6.t b/xCAT-test/unit/makehosts_ipv6.t index 41a83bb26f..1c65c21f76 100644 --- a/xCAT-test/unit/makehosts_ipv6.t +++ b/xCAT-test/unit/makehosts_ipv6.t @@ -128,4 +128,96 @@ SKIP: { ); } +# makehosts reads the entry test in four places, and an address it rejects +# reaches no host entry at all. addotherinterfaces is covered above; the three +# blocks below drive the remaining places with an IPv6 address and read the +# host entries each one produced. _set_host_lines answers the live host-entry +# array, so the later calls are read through that reference. +{ + no warnings qw(once redefine); + + my @messages; + my $collect = sub { push @messages, $_[0]; return; }; + local *xCAT_plugin::hosts::getIPdomain = sub { return ( 'example.com', 'v6net' ); }; + + my %table_rows; + local *xCAT::Table::new = sub { + my ( $class, $name ) = @_; + return bless { name => $name }, 'xCAT::Table'; + }; + local *xCAT::Table::getNodesAttribs = sub { return \%table_rows; }; + + # add_hosts_content - makehosts with a noderange. + %table_rows = ( node01 => [ { node => 'node01', ip => '2001:db8::10' } ] ); + @messages = (); + xCAT_plugin::hosts::_set_host_lines( [] ); + my @written; + xCAT_plugin::hosts::add_hosts_content( + nodelist => ['node01'], + callback => $collect, + delnode => 0, + domain => 'example.com', + hostsref => \@written + ); + is_deeply( + [ grep { /\S/ } @written ], + ["2001:db8::10 node01 node01.example.com \n"], + 'add_hosts_content writes a host entry for an IPv6 node address' + ); + is( scalar(@messages), 0, + 'add_hosts_content reports no warning for an IPv6 node address' ); + + %table_rows = ( node01 => [ { node => 'node01', ip => 'not-an-address' } ] ); + @messages = (); + xCAT_plugin::hosts::_set_host_lines( [] ); + my @rejected; + xCAT_plugin::hosts::add_hosts_content( + nodelist => ['node01'], + callback => $collect, + delnode => 0, + domain => 'example.com', + hostsref => \@rejected + ); + is_deeply( [ grep { /\S/ } @rejected ], + [], 'add_hosts_content writes no host entry for an address it rejects' ); + ok( scalar(@messages), 'add_hosts_content reports the address it rejects' ); + + # delotherinterfaces - makehosts -d on a node with IPv6 otherinterfaces. + my $entries = xCAT_plugin::hosts::_set_host_lines( + [ "2001:db8::10 node01 node01.example.com \n", + "2001:db8::20 node01-v6 node01-v6.example.com \n", + ] + ); + xCAT_plugin::hosts::delotherinterfaces( 'node01', 'node01-v6!2001:db8::20', + 'example.com' ); + is_deeply( + [ grep { /\S/ } @{$entries} ], + ["2001:db8::10 node01 node01.example.com \n"], + 'delotherinterfaces removes the entry of an IPv6 otherinterfaces address' + ); + + # add_all_hosts_content - makehosts with no noderange. + @messages = (); + $entries = xCAT_plugin::hosts::_set_host_lines( [] ); + my @allnodes = xCAT_plugin::hosts::add_all_hosts_content( + hostents => [ + { node => 'node01', ip => '2001:db8::10' }, + { node => 'node02', ip => '192.0.2.10' }, + ], + callback => $collect, + domain => 'example.com' + ); + is_deeply( \@allnodes, [ 'node01', 'node02' ], + 'add_all_hosts_content answers every node of the hosts table' ); + is_deeply( + [ grep { /\S/ } @{$entries} ], + [ "2001:db8::10 node01 node01.example.com \n", + "192.0.2.10 node02 node02.example.com \n", + ], + 'add_all_hosts_content writes a host entry for an IPv6 and an IPv4 address' + ); + is( scalar(@messages), 0, + 'add_all_hosts_content reports no error for an IPv6 address' ); +} + done_testing();