Files
mtusi2026lab3/scripts/check_dependencies.sh
T

84 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# Отказ Redis и PostgreSQL: анонимный запрос остаётся 401, живой Bearer не становится доверенным.
set -eu
cd "$(dirname "$0")/.."
mkdir -p evidence
OUT=evidence/dependencies.txt
: > "$OUT"
log() { printf '%s\n' "$1" | tee -a "$OUT"; }
code_of() {
# $1 name, $2 expected, $3 url, optional header in $4
if [ "$#" -eq 4 ]; then
got=$(curl -sS -o /tmp/lab2_dep_body -w '%{http_code}' -H "$4" "$3")
else
got=$(curl -sS -o /tmp/lab2_dep_body -w '%{http_code}' "$3")
fi
log "$1 $got"
if [ "$got" != "$2" ]; then
log "FAIL $1 want $2"
exit 1
fi
}
wait_ready() {
i=0
while [ "$i" -lt 90 ]; do
if curl -sf -o /dev/null http://127.0.0.1:8080/health/ready; then
return 0
fi
i=$((i + 1))
sleep 0.5
done
log "FAIL ready did not return"
exit 1
}
trap 'rm -f /tmp/lab2_user.json /tmp/lab2_login.json /tmp/lab2_dep_body' EXIT
wait_ready
user="dep$(date +%s)"
pass="Dep-check-$(date +%s)-xxxx"
reg=$(curl -sS -o /tmp/lab2_user.json -w '%{http_code}' \
-H 'Content-Type: application/json' \
-d "{\"username\":\"$user\",\"display_name\":\"Зависимость\",\"password\":\"$pass\"}" \
http://127.0.0.1:8080/api/v1/users)
if [ "$reg" != "201" ]; then
log "FAIL register $reg"
exit 1
fi
basic=$(printf '%s' "$user:$pass" | base64 -w 0)
login=$(curl -sS -o /tmp/lab2_login.json -w '%{http_code}' \
-X POST \
-H "Authorization: Basic $basic" \
http://127.0.0.1:8080/api/v1/auth/sessions)
if [ "$login" != "201" ]; then
log "FAIL login $login"
exit 1
fi
token=$(python3 -c 'import json; print(json.load(open("/tmp/lab2_login.json"))["token"])')
log "stop redis"
docker compose --env-file .env stop redis >> "$OUT"
code_of live_redis_down 200 http://127.0.0.1:8080/health/live
code_of ready_redis_down 503 http://127.0.0.1:8080/health/ready
code_of anon_redis_down 401 http://127.0.0.1:8080/api/v1/users/me
code_of bearer_redis_down 503 http://127.0.0.1:8080/api/v1/users/me "Authorization: Bearer $token"
log "start redis"
docker compose --env-file .env start redis >> "$OUT"
wait_ready
log "stop postgres"
docker compose --env-file .env stop postgres >> "$OUT"
code_of live_pg_down 200 http://127.0.0.1:8080/health/live
code_of ready_pg_down 503 http://127.0.0.1:8080/health/ready
code_of anon_pg_down 401 http://127.0.0.1:8080/api/v1/users/me
code_of bearer_pg_down 503 http://127.0.0.1:8080/api/v1/users/me "Authorization: Bearer $token"
log "start postgres"
docker compose --env-file .env start postgres >> "$OUT"
wait_ready
code_of bearer_after 200 http://127.0.0.1:8080/api/v1/users/me "Authorization: Bearer $token"
log "PASS dependencies"
rm -f /tmp/lab2_user.json /tmp/lab2_login.json /tmp/lab2_login_hdr /tmp/lab2_dep_body