103 lines
4.5 KiB
Python
103 lines
4.5 KiB
Python
"""Exercise a committed upload across API restart and a pre-publish crash."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Failure, arguments, run, Client # noqa: E402
|
|
|
|
|
|
def compose(*args: str, expected: int = 0, timeout: int = 90) -> subprocess.CompletedProcess:
|
|
result = subprocess.run(
|
|
["docker", "compose", "--env-file", ".env", *args],
|
|
cwd=ROOT,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=timeout,
|
|
)
|
|
if result.returncode != expected:
|
|
operation = args[0] if args else "compose"
|
|
raise Failure("Docker Compose operation failed: " + operation + " (exit " + str(result.returncode) + ")")
|
|
return result
|
|
|
|
|
|
def wait_http(client: Client, path: str = "/health/ready", seconds: int = 60) -> None:
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
client.call("GET", path, schema="Health")
|
|
return
|
|
except Failure:
|
|
time.sleep(1)
|
|
raise Failure("API did not become ready after restart")
|
|
|
|
|
|
def main() -> None:
|
|
args = arguments(__doc__, lambda parser: parser.add_argument("--output", default="evidence/outbox-recovery.json"))
|
|
client = Client(args.base_url, args.ca_file)
|
|
compose("stop", "dispatcher", "worker")
|
|
try:
|
|
alice, _bob, _eve, chat = client.bootstrap()
|
|
attachment, _content = client.attachment(alice, chat)
|
|
queued = client.call("GET", "/api/v1/attachments/" + attachment["id"], actor=alice, schema="Attachment")["json"]
|
|
if queued["status"] != "queued":
|
|
raise Failure("job should remain queued while dispatcher and worker are stopped")
|
|
message = client.message(alice, chat, "Message linked while processing is stopped", attachments=[attachment["id"]])["json"]
|
|
client.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=alice)
|
|
|
|
compose("restart", "api")
|
|
wait_http(client)
|
|
after_restart = client.call("GET", "/api/v1/attachments/" + attachment["id"], actor=alice, schema="Attachment")["json"]
|
|
if after_restart["status"] != "queued":
|
|
raise Failure("API restart changed or lost the committed queued job")
|
|
|
|
failpoint = compose(
|
|
"run", "--rm", "--no-deps", "-e", "DISPATCHER_FAILPOINT_ATTACHMENT_ID=" + attachment["id"],
|
|
"--entrypoint", "python", "dispatcher", "-m", "src.app.dispatcher", "--once",
|
|
expected=75,
|
|
)
|
|
compose("up", "-d", "dispatcher", "worker")
|
|
ready = client.wait_ready(alice, attachment, wait_seconds=90)
|
|
if ready["status"] != "ready":
|
|
raise Failure("outbox recovery did not finish the original attachment")
|
|
thumbnail = client.call(
|
|
"GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", actor=alice
|
|
)
|
|
if thumbnail["headers"].get_content_type() != "image/jpeg":
|
|
raise Failure("recovered job did not publish a JPEG thumbnail")
|
|
|
|
result = {
|
|
"result": "PASS",
|
|
"observed_at_utc": datetime.now(timezone.utc).isoformat(),
|
|
"scenario": [
|
|
"stopped dispatcher and worker",
|
|
"received 202 and persisted queued attachment plus job/outbox transaction",
|
|
"created a message referring to the queued attachment; content returned 409",
|
|
"restarted API and confirmed the same queued record remained",
|
|
"crashed one-shot dispatcher with exit 75 before Redis XADD",
|
|
"restarted dispatcher and worker; the same attachment became ready",
|
|
],
|
|
"attachment_id": attachment["id"],
|
|
"message_id": message["id"],
|
|
"status_after_api_restart": after_restart["status"],
|
|
"status_after_recovery": ready["status"],
|
|
"thumbnail_content_type": thumbnail["headers"].get_content_type(),
|
|
"dispatcher_failpoint_exit": failpoint.returncode,
|
|
}
|
|
output_path = ROOT / args.output
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
output_path.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
|
|
print(json.dumps(result, separators=(",", ":")))
|
|
finally:
|
|
compose("up", "-d", "dispatcher", "worker")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|