2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-09-21 08:33:20 +00:00
Files
xcat-core/xCAT-test/bats/genesis_console_mode.bats
T
Daniel Hilst 4b9572a0a3 test(xcat-core): the Genesis shell tests are written as Perl programs
Eight unit tests measure shell code: the Genesis dracut cmdline hooks, doxcat,
getcert, the two Genesis deb builders, go-xcat and the genesis test case. The
Perl in each one is scaffolding. It reads the script, lifts a block out with a
regular expression, writes a wrapper, shells out and reads the files back. A
reader follows two languages to reach one assertion, and the scaffolding is
longer than the assertion.

xCAT-test/bats already states this kind of assertion in the language of the
thing under test, and the xcat_test workflow runs it. The eight files move
there. Each one keeps what it proved: the rpm architecture becomes the Debian
architecture and names the deb it supersedes, the dracut hook picks the console
mode the multiplexer can provide, the hook gives root the home directory /,
getcert stops when the image ships no openssl, the genesis case defines its
node with the architecture of the management node and fails when nodeset fails,
doxcat picks dhcpcd where the release drops the ISC client, and go-xcat names
the Genesis packages the packaging builds.

helpers/shell_source.bash gains refute_grep. bash ignores errexit for a command
inverted with "!", so "! grep" anywhere but the last line of a test can never
fail it.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-09-14 10:07:49 -03:00

69 lines
2.5 KiB
Bash

#!/usr/bin/env bats
#
# Drive xcat_console_mode() out of the Genesis dracut cmdline hook.
#
# The hook cannot be sourced: it mounts filesystems, starts udev and ends in an endless
# loop. Extract the one function and run it with the terminal multiplexer shadowed.
load 'helpers/shell_source'
setup()
{
EL_HOOK="$(repo_path 'xCAT-genesis-builder/dracut_105/el/xcat-cmdline.sh')"
UBUNTU_HOOK="$(repo_path 'xCAT-genesis-builder/dracut_105/ubuntu/xcat-cmdline.sh')"
[ -r "$EL_HOOK" ] || skip "$EL_HOOK is required"
[ -r "$UBUNTU_HOOK" ] || skip "$UBUNTU_HOOK is required"
export EL_HOOK UBUNTU_HOOK
}
# Run the extracted function with the multiplexer shadowed by a stub that either starts a
# session or refuses, the way tmux refuses without a UTF-8 locale.
run_mode()
{
local hook="$1" mux="$2" mux_works="$3" body
body="$(extract_shell_function "$hook" xcat_console_mode)" ||
{ echo "xcat_console_mode() not found in $hook" >&2; return 99; }
(
eval "$body"
eval "$mux() {
[ \"\$mux_works\" = 1 ] && return 0
echo '$mux: need UTF-8 locale (LC_CTYPE) but have ANSI_X3.4-1968' >&2
return 1
}"
xcat_console_mode
) 2>/dev/null
}
# The hook reads the mode once and guards the doxcat loop with it.
assert_hook_guards_doxcat()
{
local hook="$1" mux="$2"
grep -qx 'XCAT_CONSOLE_MODE="$(xcat_console_mode)"' "$hook"
grep -qFx "if [ \"\$XCAT_CONSOLE_MODE\" = \"$mux\" ]; then" "$hook"
grep -A1 '^else$' "$hook" | grep -qx ' while :; do doxcat; sleep 5; done'
}
@test "the el hook leaves no unguarded tmux loop and exports a UTF-8 locale" {
# tmux exits under the C locale, so an unguarded tmux loop never reaches doxcat.
refute_grep -q '^while :; do tmux attach-session' "$EL_HOOK"
grep -qx 'export LC_ALL=C.UTF-8' "$EL_HOOK"
}
@test "el: xcat_console_mode reports the mode tmux can actually provide" {
[ "$(run_mode "$EL_HOOK" tmux 0)" = direct ]
[ "$(run_mode "$EL_HOOK" tmux 1)" = tmux ]
}
@test "el: the hook resolves the console mode once and runs doxcat directly without tmux" {
assert_hook_guards_doxcat "$EL_HOOK" tmux
}
@test "ubuntu: xcat_console_mode reports the mode screen can actually provide" {
[ "$(run_mode "$UBUNTU_HOOK" screen 0)" = direct ]
[ "$(run_mode "$UBUNTU_HOOK" screen 1)" = screen ]
}
@test "ubuntu: the hook resolves the console mode once and runs doxcat directly without screen" {
assert_hook_guards_doxcat "$UBUNTU_HOOK" screen
}