#!/usr/bin/env python3 """Destructive only to this repository's disposable lab services; volumes preserved.""" import json import os from pathlib import Path import subprocess import sys import time import uuid ROOT = Path(__file__).resolve().parents[1] os.chdir(ROOT) sys.path.insert(0, str(ROOT / 'tests')) from client import Client, Failure def compose(*args): return subprocess.check_output(['docker', 'compose', *args], text=True).strip() def wait_ready(client): deadline = time.monotonic() + 45 while True: try: client.call('GET', '/health/ready', schema='Health') return except Failure: if time.monotonic() > deadline: raise time.sleep(0.3) def main(): c = Client(timeout=5) alice, bob, eve, chat = c.bootstrap() result = {} query = "SELECT count(*) FROM chats;" before = compose('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', query) c.call('POST', '/api/v1/chats', 400, actor=alice, body={'title': 'must rollback', 'member_ids': [alice['id'], str(uuid.uuid4())]}) after = compose('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', query) assert before == after, 'failed chat left an orphan' result['composite_operation_rollback'] = True # Force a miss and observe the actual user cache and TTL, without printing its values. compose('exec', '-T', 'redis', 'redis-cli', 'DEL', 'user:' + alice['id']) c.call('GET', '/api/v1/users/me', actor=alice) ttl = int(compose('exec', '-T', 'redis', 'redis-cli', 'TTL', 'user:' + alice['id'])) assert 0 < ttl <= 60 result['redis_user_cache_ttl_seconds'] = ttl compose('stop', 'redis') try: started = time.monotonic() c.call('GET', '/api/v1/users/me', actor=alice) c.message(alice, chat, 'Redis unavailable; PostgreSQL remains authoritative') c.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Bearer ' + 'x' * 43}) c.call('GET', '/health/ready', schema='Health') result['redis_outage_seconds'] = time.monotonic() - started finally: compose('up', '-d', '--wait', 'redis') c.call('GET', '/api/v1/users/me', actor=alice) result['redis_recovered_without_relogin'] = True compose('stop', 'postgres') try: started = time.monotonic() c.call('GET', '/api/v1/users/me', 503, actor=alice) result['postgres_outage_response_seconds'] = time.monotonic() - started assert result['postgres_outage_response_seconds'] < 4 c.call('GET', '/health/ready', 503) c.call('GET', '/health/live', schema='Health') finally: compose('up', '-d', '--wait', 'postgres') wait_ready(c) c.call('GET', '/api/v1/users/me', actor=alice) result['postgres_recovered_without_relogin'] = True # A separate short-TTL API uses the same authoritative PostgreSQL without changing the main API. name = 'mtusi-lab2-expiry-' + uuid.uuid4().hex[:8] compose('run', '-d', '--no-deps', '--name', name, '-e', 'SESSION_TTL_SECONDS=2', '-p', '127.0.0.1:18083:8080', 'api') try: short = Client('http://127.0.0.1:18083') wait_ready(short) actor = short.user('expiry') short.call('GET', '/api/v1/users/me', actor=actor) time.sleep(2.2) short.call('GET', '/api/v1/users/me', 401, actor=actor) result['short_ttl_expired'] = True finally: subprocess.run(['docker', 'rm', '-f', name], check=True, stdout=subprocess.DEVNULL) (ROOT / 'evidence/dependencies.json').write_text(json.dumps(result, indent=2)) print('PASS: rollback, real Redis TTL/cache, Redis outage, PostgreSQL outage/recovery and session expiry.') if __name__ == '__main__': main()