2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-21 01:42:15 +00:00
Files
xcat-core/xCAT-test/bats/genesis_getcert_missing_openssl.bats
T
Daniel Hilst cd0a7fd8a9 test(xcat-core): the Genesis payload carries no openssl config, so no certificate is requested
A node boots Genesis, prints "Getting initial certificate --> MN:3001" and goes no
further. xcatd records no credential request, the node status stays at powering-on,
and every case that waits for the node fails.

genesis_openssl_config.bats drives each dracut module's install() with the dracut
helpers replaced by a recorder, so it reads what the module asks the payload to carry.
genesis_getcert_missing_openssl.bats gains one case: the give up message must repeat
what openssl printed, because a message naming only certkey.pem sends the reader to a
key that is intact.

Three of the six cases fail on this commit: both module cases, and the message case.

stub_dir defaulted count to the empty string, which left "{ ; exit 1; }" in the
openssl stub. That is a syntax error, so the stub failed for the wrong reason and
reported bash's message instead of openssl's. The default is now a no-op command.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-10-26 08:50:59 -03:00

85 lines
2.9 KiB
Bash

#!/usr/bin/env bats
#
# Drive getcert with openssl absent, and with a certificate key that is not ready yet.
# doxcat runs getcert in the foreground and ignores its status, so a wait with no bound stops
# the boot and prints nothing.
load 'helpers/shell_source'
setup()
{
GETCERT="$(repo_path 'xCAT-genesis-scripts/usr/bin/getcert')"
[ -r "$GETCERT" ] || skip "$GETCERT is required"
COUNTER="${BATS_TEST_TMPDIR}/req-count"
export GETCERT COUNTER
}
write_stub()
{
local dir="$1" name="$2" body="$3"
printf '#!/bin/sh\n%s\n' "$body" >"$dir/$name"
chmod 0755 "$dir/$name"
}
# A PATH directory holding the commands getcert runs. openssl is absent unless it is asked for.
stub_dir()
{
# 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"
mkdir -p "$dir"
write_stub "$dir" allowcred.awk 'exec sleep 3'
write_stub "$dir" hostname 'echo node1'
write_stub "$dir" logger 'echo "$@" >&2'
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 ; echo \"\${OPENSSL_REQ_ERROR:-}\" >&2 ; exit 1; }
exit 0"
fi
printf '%s\n' "$dir"
}
# Run getcert with only the stub directory on PATH. The timeout is the harness guard: a status
# of 124 means getcert never stopped.
run_getcert()
{
local bin="$1" limit="$2" csr_timeout="$3"
timeout -k 2 "$limit" env PATH="$bin" GETCERT_CSR_TIMEOUT="$csr_timeout" \
/bin/bash "$GETCERT" 192.0.2.1:3001 2>&1 </dev/null
}
@test "getcert stops and names openssl when the image ships none" {
# The el10 legacy image ships no openssl.
run run_getcert "$(stub_dir 0)" 10 60
[ "$status" -ne 124 ]
[ "$status" -ne 0 ]
[[ "$output" == *openssl* ]]
}
@test "getcert retries the certificate request, then gives up and names the key" {
# doxcat writes /etc/xcat/certkey.pem in the background, so the first requests can fail.
export COUNT_REQUESTS=1
run run_getcert "$(stub_dir 1)" 30 5
[ "$status" -ne 124 ]
[ "$status" -ne 0 ]
[[ "$output" == *certkey.pem* ]]
tries=0
[ -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* ]]
}