diff --git a/xCAT-test/bats/ubuntu_subiquity_target_netplan.bats b/xCAT-test/bats/ubuntu_subiquity_target_netplan.bats new file mode 100644 index 0000000000..febebd23c0 --- /dev/null +++ b/xCAT-test/bats/ubuntu_subiquity_target_netplan.bats @@ -0,0 +1,111 @@ +#!/usr/bin/env bats +# +# The netplan the Subiquity late-command writes into the installed system. +# +# The template renders #SUBIQUITYTARGETDHCP# into a printf argument list, so the shell decides +# which lines reach /etc/netplan/00-xcat-install.yaml and in which order. An IPv6-only node now +# gets static keys there: systemd-networkd generates its own DUID, so a node that asked for +# DHCPv6 after reboot took an address out of the dynamic range, which has no PTR record, and +# xcatd could not tell which node ran xcatdsklspost. +# +# Perl renders the arguments, the shell writes the file, so this half is a bats test. + +load 'helpers/shell_source' + +MAC='aa:bb:cc:dd:ee:07' +DHCP4_KEYS='" dhcp4: true" " dhcp4-overrides:" " use-domains: true"' +STATIC6_KEYS='" dhcp4: false" " addresses: [2001:db8:147::20/64]" " routes:" " - to: default" " via: 2001:db8:147::1" " nameservers:" " addresses: [2001:db8:147::1]"' + +setup() +{ + TMPL="$(require_repo_file \ + 'xCAT-server/share/xcat/install/ubuntu/compute.subiquity.tmpl')" + SNIPPET="$(extract_line_range "$TMPL" \ + 'installnic="#SUBIQUITYINSTALLNIC#"' 'chmod 600 /target/etc/netplan')" \ + || skip 'the netplan late-command is not in the template' + # The autoinstall carries the late-command as a single-quoted YAML scalar, so each quote in + # the shell is doubled. Undo that, and keep the writes inside the test tree. + SNIPPET="${SNIPPET//\'\'/\'}" + SNIPPET="${SNIPPET//\/target\//${BATS_TEST_TMPDIR}/target/}" + NETPLAN="${BATS_TEST_TMPDIR}/target/etc/netplan/00-xcat-install.yaml" +} + +render() +{ + local script="$SNIPPET" + script="${script//'#SUBIQUITYINSTALLNIC#'/$1}" + script="${script//'#SUBIQUITYINSTALLMAC#'/$2}" + script="${script//'#SUBIQUITYTARGETDHCP#'/$3}" + sh -c "set -e +$script" +} + +@test "an IPv4 node is written the DHCPv4 netplan it always had" { + render 'eno1' "$MAC" "$DHCP4_KEYS" + [ "$(cat "$NETPLAN")" = 'network: + version: 2 + ethernets: + xcat-install: + match: + macaddress: "aa:bb:cc:dd:ee:07" + set-name: eno1 + dhcp4: true + dhcp4-overrides: + use-domains: true' ] +} + +@test "an IPv6-only node is written its reserved address" { + render 'eno1' "$MAC" "$STATIC6_KEYS" + [ "$(cat "$NETPLAN")" = 'network: + version: 2 + ethernets: + xcat-install: + match: + macaddress: "aa:bb:cc:dd:ee:07" + set-name: eno1 + dhcp4: false + addresses: [2001:db8:147::20/64] + routes: + - to: default + via: 2001:db8:147::1 + nameservers: + addresses: [2001:db8:147::1]' ] +} + +@test "the static address survives the printf argument list" { + render 'eno1' "$MAC" "$STATIC6_KEYS" + grep -qx ' addresses: \[2001:db8:147::20/64\]' "$NETPLAN" +} + +@test "the default route is written as its own netplan list item" { + render 'eno1' "$MAC" "$STATIC6_KEYS" + grep -qx ' - to: default' "$NETPLAN" + grep -qx ' via: 2001:db8:147::1' "$NETPLAN" +} + +@test "the static keys are never mixed with a DHCP request" { + render 'eno1' "$MAC" "$STATIC6_KEYS" + # grep answers 2 for a file that is not there, and refute_grep reads that as "absent", so + # the two refutals below need a line that must be present to prove the file was written. + grep -qx ' dhcp4: false' "$NETPLAN" + refute_grep -q 'dhcp6' "$NETPLAN" + refute_grep -q 'dhcp4: true' "$NETPLAN" +} + +@test "a node with no interface name to set still gets the static address" { + render '' "$MAC" "$STATIC6_KEYS" + refute_grep -q 'set-name' "$NETPLAN" + grep -qx ' addresses: \[2001:db8:147::20/64\]' "$NETPLAN" +} + +@test "the netplan binds to the MAC of the node, whichever branch wrote it" { + render 'eno1' "$MAC" "$STATIC6_KEYS" + grep -qx " macaddress: \"$MAC\"" "$NETPLAN" + render '' "$MAC" "$STATIC6_KEYS" + grep -qx " macaddress: \"$MAC\"" "$NETPLAN" +} + +@test "netplan refuses a file other users can read, so the late-command chmods it" { + render 'eno1' "$MAC" "$STATIC6_KEYS" + [ "$(stat -c '%a' "$NETPLAN")" = '600' ] +} diff --git a/xCAT-test/unit/subiquity_install_match.t b/xCAT-test/unit/subiquity_install_match.t new file mode 100644 index 0000000000..2c1bbd5007 --- /dev/null +++ b/xCAT-test/unit/subiquity_install_match.t @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# The netplan match key of the Subiquity installer. +# +# Both autoinstall templates carried the literal glob name: "e*". A DHCPv4 node can take it: the +# lease is offered on one interface only, so the other candidates stay unaddressed. An IPv6-only +# node is given a static address instead, and three interfaces of one bridge all match "e*", so +# netplan put the same static address on each of them. The node then answered on whichever +# interface won, and the installer could not reach its package source. An IPv6-only node is +# matched by its own MAC. +use strict; +use warnings; +no warnings 'once'; + +use File::Slurper qw(read_text); +use File::Spec; +use FindBin; +BEGIN { $ENV{XCATROOT} = "$FindBin::Bin/../../xCAT-server"; } +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use Test::More; +use xCAT::Template; + +my $NODE = 'cn7'; +my %TAB = ( + 'noderes:installnic' => '', + 'noderes:primarynic' => '', + 'mac:mac' => 'AA:BB:CC:DD:EE:07', +); + +no warnings 'redefine'; +local *xCAT::Template::tabdb = sub { + my ($table, undef, $field) = @_; + my $value = $TAB{"$table:$field"}; + return defined($value) ? $value : ''; +}; + +sub match_for { + my ($ipv6_only, $indent) = @_; + local $xCAT::Template::node_ipv6_only = $ipv6_only; + return xCAT::Template::subiquity_install_match($indent, $NODE); +} + +# An IPv4 node keeps the glob the templates carried, byte for byte. +is( match_for( 0, 10 ), ' name: "e*"', + 'an IPv4 node matches every ethernet candidate, as before' ); +is( match_for( 0, 8 ), ' name: "e*"', + '... at whichever indent the tag asks for' ); + +# An IPv6-only node matches one interface, and netplan wants the address lower-cased. +is( match_for( 1, 10 ), ' macaddress: "aa:bb:cc:dd:ee:07"', + 'an IPv6-only node matches its own MAC, lower-cased' ); +unlike( match_for( 1, 10 ), qr/name:/, + '... and no longer every interface on the bridge' ); +is( match_for( 1, 8 ), ' macaddress: "aa:bb:cc:dd:ee:07"', + '... indented as the tag asks' ); + +# noderes.installnic is resolved by the same path the set-name key uses, so a MAC named there +# wins over mac.mac. +{ + local $TAB{'noderes:installnic'} = 'aa:bb:cc:dd:ee:09'; + is( match_for( 1, 10 ), ' macaddress: "aa:bb:cc:dd:ee:09"', + 'the MAC noderes.installnic names is the one netplan matches' ); +} + +# A node with no MAC cannot be matched at all. Rendering macaddress: "" gives the installer a +# netplan it cannot apply, and the install stops at the console. +{ + local $TAB{'mac:mac'} = ''; + is( match_for( 1, 10 ), '', + 'an IPv6-only node with no MAC renders no match key' ); +} + +# The templates ask for the tag, or the sub is dead code and the glob is still shipped. +foreach my $kind (qw(compute service)) { + my $tmpl = File::Spec->catfile( $FindBin::Bin, '..', '..', 'xCAT-server', 'share', + 'xcat', 'install', 'ubuntu', "$kind.subiquity.tmpl" ); + SKIP: { + skip "$kind.subiquity.tmpl not found", 3 unless -f $tmpl; + my $text = read_text($tmpl); + like( $text, qr/^#SUBIQUITYINSTALLMATCH:10#$/m, + "$kind template asks for the match key" ); + unlike( $text, qr/name: "e\*"/, + "$kind template carries no hardcoded interface glob" ); + like( $text, qr/^\s+match:\n#SUBIQUITYINSTALLMATCH:10#$/m, + "$kind template puts the key under its own match block" ); + } +} + +done_testing(); diff --git a/xCAT-test/unit/subiquity_ipv6_netplan.t b/xCAT-test/unit/subiquity_ipv6_netplan.t index a28624038a..1385483a32 100644 --- a/xCAT-test/unit/subiquity_ipv6_netplan.t +++ b/xCAT-test/unit/subiquity_ipv6_netplan.t @@ -6,36 +6,67 @@ # source and fell back to asking at the console, which on a serial console waits forever. The # installed system had no address either, which is the same defect xcatroot had for a diskless # node. +# +# DHCPv6 replaced that and was still wrong. A DHCPv6 client is identified by its DUID, so the +# installer took an address out of the dynamic range instead of the one makedhcp reserved. That +# address has no PTR record, and xcatd identifies a client over IPv6 only by the name its address +# resolves to, so post.xcat fetched a mypostscript that getpostscript could not build and the +# install stopped. Both netplans are static for an IPv6-only node now. use strict; use warnings; +no warnings 'once'; +use File::Slurper qw(read_text); use File::Spec; use FindBin; +BEGIN { $ENV{XCATROOT} = "$FindBin::Bin/../../xCAT-server"; } use lib "$FindBin::Bin/../../perl-xCAT"; use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; use Test::More; +use xCAT::Template; -my $template = File::Spec->catfile( - $FindBin::Bin, '..', '..', 'xCAT-server', 'lib', 'perl', 'xCAT', 'Template.pm' -); -plan skip_all => "Template.pm not found" unless -f $template; +my $NODE = 'cn7'; +my $ADDR = '2001:db8:147::20'; +my $PREFIX = 64; +my $MASTER = '2001:db8:147::1'; -# The subs read the file-scoped $node_ipv6_only that subvars sets, so they are driven by -# setting it rather than by stubbing a lookup. -require $template; +# The node's own network configuration, and the resolvers site.nameservers hands it. The IPv4 +# resolver is in the list on purpose: an IPv6-only node cannot reach it, and an unreachable +# server in resolv.conf makes every lookup wait for its timeout. +my $cfg6 = [ $ADDR, $NODE, '', $PREFIX ]; +my $nameservers = ',192.0.2.53'; +my $DEFAULT_CFG6 = [ @$cfg6 ]; +my $DEFAULT_NAMESERVERS = $nameservers; + +no warnings 'redefine'; +local *xCAT::NetworkUtils::getNodeNetworkCfg6 = sub { + my $candidate = shift; + $candidate = shift if defined($candidate) && $candidate eq 'xCAT::NetworkUtils'; + return @$cfg6; +}; +local *xCAT::NetworkUtils::ipv6_server_for_node = sub { + my $candidate = shift; + $candidate = shift if defined($candidate) && $candidate eq 'xCAT::NetworkUtils'; + my $server = shift; + return $MASTER if defined($server) && $server eq ''; + return $server; +}; +local *xCAT::NetworkUtils::getNodeNameservers = sub { + my $nodes = shift; + $nodes = shift if defined($nodes) && $nodes eq 'xCAT::NetworkUtils'; + return { $nodes->[0] => $nameservers }; +}; sub keys_for { my ($ipv6_only, $indent) = @_; - no warnings 'once'; local $xCAT::Template::node_ipv6_only = $ipv6_only; - return xCAT::Template::subiquity_dhcp_keys($indent); + return xCAT::Template::subiquity_dhcp_keys($indent, $NODE); } sub target_for { my ($ipv6_only) = @_; - no warnings 'once'; local $xCAT::Template::node_ipv6_only = $ipv6_only; - return xCAT::Template::subiquity_target_dhcp_keys(); + return xCAT::Template::subiquity_target_dhcp_keys($NODE); } # An IPv4 node keeps exactly what it had. @@ -45,23 +76,75 @@ is( target_for(0), '" dhcp4: true" " dhcp4-overrides:" " use-domains: true"', 'and the installed system too, byte for byte as before' ); -# An IPv6-only node asks for DHCPv6 and says so for IPv4, because netplan defaults -# dhcp4 to false only when nothing names it. -my $v6 = keys_for( 8, 8 ); -like( $v6, qr/^\s+dhcp6: true$/m, 'an IPv6-only node is addressed with DHCPv6' ); -like( $v6, qr/^\s+dhcp4: false$/m, '... and DHCPv4 is turned off explicitly' ); +# ---- the installer netplan of an IPv6-only node ------------------------------------------------ +my $v6 = keys_for( 1, 8 ); +is( $v6, <<'EOV6' =~ s/\n\z//r, 'an IPv6-only node is given its reserved address, statically' ); + dhcp4: false + addresses: [2001:db8:147::20/64] + routes: + - to: default + via: 2001:db8:147::1 + nameservers: + addresses: [2001:db8:147::1] +EOV6 +like( $v6, qr/^\s+addresses: \[\Q$ADDR\E\/$PREFIX\]$/m, + '... with the prefix length netplan needs' ); +like( $v6, qr/^\s+- to: default$/m, '... and a default route' ); +like( $v6, qr/^\s+via: \Q$MASTER\E$/m, '... through the resolved IPv6 gateway' ); +like( $v6, qr/^\s+addresses: \[\Q$MASTER\E\]$/m, '... resolving through the IPv6 resolver' ); +unlike( $v6, qr/192\.0\.2\.53/, + '... and not through the IPv4 resolver it cannot reach' ); +like( $v6, qr/^\s+dhcp4: false$/m, '... with DHCPv4 turned off explicitly' ); +unlike( $v6, qr/dhcp6/, '... asking for no DHCPv6, whose DUID matches no reservation' ); unlike( $v6, qr/dhcp4: true/, '... never asking for DHCPv4' ); +# A literal resolver address is kept as it stands; only the IPv4 one is dropped. +{ + $nameservers = '2001:db8:147::53,192.0.2.53'; + my $literal = keys_for( 1, 8 ); + $nameservers = $DEFAULT_NAMESERVERS; + like( $literal, qr/^\s+addresses: \[2001:db8:147::53\]$/m, + 'a resolver named by address is used as it stands' ); +} + +# A node with no address must fail the template rather than install and hang at post.xcat. +{ + $cfg6 = [ undef, $NODE, '', undef ]; + is( keys_for( 1, 8 ), '', + 'an IPv6-only node with no resolvable address renders nothing' ); + $cfg6 = [ @$DEFAULT_CFG6 ]; +} + +# A link with no gateway still reaches an install server on its own link. +{ + $cfg6 = [ $ADDR, $NODE, '', $PREFIX ]; + my $norouter = keys_for( 1, 8 ); + $cfg6 = [ @$DEFAULT_CFG6 ]; + like( $norouter, qr/^\s+addresses: \[\Q$ADDR\E\/$PREFIX\]$/m, + 'a node on a link with no gateway still gets its address' ); + unlike( $norouter, qr/routes:/, '... and no default route it cannot use' ); +} + +# ---- the netplan of the installed system ------------------------------------------------------- my $t6 = target_for(1); -like( $t6, qr/dhcp6: true/, 'the installed system is addressed with DHCPv6' ); -like( $t6, qr/dhcp4: false/, '... with DHCPv4 off' ); -like( $t6, qr/dhcp6-overrides/, '... and the overrides follow the family it uses' ); -unlike( $t6, qr/" dhcp4: true"/, '... never asking for DHCPv4' ); +is( $t6, + '" dhcp4: false" " addresses: [2001:db8:147::20/64]" " routes:"' + . ' " - to: default" " via: 2001:db8:147::1" " nameservers:"' + . ' " addresses: [2001:db8:147::1]"', + 'the installed system keeps the same static address' ); +like( $t6, qr/" addresses: \[\Q$ADDR\E\/$PREFIX\]"/, + '... as a quoted printf argument' ); +like( $t6, qr/" - to: default"/, '... with the default route as its own argument' ); +like( $t6, qr/" via: \Q$MASTER\E"/, '... through the resolved gateway' ); +unlike( $t6, qr/" dhcp6: true"/, + '... never asking for DHCPv6, which would take a third address' ); +unlike( $t6, qr/dhcp4: true/, '... and never for DHCPv4' ); # The indent is what keeps the YAML valid where the tag sits. -is( keys_for( 0, 6 ), ' dhcp4: true', 'the keys are indented as the tag asks' ); -like( keys_for( 1, 4 ), qr/^ dhcp6: true\n dhcp4: false$/, - 'and both lines of the IPv6 form share that indent' ); +is( keys_for( 0, 8 ), ' dhcp4: true', 'the keys are indented as the tag asks' ); +is( keys_for( 0, 6 ), ' dhcp4: true', '... at whichever indent it asks for' ); +like( keys_for( 1, 4 ), qr/^ dhcp4: false\n addresses: /, + 'and the IPv6 form shares that indent' ); # The templates carry the tags rather than a literal, or the subs are dead code. foreach my $kind (qw(compute service)) { @@ -69,7 +152,7 @@ foreach my $kind (qw(compute service)) { 'xcat', 'install', 'ubuntu', "$kind.subiquity.tmpl" ); SKIP: { skip "$kind.subiquity.tmpl not found", 3 unless -f $tmpl; - my $text = do { local ( @ARGV, $/ ) = ($tmpl); <> }; + my $text = read_text($tmpl); like( $text, qr/#SUBIQUITYDHCP:\d+#/, "$kind template asks for the installer keys" ); is( scalar( () = $text =~ /#SUBIQUITYTARGETDHCP#/g ), 2,