Files
mtusi2026lab6/scripts/test-worker-outage.py
T
2026-11-20 17:52:56 +03:00

50 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Check that an upload survives a temporary worker outage."""
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, arguments, check, run
def compose(action):
try:
subprocess.run(['docker', 'compose', action, 'worker'], cwd=ROOT, check=True)
except subprocess.CalledProcessError as exc:
raise SystemExit(f'docker compose {action} worker failed ({exc.returncode})') from None
def main():
args = arguments(__doc__)
client = Client(args.base_url, args.ca_file)
alice, _, _, chat = client.bootstrap()
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
try:
compose('stop')
upload = client.call(
'POST', f'/api/v1/chats/{chat["id"]}/attachments?filename=sample.png',
expected=202, actor=alice, raw=content,
headers={'Content-Type': 'image/png'}, schema='Attachment',
)
trace_id = upload['headers'].get('X-Trace-Id')
check(bool(trace_id), 'missing upload trace ID')
attachment = upload['json']
path = f'/api/v1/attachments/{attachment["id"]}'
metadata = client.call('GET', path, actor=alice, schema='Attachment')['json']
check(metadata['status'] == 'queued', 'expected queued while worker is stopped')
print(f'worker stopped, attachment queued, trace_id={trace_id}', flush=True)
finally:
compose('start')
client.wait_ready(alice, attachment)
original = client.call('GET', f'{path}/content', actor=alice)['bytes']
check(original == content, 'downloaded original differs from the upload')
print(f'PASS: attachment ready, original verified, trace_id={trace_id}')
if __name__ == '__main__':
run(main)