79 lines
3.3 KiB
Python
79 lines
3.3 KiB
Python
"""Crash a worker after the durable result commit and verify safe redelivery."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
|
|
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 main() -> None:
|
|
args = arguments(__doc__, lambda parser: parser.add_argument("--output", default="evidence/worker-redelivery.json"))
|
|
client = Client(args.base_url, args.ca_file)
|
|
compose("stop", "worker")
|
|
compose("up", "-d", "dispatcher")
|
|
try:
|
|
alice, _bob, _eve, chat = client.bootstrap()
|
|
attachment, _content = client.attachment(alice, chat)
|
|
|
|
crashed_worker = compose(
|
|
"run", "--rm", "--no-deps", "-e", "WORKER_FAILPOINT_AFTER_COMMIT_ATTACHMENT_ID=" + attachment["id"],
|
|
"--entrypoint", "python", "worker", "-m", "src.app.worker", "--once", "--idle-ms", "0",
|
|
expected=75,
|
|
timeout=120,
|
|
)
|
|
durable = client.call("GET", "/api/v1/attachments/" + attachment["id"], actor=alice, schema="Attachment")["json"]
|
|
if durable["status"] != "ready":
|
|
raise Failure("worker failpoint did not occur after the ready result committed")
|
|
before = json.loads(json.dumps(durable["variants"], sort_keys=True))
|
|
|
|
reclaimed_worker = compose(
|
|
"run", "--rm", "--no-deps", "--entrypoint", "python", "worker",
|
|
"-m", "src.app.worker", "--once", "--idle-ms", "0", timeout=120,
|
|
)
|
|
after = client.call("GET", "/api/v1/attachments/" + attachment["id"], actor=alice, schema="Attachment")["json"]
|
|
if after["status"] != "ready" or json.loads(json.dumps(after["variants"], sort_keys=True)) != before:
|
|
raise Failure("redelivery changed the already committed image result")
|
|
client.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", actor=alice)
|
|
|
|
result = {
|
|
"result": "PASS",
|
|
"observed_at_utc": datetime.now(timezone.utc).isoformat(),
|
|
"attachment_id": attachment["id"],
|
|
"status_after_side_effect_commit": durable["status"],
|
|
"status_after_redelivery": after["status"],
|
|
"variants_unchanged_after_redelivery": True,
|
|
"worker_crash_exit": crashed_worker.returncode,
|
|
"redelivery_worker_exit": reclaimed_worker.returncode,
|
|
"ack_happened_after_committed_effect": True,
|
|
}
|
|
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", "worker")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|