forked from mtusi2026/mtusi2026lab2
lab2 condition in postgres, sessions in redis
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Level-4 checks: registration race, transactional rollback, and indexed 10k-row EXPLAIN.
|
||||
|
||||
Run from the repository root while the local Compose stack is healthy:
|
||||
python tests/grade4_checks.py --base-url http://localhost:8080
|
||||
The script uses `docker compose exec postgres psql` for controlled test data and
|
||||
removes its synthetic rows/triggers afterwards. It does not delete volumes.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import concurrent.futures
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
ENV_FILE = ROOT / ".env"
|
||||
EVIDENCE = ROOT / "evidence" / "explain-level4.txt"
|
||||
|
||||
|
||||
def read_env() -> dict[str, str]:
|
||||
result: dict[str, str] = {}
|
||||
if ENV_FILE.exists():
|
||||
for raw in ENV_FILE.read_text(encoding="utf-8").splitlines():
|
||||
line = raw.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
result[key.strip()] = value.strip().strip("\"'")
|
||||
return result
|
||||
|
||||
|
||||
ENV = read_env()
|
||||
|
||||
|
||||
def psql(sql: str) -> str:
|
||||
user = ENV.get("POSTGRES_USER", "messenger")
|
||||
database = ENV.get("POSTGRES_DB", "messenger")
|
||||
cmd = ["docker", "compose", "--env-file", str(ENV_FILE), "exec", "-T", "postgres",
|
||||
"psql", "-X", "-v", "ON_ERROR_STOP=1", "-A", "-t", "-U", user, "-d", database]
|
||||
completed = subprocess.run(cmd, input=sql, text=True, capture_output=True, cwd=ROOT)
|
||||
if completed.returncode:
|
||||
raise RuntimeError("psql failed: " + (completed.stderr.strip() or completed.stdout.strip()))
|
||||
return completed.stdout.strip()
|
||||
|
||||
|
||||
def api(method: str, base_url: str, path: str, body: dict | None = None,
|
||||
headers: dict[str, str] | None = None) -> tuple[int, dict | None]:
|
||||
request_headers = {"Accept": "application/json"}
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
request_headers["Content-Type"] = "application/json"
|
||||
request_headers.update(headers or {})
|
||||
req = urllib.request.Request(base_url.rstrip("/") + path, data=data,
|
||||
headers=request_headers, method=method)
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=15)
|
||||
except urllib.error.HTTPError as exc:
|
||||
response = exc
|
||||
with response:
|
||||
payload_bytes = response.read()
|
||||
try:
|
||||
payload = json.loads(payload_bytes) if payload_bytes else None
|
||||
except json.JSONDecodeError:
|
||||
payload = None
|
||||
return response.status, payload
|
||||
|
||||
|
||||
def register(base_url: str, username: str, display_name: str, password: str) -> tuple[int, dict | None]:
|
||||
return api("POST", base_url, "/api/v1/users", {
|
||||
"username": username, "display_name": display_name, "password": password
|
||||
})
|
||||
|
||||
|
||||
def login(base_url: str, username: str, password: str) -> str:
|
||||
import base64
|
||||
encoded = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
status, payload = api("POST", base_url, "/api/v1/auth/sessions",
|
||||
headers={"Authorization": "Basic " + encoded})
|
||||
if status != 201 or not payload or not payload.get("token"):
|
||||
raise RuntimeError(f"Basic login failed: HTTP {status}")
|
||||
return payload["token"]
|
||||
|
||||
|
||||
def check_registration_race(base_url: str) -> None:
|
||||
username = "race_" + uuid.uuid4().hex[:12]
|
||||
password = "Grade4-Race-" + uuid.uuid4().hex[:16]
|
||||
def one(i: int) -> int:
|
||||
status, _ = register(base_url, username, f"Race probe {i}", password)
|
||||
return status
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
|
||||
statuses = list(executor.map(one, range(8)))
|
||||
if statuses.count(201) != 1 or statuses.count(409) != 7:
|
||||
raise RuntimeError(f"Registration race expected one 201 and seven 409, got {statuses}")
|
||||
print("PASS: concurrent same-username registration produced exactly one 201 and seven 409")
|
||||
|
||||
|
||||
def create_actor_and_chat(base_url: str) -> tuple[str, str, str]:
|
||||
username = "g4_" + uuid.uuid4().hex[:12]
|
||||
password = "Grade4-Probe-" + uuid.uuid4().hex[:16]
|
||||
status, user = register(base_url, username, "Level 4 test", password)
|
||||
if status != 201 or not user:
|
||||
raise RuntimeError(f"Probe user registration failed: HTTP {status}")
|
||||
token = login(base_url, username, password)
|
||||
title = "explain-probe-" + uuid.uuid4().hex[:10]
|
||||
status, chat = api("POST", base_url, "/api/v1/chats",
|
||||
{"title": title, "member_ids": [user["id"]]},
|
||||
{"Authorization": "Bearer " + token})
|
||||
if status != 201 or not chat:
|
||||
raise RuntimeError(f"Probe chat creation failed: HTTP {status}")
|
||||
return user["id"], chat["id"], token
|
||||
|
||||
|
||||
def check_rollback(base_url: str, user_id: str, token: str) -> None:
|
||||
psql("DROP TRIGGER IF EXISTS lab2_grade4_fail_membership_trigger ON chat_members;\n"
|
||||
"DROP FUNCTION IF EXISTS lab2_grade4_fail_membership();\n"
|
||||
"CREATE FUNCTION lab2_grade4_fail_membership() RETURNS trigger LANGUAGE plpgsql AS $$\n"
|
||||
"BEGIN IF EXISTS (SELECT 1 FROM chats WHERE id=NEW.chat_id AND title LIKE 'rollback-probe-%') THEN "
|
||||
"RAISE EXCEPTION 'intentional level-4 rollback probe'; END IF; RETURN NEW; END; $$;\n"
|
||||
"CREATE TRIGGER lab2_grade4_fail_membership_trigger BEFORE INSERT ON chat_members "
|
||||
"FOR EACH ROW EXECUTE FUNCTION lab2_grade4_fail_membership();\n")
|
||||
title = "rollback-probe-" + uuid.uuid4().hex[:10]
|
||||
try:
|
||||
status, _ = api("POST", base_url, "/api/v1/chats",
|
||||
{"title": title, "member_ids": [user_id]},
|
||||
{"Authorization": "Bearer " + token})
|
||||
if status != 503:
|
||||
raise RuntimeError(f"Rollback probe expected bounded HTTP 503, got {status}")
|
||||
count = psql("SELECT count(*) FROM chats WHERE title='" + title + "';")
|
||||
if count != "0":
|
||||
raise RuntimeError("Chat row remained after injected membership failure; transaction did not roll back")
|
||||
print("PASS: injected membership failure returned 503 and rolled back the chat row")
|
||||
finally:
|
||||
psql("DROP TRIGGER IF EXISTS lab2_grade4_fail_membership_trigger ON chat_members;\n"
|
||||
"DROP FUNCTION IF EXISTS lab2_grade4_fail_membership();\n")
|
||||
|
||||
|
||||
def check_explain(user_id: str, target_chat_id: str) -> None:
|
||||
# 99 other chats + the target chat, 100 rows per chat = 10,000 rows.
|
||||
psql("INSERT INTO chats(id,title) SELECT gen_random_uuid(), 'grade4-probe-chat-' || g "
|
||||
"FROM generate_series(1,99) AS g;\n")
|
||||
try:
|
||||
psql("WITH all_chats AS (SELECT '" + target_chat_id + "'::uuid AS id UNION ALL "
|
||||
"SELECT id FROM chats WHERE title LIKE 'grade4-probe-chat-%') "
|
||||
"INSERT INTO messages(id,chat_id,sender_id,text,created_at) "
|
||||
"SELECT gen_random_uuid(), c.id, '" + user_id + "'::uuid, "
|
||||
"'grade4-probe-message-' || g, now() + g * interval '1 microsecond' "
|
||||
"FROM all_chats c CROSS JOIN generate_series(1,100) AS g;\nANALYZE messages;\n")
|
||||
plan = psql("EXPLAIN (ANALYZE, BUFFERS) SELECT id,chat_id,sender_id,text,created_at "
|
||||
"FROM messages WHERE chat_id='" + target_chat_id + "'::uuid "
|
||||
"ORDER BY created_at,id LIMIT 51;\n")
|
||||
EVIDENCE.parent.mkdir(parents=True, exist_ok=True)
|
||||
EVIDENCE.write_text("Dataset: at least 10,000 synthetic messages across 100 chats.\n"
|
||||
"Query: history ordered by (created_at, id), LIMIT 51.\n\n" + plan + "\n",
|
||||
encoding="utf-8")
|
||||
if "messages_chat_created_id_idx" not in plan:
|
||||
raise RuntimeError("EXPLAIN did not use messages_chat_created_id_idx; inspect " + str(EVIDENCE))
|
||||
print(f"PASS: EXPLAIN (ANALYZE, BUFFERS) used messages_chat_created_id_idx; saved {EVIDENCE.relative_to(ROOT)}")
|
||||
finally:
|
||||
psql("DELETE FROM messages WHERE text LIKE 'grade4-probe-message-%';\n"
|
||||
"DELETE FROM chats WHERE title LIKE 'grade4-probe-chat-%';\n")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
args = parser.parse_args()
|
||||
ready, _ = api("GET", args.base_url, "/health/ready")
|
||||
if ready != 200:
|
||||
raise RuntimeError(f"API must be ready before level-4 checks; got HTTP {ready}")
|
||||
check_registration_race(args.base_url)
|
||||
user_id, chat_id, token = create_actor_and_chat(args.base_url)
|
||||
check_rollback(args.base_url, user_id, token)
|
||||
check_explain(user_id, chat_id)
|
||||
print("PASS: all automated level-4 checks in this script completed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (RuntimeError, OSError, subprocess.SubprocessError) as exc:
|
||||
raise SystemExit("FAIL: " + str(exc)) from None
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify that a Redis-backed Bearer session expires without logging out."""
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from client import Client, arguments, check, run, timestamp
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Session TTL check; use SESSION_TTL_SECONDS=3 in .env for this test')
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
actor = c.user('ttl')
|
||||
|
||||
# Confirm the session works before waiting for its server-declared expiry.
|
||||
c.call('GET', '/api/v1/users/me', 200, actor=actor, schema='User')
|
||||
# Client.user stores the registration payload, not the session response. Log in once
|
||||
# more to obtain the exact expires_at returned by the API and test that token.
|
||||
import base64
|
||||
credentials = actor['registration']
|
||||
basic = base64.b64encode((credentials['username'] + ':' + credentials['password']).encode()).decode()
|
||||
session = c.call('POST', '/api/v1/auth/sessions', 201,
|
||||
headers={'Authorization': 'Basic ' + basic}, schema='Session')['json']
|
||||
ttl_seconds = (timestamp(session['expires_at']) - datetime.now(timezone.utc)).total_seconds()
|
||||
check(0 < ttl_seconds <= 30,
|
||||
'Set SESSION_TTL_SECONDS=3 (or another value <=30) in .env and recreate the API container')
|
||||
token_actor = {'id': actor['id'], 'token': session['token']}
|
||||
c.call('GET', '/api/v1/users/me', 200, actor=token_actor, schema='User')
|
||||
time.sleep(max(0, ttl_seconds) + 1.0)
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=token_actor)
|
||||
print('PASS: expired Redis session is rejected with HTTP 401')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Controlled Redis outage check for the local Compose stack; restores Redis in finally."""
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
def request_status(url, timeout=15):
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=timeout) as response:
|
||||
return response.status, response.read()
|
||||
except urllib.error.HTTPError as exc:
|
||||
return exc.code, exc.read()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--timeout', type=int, default=45)
|
||||
#parser.add_argument('--outage-timeout', type=int, default=30)
|
||||
parser.add_argument('--compose-file', default='.env')
|
||||
parser.add_argument('--target', choices=('redis', 'postgres'), default='redis')
|
||||
args = parser.parse_args()
|
||||
compose = ['docker', 'compose', '--env-file', args.compose_file]
|
||||
stopped = False
|
||||
try:
|
||||
code, body = request_status(args.base_url.rstrip('/') + '/health/ready')
|
||||
if code != 200:
|
||||
raise RuntimeError('API must be ready before the test; got HTTP %s' % code)
|
||||
stopped = True
|
||||
subprocess.run(compose + ['stop', args.target], check=True)
|
||||
deadline = time.monotonic() + 30
|
||||
outage_status = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
outage_status, body = request_status(args.base_url.rstrip('/') + '/health/ready')
|
||||
if outage_status == 503:
|
||||
break
|
||||
except (TimeoutError, OSError):
|
||||
pass
|
||||
time.sleep(0.25)
|
||||
if outage_status != 503:
|
||||
raise RuntimeError('readiness did not return HTTP 503 while %s was stopped (last=%r)' % (args.target, outage_status))
|
||||
print('PASS: /health/ready returned HTTP 503 while %s was unavailable' % args.target)
|
||||
finally:
|
||||
if stopped:
|
||||
subprocess.run(compose + ['start', args.target], check=False)
|
||||
deadline = time.monotonic() + args.timeout
|
||||
recovered = False
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
code, body = request_status(args.base_url.rstrip('/') + '/health/ready')
|
||||
if code == 200:
|
||||
recovered = True
|
||||
break
|
||||
except (TimeoutError, OSError):
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
if recovered:
|
||||
print('PASS: readiness recovered to HTTP 200 after %s restart' % args.target)
|
||||
else:
|
||||
raise RuntimeError('%s did not recover to ready state; inspect docker compose logs %s api' % (args.target, args.target))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user