Files
cozystack/hack/testdata/run-migrations/kubectl
Aleksei Sviridkin cbbb69afc3 fix(platform): stop treating a failed ConfigMap read as a fresh cluster
run-migrations.sh decided whether cozystack-version exists by the exit
status of a silenced kubectl get, so a timeout or a dropped connection
counted as "no ConfigMap": the runner stamped the target version and
exited 0, and every pending migration was skipped for good. The hook Job
only renders when the ConfigMap already exists, so in practice an error
was the only way to reach that branch.

--ignore-not-found suppresses NotFound alone; any other error keeps its
non-zero status and now fails the pod, so the Job retries the read.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-07 12:06:18 +03:00

38 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
# Fake kubectl for hack/run-migrations.bats. It serves the three calls
# run-migrations.sh and lib/cozystack-version.sh make against the
# cozystack-version ConfigMap, backed by a state file so a stamp made by one
# pod is what the next pod reads.
#
# FAKE_STATE file holding the stamped version; absent => no ConfigMap
# FAKE_READ_FAIL non-empty => reading .data.version exits 1 with this stderr
# FAKE_EXIST_FAIL non-empty => the existence check exits 1 with this stderr,
# --ignore-not-found or not, as a real non-NotFound error does
set -u
case "$*" in
*"apply --filename -"*)
v=$(sed -n 's/^ version: "\([0-9]*\)"$/\1/p')
[ -n "$v" ] || { echo "fake kubectl: no version in the applied manifest" >&2; exit 1; }
printf '%s' "$v" > "$FAKE_STATE"
;;
*"get configmap"*"cozystack-version"*"jsonpath"*)
if [ -n "${FAKE_READ_FAIL:-}" ]; then echo "$FAKE_READ_FAIL" >&2; exit 1; fi
[ -f "$FAKE_STATE" ] || { echo 'Error from server (NotFound): configmaps "cozystack-version" not found' >&2; exit 1; }
cat "$FAKE_STATE"
;;
*"get configmap"*"cozystack-version"*)
if [ -n "${FAKE_EXIST_FAIL:-}" ]; then echo "$FAKE_EXIST_FAIL" >&2; exit 1; fi
if [ ! -f "$FAKE_STATE" ]; then
case "$*" in *--ignore-not-found*) exit 0 ;; esac
echo 'Error from server (NotFound): configmaps "cozystack-version" not found' >&2
exit 1
fi
echo "configmap/cozystack-version"
;;
*)
echo "fake kubectl: unexpected call: $*" >&2
exit 1
;;
esac