Files
mtusi2026lab2/scripts/test_failures.sh
T
2026-10-08 02:08:08 +03:00

189 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE_URL="http://localhost:8080"
TOKEN_FILE=$(mktemp)
restore_services() {
echo "Restoring PostgreSQL and Redis..."
docker compose up -d postgres redis
echo "Waiting for API readiness..."
for attempt in $(seq 1 60); do
if curl --silent --fail \
"$BASE_URL/health/ready" > /dev/null; then
rm -f "$TOKEN_FILE"
echo "Services restored."
return
fi
sleep 1
done
rm -f "$TOKEN_FILE"
echo "WARNING: API did not become ready after recovery." >&2
}
trap restore_services EXIT
echo "Waiting for API readiness before testing..."
api_ready=false
for attempt in $(seq 1 60); do
if curl --silent --fail --max-time 3 \
"$BASE_URL/health/ready" > /dev/null; then
api_ready=true
break
fi
sleep 1
done
if [[ "$api_ready" != "true" ]]; then
echo "FAIL: API did not become ready before testing." >&2
exit 1
fi
check_status() {
local expected="$1"
local path="$2"
local authorization="${3:-}"
local result actual_status elapsed
if [[ -n "$authorization" ]]; then
result=$(curl --silent --output /dev/null \
--max-time 12 \
--write-out "%{http_code} %{time_total}" \
-H "Authorization: Bearer $authorization" \
"$BASE_URL$path")
else
result=$(curl --silent --output /dev/null \
--max-time 12 \
--write-out "%{http_code} %{time_total}" \
"$BASE_URL$path")
fi
read -r actual_status elapsed <<< "$result"
if [[ "$actual_status" != "$expected" ]]; then
echo "FAIL: $path expected $expected, got $actual_status" >&2
exit 1
fi
echo "PASS: $path returned $actual_status in ${elapsed}s"
}
echo "Creating a test user and session..."
python - "$TOKEN_FILE" <<'PY'
import base64
import json
import sys
import urllib.request
import uuid
base = "http://localhost:8080"
username = "failure_" + uuid.uuid4().hex[:16]
password = "correct-password-123"
def post(path, data=None, authorization=None):
headers = {}
body = None
if data is not None:
headers["Content-Type"] = "application/json"
body = json.dumps(data).encode("utf-8")
if authorization:
headers["Authorization"] = authorization
request = urllib.request.Request(
base + path,
data=body,
headers=headers,
method="POST",
)
with urllib.request.urlopen(request, timeout=10) as response:
assert response.status == 201
return json.load(response)
post("/api/v1/users", {
"username": username,
"display_name": "Failure Test User",
"password": password,
})
credentials = base64.b64encode(
f"{username}:{password}".encode()
).decode()
result = post(
"/api/v1/auth/sessions",
authorization=f"Basic {credentials}",
)
with open(sys.argv[1], "w", encoding="utf-8") as file:
file.write(result["token"])
print("Test session created.")
PY
TOKEN=$(cat "$TOKEN_FILE")
echo
echo "Checking baseline..."
check_status 200 "/health/live"
check_status 200 "/health/ready"
check_status 200 "/api/v1/users/me" "$TOKEN"
echo
echo "=== Redis failure ==="
docker compose stop redis
check_status 200 "/health/live"
check_status 503 "/health/ready"
check_status 503 "/api/v1/users/me" "$TOKEN"
echo "Restarting Redis..."
docker compose start redis
for attempt in $(seq 1 60); do
if curl --silent --fail \
"$BASE_URL/health/ready" > /dev/null; then
break
fi
sleep 1
done
check_status 200 "/health/ready"
check_status 200 "/api/v1/users/me" "$TOKEN"
echo
echo "=== PostgreSQL failure ==="
docker compose stop postgres
check_status 200 "/health/live"
check_status 503 "/health/ready"
check_status 503 "/api/v1/users/me" "$TOKEN"
echo "Restarting PostgreSQL..."
docker compose start postgres
for attempt in $(seq 1 60); do
if curl --silent --fail \
"$BASE_URL/health/ready" > /dev/null; then
break
fi
sleep 1
done
check_status 200 "/health/ready"
check_status 200 "/api/v1/users/me" "$TOKEN"
echo
echo "PASS: dependency failure and recovery tests completed."