#!/usr/bin/env python3 """Verify explicit replay changes the deployed pipeline while preserving the original.""" 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 MARK_FAILED_CODE = """ import sys from src.store import Store store = Store() store.open() try: with store.pool.connection() as conn: updated = conn.execute( "UPDATE attachments SET status = 'failed', error_code = 'injected_test_failure', " "variants = '[]'::jsonb, lease_until = NULL WHERE id = %s AND status = 'ready'", (sys.argv[1],), ) if updated.rowcount != 1: raise RuntimeError("test attachment was not ready for failed replay injection") finally: store.close() """ AUDIT_CODE = """ import json import sys from src.store import Store store = Store() store.open() try: with store.pool.connection() as conn: rows = conn.execute( "SELECT prior_version, pipeline_version, action FROM attachment_pipeline_events " "WHERE attachment_id = %s ORDER BY id", (sys.argv[1],), ).fetchall() print(json.dumps(rows, sort_keys=True)) finally: store.close() """ def docker_exec(container_id, *args): return subprocess.run( ["docker", "exec", container_id, *args], cwd=ROOT, check=True, capture_output=True, text=True, ) def replay(container_id, attachment_id, version): result = docker_exec( container_id, "python3", "-m", "src.replay_attachment", "--attachment-id", attachment_id, "--pipeline-version", str(version), ) return json.loads(result.stdout) def main(): args = arguments("Verify versioned attachment replay against the running Compose stack") client = Client(args.base_url, args.ca_file) alice, _, _, chat = client.bootstrap() attachment, original = client.attachment(alice, chat) ready_v1 = client.wait_ready(alice, attachment) v1_thumbnail = next(item for item in ready_v1["variants"] if item["name"] == "thumbnail") v1_bytes = client.call( "GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", actor=alice, )["bytes"] check(hashlib.sha256(v1_bytes).hexdigest() == v1_thumbnail["sha256"], "v1 thumbnail checksum mismatch") containers = subprocess.run( ["docker", "compose", "ps", "--quiet", "--status", "running", "api"], cwd=ROOT, check=True, capture_output=True, text=True, ).stdout.split() check(bool(containers), "no running API container found") replay_v2 = replay(containers[0], attachment["id"], 2) check(replay_v2["status"] == "queued", "replay was not queued") check(replay_v2["pipeline_version"] == 2, "replay did not select pipeline v2") check(replay_v2["job_id"] != replay_v2["previous_job_id"], "replay reused the previous logical job id") ready_v2 = client.wait_ready(alice, ready_v1) v2_thumbnail = next(item for item in ready_v2["variants"] if item["name"] == "thumbnail") v2_bytes = client.call( "GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", actor=alice, )["bytes"] original_bytes = client.call( "GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=alice, )["bytes"] check(v2_thumbnail["sha256"] != v1_thumbnail["sha256"], "pipeline v2 did not change thumbnail encoding") check(hashlib.sha256(v2_bytes).hexdigest() == v2_thumbnail["sha256"], "v2 thumbnail checksum mismatch") check(original_bytes == original, "pipeline replay changed the original file") docker_exec( containers[0], "python3", "-c", MARK_FAILED_CODE, attachment["id"], ) failed_replay = replay(containers[0], attachment["id"], 2) check(failed_replay["status"] == "queued", "failed attachment replay was not queued") check(failed_replay["pipeline_version"] == 2, "failed replay changed pipeline version") check(failed_replay["job_id"] != failed_replay["previous_job_id"], "failed replay reused a job id") ready_replayed = client.wait_ready(alice, ready_v2) audit = json.loads( docker_exec( containers[0], "python3", "-c", AUDIT_CODE, attachment["id"], ).stdout ) check( [(row["prior_version"], row["pipeline_version"], row["action"]) for row in audit] == [(1, 2, "replay"), (2, 2, "replay")], "pipeline replay audit trail is incomplete", ) check(ready_replayed["status"] == "ready", "failed attachment did not recover") print( json.dumps( { "result": "PASS", "previous_job_id": replay_v2["previous_job_id"], "job_id": replay_v2["job_id"], "pipeline_version": replay_v2["pipeline_version"], "failed_replay_job_id": failed_replay["job_id"], "audited_replays": len(audit), "v1_thumbnail_sha256": v1_thumbnail["sha256"], "v2_thumbnail_sha256": v2_thumbnail["sha256"], "original_sha256": hashlib.sha256(original_bytes).hexdigest(), }, sort_keys=True, ) ) if __name__ == "__main__": run(main)