ananke/testing/scripts/self_update_test.sh

62 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Exercise updater exit/status behavior with a local repository and fake installer.
# Run as root; all writes, Git configuration and installer calls stay in a temp dir.
set -euo pipefail
if [[ "${EUID}" -ne 0 ]]; then
echo "Run this isolated updater regression as root" >&2
exit 1
fi
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
FIXTURE="$(mktemp -d /tmp/ananke-update-test.XXXXXX)"
trap 'rm -rf "${FIXTURE}"' EXIT
export GIT_CONFIG_GLOBAL="${FIXTURE}/gitconfig"
export GIT_CONFIG_NOSYSTEM=1
git config --global user.name 'Updater regression'
git config --global user.email 'updater-test@example.invalid'
mkdir -p "${FIXTURE}/origin/scripts"
git init -q -b main "${FIXTURE}/origin"
cat > "${FIXTURE}/origin/scripts/install.sh" <<'INSTALLER'
#!/usr/bin/env bash
set -euo pipefail
printf '%s\n' "${ANANKE_ENFORCE_QUALITY_GATE}" >> "${FIXTURE_CALLS}"
if [[ "${ANANKE_ENFORCE_QUALITY_GATE}" == 1 ]]; then
exit "${FIXTURE_INSTALL_RC}"
fi
INSTALLER
chmod 0755 "${FIXTURE}/origin/scripts/install.sh"
git -C "${FIXTURE}/origin" add scripts/install.sh
git -C "${FIXTURE}/origin" commit -qm fixture
# Inputs: case name, installer status, fallback policy, expected exit/state/calls.
# Output: assertion failure or one concise pass line; no host services are invoked.
check_update() {
local name="$1" install_rc="$2" fallback="$3" expected_rc="$4"
local expected_status="$5" expected_calls="$6" actual_rc=0
local directory="${FIXTURE}/${name}"
mkdir -p "${directory}"
env ANANKE_REPO_URL="${FIXTURE}/origin" \
ANANKE_REPO_DIR="${directory}/repo" \
ANANKE_REPO_BRANCH=main \
ANANKE_UPDATE_LOG_FILE="${directory}/update.log" \
ANANKE_UPDATE_STATE_FILE="${directory}/state" \
ANANKE_UPDATE_LOCK_FILE="${directory}/lock" \
ANANKE_UPDATE_ALLOW_QUALITY_FALLBACK="${fallback}" \
ANANKE_ENFORCE_QUALITY_GATE=1 \
FIXTURE_CALLS="${directory}/calls" FIXTURE_INSTALL_RC="${install_rc}" \
bash "${REPO_ROOT}/scripts/ananke-self-update.sh" > "${directory}/output" 2>&1 || actual_rc=$?
[[ "${actual_rc}" == "${expected_rc}" ]]
grep -qx "status=${expected_status}" "${directory}/state"
[[ "$(wc -l < "${directory}/calls")" -eq "${expected_calls}" ]]
if [[ "${expected_status}" == failed ]]; then
grep -qx "detail=install-failed;rc=${install_rc}" "${directory}/state"
fi
printf 'PASS %s: exit=%s state=%s calls=%s\n' \
"${name}" "${actual_rc}" "${expected_status}" "${expected_calls}"
}
check_update strict_failure 42 0 42 failed 1
check_update explicit_legacy_fallback 42 1 0 degraded-ok 2
check_update strict_success 0 0 0 ok 1