#!/usr/bin/env python3 """Automated lab 2 level-4 checks; operates only on synthetic test records.""" import base64 import concurrent.futures import json import os from pathlib import Path import subprocess import time import urllib.error import urllib.request import uuid from client import Client, Failure, arguments, check, run ROOT = Path(__file__).resolve().parents[1] ENV_FILE = ROOT / '.env' def compose(*args, check_ok=True): result = subprocess.run(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT, capture_output=True, text=True) if check_ok and result.returncode: raise Failure('docker compose operation failed: ' + ' '.join(args)) return result def wait_ready(client, timeout=90): deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: client.call('GET', '/health/ready', schema='Health') return except Failure: time.sleep(0.5) raise Failure('API did not recover within the readiness deadline') def parse_env(raw): result = {} for line in raw.decode('ascii').splitlines(): if '=' in line and not line.lstrip().startswith('#'): key, value = line.split('=', 1) result[key] = value return result def write_env(values): ENV_FILE.write_text(''.join(f'{key}={value}\n' for key, value in values.items()), encoding='ascii') def registration_race(client): username = 'race_' + uuid.uuid4().hex[:16] body = {'username': username, 'display_name': 'Concurrent synthetic user', 'password': 'Race-Pass-' + uuid.uuid4().hex} def post_once(): request = urllib.request.Request(client.base + '/api/v1/users', data=json.dumps(body).encode('utf-8'), headers={'Content-Type': 'application/json'}, method='POST') opener = urllib.request.build_opener() try: response = opener.open(request, timeout=10) except urllib.error.HTTPError as error: response = error with response: return response.status with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: statuses = sorted(executor.map(lambda _: post_once(), range(2))) check(statuses == [201, 409], 'concurrent duplicate registration was not serialized') def list_all_chats(client, actor): path = '/api/v1/chats?limit=100' rows = [] while True: page = client.call('GET', path, actor=actor, schema='ChatPage')['json'] rows.extend(page['items']) if page['next_cursor'] is None: return rows from urllib.parse import quote path = '/api/v1/chats?limit=100&cursor=' + quote(page['next_cursor'], safe='') def database_sql(sql, variables=None): values = parse_env(ENV_FILE.read_bytes()) args = ['docker', 'compose', '--env-file', '.env', 'exec', '-T', 'db', 'psql', '-U', values['POSTGRES_USER'], '-d', values['POSTGRES_DB'], '-v', 'ON_ERROR_STOP=1'] for name, value in (variables or {}).items(): args.extend(['-v', name + '=' + value]) result = subprocess.run(args, cwd=ROOT, input=sql, capture_output=True, text=True) if result.returncode: raise Failure('database verification query failed') return result.stdout def internal_status(path, token=None): # During an unhealthy dependency state Docker Desktop may stop forwarding # the published host port. Probe the API loopback from its own container. code = """ import os, sys, urllib.request, urllib.error request = urllib.request.Request('http://127.0.0.1:8080' + sys.argv[1]) token = os.getenv('CHECK_TOKEN') if token: request.add_header('Authorization', 'Bearer ' + token) try: response = urllib.request.urlopen(request, timeout=30) print(response.status) except urllib.error.HTTPError as error: print(error.code) """ command = ['docker', 'compose', '--env-file', '.env', 'exec', '-T'] if token: command.extend(['-e', 'CHECK_TOKEN=' + token]) command.extend(['api', 'python', '-', path]) result = subprocess.run(command, cwd=ROOT, input=code, capture_output=True, text=True, timeout=32) if result.returncode: raise Failure('in-container dependency probe failed: ' + result.stderr[-500:]) return int(result.stdout.strip().splitlines()[-1]) def check_index_plan(client, actor): chat = client.call('POST', '/api/v1/chats', 201, actor=actor, body={'title': 'synthetic index plan', 'member_ids': [actor['id']]}, schema='Chat')['json'] seed_sql = """ INSERT INTO messages (id, chat_id, sender_id, text, created_at) SELECT gen_random_uuid()::text, :'chat_id', :'sender_id', 'plan-probe-' || n, CURRENT_TIMESTAMP - interval '12000 seconds' + n * interval '1 second' FROM generate_series(1, 12000) AS n; ANALYZE messages; SELECT count(*) FROM messages WHERE chat_id = :'chat_id'; """ seeded = database_sql(seed_sql, {'chat_id': chat['id'], 'sender_id': actor['id']}) check('12000' in {line.strip() for line in seeded.splitlines()}, 'expected 12,000 synthetic history rows') plan_sql = """ EXPLAIN (ANALYZE, BUFFERS) SELECT id, created_at FROM messages WHERE chat_id = :'chat_id' AND (created_at, id) > (CURRENT_TIMESTAMP - interval '6000 seconds', '') ORDER BY created_at, id LIMIT 50; """ plan = database_sql(plan_sql, {'chat_id': chat['id']}) check('ix_messages_chat_created_id' in plan, 'keyset history plan did not use the composite index') evidence = ROOT / 'evidence' / 'history-plan.txt' evidence.write_text('Synthetic chat rows: 12000\nQuery: keyset range on (created_at, id), LIMIT 50\n\n' + plan, encoding='utf-8') page = client.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=50', actor=actor, schema='MessagePage')['json'] check(len(page['items']) == 50 and page['next_cursor'], 'keyset history did not return a next page') def check_dependency_failures(client, actor): compose('stop', 'redis') try: check(internal_status('/health/ready') == 503, 'readiness did not fail when Redis was down') check(internal_status('/api/v1/chats', actor['token']) == 503, 'authenticated request did not fail in a bounded way when Redis was down') finally: compose('start', 'redis') wait_ready(client) # Redis AOF plus PostgreSQL source of truth must preserve/rebuild this session. client.call('GET', '/api/v1/users/me', actor=actor, schema='User') compose('stop', 'db') try: check(internal_status('/health/ready') == 503, 'readiness did not fail when PostgreSQL was down') check(internal_status('/api/v1/chats', actor['token']) == 503, 'authenticated request did not fail when PostgreSQL was down') finally: compose('start', 'db') wait_ready(client) client.call('GET', '/api/v1/users/me', actor=actor, schema='User') def check_short_ttl(client): saved_env = ENV_FILE.read_bytes() values = parse_env(saved_env) try: values['SESSION_TTL_SECONDS'] = '2' write_env(values) compose('up', '-d', '--force-recreate', 'api') wait_ready(client) actor = client.user('expiry') time.sleep(3) client.call('GET', '/api/v1/users/me', 401, actor=actor) finally: ENV_FILE.write_bytes(saved_env) compose('up', '-d', '--force-recreate', 'api') wait_ready(client) def main(): args = arguments('Lab 2 level-4 concurrency, rollback, expiry, failure, and index checks') client = Client(args.base_url, args.ca_file, timeout=5) wait_ready(client) alice, bob, _eve, chat = client.bootstrap() bad = base64.b64encode((alice['registration']['username'] + ':wrong-password').encode()).decode() client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + bad}) registration_race(client) before = {item['id'] for item in list_all_chats(client, alice)} client.call('POST', '/api/v1/chats', 400, actor=alice, body={'title': 'must rollback', 'member_ids': [alice['id'], str(uuid.uuid4())]}) after = {item['id'] for item in list_all_chats(client, alice)} check(before == after, 'failed multi-row chat creation left a partial chat') cache_size = compose('exec', '-T', 'redis', 'redis-cli', 'DBSIZE').stdout.strip() check(cache_size.isdigit() and int(cache_size) > 0, 'Redis does not contain active session keys') check_dependency_failures(client, bob) check_index_plan(client, alice) logout_actor = client.user('logout') client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=logout_actor) client.call('GET', '/api/v1/users/me', 401, actor=logout_actor) check_short_ttl(client) print('PASS: level-4 Redis TTL/recovery, dependency failures, expiry/logout/bad password, ' 'registration race, chat rollback, and indexed 12,000-row history.') if __name__ == '__main__': run(main)