Files
cozystack/hack/kubectl-wait-retry_test.bats
T
Aleksei Sviridkin 40dac09aaa test(bats): spell absences so they can fail
An assertion written `! cmd` cannot fail a test. POSIX exempts a
command whose return value is inverted with `!` from errexit, so the
status is dropped where it is produced. hack/cozytest.sh then closes
each test function with `return 0`, and the bats binary's ERR trap
carries the same exemption -- it reports only a negated assertion
standing last in the body, where the inverted status happens to be
what the function returns.

Every absence these suites assert was written that way: a command that
must not reach the log, an image that must not be copied, a flag that
must not be passed, an object that must not exist. On that half of
each suite's contract the tests held for any input.

Rewrite each as `if cmd; then echo "FAIL: ..."; false; fi`, the form
already used where the trap had been noticed one file at a time, and
give every one a message naming what must not have happened. The
message states the property, not the pattern: where the forbidden
string is a substring of a required value -- an https endpoint the
test requires in its http form -- naming the string would describe the
opposite of what is asserted.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-02 15:01:49 +03:00

62 lines
2.4 KiB
Bash

#!/usr/bin/env bats
# -----------------------------------------------------------------------------
# Unit tests for kubectl_wait_retry in hack/e2e-chainsaw/_lib/run-kubernetes.sh
#
# The wrapper exists to retry `kubectl wait` against a curated allowlist of
# transient server errors (etcd leader flap et al.) while letting real failures
# (NotFound, --timeout expired) surface. The chainsaw scripts source the lib
# under `set -eu`, where a bare `_out=$(kubectl wait ...)` on a failing wait
# aborts the whole script before the retry loop runs -- silently, since stderr
# is captured into the substitution. These tests execute the wrapper in a
# `sh -eu` subshell, exactly as chainsaw runs it, against a fake kubectl.
#
# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its
# own line; there is no bats `run`/`$status`. Assertions are direct shell tests.
# Run with: hack/cozytest.sh hack/kubectl-wait-retry_test.bats
# -----------------------------------------------------------------------------
FAKEBIN="$PWD/hack/testdata/kubectl-wait-retry"
prep() {
chmod +x "$FAKEBIN/kubectl"
WORK=$(mktemp -d)
export FAKE_CMDLOG="$WORK/cmdlog"
: > "$FAKE_CMDLOG"
}
@test "a transient etcd error is retried under set -eu instead of silently killing the script" {
prep
export FAKE_MODE=transient-once
rc=0
out=$(PATH="$FAKEBIN:$PATH" sh -euc '
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
kubectl_wait_retry deploy x -n tenant-test --for=condition=available
echo SCRIPT-REACHED-END
' 2>&1) || rc=$?
echo "$out"
# The wrapper retried past the transient error and the sourcing script lived.
[ "$rc" -eq 0 ]
echo "$out" | grep -q 'retrying'
echo "$out" | grep -q 'SCRIPT-REACHED-END'
[ "$(grep -c . "$FAKE_CMDLOG")" -eq 2 ]
rm -rf "$WORK"
}
@test "NotFound is not retried, fails the script, and its message is printed (fail-loud preserved)" {
prep
export FAKE_MODE=notfound
rc=0
out=$(PATH="$FAKEBIN:$PATH" sh -euc '
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
kubectl_wait_retry deploy x -n tenant-test --for=condition=available
echo SCRIPT-REACHED-END
' 2>&1) || rc=$?
echo "$out"
# A real failure still aborts, is not retried, and the error is visible.
[ "$rc" -ne 0 ]
echo "$out" | grep -q 'NotFound'
if echo "$out" | grep -q 'SCRIPT-REACHED-END'; then echo "FAIL: the script must not have run on past a NotFound"; false; fi
[ "$(grep -c . "$FAKE_CMDLOG")" -eq 1 ]
rm -rf "$WORK"
}