Files
mtusi2026lab6/student-tests/retry_evidence.py
T
2026-11-06 11:12:38 +03:00

31 lines
1.9 KiB
Python

"""Crash one idle lab worker after S3 writes; restore normal configuration in finally."""
import json
import os
from pathlib import Path
import subprocess
import sys
import time
ROOT=Path(__file__).resolve().parents[1]
def compose(*args, env=None):
return subprocess.run(['docker','compose',*args],cwd=ROOT,env=env,check=True,capture_output=True,text=True).stdout
count=compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-Atc',"SELECT count(*) FROM attachment_jobs WHERE status IN ('queued','processing')")
if int(count.strip()):raise RuntimeError('Pending work; refusing crash injection')
try:
compose('stop','-t','5','worker')
compose('up','-d','--no-deps','worker',env={**os.environ,'WORKER_FAULT_AFTER_S3_PUT':'1'})
subprocess.run([sys.executable,'student-tests/trace_retry.py'],cwd=ROOT,check=True,timeout=180)
out=json.loads((ROOT/'evidence/review-retry-job.json').read_text())
from uuid import UUID
identity=str(UUID(out['attachment_id']))
out['db_attempt']=int(compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-Atc',f"SELECT attempt FROM attachment_jobs WHERE attachment_id='{identity}'").strip())
cid=compose('ps','-q','worker').strip()
out['restart_count']=int(subprocess.run(['docker','inspect','--format','{{.RestartCount}}',cid],check=True,capture_output=True,text=True).stdout.strip())
assert out['db_attempt']==2 and out['restart_count']==1,out
(ROOT/'evidence/review-retry-job.json').write_text(json.dumps(out,indent=2)+'\n')
time.sleep(10)
subprocess.run([sys.executable,'student-tests/capture_observability.py','--trace-id',out['trace_id'],'--output','evidence/review-retry-trace.json'],cwd=ROOT,check=True)
finally:
compose('stop','-t','5','worker')
compose('up','-d','--no-deps','worker',env={**os.environ,'WORKER_FAULT_AFTER_S3_PUT':'0'})
print('PASS: attempt 2, one restart, normal worker restored')