"""lab4, уровень 3: upload -> остановка ОБСЛУЖИВШЕЙ загрузку реплики -> download через другую. make attachment-failover (= python student-tests/attachment_failover.py) Доказывает, что оригинал не лежит на диске реплики: реплика, принявшая файл, остановлена (`docker stop`), а другая реплика отдаёт тот же файл байт в байт (сравнение SHA-256). В конце остановленная реплика запускается обратно (`docker start`). """ import hashlib from filegen import upload_when_ready, wait_until from helpers import Client, check, run from stand import api_containers, docker, lb_summary, lb_up_count, ps_table, wait_balanced def main(): api = Client() replicas = wait_balanced(2) alice, bob, _eve, chat = api.bootstrap() reply = upload_when_ready(api, alice, chat, name="failover.png") attachment, served = reply["json"], reply["headers"]["X-Instance-Id"] target = [c for c in replicas if c.startswith(served)] check(len(target) == 1, f"X-Instance-Id {served} does not match a running api container") print(f"uploaded {attachment['size_bytes']} bytes, sha256 {attachment['sha256'][:16]}..., " f"served by api replica {served}") print(ps_table()) docker("stop", target[0]) try: wait_until(lambda: lb_up_count() == len(replicas) - 1, 30, "HAProxy did not remove the stopped replica") print(f"stopped {served} (docker stop); HAProxy: {lb_summary()}") seen = set() for _ in range(4): got = api.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob) check(hashlib.sha256(got["bytes"]).hexdigest() == attachment["sha256"], "downloaded file differs") seen.add(got["headers"]["X-Instance-Id"]) check(served not in seen, "the stopped replica still answered") print(f"downloaded 4 times through {sorted(seen)}: same SHA-256 as uploaded") finally: docker("start", target[0]) wait_balanced(len(replicas)) print(f"restored {served}; HAProxy: {lb_summary()}") print(ps_table()) print("PASS: the replica that accepted the upload was stopped; another replica returned the same bytes " "(the original lives in S3, not on the replica)") if __name__ == "__main__": run(main)