Files
cozystack/hack/kubectl-wait-retry_test.bats
T
Timofei Larkin f0b99da43a test(kubernetes): make kubectl_wait_retry set -e safe
The chainsaw scripts source run-kubernetes.sh under `set -eu`, where the
wrapper's bare `_out=$(kubectl wait ... 2>&1)` on a failing wait aborts
the whole sourcing script at the assignment -- before `_rc=$?`, before
the retry loop, before anything prints, with the error text captured
into the substitution and lost. Every `kubectl wait` failure therefore
killed the suite instantly and silently, and the transient-retry
machinery the wrapper exists for (etcd leader flap et al.) has never
been reachable. This is how the kubernetes-latest suite died in CI with
an empty stderr: the wait it was executing failed, and the script
simply stopped.

Guard the assignment (`_rc=0; _out=$(...) || _rc=$?`) so the wrapper's
own logic decides: allowlisted transient errors retry with a visible
notice, everything else prints and propagates, exactly the documented
contract.

New unit suite executes the wrapper in a `sh -eu` subshell against a
fake kubectl, the same way chainsaw runs it: a transient etcd error
must be retried with the script surviving (against the previous lib
this exits 1 after one call with empty output), and NotFound must not
be retried, must fail, and must be printed.

Assisted-By: Claude <noreply@anthropic.com>
Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
2026-07-31 16:11:08 +03:00

62 lines
2.3 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'
! echo "$out" | grep -q 'SCRIPT-REACHED-END'
[ "$(grep -c . "$FAKE_CMDLOG")" -eq 1 ]
rm -rf "$WORK"
}