forked from mtusi2026/mtusi2026lab4
66 lines
3.5 KiB
Python
66 lines
3.5 KiB
Python
"""lab4, уровень 3: недоступность S3.
|
||
|
||
make failure-s3 (= python student-tests/failure_s3.py)
|
||
|
||
Останавливает контейнер s3 (SeaweedFS) и проверяет:
|
||
- загрузка -> 503 unavailable за несколько секунд (таймауты S3), не 500 и не зависание;
|
||
- метаданные готового вложения читаются (они в PostgreSQL), а байты -> 503;
|
||
- текстовые сообщения и история работают, /health/ready = 200: S3 не критичен для обязательного
|
||
текстового пути, поэтому реплики не выводятся из балансировки;
|
||
после запуска S3 тот же файл скачивается без изменений, новая загрузка снова 201.
|
||
"""
|
||
import hashlib
|
||
import time
|
||
|
||
from filegen import SAMPLE_PNG, upload_when_ready, wait_until
|
||
from helpers import Client, Failure, check, idempotency, run
|
||
from stand import compose_quiet, ps_table
|
||
|
||
|
||
def main():
|
||
api = Client()
|
||
alice, bob, _eve, chat = api.bootstrap()
|
||
upload_path = f"/api/v1/chats/{chat['id']}/attachments?filename=before.png"
|
||
att = upload_when_ready(api, alice, chat, name="before.png")["json"]
|
||
content = f"/api/v1/attachments/{att['id']}/content"
|
||
compose_quiet("stop", "s3")
|
||
try:
|
||
print("=== s3 stopped")
|
||
print(ps_table())
|
||
started = time.monotonic()
|
||
failed = api.call("POST", upload_path, 503, actor=alice, raw=SAMPLE_PNG, headers={"Content-Type": "image/png"})
|
||
print(f"upload while S3 is down: {failed['status']} {failed['json']['error']['code']} "
|
||
f"in {time.monotonic() - started:.2f} s")
|
||
meta = api.call("GET", f"/api/v1/attachments/{att['id']}", actor=bob, schema="Attachment")
|
||
print(f"metadata of the ready attachment (PostgreSQL): {meta['status']}")
|
||
got = api.call("GET", content, 503, actor=bob)
|
||
print(f"download while S3 is down: {got['status']} {got['json']['error']['code']}")
|
||
sent = api.call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, actor=alice,
|
||
body={"text": "S3 недоступен, текст работает"}, headers=idempotency(), schema="Message")
|
||
history = api.call("GET", f"/api/v1/chats/{chat['id']}/messages", actor=bob, schema="MessagePage")
|
||
ready = api.call("GET", "/health/ready", schema="Health")
|
||
print(f"text message: {sent['status']}; history: {history['status']}; /health/ready: {ready['status']}")
|
||
finally:
|
||
compose_quiet("start", "s3")
|
||
print("=== s3 started")
|
||
|
||
def downloaded():
|
||
try:
|
||
return api.call("GET", content, actor=bob)["bytes"] == SAMPLE_PNG
|
||
except Failure:
|
||
return False
|
||
|
||
started = time.monotonic()
|
||
wait_until(downloaded, 60, "the old file is not downloadable after S3 restart")
|
||
print(f"the file uploaded before the outage is back after {time.monotonic() - started:.1f} s, same bytes "
|
||
f"(sha256 {hashlib.sha256(SAMPLE_PNG).hexdigest()[:16]}...)")
|
||
api.call("POST", upload_path, 201, actor=alice, raw=SAMPLE_PNG, headers={"Content-Type": "image/png"},
|
||
schema="Attachment")
|
||
print(ps_table())
|
||
print("PASS: S3 down -> upload/download 503 (no 500), metadata, text messages and readiness keep working; "
|
||
"after S3 is back the original is intact and uploads work again")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
run(main)
|