Files
mtusi2026lab5/scripts/verify-attachment-failover.sh

56 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
# Verify the durability claim: accept an upload through the load balancer
# (202 after durable staging plus outbox), let the worker make it ready, stop
# the API replica that accepted it, then download the same attachment through
# another replica and compare the SHA-256. The stopped replica is restored on
# exit so the stand is left as it was found.
set -eu
cd "$(dirname "$0")/.."
COMPOSE="docker compose --env-file .env"
STATE_DIR="$PWD/evidence/.tmp-attachment-failover.$$"
mkdir -p "$STATE_DIR"
chmod 700 "$STATE_DIR"
restore() {
rm -rf "$STATE_DIR"
$COMPOSE up -d --scale api=2 api >/dev/null 2>&1 || true
attempts=0
while [ "$attempts" -lt 60 ]; do
ids=$($COMPOSE ps -q api)
count=$(printf '%s\n' "$ids" | sed '/^$/d' | wc -l)
healthy=$(for id in $ids; do docker inspect -f '{{.State.Health.Status}}' "$id"; done | grep -c '^healthy$' || true)
if [ "$count" -eq 2 ] && [ "$healthy" -eq 2 ]; then
return 0
fi
attempts=$((attempts + 1))
sleep 1
done
echo "warning: API replicas did not return to two healthy instances" >&2
}
trap restore EXIT INT TERM
network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)")
[ -n "$network" ] || {
echo "cannot determine the Compose network; is the stack running?" >&2
exit 2
}
run_python() {
docker run --rm --network "$network" \
-v "$PWD:/work:ro" -v "$STATE_DIR:/state" -w /work \
python:3.12-alpine \
python3 tests/custom/attachment_flow.py --base-url http://haproxy:8080 "$@"
}
upload_json=$(run_python --action upload --state /state/upload.json)
printf '%s\n' "$upload_json"
instance=$(printf '%s' "$upload_json" | python3 -c 'import json,sys; print(json.load(sys.stdin)["uploaded_by"])')
echo "stopping API replica $instance"
TARGET_INSTANCE_ID="$instance" scripts/stop-one.sh
run_python --action download --state /state/upload.json --timeout 30
echo "PASS: attachment survived the stop of the replica that accepted it"