Files
mtusi2026lab2-fork/scripts/failure_demo.sh
T
2026-10-27 00:18:03 +03:00

65 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
BASE_URL="${BASE_URL:-http://localhost:8080}"
API="$BASE_URL/api/v1"
USERNAME="demo_$(date +%s)"
PASSWORD="demo-password-123"
compose() {
docker compose --env-file .env "$@"
}
code() {
curl -s -o /dev/null -w '%{http_code} (%{time_total}s)' "$@"
}
wait_healthy() {
attempts=0
until [ "$(compose ps --format '{{.Health}}' "$1")" = "healthy" ]; do
attempts=$((attempts + 1))
[ "$attempts" -lt 60 ] || { echo "$1 не стал healthy" >&2; exit 1; }
sleep 1
done
}
curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"$USERNAME\",\"display_name\":\"Demo\",\"password\":\"$PASSWORD\"}"
TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \
| python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
AUTH="Authorization: Bearer $TOKEN"
echo "== baseline"
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
echo
echo "== redis stopped"
compose stop redis >/dev/null 2>&1
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
echo "POST /auth/sessions (Basic) -> $(code -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD")"
echo "GET /users/me (no token) -> $(code "$API/users/me")"
echo "GET /health/ready -> $(code "$BASE_URL/health/ready")"
echo "GET /health/live -> $(code "$BASE_URL/health/live")"
compose start redis >/dev/null 2>&1
wait_healthy redis
echo "-- redis started again (AOF restored)"
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
echo
echo "== postgres stopped"
compose stop postgres >/dev/null 2>&1
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
echo "GET /api/v1/chats (Bearer) -> $(code "$API/chats" -H "$AUTH")"
echo "GET /health/ready -> $(code "$BASE_URL/health/ready")"
echo "GET /health/live -> $(code "$BASE_URL/health/live")"
compose start postgres >/dev/null 2>&1
wait_healthy postgres
echo "-- postgres started again"
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
echo
echo "== logout"
echo "DELETE /auth/sessions/current -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")"
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"