Files
mtusi2026lab2-fork/tests/grade4_checks.py
T

191 lines
8.7 KiB
Python

#!/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