Skeleton contracts / contracts (pull_request) Canceled after 0s
Hook теперь реально перезапускает api; хвост JSON и тело login отклоняются; текст сообщений сохраняется целиком; отказы deps проверяются assert-ами; pg_restore замеряется.
95 lines
3.2 KiB
Bash
Executable File
95 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# Автоматическая проверка отказа Redis и PostgreSQL.
|
|
# Падает с ненулевым кодом, если ready/защищённый запрос ведут себя неверно.
|
|
# Контейнеры в конце снова подняты. Токены в stdout не печатаются.
|
|
set -eu
|
|
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
|
|
cd "$ROOT"
|
|
BASE=${BASE_URL:-http://127.0.0.1:8080}
|
|
|
|
ready() {
|
|
curl -s -o /tmp/lab2-ready-body -w "%{http_code}" "$BASE/health/ready" || echo "000"
|
|
}
|
|
|
|
# Защищённый GET без печати токена. Ожидаем конкретный HTTP-код.
|
|
me_status() {
|
|
token=$1
|
|
curl -s -o /tmp/lab2-me-body -w "%{http_code}" \
|
|
-H "Authorization: Bearer ${token}" \
|
|
"$BASE/api/v1/users/me" || echo "000"
|
|
}
|
|
|
|
assert_eq() {
|
|
name=$1
|
|
got=$2
|
|
want=$3
|
|
echo "${name}=${got} (want ${want})"
|
|
if [ "$got" != "$want" ]; then
|
|
echo "FAIL: ${name}: got ${got}, want ${want}" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
wait_ready() {
|
|
i=0
|
|
while [ "$i" -lt 30 ]; do
|
|
code=$(ready)
|
|
if [ "$code" = "200" ]; then
|
|
echo "$code"
|
|
return 0
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 1
|
|
done
|
|
echo "$code"
|
|
return 1
|
|
}
|
|
|
|
# Синтетический пользователь только для проверки 503 на Bearer.
|
|
suffix=$(date +%s)
|
|
user_body=$(printf '{"username":"dep_%s","display_name":"Deps","password":"correct-horse-1"}' "$suffix")
|
|
reg=$(curl -s -o /tmp/lab2-reg.json -w "%{http_code}" -H 'Content-Type: application/json' \
|
|
-d "$user_body" "$BASE/api/v1/users")
|
|
assert_eq "register" "$reg" "201"
|
|
auth_hdr=$(printf 'Authorization: Basic %s' "$(printf 'dep_%s:correct-horse-1' "$suffix" | base64 | tr -d '\n')")
|
|
# Пустое тело (-d '') обязательно: иначе curl сделает GET и получит 405.
|
|
sess=$(curl -s -o /tmp/lab2-sess.json -w "%{http_code}" -X POST -d '' -H "$auth_hdr" \
|
|
"$BASE/api/v1/auth/sessions")
|
|
assert_eq "login" "$sess" "201"
|
|
token=$(python3 -c 'import json; print(json.load(open("/tmp/lab2-sess.json"))["token"])')
|
|
|
|
assert_eq "ready_before" "$(ready)" "200"
|
|
assert_eq "me_before" "$(me_status "$token")" "200"
|
|
|
|
echo "stop redis"
|
|
docker compose --env-file .env stop redis
|
|
sleep 1
|
|
assert_eq "ready_redis_down" "$(ready)" "503"
|
|
assert_eq "me_redis_down" "$(me_status "$token")" "503"
|
|
# Анонимный ответ недопустим: 200 с телом пользователя = провал.
|
|
if grep -q '"username"' /tmp/lab2-me-body 2>/dev/null; then
|
|
echo "FAIL: redis down returned user payload" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "start redis"
|
|
docker compose --env-file .env start redis
|
|
assert_eq "ready_redis_back" "$(wait_ready)" "200"
|
|
assert_eq "me_redis_back" "$(me_status "$token")" "200"
|
|
|
|
echo "stop postgres"
|
|
docker compose --env-file .env stop postgres
|
|
sleep 1
|
|
assert_eq "ready_postgres_down" "$(ready)" "503"
|
|
assert_eq "me_postgres_down" "$(me_status "$token")" "503"
|
|
if grep -q '"username"' /tmp/lab2-me-body 2>/dev/null; then
|
|
echo "FAIL: postgres down returned user payload" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "start postgres"
|
|
docker compose --env-file .env start postgres
|
|
assert_eq "ready_postgres_back" "$(wait_ready)" "200"
|
|
assert_eq "me_postgres_back" "$(me_status "$token")" "200"
|
|
echo "PASS: dependency outages return 503 and recover"
|