#!/usr/bin/env python3 """Verify queued uploads and message links survive an API/worker restart.""" import hashlib import json import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, arguments, check, run def compose(*args): return subprocess.run( ["docker", "compose", *args], cwd=ROOT, check=True, capture_output=True, text=True, ) def main(): args = arguments("Verify queued attachment recovery after API and worker restart") client = Client(args.base_url, args.ca_file) alice, bob, _, chat = client.bootstrap() compose("stop", "worker") worker_stopped = True try: attachment, original = client.attachment(alice, chat) check(attachment["status"] == "queued", "upload was not accepted while worker was stopped") message = client.message(alice, chat, "", attachments=[attachment["id"]])["json"] check(message["attachment_ids"] == [attachment["id"]], "queued attachment was not linked to message") client.call( "GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=bob, ) compose("restart", "api") compose("start", "worker") worker_stopped = False client.call("GET", "/health/ready", schema="Health") ready = client.wait_ready(bob, attachment) original_bytes = client.call( "GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=bob, )["bytes"] check(original_bytes == original, "recovered worker changed original bytes") page = client.call( "GET", "/api/v1/chats/" + chat["id"] + "/messages", actor=bob, schema="MessagePage", )["json"] recovered_message = next(item for item in page["items"] if item["id"] == message["id"]) check(recovered_message["attachment_ids"] == [attachment["id"]], "message lost attachment link") print( json.dumps( { "result": "PASS", "initial_status": attachment["status"], "final_status": ready["status"], "message_attachment_link_survived": True, "original_sha256": hashlib.sha256(original_bytes).hexdigest(), }, sort_keys=True, ) ) finally: if worker_stopped: compose("start", "worker") if __name__ == "__main__": run(main)