forked from mtusi2026/mtusi2026lab2
157 lines
5.6 KiB
Python
157 lines
5.6 KiB
Python
"""Автотест отказа PostgreSQL.
|
|
|
|
Проверяет, что при остановленной базе API:
|
|
|
|
* отвечает за ограниченное время (не «висит»);
|
|
* отдаёт 503 в общем формате ошибки, а не 500 и не пустое соединение;
|
|
* /health/live остаётся 200 (процесс жив), /health/ready — 503;
|
|
* анонимный запрос по-прежнему получает 401 (аутентификация до БД);
|
|
* после возврата базы восстанавливается: readiness 200, прежний Bearer-токен
|
|
работает, подтверждённое до отказа сообщение читается.
|
|
|
|
Запуск (стек должен быть поднят):
|
|
|
|
python student-tests/test_postgres_down.py
|
|
"""
|
|
import base64
|
|
import uuid
|
|
|
|
from _harness import (
|
|
assert_error_payload,
|
|
assert_within_budget,
|
|
call,
|
|
container_id,
|
|
start_service,
|
|
stop_service,
|
|
wait_healthy,
|
|
wait_ready,
|
|
)
|
|
|
|
|
|
def register_and_login(username, password):
|
|
status, payload, _ = call(
|
|
"POST",
|
|
"/api/v1/users",
|
|
body={
|
|
"username": username,
|
|
"display_name": "Postgres Down Test",
|
|
"password": password,
|
|
},
|
|
)
|
|
assert status == 201, "register failed: %s %s" % (status, payload)
|
|
|
|
credential = base64.b64encode(
|
|
("%s:%s" % (username, password)).encode()
|
|
).decode()
|
|
status, payload, _ = call(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + credential},
|
|
)
|
|
assert status == 201, "login failed: %s %s" % (status, payload)
|
|
return payload["token"]
|
|
|
|
|
|
def main():
|
|
postgres = container_id("postgres")
|
|
wait_healthy(postgres)
|
|
wait_ready()
|
|
|
|
suffix = uuid.uuid4().hex[:8]
|
|
token = register_and_login("pgdown_" + suffix, "PgDownPass123")
|
|
auth = {"Authorization": "Bearer " + token}
|
|
|
|
status, user, _ = call("GET", "/api/v1/users/me", headers=auth)
|
|
assert status == 200, "token must work before the outage: %s %s" % (status, user)
|
|
|
|
status, chat, _ = call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
body={"title": "Postgres outage", "member_ids": [user["id"]]},
|
|
headers=auth,
|
|
)
|
|
assert status == 201, "chat creation failed: %s %s" % (status, chat)
|
|
|
|
status, message, _ = call(
|
|
"POST",
|
|
"/api/v1/chats/%s/messages" % chat["id"],
|
|
body={"text": "подтверждено до отказа"},
|
|
headers=auth,
|
|
)
|
|
assert status == 201, "message creation failed: %s %s" % (status, message)
|
|
print("Setup: user, chat and message created")
|
|
|
|
try:
|
|
stop_service("postgres")
|
|
print("PostgreSQL stopped")
|
|
|
|
status, payload, elapsed = call("GET", "/health/ready")
|
|
assert_within_budget(elapsed, "readiness with PostgreSQL down")
|
|
assert status == 503, "readiness must be 503, got %s %s" % (status, payload)
|
|
assert_error_payload(payload, "unavailable")
|
|
print("readiness: 503 in %.2fs (budget %.0fs)" % (elapsed, 6.0))
|
|
|
|
status, payload, elapsed = call("GET", "/health/live")
|
|
assert status == 200, "liveness must stay 200, got %s" % status
|
|
print("liveness: 200")
|
|
|
|
status, payload, elapsed = call(
|
|
"GET", "/api/v1/users/me", headers=auth
|
|
)
|
|
assert_within_budget(elapsed, "authenticated request with PostgreSQL down")
|
|
assert status == 503, (
|
|
"authenticated request must be 503, got %s %s" % (status, payload)
|
|
)
|
|
assert_error_payload(payload, "unavailable")
|
|
print("authenticated request: 503 in %.2fs" % elapsed)
|
|
|
|
status, payload, elapsed = call("GET", "/api/v1/users/me")
|
|
assert status == 401, (
|
|
"anonymous request must stay 401, got %s %s" % (status, payload)
|
|
)
|
|
assert_within_budget(elapsed, "anonymous request with PostgreSQL down")
|
|
print("anonymous request: 401 (no trust granted without token)")
|
|
|
|
status, payload, elapsed = call(
|
|
"POST",
|
|
"/api/v1/chats/%s/messages" % chat["id"],
|
|
body={"text": "во время отказа"},
|
|
headers=auth,
|
|
)
|
|
assert status == 503, (
|
|
"write during outage must be 503, got %s %s" % (status, payload)
|
|
)
|
|
assert_within_budget(elapsed, "write with PostgreSQL down")
|
|
print("write during outage: 503 in %.2fs, message not acknowledged" % elapsed)
|
|
finally:
|
|
start_service("postgres")
|
|
wait_healthy(postgres)
|
|
print("PostgreSQL restarted")
|
|
|
|
wait_ready()
|
|
print("readiness after recovery: 200")
|
|
|
|
status, payload, _ = call("GET", "/api/v1/users/me", headers=auth)
|
|
assert status == 200, (
|
|
"existing token must work after recovery, got %s %s" % (status, payload)
|
|
)
|
|
print("existing token after recovery: 200")
|
|
|
|
status, page, _ = call(
|
|
"GET", "/api/v1/chats/%s/messages?limit=100" % chat["id"], headers=auth
|
|
)
|
|
assert status == 200, "history read failed after recovery: %s" % status
|
|
identifiers = [item["id"] for item in page["items"]]
|
|
assert message["id"] in identifiers, "confirmed message disappeared after outage"
|
|
assert len(identifiers) == len(set(identifiers)), "history contains duplicates"
|
|
assert "во время отказа" not in [item["text"] for item in page["items"]], (
|
|
"unacknowledged message must not appear in history"
|
|
)
|
|
print("confirmed message preserved, unacknowledged write absent")
|
|
|
|
print("PASS: PostgreSQL outage handled with bounded 503 and full recovery")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|