Files
cozystack/hack/kubectl-wait-retry_test.bats
T
Myasnikov Daniil cc942fed68 test(tests): run unit suites with Bats and retain POSIX coverage
The shell translator rewrites test bodies and loses Bats reporting.
The unit lane needs complete TAP results and JUnit reports without
changing how live-cluster suites run.

Sourced production helpers still need a dash pass, and unit test bodies
need nounset without exporting shell options into subprocesses.

Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-10-07 11:04:00 +03:00

66 lines
2.6 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.
#
# CI runs this file under Bats through `make bats-unit-tests`. It also remains
# compatible with the legacy `hack/cozytest.sh` translator, whose awk parser
# recognizes only @test blocks and a bare `}` on its own line. The tests avoid
# `run` and `$status` for that compatibility path and use direct shell assertions.
# Run with: bats hack/kubectl-wait-retry_test.bats
# -----------------------------------------------------------------------------
load test_helper
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"
}