From 501b1ba44d1efa85533cf28776153e08b2990d78 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Mon, 14 Sep 2026 15:44:56 -0300 Subject: [PATCH] test(xcatdsklspost): cover the /xcatpost cleanup after an updatenode run The site table has two attributes for the same directory and only one of them is covered: nothing pins what xcatdsklspost appends to mypostscript for site.cleanupdiskfullxcatpost, on the path every updatenode run takes. Drive append_xcatpost_cleanup against a scratch /xcatpost, run the mypostscript it writes, and assert on the directory that comes back: the postscripts are gone after a run that succeeds, updateflag.awk stays, and a run that fails keeps everything. Add refute_grep to the helpers, because a "! grep" line cannot fail a bats test unless it is the last line of one. Three of the six tests fail on this commit. xcatdsklspost reads CLEANUPXCATPOST and never reads CLEANUPDISKFULLXCATPOST. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- xCAT-test/bats/helpers/shell_source.bash | 10 ++ .../bats/xcatdsklspost_xcatpost_cleanup.bats | 121 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 xCAT-test/bats/xcatdsklspost_xcatpost_cleanup.bats diff --git a/xCAT-test/bats/helpers/shell_source.bash b/xCAT-test/bats/helpers/shell_source.bash index 61a0bea8d..4f4384fb2 100644 --- a/xCAT-test/bats/helpers/shell_source.bash +++ b/xCAT-test/bats/helpers/shell_source.bash @@ -130,3 +130,13 @@ extract_first_matching_line() } ' "$file" } + +# grep that fails when the pattern IS present. +# +# Do not write "! grep ..." for this. bash ignores errexit for a command inverted with "!", +# so such a line never fails a test unless it is the last line of one. +refute_grep() +{ + ! grep "$@" + return $? +} diff --git a/xCAT-test/bats/xcatdsklspost_xcatpost_cleanup.bats b/xCAT-test/bats/xcatdsklspost_xcatpost_cleanup.bats new file mode 100644 index 000000000..b6658dcd4 --- /dev/null +++ b/xCAT-test/bats/xcatdsklspost_xcatpost_cleanup.bats @@ -0,0 +1,121 @@ +#!/usr/bin/env bats + +load 'helpers/shell_source' + +setup() +{ + DSKLSPOST="$(repo_path 'xCAT/postscripts/xcatdsklspost')" + [ -r "$DSKLSPOST" ] || skip "$DSKLSPOST is required" + XCATPOST="${BATS_TEST_TMPDIR}/xcatpost" + MYPS="${BATS_TEST_TMPDIR}/mypostscript" + MSGLOG="${BATS_TEST_TMPDIR}/msgutil.log" + export DSKLSPOST XCATPOST MYPS MSGLOG +} + +# A scratch /xcatpost with what an updatenode run downloads into it. +make_xcatpost() +{ + mkdir -p "$XCATPOST/_xcat" + printf 'updateflag\n' >"$XCATPOST/updateflag.awk" + printf 'setroute\n' >"$XCATPOST/setroute" + printf 'xcatlib\n' >"$XCATPOST/xcatlib.sh" + printf 'credentials\n' >"$XCATPOST/_xcat/postscript.cfg" +} + +# A scratch mypostscript that carries the site values and the run result. +# $1 is the value of return_value, the rest are the site table lines. +make_mypostscript() +{ + local result=$1 + shift + { + printf '%s\n' '#!/bin/bash' + printf 'msgutil_r() { printf "%%s\\n" "$3" >>"%s"; }\n' "$MSGLOG" + printf 'MASTER_IP=192.0.2.1\n' + printf 'NODE=node1\n' + printf 'log_label=xcat.updatenode\n' + printf 'return_value=%s\n' "$result" + printf '%s\n' "$@" + } >"$MYPS" +} + +append_cleanup() +{ + ( + XCATDSKLSPOST_SOURCE_ONLY=1 + export XCATDSKLSPOST_SOURCE_ONLY + # shellcheck disable=SC1090 + . "$DSKLSPOST" + type -t append_xcatpost_cleanup >/dev/null \ + || { echo "xcatdsklspost defines no append_xcatpost_cleanup" >&2; exit 99; } + append_xcatpost_cleanup "$MYPS" "$XCATPOST" + ) +} + +@test "an updatenode run that succeeds removes the postscripts when site.cleanupdiskfullxcatpost is set" { + make_xcatpost + make_mypostscript 0 "CLEANUPXCATPOST='no'" "CLEANUPDISKFULLXCATPOST='yes'" + append_cleanup + + run bash "$MYPS" + [ "$status" -eq 0 ] + [ ! -e "$XCATPOST/setroute" ] + [ ! -e "$XCATPOST/xcatlib.sh" ] + [ ! -e "$XCATPOST/_xcat" ] +} + +@test "the cleanup keeps updateflag.awk so the node can still report its status" { + make_xcatpost + make_mypostscript 0 "CLEANUPXCATPOST='no'" "CLEANUPDISKFULLXCATPOST='yes'" + append_cleanup + + run bash "$MYPS" + [ "$status" -eq 0 ] + [ -f "$XCATPOST/updateflag.awk" ] + [ -d "$XCATPOST" ] + grep -q 'cleanup of .* completed' "$MSGLOG" +} + +@test "an updatenode run that fails keeps the postscripts for diagnosis" { + make_xcatpost + make_mypostscript 1 "CLEANUPXCATPOST='no'" "CLEANUPDISKFULLXCATPOST='yes'" + append_cleanup + + run bash "$MYPS" + [ "$status" -eq 0 ] + [ -f "$XCATPOST/setroute" ] + [ -f "$XCATPOST/_xcat/postscript.cfg" ] + refute_grep -q 'cleanup of .* completed' "$MSGLOG" +} + +@test "site.cleanupdiskfullxcatpost accepts the other true values of the site table" { + make_xcatpost + make_mypostscript 0 "CLEANUPDISKFULLXCATPOST='1'" + append_cleanup + + run bash "$MYPS" + [ "$status" -eq 0 ] + [ ! -e "$XCATPOST/setroute" ] +} + +@test "site.cleanupxcatpost removes every file including updateflag.awk" { + make_xcatpost + make_mypostscript 0 "CLEANUPXCATPOST='yes'" "CLEANUPDISKFULLXCATPOST='no'" + append_cleanup + + run bash "$MYPS" + [ "$status" -eq 0 ] + [ -z "$(ls -A "$XCATPOST")" ] +} + +@test "neither setting leaves the downloaded postscripts in place" { + make_xcatpost + make_mypostscript 0 "CLEANUPXCATPOST='no'" "CLEANUPDISKFULLXCATPOST='no'" + append_cleanup + + run bash "$MYPS" + [ "$status" -eq 0 ] + [ -f "$XCATPOST/setroute" ] + [ -f "$XCATPOST/updateflag.awk" ] + refute_grep -q -E 'rm -rf|-delete' "$MYPS" +}