import concurrent.futures import json from pathlib import Path import sys import time import uuid from acceptance import ROOT, docker, internal sys.path.insert(0, str(ROOT / 'tests')) from client import Client, check def main(): c = Client(timeout=40) alice, bob, _, chat = c.bootstrap() attachment, original = c.attachment(alice, chat) message = c.message(alice, chat, '', attachments=[attachment['id']])['json'] internal("""import psycopg from src.migrate import database_url with psycopg.connect(database_url()) as db: db.execute("UPDATE attachments SET created_at=clock_timestamp()-interval '2 hours' WHERE id=%s", ('""" + attachment['id'] + """',)) db.execute("ALTER TABLE attachments ADD CONSTRAINT injected_database_failure CHECK(filename <> 'reject-db.png') NOT VALID") """) def count(): return int(internal("from src.storage import client,BUCKET\nprint(sum(len(p.get('Contents',[])) for p in client().get_paginator('list_objects_v2').paginate(Bucket=BUCKET)))")) before = count() try: c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=reject-db.png', 503, actor=alice, raw=original, headers={'Content-Type': 'image/png'}) after = count() check(before == after, 'S3 compensation left an orphan') finally: internal("import psycopg\nfrom src.migrate import database_url\nwith psycopg.connect(database_url()) as db:\n db.execute('ALTER TABLE attachments DROP CONSTRAINT injected_database_failure')") results = {'database_failure_after_put': {'before_objects': before, 'after_objects': after, 'http_status': 503}, 'scan_cost': []} prefix = 'originals/study-' + uuid.uuid4().hex + '-' try: for size in (10, 100): internal("""import concurrent.futures from src.storage import client,BUCKET s=client() with concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool: list(pool.map(lambda i:s.put_object(Bucket=BUCKET,Key='""" + prefix + """'+str(i),Body=b'fresh'),range(""" + str(size) + """))) """) observations = [] for _ in range(3): observations.append(json.loads(internal('from src.reconcile import run\nimport json\nprint(json.dumps(run(False,3600)))'))) results['scan_cost'].append({'extra_objects': size, 'repeats': observations}) applied = json.loads(internal('from src.reconcile import run\nimport json\nprint(json.dumps(run(True,3600)))')) check(c.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob)['bytes'] == original, 'Used old attachment removed') check(c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json']['items'][0] == message, 'Message reference changed') results['old_used_attachment'] = {'checksum_matches': True, 'message_reference_preserved': True, 'apply': applied} finally: internal("""from src.storage import client,BUCKET s=client() for p in s.get_paginator('list_objects_v2').paginate(Bucket=BUCKET,Prefix='""" + prefix + """'): for o in p.get('Contents',[]): s.delete_object(Bucket=BUCKET,Key=o['Key']) """) (ROOT / 'evidence/consistency.json').write_text(json.dumps(results, indent=2), encoding='utf-8') print('PASS: failure after S3 PUT compensates, old used attachment survives, scan cost measured twice with three repeats') if __name__ == '__main__': main()