forked from mtusi2026/mtusi2026lab2
55 lines
2.0 KiB
Bash
Executable File
55 lines
2.0 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
|
|
cd "$ROOT"
|
|
BASE="${BASE_URL:-http://127.0.0.1:8080}"
|
|
|
|
USER="dep_$(date +%s)"
|
|
PASS="T3st-$(openssl rand -hex 8)"
|
|
curl -sf -X POST "$BASE/api/v1/users" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "{\"username\":\"$USER\",\"display_name\":\"Deps\",\"password\":\"$PASS\"}" >/tmp/dep_user.json
|
|
CRED=$(printf '%s:%s' "$USER" "$PASS" | base64 -w0 2>/dev/null || printf '%s:%s' "$USER" "$PASS" | base64)
|
|
SESS=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -H "Authorization: Basic $CRED")
|
|
TOKEN=$(printf '%s' "$SESS" | python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])')
|
|
|
|
echo "=== stop valkey (cache bypass) ==="
|
|
docker compose --env-file .env stop valkey >/dev/null
|
|
sleep 1
|
|
code=$(curl -sS -o /tmp/dep_vk.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me")
|
|
echo "with valkey down /users/me -> $code (expect 200)"
|
|
test "$code" = "200"
|
|
|
|
echo "=== start valkey, stop postgres ==="
|
|
docker compose --env-file .env start valkey >/dev/null
|
|
i=0
|
|
while [ "$i" -lt 20 ]; do
|
|
if docker compose --env-file .env exec -T valkey valkey-cli ping 2>/dev/null | grep -q PONG; then
|
|
break
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 0.5
|
|
done
|
|
docker compose --env-file .env stop postgres >/dev/null
|
|
sleep 1
|
|
ready=$(curl -sS -o /tmp/dep_ready.json -w '%{http_code}' "$BASE/health/ready" || true)
|
|
me=$(curl -sS -o /tmp/dep_me.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me" || true)
|
|
echo "postgres down ready=$ready me=$me (expect 503/503)"
|
|
test "$ready" = "503"
|
|
test "$me" = "503"
|
|
|
|
echo "=== restore postgres ==="
|
|
docker compose --env-file .env start postgres >/dev/null
|
|
i=0
|
|
while [ "$i" -lt 40 ]; do
|
|
if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then
|
|
break
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 0.5
|
|
done
|
|
code=$(curl -sS -o /tmp/dep_ok.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me")
|
|
echo "restored /users/me -> $code (expect 200)"
|
|
test "$code" = "200"
|
|
echo "PASS: dependency failures"
|