diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index 63c742b3fb..fa31e51ee5 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -62,6 +62,7 @@ jobs: xCAT-test/native/ubuntu_genimage_debootstrap_arch.t xCAT-test/native/ubuntu_genimage_apt_mirror.t xCAT-test/native/ubuntu_genimage_resolver_libs.t + xCAT-test/native/debian_genimage_profile_assets.t - name: Exercise the liteimg ramdisk archive if: ${{ !cancelled() }} run: prove xCAT-test/native/liteimg_ramdisk_archive.t diff --git a/docs/source/guides/admin-guides/references/man1/genimage.1.rst b/docs/source/guides/admin-guides/references/man1/genimage.1.rst index 31022bf893..55399c64f7 100644 --- a/docs/source/guides/admin-guides/references/man1/genimage.1.rst +++ b/docs/source/guides/admin-guides/references/man1/genimage.1.rst @@ -55,6 +55,9 @@ If this does not work or the management node is not the same distro level, copy /opt/xcat/share/xcat/netboot/ to a system that is the proper architecture, and mount /install from the management node to that system. Then change directory to /opt/xcat/share/xcat/netboot/ and run ./genimage. +When copying Debian scripts from a source checkout, use \ **cp -RL**\ to include +the shared profile helper. Installed packages include the helper in the Debian directory. + ********** Parameters diff --git a/perl-xCAT/xCAT/BootUtils.pm b/perl-xCAT/xCAT/BootUtils.pm index 69158b9c86..95399a3ee8 100644 --- a/perl-xCAT/xCAT/BootUtils.pm +++ b/perl-xCAT/xCAT/BootUtils.pm @@ -6,6 +6,7 @@ use warnings; use Exporter qw(import); use xCAT::Utils; +use xCAT::Table; our @EXPORT_OK = qw(find_genesis_boot_files); @@ -42,4 +43,41 @@ sub volatile_addkcmdline { return $cmdhashref->{volatile} // ''; } +sub expand_node_template { + my ($node, $text, $on_error) = @_; + return $text unless defined($text); + + # Each initial token contributes to its pass's shared replacement budget. + my $replacements_per_token = 16; + my $remaining = $replacements_per_token * (() = $text =~ /#NODEATTRIB:[^:#]+:[^:#]+#/g); + while ($text =~ /#NODEATTRIB:([^:#]+):([^:#]+)#/) { + unless ($remaining--) { + $on_error->("Node template expansion exceeded its replacement limit at #NODEATTRIB:$1:$2#"); + return; + } + my $table = xCAT::Table->new($1); + my $entry = $table->getNodeAttribs($node, [$2]); + my $value = $entry->{$2} // ''; + $text =~ s/#NODEATTRIB:([^:#]+):([^:#]+)#/$value/; + } + $remaining = $replacements_per_token * (() = $text =~ /#TABLE:[^:#]+:[^:#]+:[^:#]+#/g); + while ($text =~ /#TABLE:([^:#]+):([^:#]+):([^:#]+)#/) { + unless ($remaining--) { + $on_error->("Node template expansion exceeded its replacement limit at #TABLE:$1:$2:$3#"); + return; + } + my ($table_name, $key, $column) = ($1, $2, $3); + if ($key =~ /THISNODE/ or $key =~ /\$NODE/) { + my $table = xCAT::Table->new($table_name); + my $entry = $table->getNodeAttribs($node, [$column]); + my $value = $entry->{$column} // ''; + $text =~ s/#TABLE:([^:#]+):([^:#]+):([^:#]+)#/$value/; + } else { + $on_error->("Table key of $key not yet supported by boottarget mini-template"); + return; + } + } + return $text; +} + 1; diff --git a/xCAT-client/pods/man1/genimage.1.pod b/xCAT-client/pods/man1/genimage.1.pod index 48633e5972..2d1988d395 100644 --- a/xCAT-client/pods/man1/genimage.1.pod +++ b/xCAT-client/pods/man1/genimage.1.pod @@ -38,6 +38,9 @@ If this does not work or the management node is not the same distro level, copy /opt/xcat/share/xcat/netboot/ to a system that is the proper architecture, and mount /install from the management node to that system. Then change directory to /opt/xcat/share/xcat/netboot/ and run ./genimage. +When copying Debian scripts from a source checkout, use B to include +the shared profile helper. Installed packages include the helper in the Debian directory. + =head1 Parameters diff --git a/xCAT-genesis-base/dracut_105/el/module-setup.sh b/xCAT-genesis-base/dracut_105/el/module-setup.sh index 131dadb344..5c113a41f6 100755 --- a/xCAT-genesis-base/dracut_105/el/module-setup.sh +++ b/xCAT-genesis-base/dracut_105/el/module-setup.sh @@ -40,10 +40,25 @@ _dracut_install_opt() { fi } +# openssl req reads the config file at OPENSSLDIR/openssl.cnf and exits 1 without it, so the +# node makes no certificate request. dracut_install copies the program and its libraries only. +# OPENSSLDIR is /usr/lib/ssl on Ubuntu and /etc/pki/tls on EL, so ask openssl where it looks. +_dracut_install_openssl_config() { + local dir cnf + dir=$(openssl version -d 2>/dev/null | sed -e 's/^OPENSSLDIR: *"*//' -e 's/"*$//') + cnf="${dir:-/etc/ssl}/openssl.cnf" + if [ ! -e "$cnf" ]; then + dfatal "xcat: no openssl config at $cnf; Genesis cannot request a certificate" + exit 1 + fi + dracut_install "$cnf" +} + install() { local genesis_openeuler=0 timezone timezone_files locale_file locale_files [[ -f /etc/openEuler-release ]] && genesis_openeuler=1 dracut_install wget openssl tar mstflint ipmitool cpio gzip lsmod ethtool modprobe touch echo cut wc bash + _dracut_install_openssl_config dracut_install netstat # broadcom update requires dracut_install uniq # mellanox update requires dracut_install grep ip hostname /usr/bin/awk egrep grep dirname expr diff --git a/xCAT-genesis-base/dracut_105/ubuntu/module-setup.sh b/xCAT-genesis-base/dracut_105/ubuntu/module-setup.sh index 367439ce4f..325807bcd3 100755 --- a/xCAT-genesis-base/dracut_105/ubuntu/module-setup.sh +++ b/xCAT-genesis-base/dracut_105/ubuntu/module-setup.sh @@ -40,12 +40,27 @@ _dracut_install_opt() { fi } +# openssl req reads the config file at OPENSSLDIR/openssl.cnf and exits 1 without it, so the +# node makes no certificate request. dracut_install copies the program and its libraries only. +# OPENSSLDIR is /usr/lib/ssl on Ubuntu and /etc/pki/tls on EL, so ask openssl where it looks. +_dracut_install_openssl_config() { + local dir cnf + dir=$(openssl version -d 2>/dev/null | sed -e 's/^OPENSSLDIR: *"*//' -e 's/"*$//') + cnf="${dir:-/etc/ssl}/openssl.cnf" + if [ ! -e "$cnf" ]; then + dfatal "xcat: no openssl config at $cnf; Genesis cannot request a certificate" + exit 1 + fi + dracut_install "$cnf" +} + install() { # Ubuntu multiarch triplet for architecture-specific library paths local TRIPLET TRIPLET=$(dpkg-architecture -qDEB_HOST_MULTIARCH 2>/dev/null || echo x86_64-linux-gnu) dracut_install wget openssl tar ipmitool cpio gzip lsmod ethtool modprobe touch echo cut wc bash + _dracut_install_openssl_config _dracut_install_opt mstflint dracut_install netstat # broadcom update requires dracut_install uniq # mellanox update requires diff --git a/xCAT-genesis-scripts/usr/bin/getcert b/xCAT-genesis-scripts/usr/bin/getcert index 7bad27285e..fd71c7694c 100755 --- a/xCAT-genesis-scripts/usr/bin/getcert +++ b/xCAT-genesis-scripts/usr/bin/getcert @@ -21,9 +21,15 @@ fi # requests fail. An emulated node needs minutes for that key. CSR_TIMEOUT=${GETCERT_CSR_TIMEOUT:-600} CSR_DEADLINE=$((SECONDS + CSR_TIMEOUT)) -while ! openssl req -new -key /etc/xcat/certkey.pem -out /tmp/tls.csr -subj "/CN=$(hostname)" >& /dev/null; do +CSR_ERROR=/tmp/getcert.csr.err +while ! openssl req -new -key /etc/xcat/certkey.pem -out /tmp/tls.csr -subj "/CN=$(hostname)" > /dev/null 2> $CSR_ERROR; do if [ "$SECONDS" -ge "$CSR_DEADLINE" ]; then - give_up "no certificate request after ${CSR_TIMEOUT}s; /etc/xcat/certkey.pem is not usable" + # Name the key that was used AND what openssl printed: the key is usually intact and + # the absent file is OPENSSLDIR/openssl.cnf. + # Read and flatten with bash builtins. The Genesis payload is a minimal initramfs + # and need not carry tr or cat. + csr_error=$(< $CSR_ERROR) + give_up "no certificate request after ${CSR_TIMEOUT}s using /etc/xcat/certkey.pem: ${csr_error//$'\n'/ }" fi sleep 1 done diff --git a/xCAT-server/debian/rules b/xCAT-server/debian/rules index 4e7eb5bb70..42e73c583f 100755 --- a/xCAT-server/debian/rules +++ b/xCAT-server/debian/rules @@ -44,6 +44,9 @@ binary-arch: dh_installdocs # dh_installexamples dh_install -X".svn" + # Debian scripts must remain self-contained when copied from an installed package. + rm -f $(rootdir)/share/xcat/netboot/debian/xCAT/ProfileUtils.pm + install -m 0644 share/xcat/netboot/imgutils/xCAT/ProfileUtils.pm $(rootdir)/share/xcat/netboot/debian/xCAT/ProfileUtils.pm chmod 755 $(rootdir)/bin/* chmod 755 $(rootdir)/sbin/* chmod 644 $(rootdir)/share/xcat/ca/* diff --git a/xCAT-server/lib/xcat/plugins/boottarget.pm b/xCAT-server/lib/xcat/plugins/boottarget.pm index 39607ff5a2..fce25bce03 100644 --- a/xCAT-server/lib/xcat/plugins/boottarget.pm +++ b/xCAT-server/lib/xcat/plugins/boottarget.pm @@ -12,6 +12,7 @@ use Thread qw(yield); use POSIX qw(WNOHANG nice); use xCAT::Table; use xCAT::Utils; +use xCAT::BootUtils; use xCAT::TableUtils; use xCAT::MsgUtils; use xCAT::Template; @@ -158,29 +159,14 @@ sub mknetboot $kcmdline = $tent->{kcmdline}; #TODO: big todo, cheap and rapid, but should be more like esx.pm implementation, i.e. more scalable - while ($kcmdline =~ /#NODEATTRIB:([^:#]+):([^:#]+)#/) { - my $natab = xCAT::Table->new($1); - my $naent = $natab->getNodeAttribs($node, [$2]); - my $naval = $naent->{$2}; - $kcmdline =~ s/#NODEATTRIB:([^:#]+):([^:#]+)#/$naval/; - } - while ($kcmdline =~ /#TABLE:([^:#]+):([^:#]+):([^:#]+)#/) { - my $tabname = $1; - my $keyname = $2; - my $colname = $3; - if ($2 =~ /THISNODE/ or $2 =~ /\$NODE/) { - my $natab = xCAT::Table->new($tabname); - my $naent = $natab->getNodeAttribs($node, [$colname]); - my $naval = $naent->{$colname}; - $kcmdline =~ s/#TABLE:([^:#]+):([^:#]+):([^:#]+)#/$naval/; - } else { - my $msg = "Table key of $2 not yet supported by boottarget mini-template"; - $callback->({ - error => ["$msg"], - errorcode => [1] - }); - - } + my $template_error; + $kcmdline = xCAT::BootUtils::expand_node_template( + $node, $kcmdline, + sub { $template_error = shift; } + ); + if (defined($template_error)) { + xCAT::MsgUtils->report_node_error($callback, $node, $template_error); + next; } if ($initrd eq '') { diff --git a/xCAT-server/lib/xcat/plugins/pxe.pm b/xCAT-server/lib/xcat/plugins/pxe.pm index 51913d8540..b4ab8c2149 100644 --- a/xCAT-server/lib/xcat/plugins/pxe.pm +++ b/xCAT-server/lib/xcat/plugins/pxe.pm @@ -118,29 +118,12 @@ sub setstate { $kcmdlinehack = xCAT::BootUtils::volatile_addkcmdline($kcmdlinehack); - while ($kcmdlinehack =~ /#NODEATTRIB:([^:#]+):([^:#]+)#/) { - my $natab = xCAT::Table->new($1); - my $naent = $natab->getNodeAttribs($node, [$2]); - my $naval = $naent->{$2}; - $kcmdlinehack =~ s/#NODEATTRIB:([^:#]+):([^:#]+)#/$naval/; - } - while ($kcmdlinehack =~ /#TABLE:([^:#]+):([^:#]+):([^:#]+)#/) { - my $tabname = $1; - my $keyname = $2; - my $colname = $3; - if ($2 =~ /THISNODE/ or $2 =~ /\$NODE/) { - my $natab = xCAT::Table->new($tabname); - my $naent = $natab->getNodeAttribs($node, [$colname]); - my $naval = $naent->{$colname}; - $kcmdlinehack =~ s/#TABLE:([^:#]+):([^:#]+):([^:#]+)#/$naval/; - } else { - my $msg = "Table key of $2 not yet supported by boottarget mini-template"; - $::PXE_callback->({ - error => ["$msg"], - errorcode => [1] - }); - } - } + my $template_error; + $kcmdlinehack = xCAT::BootUtils::expand_node_template( + $node, $kcmdlinehack, + sub { $template_error = shift; } + ); + return (1, $template_error) if defined($template_error); #$kern->{kcmdline} .= " ".$kern->{addkcmdline}; @@ -533,7 +516,7 @@ sub process_request { ($rc, $errstr) = setstate($_, \%bphash, \%chainhash, \%machash, \%nthash, $tftpdir, $linuximghash); if ($rc) { $response{node}->[0]->{errorcode}->[0] = $rc; - $response{node}->[0]->{errorc}->[0] = $errstr; + $response{node}->[0]->{error}->[0] = $errstr; $::PXE_callback->(\%response); } } diff --git a/xCAT-server/lib/xcat/plugins/xnba.pm b/xCAT-server/lib/xcat/plugins/xnba.pm index 9df311f54d..4dd715bdeb 100644 --- a/xCAT-server/lib/xcat/plugins/xnba.pm +++ b/xCAT-server/lib/xcat/plugins/xnba.pm @@ -203,29 +203,12 @@ sub setstate { $kcmdlinehack = xCAT::BootUtils::volatile_addkcmdline($kcmdlinehack); - while ($kcmdlinehack =~ /#NODEATTRIB:([^:#]+):([^:#]+)#/) { - my $natab = xCAT::Table->new($1); - my $naent = $natab->getNodeAttribs($node, [$2]); - my $naval = $naent->{$2}; - $kcmdlinehack =~ s/#NODEATTRIB:([^:#]+):([^:#]+)#/$naval/; - } - while ($kcmdlinehack =~ /#TABLE:([^:#]+):([^:#]+):([^:#]+)#/) { - my $tabname = $1; - my $keyname = $2; - my $colname = $3; - if ($2 =~ /THISNODE/ or $2 =~ /\$NODE/) { - my $natab = xCAT::Table->new($tabname); - my $naent = $natab->getNodeAttribs($node, [$colname]); - my $naval = $naent->{$colname}; - $kcmdlinehack =~ s/#TABLE:([^:#]+):([^:#]+):([^:#]+)#/$naval/; - } else { - my $msg = "Table key of $2 not yet supported by boottarget mini-template"; - $::XNBA_callback->({ - error => ["$msg"], - errorcode => [1] - }); - } - } + my $template_error; + $kcmdlinehack = xCAT::BootUtils::expand_node_template( + $node, $kcmdlinehack, + sub { $template_error = shift; } + ); + return (1, $template_error) if defined($template_error); #$kern->{kcmdline} .= " ".$kern->{addkcmdline}; $kern->{kcmdline} .= " " . $kcmdlinehack; diff --git a/xCAT-server/share/xcat/netboot/debian/genimage b/xCAT-server/share/xcat/netboot/debian/genimage index 35219e8fd3..afd4119116 100755 --- a/xCAT-server/share/xcat/netboot/debian/genimage +++ b/xCAT-server/share/xcat/netboot/debian/genimage @@ -11,6 +11,9 @@ use File::Copy; use File::Find; use Getopt::Long; use Cwd qw(realpath); +use FindBin; +use lib "$FindBin::Bin"; +use xCAT::ProfileUtils; #use strict; Getopt::Long::Configure("bundling"); @@ -1302,21 +1305,8 @@ sub get_profile_def_filename { my $ext = shift; my $dotpos = rindex($osver, "."); my $osbase = substr($osver, 0, $dotpos); - if (-r "$base/$profile.$osver.$arch.$ext") { - return "$base/$profile.$osver.$arch.$ext"; - } elsif (-r "$base/$profile.$osbase.$arch.$ext") { - return "$base/$profile.$osbase.$arch.$ext"; - } elsif (-r "$base/$profile.$arch.$ext") { - return "$base/$profile.$arch.$ext"; - } elsif (-r "$base/$profile.$osver.$ext") { - return "$base/$profile.$osver.$ext"; - } elsif (-r "$base/$profile.$osbase.$ext") { - return "$base/$profile.$osbase.$ext"; - } elsif (-r "$base/$profile.$ext") { - return "$base/$profile.$ext"; - } - - return ""; + return xCAT::ProfileUtils::find_profile_asset( + $base, $profile, $arch, $ext, $osver, $osbase); } diff --git a/xCAT-server/share/xcat/netboot/debian/xCAT/ProfileUtils.pm b/xCAT-server/share/xcat/netboot/debian/xCAT/ProfileUtils.pm new file mode 120000 index 0000000000..39bd0e65dd --- /dev/null +++ b/xCAT-server/share/xcat/netboot/debian/xCAT/ProfileUtils.pm @@ -0,0 +1 @@ +../../imgutils/xCAT/ProfileUtils.pm \ No newline at end of file diff --git a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm index 160bc00891..9cc0b35c7b 100644 --- a/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm +++ b/xCAT-server/share/xcat/netboot/imgutils/imgutils.pm @@ -11,6 +11,8 @@ use File::Copy qw(move); use File::Find; use File::Path; use Cwd qw(realpath); +use lib dirname(__FILE__); +use xCAT::ProfileUtils; use xCAT::SvrUtils; sub el_major_version { @@ -241,33 +243,9 @@ sub get_profile_def_filename { if ($osver =~ /^leap15/) { $fallbackos = "sle15"; } - if (-r "$base/$profile.$osver.$arch.$ext") { - return "$base/$profile.$osver.$arch.$ext"; - } - foreach my $osbase (@osbase) { - if (-r "$base/$profile.$osbase.$arch.$ext") { - return "$base/$profile.$osbase.$arch.$ext"; - } - } - if ($fallbackos && -r "$base/$profile.$fallbackos.$arch.$ext") { - return "$base/$profile.$fallbackos.$arch.$ext"; - } elsif (-r "$base/$profile.$arch.$ext") { - return "$base/$profile.$arch.$ext"; - } elsif (-r "$base/$profile.$osver.$ext") { - return "$base/$profile.$osver.$ext"; - } - foreach my $osbase (@osbase) { - if (-r "$base/$profile.$osbase.$ext") { - return "$base/$profile.$osbase.$ext"; - } - } - if ($fallbackos && -r "$base/$profile.$fallbackos.$ext") { - return "$base/$profile.$fallbackos.$ext"; - } elsif (-r "$base/$profile.$ext") { - return "$base/$profile.$ext"; - } - - return ""; + return xCAT::ProfileUtils::find_profile_asset( + $base, $profile, $arch, $ext, $osver, @osbase, + ($fallbackos ? ($fallbackos) : ())); } sub include_file diff --git a/xCAT-server/share/xcat/netboot/imgutils/xCAT/ProfileUtils.pm b/xCAT-server/share/xcat/netboot/imgutils/xCAT/ProfileUtils.pm new file mode 100644 index 0000000000..fb6e53e1e2 --- /dev/null +++ b/xCAT-server/share/xcat/netboot/imgutils/xCAT/ProfileUtils.pm @@ -0,0 +1,17 @@ +package xCAT::ProfileUtils; + +use strict; +use warnings; + +sub find_profile_asset { + my ($base, $profile, $arch, $ext, @versions) = @_; + + my @suffixes = ((map { "$_.$arch" } @versions), $arch, @versions, ''); + foreach my $suffix (@suffixes) { + my $file = "$base/$profile" . (length($suffix) ? ".$suffix" : '') . ".$ext"; + return $file if -r $file; + } + return ''; +} + +1; diff --git a/xCAT-server/xCAT-server.spec b/xCAT-server/xCAT-server.spec index 8fc21abee1..5f583aa410 100644 --- a/xCAT-server/xCAT-server.spec +++ b/xCAT-server/xCAT-server.spec @@ -183,6 +183,11 @@ cp -hpR share/xcat/install/* $RPM_BUILD_ROOT/%{prefix}/share/xcat/install/ cp -hpR share/xcat/netboot/* $RPM_BUILD_ROOT/%{prefix}/share/xcat/netboot/ %endif +# Debian scripts must remain self-contained when copied from an installed package. +rm -f $RPM_BUILD_ROOT/%{prefix}/share/xcat/netboot/debian/xCAT/ProfileUtils.pm +cp share/xcat/netboot/imgutils/xCAT/ProfileUtils.pm $RPM_BUILD_ROOT/%{prefix}/share/xcat/netboot/debian/xCAT/ProfileUtils.pm +chmod 644 $RPM_BUILD_ROOT/%{prefix}/share/xcat/netboot/debian/xCAT/ProfileUtils.pm $RPM_BUILD_ROOT/%{prefix}/share/xcat/netboot/imgutils/xCAT/ProfileUtils.pm + %ifos linux # pwd cp -d sbin/* $RPM_BUILD_ROOT/%{prefix}/sbin diff --git a/xCAT-test/bats/genesis_getcert_missing_openssl.bats b/xCAT-test/bats/genesis_getcert_missing_openssl.bats index f88914eda6..a683dbd60c 100644 --- a/xCAT-test/bats/genesis_getcert_missing_openssl.bats +++ b/xCAT-test/bats/genesis_getcert_missing_openssl.bats @@ -24,7 +24,9 @@ write_stub() # A PATH directory holding the commands getcert runs. openssl is absent unless it is asked for. stub_dir() { - local with_openssl="${1:-0}" count="" + # count defaults to a no-op: an empty value leaves "{ ; exit 1; }", which is a syntax + # error, so the stub would fail for the wrong reason and report bash's message. + local with_openssl="${1:-0}" count=":" local dir="${BATS_TEST_TMPDIR}/bin" rm -rf "$dir" @@ -35,7 +37,7 @@ stub_dir() write_stub "$dir" sleep 'exec /bin/sleep "$@"' if [ "$with_openssl" = 1 ]; then [ -n "${COUNT_REQUESTS:-}" ] && count="echo req >> '$COUNTER'" - write_stub "$dir" openssl "[ \"\$1\" = req ] && { $count ; exit 1; } + write_stub "$dir" openssl "[ \"\$1\" = req ] && { $count ; echo \"\${OPENSSL_REQ_ERROR:-}\" >&2 ; exit 1; } exit 0" fi printf '%s\n' "$dir" @@ -70,3 +72,13 @@ run_getcert() [ -f "$COUNTER" ] && tries="$(grep -c req "$COUNTER")" [ "$tries" -gt 1 ] } + +@test "the give up message repeats what openssl printed" { + # OPENSSLDIR/openssl.cnf is absent more often than the key is broken, and the key is + # usually intact. A message that names only certkey.pem sends the reader to the wrong file. + export OPENSSL_REQ_ERROR="Can't open \"/usr/lib/ssl/openssl.cnf\" for reading, No such file or directory" + run run_getcert "$(stub_dir 1)" 30 5 + [ "$status" -ne 124 ] + [ "$status" -ne 0 ] + [[ "$output" == *openssl.cnf* ]] +} diff --git a/xCAT-test/bats/genesis_openssl_config.bats b/xCAT-test/bats/genesis_openssl_config.bats new file mode 100644 index 0000000000..5eb4f4328f --- /dev/null +++ b/xCAT-test/bats/genesis_openssl_config.bats @@ -0,0 +1,57 @@ +#!/usr/bin/env bats +# +# The Genesis payload must carry the openssl config file. openssl req reads +# OPENSSLDIR/openssl.cnf and exits 1 when it is absent, so getcert makes no certificate +# request and the node never reports a destiny. + +load 'helpers/shell_source' + +setup() +{ + HOST_CNF="$(openssl version -d 2>/dev/null | sed -e 's/^OPENSSLDIR: *"*//' -e 's/"*$//')/openssl.cnf" + [ -e "$HOST_CNF" ] || skip "this host has no $HOST_CNF to install" + export HOST_CNF +} + +# Run the module's install() with every dracut helper replaced by a recorder, so the test reads +# what the module asks the payload to carry rather than matching the module's source. +record_install() +{ + local module="$1" log="$2" + : >"$log" + ( + for helper in dracut_install inst inst_simple inst_multiple inst_binary inst_script \ + inst_symlink inst_dir inst_hook inst_rules instmods; do + eval "$helper() { printf '%s\n' \"\$@\" >>'$log'; return 0; }" + done + # Anything the module calls that this host lacks must not stop install(). + command_not_found_handle() { return 0; } + dfatal() { printf 'dfatal %s\n' "$*" >>"$log"; } + moddir="${BATS_TEST_TMPDIR}"; hostonly=''; dracutsysrootdir=''; srcmods='' + . "$module" >/dev/null 2>&1 || true + install >/dev/null 2>&1 || true + ) +} + +@test "the ubuntu module installs the openssl config its own openssl names" { + module="$(repo_path 'xCAT-genesis-base/dracut_105/ubuntu/module-setup.sh')" + [ -r "$module" ] || skip "$module is required" + record_install "$module" "${BATS_TEST_TMPDIR}/ubuntu.log" + [ -s "${BATS_TEST_TMPDIR}/ubuntu.log" ] + grep -q "$HOST_CNF" "${BATS_TEST_TMPDIR}/ubuntu.log" +} + +@test "the el module installs the openssl config its own openssl names" { + module="$(repo_path 'xCAT-genesis-base/dracut_105/el/module-setup.sh')" + [ -r "$module" ] || skip "$module is required" + record_install "$module" "${BATS_TEST_TMPDIR}/el.log" + [ -s "${BATS_TEST_TMPDIR}/el.log" ] + grep -q "$HOST_CNF" "${BATS_TEST_TMPDIR}/el.log" +} + +@test "openssl itself is still installed, so the config is an addition" { + module="$(repo_path 'xCAT-genesis-base/dracut_105/ubuntu/module-setup.sh')" + [ -r "$module" ] || skip "$module is required" + record_install "$module" "${BATS_TEST_TMPDIR}/u2.log" + grep -qx 'openssl' "${BATS_TEST_TMPDIR}/u2.log" +} diff --git a/xCAT-test/native/README.md b/xCAT-test/native/README.md index da1f1d859c..e875373802 100644 --- a/xCAT-test/native/README.md +++ b/xCAT-test/native/README.md @@ -45,6 +45,15 @@ packages, execute target-architecture binaries or prove that the generated image ## Other native tests +`debian_genimage_profile_assets.t` runs the Debian genimage command in an +isolated filesystem. It checks custom and shipped package lists, +extra-package lists and postinstall scripts. Package tools and postinstall +scripts stop with deliberate failures after recording the selected input. +Only the Debian script directory is copied, with no installed xCAT libraries. +Source-checkout copies follow symlinks. An optional directory argument checks +a package payload without following symlinks. +It does not install packages or build an initrd. + The pull-request workflow runs these packaging cases explicitly as an ordinary user: diff --git a/xCAT-test/native/debian_genimage_profile_assets.t b/xCAT-test/native/debian_genimage_profile_assets.t new file mode 100644 index 0000000000..30326fa183 --- /dev/null +++ b/xCAT-test/native/debian_genimage_profile_assets.t @@ -0,0 +1,113 @@ +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Path qw(make_path); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::ImageSandbox; +use XCAT::Test::Process qw(run_command); + +plan skip_all => 'Linux namespaces are required' unless $^O eq 'linux'; +die 'Run image tests as an unprivileged user' unless $>; + +my $payload = shift @ARGV; +my $source = $payload // repo_path('xCAT-server/share/xcat/netboot/debian'); + +sub standalone_image { + my $box = XCAT::Test::ImageSandbox->new(); + make_path("$box->{root}/work/share/xcat/netboot"); + my @copy = ('-R', '--no-preserve=mode,ownership'); + push @copy, '-L' unless defined $payload; + my ($rc, $output) = run_command('cp', @copy, $source, "$box->{root}/work/share/xcat/netboot/"); + die "copy Debian scripts: $output" if $rc; + return $box; +} + +for my $extension (qw(pkglist otherpkgs.pkglist postinstall)) { + for my $custom (0, 1) { + subtest "$extension " . ($custom ? 'custom' : 'shipped') => sub { + my $box = standalone_image(); + my $shipped = 'work/share/xcat/netboot/debian'; + my $customdir = 'install/custom/netboot/debian'; + $box->write('install/debian12.5/x86_64/Packages.gz', 'index'); + make_path("$box->{root}/install/debian12.5/x86_64/dists/bookworm"); + $box->write('work/image/rootimg/lib/modules/fixture/modules.dep', ''); + + my $file = "profile.debian12.5.x86_64.$extension"; + my $selected = $custom ? "/$customdir/profile.$extension" + : "share/xcat/netboot/debian/$file"; + my $content = $extension eq 'postinstall' + ? "#!/bin/sh\nprintf '%s\\n' \"\$0\" \"\$@\" > /work/selected\nexit 23\n" + : "selected-package\n"; + my $target = $box->write("$shipped/$file", $custom ? "wrong-package\n" : $content); + chmod(0755, $target) or die "chmod: $!"; + if ($custom) { + $target = $box->write("$customdir/profile.$extension", $content); + chmod(0755, $target) or die "chmod: $!"; + } + + $box->command('debootstrap', $extension eq 'pkglist' ? <<'SH' : 'exit 0'); +printf '%s\n' "$@" > /work/selected +exit 23 +SH + $box->command('yum', <<'SH'); +printf '%s\n' "$@" > /work/selected +exit 23 +SH + $box->write("$shipped/profile.pkglist", '') if $extension eq 'otherpkgs.pkglist'; + + my @args = ('-o', 'debian12.5', '-a', 'x86_64', '-p', 'profile', + '-i', 'eth0', '-n', 'fixture', '-k', 'fixture', + '--rootimgdir', '/work/image', '--noupdate'); + push @args, '--onlyinitrd' if $extension eq 'postinstall'; + my ($rc, $output, $error) = $box->run('env', 'PERL5LIB=', 'XCATROOT=/nonexistent', + 'perl', 'share/xcat/netboot/debian/genimage', @args); + is($rc, 1, 'caller stops at the selected external command failure') or diag($output, $error); + my $observed = -f "$box->{root}/work/selected" ? $box->read('work/selected') : ''; + if ($extension eq 'postinstall') { + is($observed, "$selected\n/work/image/rootimg\ndebian12.5\nx86_64\nprofile\n", + 'postinstall gets the unchanged path spelling and arguments'); + like($output, qr/postinstall script \Q$selected\E failed/, 'caller reports the selected postinstall path'); + } elsif ($extension eq 'pkglist') { + like($observed, qr/^--include=selected-package$/m, 'debootstrap receives the selected package'); + unlike($observed, qr/wrong-package/, 'custom directory takes precedence over a shipped exact match'); + like($output, qr/apt invocation failed/, 'caller reaches the bootstrap failure'); + } else { + like($observed, qr/^selected-package$/m, 'extra-package installation consumes the selected list'); + unlike($observed, qr/wrong-package/, 'custom directory takes precedence over a shipped exact match'); + like($output, qr/yum invocation failed/, 'caller reaches the extra-package failure'); + } + }; + } +} + +subtest 'missing package list' => sub { + my $box = standalone_image(); + $box->write('install/debian12.5/x86_64/Packages.gz', 'index'); + make_path("$box->{root}/install/debian12.5/x86_64/dists/bookworm"); + my ($rc, $output, $error) = $box->run('env', 'PERL5LIB=', 'XCATROOT=/nonexistent', + 'perl', 'share/xcat/netboot/debian/genimage', + '-o', 'debian12.5', '-a', 'x86_64', '-p', 'missing', '-n', 'fixture', '-i', 'eth0', + '--rootimgdir', '/work/image'); + is($rc, 1, 'missing package list stops image creation') or diag($output, $error); + like($output, qr/Unable to find package list for missing!/, 'original missing-list diagnostic remains'); +}; + +subtest 'standalone explicit image definition' => sub { + my $box = standalone_image(); + my $postinstall = $box->write('work/postinstall', "#!/bin/sh\necho explicit > /work/selected\nexit 23\n"); + chmod(0755, $postinstall) or die "chmod: $!"; + $box->write('work/image/rootimg/lib/modules/fixture/modules.dep', ''); + my ($rc, $output, $error) = $box->run('env', 'PERL5LIB=', 'XCATROOT=/nonexistent', + 'perl', 'share/xcat/netboot/debian/genimage', '--onlyinitrd', + '-o', 'debian12.5', '-a', 'x86_64', '-p', 'profile', '-n', 'fixture', '-i', 'eth0', + '-k', 'fixture', '--postinstall', '/work/postinstall', '--rootimgdir', '/work/image', 'explicit-image'); + is($rc, 1, 'standalone command reaches the explicit postinstall failure') or diag($output, $error); + is(-f "$box->{root}/work/selected" ? $box->read('work/selected') : '', "explicit\n", + 'an explicit image definition runs without installed xCAT modules'); + like($output, qr/postinstall script \/work\/postinstall failed/, 'failure comes from the explicit postinstall'); +}; + +done_testing(); diff --git a/xCAT-test/native/profile_package_payload.t b/xCAT-test/native/profile_package_payload.t index b380ccd92e..a97a0da1e7 100644 --- a/xCAT-test/native/profile_package_payload.t +++ b/xCAT-test/native/profile_package_payload.t @@ -48,6 +48,21 @@ my ($status, $output) = run_command($^X, repo_path('xCAT-test/unit/profile_customization.t'), "$root/opt/xcat"); is($status, 0, 'packaged profiles resolve and remain independently customizable') or diag($output); +my $netboot = "$root/opt/xcat/share/xcat/netboot"; +for my $directory (qw(debian imgutils)) { + my $helper = "$netboot/$directory/xCAT/ProfileUtils.pm"; + ok(-f $helper && !-l $helper, "$directory ships a regular profile helper") or next; + is(sprintf('%04o', S_IMODE((stat($helper))[2])), '0644', "$directory helper is readable without write access for others"); +} +for my $case ( + ['unit/imgutils_standalone.t', 'imgutils'], + ['native/debian_genimage_profile_assets.t', 'debian'], +) { + my ($test, $directory) = @$case; + my ($rc, $result) = run_command($^X, repo_path("xCAT-test/$test"), "$netboot/$directory"); + is($rc, 0, "packaged $directory works when copied without sibling directories") or diag($result); +} + if (!-f '/etc/debian_version') { local $ENV{fsm} = 1; my $reduced = tempdir(CLEANUP => 1); @@ -65,7 +80,7 @@ if (!-f '/etc/debian_version') { my $data = 'install/rh/compute.rhels10.riscv64.pkglist'; my $postinstall = 'netboot/rocky/compute.rocky10.riscv64.postinstall'; chmod(oct('0666'), "$profiles/$data") == 1 or die "chmod: $!"; - for my $mask ('0022', '0002') { + for my $mask ('0022', '0002', '0077') { subtest "Debian writable sources with umask $mask" => sub { my $previous = umask oct($mask); my $deb = XCAT::Test::Lifecycle::build_deb('xCAT-server', $fixture); @@ -80,6 +95,12 @@ if (!-f '/etc/debian_version') { my $payload = tempdir(CLEANUP => 1); XCAT::Test::Lifecycle::checked('dpkg-deb', '-x', $deb, $payload); check_assets($payload, '', @assets); + for my $directory (qw(debian imgutils)) { + my $helper = "$payload/opt/xcat/share/xcat/netboot/$directory/xCAT/ProfileUtils.pm"; + ok(-f $helper && !-l $helper, "$directory ships a regular helper with umask $mask"); + is(sprintf('%04o', S_IMODE((stat($helper))[2])), '0644', + "$directory helper retains readable permissions with umask $mask"); + } for my $path (qw(install install/rh netboot netboot/rocky netboot/ubuntu/genimage)) { is(sprintf('%04o', S_IMODE((stat("$payload/opt/xcat/share/xcat/$path"))[2])), '0755', "$path remains executable without group or other write access"); diff --git a/xCAT-test/unit/boot_node_template.t b/xCAT-test/unit/boot_node_template.t new file mode 100644 index 0000000000..4013eb68f7 --- /dev/null +++ b/xCAT-test/unit/boot_node_template.t @@ -0,0 +1,333 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin/../lib"; +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_binary write_text); +use File::Temp qw(tempdir); +use Storable qw(dclone); +use Test::More; +use XCAT::Test::File qw(repo_path); + +my $root = tempdir(CLEANUP => 1); +my $tftp = "$root/tftp"; +$ENV{XCATROOT} = repo_path('xCAT-server'); +$ENV{XCATCFG} = "$root/config"; +make_path($ENV{XCATCFG}, $tftp); +write_text("$tftp/pxelinux.0", 'loader'); +require xCAT::TableUtils; +{ + no warnings qw(redefine once); + local *xCAT::TableUtils::getTftpDir = sub { return $tftp; }; + for my $plugin (qw(pxe xnba ipxe boottarget)) { + require(repo_path("xCAT-server/lib/xcat/plugins/$plugin.pm")); + } +} + +my (%rows, %written, %kernels); +my @responses; +sub render { + my ($method, $input, %options) = @_; + my @nodes = @{ $options{nodes} || ['192.0.2.10'] }; + my $node = $nodes[0]; + %written = (); + @responses = (); + %kernels = ($node => { + kernel => $options{kernel} || 'vmlinuz', initrd => 'initrd', kcmdline => 'quiet', + addkcmdline => $input, + }); + %rows = ( + noderes => {$node => {tftpdir => $tftp}}, + chain => {$node => {currstate => 'install'}}, + nodetype => {$node => {os => 'boottarget', profile => 'custom', + provmethod => $options{image} ? 'image' : 'install'}}, + mac => {}, iscsi => {}, bootparams => {}, nodehm => {}, + boottarget => {custom => {kernel => 'vmlinuz', initrd => 'initrd', kcmdline => $input}}, + linuximage => {image => {addkcmdline => $options{image}, boottarget => $options{boottarget}}}, + hosts => {$node => { + ip => $node, empty => '', zero => '0', + nested => '#NODEATTRIB:hosts:ip#', + table => '#TABLE:hosts:THISNODE:ip#', + special => 'a$1\\b&c', + selfref => '#NODEATTRIB:hosts:selfref#', + tableself => '#TABLE:hosts:THISNODE:tableself#', + cycle_a => '#NODEATTRIB:hosts:cycle_b#', + cycle_b => '#NODEATTRIB:hosts:cycle_a#', + table_a => '#TABLE:hosts:THISNODE:table_b#', + table_b => '#TABLE:hosts:THISNODE:table_a#', + growing => 'x#NODEATTRIB:hosts:growing#', + tablegrow => 'x#TABLE:hosts:THISNODE:tablegrow#', + (map { ("depth$_" => $_ == 16 ? $node : '#NODEATTRIB:hosts:depth' . ($_ + 1) . '#') } 0 .. 16), + (map { ("tabledepth$_" => $_ == 16 ? $node : '#TABLE:hosts:THISNODE:tabledepth' . ($_ + 1) . '#') } 0 .. 16), + }}, + ); + for my $other (@nodes) { + next if $other eq $node; + $kernels{$other} = dclone($kernels{$node}); + for my $table (qw(noderes chain nodetype hosts)) { + $rows{$table}{$other} = dclone($rows{$table}{$node}); + } + $rows{hosts}{$other}{ip} = $other; + } + if ($options{bad_node}) { + my $bad = $options{bad_node}; + $kernels{$bad}{addkcmdline} = $options{bad_input}; + $rows{nodetype}{$bad}{profile} = 'bad'; + $rows{boottarget}{bad} = {kernel => 'vmlinuz', initrd => 'initrd', kcmdline => $options{bad_input}}; + } + no warnings qw(redefine once); + local @ARGV; + local $::myxcatname = 'mn.example'; + local *xCAT::Table::new = sub { + my ($class, $table) = @_; + die "Unexpected table $table" unless exists $rows{$table}; + return bless {table => $table}, 'Local::TemplateTable'; + }; + local *xCAT::TableUtils::getTftpDir = sub { return $tftp; }; + local *xCAT::TableUtils::getInstallDir = sub { return "$root/install"; }; + local *xCAT::TableUtils::get_site_attribute = sub { + return ('n') if $_[1] eq 'dhcpsetup'; + return () if $_[1] eq 'httpport'; + die "Unexpected site attribute $_[1]"; + }; + local *xCAT::NetworkUtils::determinehostname = sub { return ('mn.example'); }; + local *xCAT::NetworkUtils::checkNodeIPaddress = sub { return {ip => $_[1]}; }; + local *xCAT::MsgUtils::trace = sub { return; }; + my $callback = sub { + die "too many callback responses\n" if @responses == 5; + push @responses, dclone($_[0]); + }; + my $completed = eval { + local $SIG{ALRM} = sub { die "template expansion timed out\n"; }; + alarm(2); + "xCAT_plugin::$method"->can('process_request')->( + {command => [$method eq 'boottarget' ? 'mknetboot' : 'nodeset'], + node => \@nodes, arg => ['install']}, + $callback, + sub { + my ($request, $reply) = @_; + if ($request->{command}[0] eq 'setdestiny') { + $request->{bootparams}{$_} = [$kernels{$_}] for @nodes; + $reply->({node => [{name => [$node], _addkcmdlinehandled => 1}]}) + if $options{handled}; + } elsif ($request->{command}[0] !~ /\A(?:runbeginpre|runendpre)\z/) { + die "Unexpected command $request->{command}[0]"; + } + }, + ); + alarm(0); + 1; + }; + my $exception = $@; + alarm(0); + return $exception if $options{bad_node}; + die $exception unless $completed; + is_deeply(\@responses, [], "$method reports no errors"); + return $written{$node}{kcmdline} if $method eq 'boottarget'; + return $kernels{$node}{kcmdline}; +} + +my @cases = ( + ['undefined', undef, undef, 'quiet'], + ['empty', '', '', 'quiet'], + ['zero', '0', '0', 'quiet'], + ['literal', 'console=ttyS0', 'console=ttyS0', 'quiet console=ttyS0 '], + ['node attribute', 'ip=#NODEATTRIB:hosts:ip#', 'ip=192.0.2.10', 'quiet ip=192.0.2.10 '], + ['table key', 'ip=#TABLE:hosts:THISNODE:ip#', 'ip=192.0.2.10', 'quiet ip=192.0.2.10 '], + ['dollar key', 'ip=#TABLE:hosts:$NODE:ip#', 'ip=192.0.2.10', 'quiet ip=192.0.2.10 '], + ['node selector', '#TABLE:hosts:node=$NODE:ip#', '192.0.2.10', 'quiet 192.0.2.10 '], + ['mixed order', '#TABLE:hosts:THISNODE:ip#,#NODEATTRIB:hosts:zero#,#TABLE:hosts:$NODE:ip#', '192.0.2.10,0,192.0.2.10', 'quiet 192.0.2.10,0,192.0.2.10 '], + ['missing', 'a=#NODEATTRIB:hosts:missing# b=#TABLE:hosts:THISNODE:missing#', 'a= b=', 'quiet a= b= '], + ['empty and zero', 'a=#NODEATTRIB:hosts:empty# b=#NODEATTRIB:hosts:zero#', 'a= b=0', 'quiet a= b=0 '], + ['repeat', '#NODEATTRIB:hosts:ip#,#NODEATTRIB:hosts:ip#', '192.0.2.10,192.0.2.10', 'quiet 192.0.2.10,192.0.2.10 '], + ['nested node', '#NODEATTRIB:hosts:nested#', '192.0.2.10', 'quiet 192.0.2.10 '], + ['node replacement limit', '#NODEATTRIB:hosts:depth1#', '192.0.2.10', 'quiet 192.0.2.10 '], + ['table replacement limit', '#TABLE:hosts:THISNODE:tabledepth1#', '192.0.2.10', 'quiet 192.0.2.10 '], + ['shared node budget', '#NODEATTRIB:hosts:depth0#,#NODEATTRIB:hosts:ip#', + '192.0.2.10,192.0.2.10', 'quiet 192.0.2.10,192.0.2.10 '], + ['shared table budget', '#TABLE:hosts:THISNODE:tabledepth0#,#TABLE:hosts:THISNODE:ip#', + '192.0.2.10,192.0.2.10', 'quiet 192.0.2.10,192.0.2.10 '], + ['many independent nodes', join(',', ('#NODEATTRIB:hosts:ip#') x 20), + join(',', ('192.0.2.10') x 20), 'quiet ' . join(',', ('192.0.2.10') x 20) . ' '], + ['many independent tables', join(',', ('#TABLE:hosts:THISNODE:ip#') x 20), + join(',', ('192.0.2.10') x 20), 'quiet ' . join(',', ('192.0.2.10') x 20) . ' '], + ['node then table', '#NODEATTRIB:hosts:table#', '192.0.2.10', 'quiet 192.0.2.10 '], + ['table recursion', '#TABLE:hosts:THISNODE:table#', '192.0.2.10', 'quiet 192.0.2.10 '], + ['table does not restart node pass', '#TABLE:hosts:THISNODE:nested#', '#NODEATTRIB:hosts:ip#', 'quiet #NODEATTRIB:hosts:ip# '], + ['literal replacement', '#NODEATTRIB:hosts:special#', 'a$1\\b&c', 'quiet a$1\\b&c '], + ['persistent', 'R::keep=1 marker=#NODEATTRIB:hosts:ip#', 'R::keep=1 marker=192.0.2.10', 'quiet marker=192.0.2.10 '], + ['persistent only', 'R::keep=1', 'R::keep=1', 'quiet '], + ['partial token', '#NODEATTRIB:hosts:ip', '#NODEATTRIB:hosts:ip', 'quiet #NODEATTRIB:hosts:ip '], +); + +for my $method (qw(pxe xnba ipxe boottarget)) { + for my $case (@cases) { + my ($name, $input, $target, $netboot) = @$case; + subtest "$method $name" => sub { + my $expected = $method eq 'boottarget' ? $target : $netboot; + is(render($method, $input), $expected, 'expansion keeps the caller contract'); + if ($method eq 'pxe') { + is(read_text("$tftp/pxelinux.cfg/192.0.2.10"), + "#install\nDEFAULT xCAT\nLABEL xCAT\n KERNEL vmlinuz\n APPEND initrd=initrd $expected\n IPAPPEND 2\n", + 'PXE writes the complete expected configuration'); + } elsif ($method ne 'boottarget') { + like(read_text("$tftp/xcat/$method/nodes/192.0.2.10"), + qr/^imgargs kernel \Q$expected\E BOOTIF=01-\$\{netX\/mac:hexhyp\}$/m, + 'the selected method writes the expanded BIOS arguments'); + like(read_text("$tftp/xcat/$method/nodes/192.0.2.10.elilo"), + qr/append="\Q$expected\E BOOTIF=%B"/, + 'the selected method writes the expanded UEFI fallback arguments'); + } else { + is_deeply($written{'192.0.2.10'}, + {kernel => 'vmlinuz', initrd => 'initrd', kcmdline => $expected}, + 'boottarget persists the kernel, initrd and expanded arguments'); + } + }; + } + next if $method eq 'boottarget'; + subtest "$method image arguments" => sub { + is(render($method, 'node=#NODEATTRIB:hosts:ip#', image => 'R::keep=1 image=#TABLE:hosts:$NODE:ip#'), + 'quiet node=192.0.2.10 image=192.0.2.10 ', 'node and image arguments retain their order'); + }; + subtest "$method handled arguments" => sub { + is(render($method, '#TABLE:hosts:unsupported:ip#', handled => 1), + 'quiet', 'a prior owner can suppress mini-template expansion'); + }; + next if $method eq 'pxe'; + subtest "$method direct UEFI" => sub { + write_binary("$tftp/modern-kernel", "\0" x 64 . pack('H*', '504500006486') . "\0" x 440); + is(render($method, 'marker=#NODEATTRIB:hosts:ip#', kernel => 'modern-kernel'), 'quiet marker=192.0.2.10 ', + 'a modern kernel gets the same expansion'); + like(read_text("$tftp/xcat/$method/nodes/192.0.2.10.uefi"), + qr/^imgargs kernel quiet marker=192\.0\.2\.10 BOOTIF=01-\$\{netX\/mac:hexhyp\} initrd=initrd$/m, + 'direct UEFI boot receives the expanded arguments'); + unlink("$tftp/modern-kernel") or die "unlink kernel: $!"; + }; +} + +my @failures; +for my $position (0, 1, 2) { + for my $existing (0, 1) { + push @failures, [$position, $existing, 'unsupported key', + 'before=#NODEATTRIB:hosts:ip# #TABLE:hosts:unsupported:ip# #TABLE:hosts:other:ip#', + 'Table key of unsupported not yet supported by boottarget mini-template']; + } +} +for my $case ( + ['node self-reference', '#NODEATTRIB:hosts:selfref#'], + ['table self-reference', '#TABLE:hosts:THISNODE:tableself#'], + ['node cycle', '#NODEATTRIB:hosts:cycle_a#'], + ['table cycle', '#TABLE:hosts:THISNODE:table_a#'], + ['growing node', '#NODEATTRIB:hosts:growing#'], + ['growing table', '#TABLE:hosts:THISNODE:tablegrow#'], + ['node over limit', '#NODEATTRIB:hosts:depth0#', '#NODEATTRIB:hosts:depth16#'], + ['table over limit', '#TABLE:hosts:THISNODE:tabledepth0#', '#TABLE:hosts:THISNODE:tabledepth16#'], + ['node budget stays separate', '#NODEATTRIB:hosts:depth0#,#TABLE:hosts:THISNODE:ip#', + '#NODEATTRIB:hosts:depth16#'], + ['table budget stays separate', '#NODEATTRIB:hosts:ip#,#TABLE:hosts:THISNODE:tabledepth0#', + '#TABLE:hosts:THISNODE:tabledepth16#'], +) { + my ($name, $input, $token) = @$case; + $token //= $input; + push @failures, [1, 1, $name, $input, + "Node template expansion exceeded its replacement limit at $token"]; +} + +for my $method (qw(pxe xnba ipxe boottarget)) { + for my $failure (@failures) { + my ($position, $existing, $name, $input, $message) = @$failure; + subtest "$method $name at $position existing=$existing" => sub { + my @nodes = qw(192.0.2.20 192.0.2.21 192.0.2.22); + my $bad = $nodes[$position]; + for my $good (grep { $_ ne $bad } @nodes) { + next if $method eq 'boottarget'; + my $file = $method eq 'pxe' ? "$tftp/pxelinux.cfg/$good" + : "$tftp/xcat/$method/nodes/$good"; + write_text($file, "previous boot configuration\n"); + } + my @files = $method eq 'pxe' ? ("$tftp/pxelinux.cfg/$bad") + : $method eq 'boottarget' ? () + : map { "$tftp/xcat/$method/nodes/$bad$_" } ('', '.pxelinux', '.uefi', '.elilo'); + for my $file (@files) { + if ($existing) { + write_text($file, "previous boot configuration\n"); + } else { + unlink($file) or die "unlink $file: $!" if -e $file; + } + } + my $link; + if ($method eq 'pxe') { + $link = "$tftp/pxelinux.cfg/" . sprintf('%02X%02X%02X%02X', split(/\./, $bad)); + unlink($link) or die "unlink $link: $!" if -e $link || -l $link; + symlink($bad, $link) or die "symlink $link: $!" if $existing; + } + my $exception = render($method, 'marker=#NODEATTRIB:hosts:ip#', + nodes => \@nodes, bad_node => $bad, + bad_input => $input); + is($exception, '', 'request completes before the timeout'); + my @expected = ({node => [{name => [$bad], errorcode => [1], error => [$message]}]}); + push @expected, {errorcode => [1], error => + ['Failed to generate xnba configurations for some node(s) on mn.example. Check xCAT log file for more details.']} + if $method eq 'xnba' || $method eq 'ipxe'; + is_deeply(\@responses, \@expected, + 'responses contain the node error and only the caller-required summary'); + for my $file (@files) { + if ($existing) { + ok(-f $file, 'failure keeps the existing artifact') or next; + is(read_text($file), "previous boot configuration\n", 'failure preserves the existing artifact'); + } else { + ok(!-e $file, 'failure creates no artifact'); + } + } + if ($link) { + if ($existing) { + ok(-l $link, 'failure preserves the existing boot symlink'); + is(readlink($link), $bad, 'failure preserves the boot symlink target'); + } else { + ok(!-e $link && !-l $link, 'failure creates no boot symlink'); + } + } + if ($method eq 'boottarget') { + ok(!exists($written{$bad}), 'failure does not update bootparams'); + } else { + is($kernels{$bad}{kcmdline}, 'quiet', 'failure does not append a partial expansion'); + } + for my $good (grep { $_ ne $bad } @nodes) { + if ($method eq 'boottarget') { + is($written{$good}{kcmdline}, "marker=$good", 'other nodes still update bootparams'); + } else { + my $file = $method eq 'pxe' ? "$tftp/pxelinux.cfg/$good" + : "$tftp/xcat/$method/nodes/$good"; + like(read_text($file), qr/marker=\Q$good\E /, 'other nodes still get boot artifacts'); + } + } + }; + } +} + +done_testing(); + +package Local::TemplateTable; +sub getNodesAttribs { + my ($self, $nodes) = @_; + return {map { $_ => [$rows{$self->{table}}{$_} || {}] } @$nodes}; +} +sub getNodeAttribs { + my ($self, $node) = @_; + return $rows{$self->{table}}{$node} || {}; +} +sub getAttribs { + my ($self, $key) = @_; + my ($value) = values %$key; + return $rows{$self->{table}}{$value}; +} +sub setNodeAttribs { + my ($self, $node, $attributes) = @_; + die "Unexpected write to $self->{table}" unless $self->{table} eq 'bootparams'; + $written{$node} = Storable::dclone($attributes); + return; +} +sub close { return; } diff --git a/xCAT-test/unit/fixtures/debian_profile_lookup.pl b/xCAT-test/unit/fixtures/debian_profile_lookup.pl new file mode 100644 index 0000000000..be78aaf3ab --- /dev/null +++ b/xCAT-test/unit/fixtures/debian_profile_lookup.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use JSON::PP qw(encode_json); + +BEGIN { + *CORE::GLOBAL::exit = sub { die "genimage exit $_[0]\n" }; +} + +my ($script, $osver, $profile, $arch, $base, $ext) = @ARGV; +@ARGV = ('-o', $osver, '-p', $profile, '-a', $arch); +my $startup; +{ + local $0 = $script; + local *STDOUT; + open STDOUT, '>', \$startup or die "capture stdout: $!"; + do $script; + die "genimage did not stop at argument validation: $@ $!\n" + unless $@ eq "genimage exit 1\n"; +} +print encode_json([main::get_profile_def_filename($base, $ext)]); diff --git a/xCAT-test/unit/imgutils_standalone.t b/xCAT-test/unit/imgutils_standalone.t new file mode 100644 index 0000000000..5b8a534733 --- /dev/null +++ b/xCAT-test/unit/imgutils_standalone.t @@ -0,0 +1,36 @@ +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use Cwd qw(realpath); +use File::Path qw(make_path); +use File::Slurper qw(write_text); +use File::Temp qw(tempdir); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); + +my $source = shift @ARGV // repo_path('xCAT-server/share/xcat/netboot/imgutils'); +my $root = tempdir(CLEANUP => 1); +my ($status, $output) = run_command('cp', '-R', $source, "$root/imgutils"); +die "copy imgutils: $output" if $status; +make_path("$root/assets"); +my $assets = realpath("$root/assets"); + +local $ENV{PERL5LIB} = ''; +local $ENV{XCATROOT} = '/nonexistent'; +for my $extension (qw(pkglist otherpkgs.pkglist postinstall)) { + my $selected = "$assets/compute.debian12.x86_64.$extension"; + write_text($selected, "selected\n"); + write_text("$root/assets/compute.$extension", "fallback\n"); + my ($rc, $result) = run_command($^X, + '-I' . repo_path('perl-xCAT'), '-I' . repo_path('xCAT-server/lib/perl'), + '-e', 'require shift @ARGV; print imgutils::get_profile_def_filename(@ARGV)', + "$root/imgutils/imgutils.pm", + 'debian12.5', 'compute', 'x86_64', "$root/assets", $extension); + is($rc, 0, "copied imgutils loads without any sibling distro directory") or diag($result); + is($result, $selected, "copied imgutils selects the $extension version fallback"); +} + +done_testing(); diff --git a/xCAT-test/unit/profile_asset_lookup.t b/xCAT-test/unit/profile_asset_lookup.t new file mode 100644 index 0000000000..54185b263f --- /dev/null +++ b/xCAT-test/unit/profile_asset_lookup.t @@ -0,0 +1,95 @@ +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib", "$FindBin::Bin/../../perl-xCAT", + "$FindBin::Bin/../../xCAT-server/lib/perl", + "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Cwd qw(getcwd realpath); +use File::Path qw(make_path); +use File::Slurper qw(write_text); +use File::Temp qw(tempdir); +use JSON::PP qw(decode_json); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); +use imgutils; + +my $root = tempdir(CLEANUP => 1); +my $original_dir = getcwd(); +chdir($root) or die "chdir $root: $!"; +make_path('assets'); +symlink('assets', 'alias') or die "symlink: $!"; + +sub lookup { + my ($owner, $version, $arch, $base, $extension) = @_; + return imgutils::get_profile_def_filename($version, 'compute', $arch, $base, $extension) + if $owner eq 'imgutils'; + local $ENV{PERL5LIB} = ''; + local $ENV{XCATROOT} = '/nonexistent'; + my ($rc, $output) = run_command($^X, + repo_path('xCAT-test/unit/fixtures/debian_profile_lookup.pl'), + repo_path('xCAT-server/share/xcat/netboot/debian/genimage'), + $version, 'compute', $arch, $base, $extension); + die "Debian lookup failed ($rc): $output" if $rc; + return decode_json($output)->[0]; +} + +for my $owner (qw(debian imgutils)) { + for my $arch (qw(x86_64 ppc64le s390x)) { + for my $extension (qw(pkglist otherpkgs.pkglist postinstall)) { + subtest "$owner $arch $extension priority" => sub { + my @names = ( + "compute.debian12.5.$arch.$extension", + "compute.debian12.$arch.$extension", + "compute.$arch.$extension", + "compute.debian12.5.$extension", + "compute.debian12.$extension", + "compute.$extension", + ); + write_text("assets/$_", "fixture\n") for @names; + for my $name (@names) { + for my $base ('assets', './alias', realpath('assets')) { + my $expected_base = $owner eq 'debian' ? $base : realpath($base); + is(lookup($owner, 'debian12.5', $arch, $base, $extension), + "$expected_base/$name", "selects $name through $base"); + } + unlink("assets/$name") or die "unlink $name: $!"; + } + is(lookup($owner, 'debian12.5', $arch, 'assets', $extension), '', 'miss returns an empty string'); + is(lookup($owner, 'debian12.5', $arch, 'missing', $extension), '', 'missing directory returns an empty string'); + SKIP: { + skip 'root can read mode-0000 files', 1 unless $>; + write_text("assets/$names[0]", 'unreadable'); + chmod(0000, "assets/$names[0]") or die "chmod: $!"; + write_text("assets/$names[-1]", 'readable'); + is(lookup($owner, 'debian12.5', $arch, 'assets', $extension), + ($owner eq 'debian' ? 'assets' : realpath('assets')) . "/$names[-1]", + 'an unreadable exact match does not hide the generic asset'); + unlink(map { "assets/$_" } @names[0, -1]) == 2 or die "unlink: $!"; + } + }; + } + } +} + +for my $case ( + ['debian', 'debian12.5', 'compute.debian12.x86_64.pkglist', 'compute.debian12.4.x86_64.pkglist'], + ['debian', 'debian12.5.1', 'compute.debian12.5.x86_64.pkglist', 'compute.debian12.x86_64.pkglist'], + ['imgutils', 'rocky9.6', 'compute.rocky9.5.x86_64.pkglist', 'compute.rocky9.x86_64.pkglist'], + ['imgutils', 'ubuntu24.04', 'compute.pkglist', 'compute.ubuntu24.x86_64.pkglist'], + ['imgutils', 'sles11sp1', 'compute.sles11.x86_64.pkglist', 'compute.pkglist'], + ['imgutils', 'leap15.6', 'compute.sle15.x86_64.pkglist', 'compute.x86_64.pkglist'], + ['imgutils', 'leap15.6', 'compute.sle15.pkglist', 'compute.pkglist'], + ['debian', 'debian12', 'compute.debian12.x86_64.pkglist', 'compute.pkglist'], +) { + my ($owner, $version, $wanted, $other) = @$case; + write_text("assets/$_", "fixture\n") for ($wanted, $other); + is(lookup($owner, $version, 'x86_64', 'assets', 'pkglist'), + ($owner eq 'debian' ? 'assets' : realpath('assets')) . "/$wanted", + "$owner retains its $version fallback policy"); + unlink("assets/$wanted", "assets/$other") == 2 or die "unlink: $!"; +} + +chdir($original_dir) or die "chdir $original_dir: $!"; +done_testing();