Files
mtusi2026lab2/tests/operational.py
T

198 lines
7.5 KiB
Python

#!/usr/bin/env python3
"""Destructive local Compose checks for cache, outages, races, and rollback."""
import base64
import json
import os
from pathlib import Path
import subprocess
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from client import Client, Failure, check, run
ROOT = Path(__file__).resolve().parents[1]
def compose(*args, env=None, capture=False):
result = subprocess.run(
['docker', 'compose', '--env-file', '.env', *args],
cwd=ROOT,
env=env,
check=False,
text=True,
stdout=subprocess.PIPE if capture else subprocess.DEVNULL,
stderr=subprocess.PIPE if capture else subprocess.DEVNULL,
)
check(result.returncode == 0, 'docker compose command failed: ' + ' '.join(args))
return result.stdout.strip() if capture else ''
def psql(sql, capture=False):
result = subprocess.run(
[
'docker', 'compose', '--env-file', '.env', 'exec', '-T', 'postgres',
'sh', '-lc', 'psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At'
],
cwd=ROOT,
input=sql,
text=True,
check=False,
stdout=subprocess.PIPE if capture else subprocess.DEVNULL,
stderr=subprocess.PIPE,
)
check(result.returncode == 0, 'PostgreSQL probe failed')
return result.stdout.strip() if capture else ''
def wait_ready(client, seconds=90):
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
try:
client.call('GET', '/health/ready', schema='Health')
return
except Failure:
time.sleep(0.5)
raise Failure('API did not become ready')
def basic(username, password):
value = base64.b64encode((username + ':' + password).encode()).decode()
return {'Authorization': 'Basic ' + value}
def main():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--base-url', default='http://localhost:8080')
args = parser.parse_args()
client = Client(args.base_url)
normal_env = dict(os.environ)
normal_env.pop('SESSION_TTL_SECONDS', None)
short_env = dict(normal_env)
short_env['SESSION_TTL_SECONDS'] = '2'
trigger_installed = False
try:
compose('up', '-d', '--force-recreate', '--no-deps', '--wait', 'api', env=short_env)
expiring = client.user('expiry')
client.call('GET', '/api/v1/users/me', actor=expiring, schema='User')
time.sleep(2.5)
client.call('GET', '/api/v1/users/me', 401, actor=expiring)
compose('up', '-d', '--force-recreate', '--no-deps', '--wait', 'api', env=normal_env)
wait_ready(client)
actor = client.user('operational')
password = actor['registration']['password']
client.call(
'POST', '/api/v1/users', 400,
body={
'username': 'blank_password_' + uuid.uuid4().hex[:12],
'display_name': 'Blank password probe',
'password': ' ',
})
client.call(
'POST', '/api/v1/auth/sessions', 401,
headers=basic(actor['registration']['username'], password + '-wrong'))
client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
cache_key = 'messenger:user:' + actor['id']
ttl = compose('exec', '-T', 'redis', 'redis-cli', 'TTL', cache_key, capture=True)
check(ttl.isdigit() and 0 < int(ttl) <= 300, 'Redis cache key has no bounded TTL')
compose('exec', '-T', 'redis', 'redis-cli', 'SET', cache_key, 'null')
client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
cached_user = compose(
'exec', '-T', 'redis', 'redis-cli', '--raw', 'GET', cache_key, capture=True)
try:
cached_payload = json.loads(cached_user)
except json.JSONDecodeError as error:
raise Failure('JSON null cache entry was not replaced with valid JSON') from error
check(
isinstance(cached_payload, dict) and cached_payload.get('id') == actor['id'],
'JSON null cache entry was not replaced from PostgreSQL')
appendonly = compose(
'exec', '-T', 'redis', 'redis-cli', '--raw', 'CONFIG', 'GET', 'appendonly',
capture=True)
check(appendonly.splitlines()[-1:] == ['yes'], 'Redis AOF persistence is disabled')
compose('stop', 'redis')
client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
compose('start', 'redis')
compose('up', '-d', '--wait', 'redis')
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)
compose('stop', 'postgres')
client.call('GET', '/api/v1/users/me', 401)
client.call('GET', '/api/v1/users/me', 503, actor=actor)
compose('start', 'postgres')
compose('up', '-d', '--wait', 'postgres')
wait_ready(client)
race_name = 'race_' + uuid.uuid4().hex[:12]
race_body = {
'username': race_name,
'display_name': 'Race probe',
'password': 'correct horse battery staple',
}
def register_once(_):
local = Client(args.base_url)
return local.call('POST', '/api/v1/users', [201, 409], body=race_body)['status']
with ThreadPoolExecutor(max_workers=12) as executor:
statuses = list(executor.map(register_once, range(12)))
check(statuses.count(201) == 1, 'username race created more or less than one user')
check(statuses.count(409) == 11, 'username race did not return conflict to every loser')
owner = client.user('rollback_owner')
probe = client.user('rollback_member')
function_name = 'fail_rollback_probe'
title = 'rollback-probe-' + uuid.uuid4().hex
psql(f"""
DROP TRIGGER IF EXISTS {function_name}_trigger ON chat_members;
DROP FUNCTION IF EXISTS {function_name}();
CREATE OR REPLACE FUNCTION {function_name}() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF NEW.user_id = '{probe['id']}'::uuid THEN
RAISE EXCEPTION 'intentional rollback probe';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER {function_name}_trigger
BEFORE INSERT ON chat_members
FOR EACH ROW EXECUTE FUNCTION {function_name}();
""")
trigger_installed = True
client.call(
'POST', '/api/v1/chats', 503, actor=owner,
body={'title': title, 'member_ids': [owner['id'], probe['id']]})
count = psql(
"SELECT count(*) FROM chats WHERE title = '" + title + "';",
capture=True)
check(count == '0', 'failed composite chat operation was not rolled back')
print('PASS: blank password, JSON null cache recovery, expiry, logout, wrong password, '
'Redis fallback, DB failure, username race, and chat rollback verified.')
finally:
compose('up', '-d', '--wait', 'postgres', 'redis')
if trigger_installed:
try:
psql(f"""
DROP TRIGGER IF EXISTS {function_name}_trigger ON chat_members;
DROP FUNCTION IF EXISTS {function_name}();
""")
except Failure:
pass
compose('up', '-d', '--force-recreate', '--wait', 'api', env=normal_env)
if __name__ == '__main__':
run(main)