"""Exercise real HTTP client aborts and total receive deadlines, then inspect DB/S3.""" import argparse import base64 import json from pathlib import Path import subprocess import sys import uuid ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client, Failure, check def docker(*args, **kwargs): return subprocess.run(['docker', *args], cwd=ROOT, capture_output=True, text=True, check=True, timeout=180, **kwargs).stdout def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--targets', choices=['all', 'nginx', 'api'], default='all') args = parser.parse_args() client = Client('http://127.0.0.1:8080', timeout=10) run_id = str(uuid.uuid4()) actors, chat = [], None report = {'status': 'failed'} try: containers = docker('compose', 'ps', '-q', 'api').split() check(len(containers) == 2, 'Expected two API replicas') states = json.loads(docker('inspect', *containers)) targets = [] for state in states: env = dict(item.split('=', 1) for item in state['Config']['Env'] if '=' in item) timeout_ms = int(env.get('UPLOAD_BODY_TIMEOUT_MS', '15000')) address = next(iter(state['NetworkSettings']['Networks'].values()))['IPAddress'] targets.append({'name': state['Config']['Hostname'], 'url': 'http://' + address + ':8080', 'timeoutMs': timeout_ms}) check(len({target['timeoutMs'] for target in targets}) == 1, 'API receive deadlines differ') targets.append({'name': 'nginx', 'url': 'http://nginx', 'timeoutMs': targets[0]['timeoutMs']}) if args.targets != 'all': targets = [target for target in targets if (target['name'] == 'nginx') == (args.targets == 'nginx')] for suffix in ['a', 'b']: actors.append(client.user('cu' + run_id[:8] + suffix)) chat = client.call('POST', '/api/v1/chats', 201, actor=actors[0], schema='Chat', body={'title': 'upload-interruption:' + run_id, 'member_ids': [actor['id'] for actor in actors]})['json'] payload = {'chatId': chat['id'], 'ownerToken': actors[0]['token'], 'readerToken': actors[1]['token'], 'image': base64.b64encode((ROOT / 'tests/fixtures/sample.png').read_bytes()).decode(), 'targets': targets} source = (ROOT / 'test/upload-interruption.mjs').read_text(encoding='utf-8') result = subprocess.run(['docker', 'compose', 'exec', '-T', 'reconciler', 'node', '--input-type=module', '-e', source], cwd=ROOT, input=json.dumps(payload), text=True, capture_output=True, timeout=sum(target['timeoutMs'] for target in targets) / 1000 + 120) for line in result.stdout.splitlines(): if line.startswith('{'): item = json.loads(line) if item.get('event') == 'upload_interruption_summary': report = item elif line.startswith('PASS:'): print(line, flush=True) check(result.returncode == 0 and report.get('status') == 'passed', 'Upload interruption test failed; see evidence/upload-interruption.json') finally: try: source = (ROOT / 'test/concurrent-cleanup.mjs').read_text(encoding='utf-8') payload = {'runId': run_id, 'chatId': chat['id'] if chat else None, 'userIds': [actor['id'] for actor in actors], 'kind': 'upload-interruption'} docker('compose', 'exec', '-T', '--index', '1', 'api', 'node', '--input-type=module', '-e', source, input=json.dumps(payload)) report['fixturesRemoved'] = True except (OSError, subprocess.SubprocessError): report['fixturesRemoved'] = False if report['status'] == 'passed': report['status'] = 'failed' raise Failure('Upload interruption fixture cleanup failed') from None finally: (ROOT / 'evidence/upload-interruption.json').write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8') print('PASS: upload interruption and receive deadline; targets=' + args.targets + '; fixtures removed') if __name__ == '__main__': try: main() except (Failure, ValueError, OSError, subprocess.SubprocessError) as error: print('FAIL: ' + (str(error) if isinstance(error, Failure) else type(error).__name__), file=sys.stderr) sys.exit(1)