forked from mtusi2026/mtusi2026lab2
feat(app) lab2 prepared for mr
This commit is contained in:
Executable
+203
@@ -0,0 +1,203 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Grade 4 checks that the public smoke does not cover.
|
||||
|
||||
Covers: concurrent duplicate registration, atomic chat rollback, short TTL
|
||||
expiry, logout invalidation, wrong password, and Redis/PostgreSQL outage
|
||||
behaviour. Failure drills use `docker compose stop/start` and never remove
|
||||
volumes. Tokens, passwords and hook output are not printed.
|
||||
"""
|
||||
import argparse
|
||||
import concurrent.futures
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client, Failure, check # noqa: E402
|
||||
|
||||
|
||||
def say(message):
|
||||
print("PASS: " + message, flush=True)
|
||||
|
||||
|
||||
def compose(args, *command):
|
||||
env_file = ROOT / ".env"
|
||||
check(env_file.is_file(), ".env is missing; copy .env.example to .env")
|
||||
full = [
|
||||
"docker", "compose",
|
||||
"--env-file", str(env_file),
|
||||
"-p", args.compose_project,
|
||||
"-f", str((ROOT / args.compose_file).resolve()),
|
||||
*command,
|
||||
]
|
||||
result = subprocess.run(full, cwd=ROOT, stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.PIPE, text=True, timeout=60)
|
||||
if result.returncode != 0:
|
||||
# stderr may contain paths, never credentials in normal compose output.
|
||||
raise Failure("docker compose " + command[0] + " failed: " + result.stderr.strip()[:300])
|
||||
|
||||
|
||||
def readiness(client):
|
||||
try:
|
||||
client.call("GET", "/health/ready", 200, schema="Health")
|
||||
return 200
|
||||
except Failure:
|
||||
try:
|
||||
client.call("GET", "/health/ready", 503)
|
||||
return 503
|
||||
except Failure:
|
||||
return None
|
||||
|
||||
|
||||
def wait_for(client, expected, seconds=45):
|
||||
deadline = time.monotonic() + seconds
|
||||
while time.monotonic() < deadline:
|
||||
if readiness(client) == expected:
|
||||
return
|
||||
time.sleep(0.5)
|
||||
raise Failure("/health/ready did not become " + str(expected))
|
||||
|
||||
|
||||
def check_wrong_password(args):
|
||||
client = Client(args.base_url)
|
||||
actor = client.user("wrongpw")
|
||||
registration = actor["registration"]
|
||||
credential = _basic(registration["username"], "definitely-wrong-password")
|
||||
response = client.call("POST", "/api/v1/auth/sessions", 401,
|
||||
headers={"Authorization": "Basic " + credential})
|
||||
check(response["headers"].get("WWW-Authenticate", "").lower().startswith("basic"),
|
||||
"wrong password did not return a Basic challenge")
|
||||
say("wrong password -> 401 with Basic challenge")
|
||||
|
||||
|
||||
def check_logout(args):
|
||||
client = Client(args.base_url)
|
||||
actor = client.user("logout")
|
||||
client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=actor)
|
||||
client.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
say("logout revoked the current token (repeat -> 401)")
|
||||
|
||||
|
||||
def check_x_user_id(args):
|
||||
client = Client(args.base_url)
|
||||
alice = client.user("xalice")
|
||||
bob = client.user("xbob")
|
||||
me = client.call("GET", "/api/v1/users/me", actor=alice,
|
||||
headers={"X-User-Id": bob["id"]}, schema="User")["json"]
|
||||
check(me["id"] == alice["id"], "X-User-Id overrode bearer identity")
|
||||
client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
|
||||
say("X-User-Id is ignored and grants no access")
|
||||
|
||||
|
||||
def check_concurrent_registration(args):
|
||||
username = "race_" + uuid.uuid4().hex[:12]
|
||||
body = {"username": username, "display_name": "Race", "password": "concurrent-password-123"}
|
||||
|
||||
def register(_):
|
||||
client = Client(args.base_url)
|
||||
response = client.call("POST", "/api/v1/users", (201, 409), body=body)
|
||||
return response["status"]
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=12) as pool:
|
||||
statuses = list(pool.map(register, range(12)))
|
||||
|
||||
check(statuses.count(201) == 1, "expected exactly one 201, got " + str(statuses.count(201)))
|
||||
check(statuses.count(409) == 11, "expected eleven 409, got " + str(statuses.count(409)))
|
||||
say("12 concurrent registrations of one username -> one 201, eleven 409")
|
||||
|
||||
|
||||
def check_chat_rollback(args):
|
||||
client = Client(args.base_url)
|
||||
actor = client.user("rollback")
|
||||
title = "rollback-" + uuid.uuid4().hex
|
||||
unknown = str(uuid.uuid4())
|
||||
client.call("POST", "/api/v1/chats", 400, actor=actor,
|
||||
body={"title": title, "member_ids": [actor["id"], unknown]})
|
||||
page = client.call("GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage")["json"]
|
||||
check(all(chat["title"] != title for chat in page["items"]),
|
||||
"failed chat creation left a partial chat behind")
|
||||
say("failed composite chat creation rolled back completely")
|
||||
|
||||
|
||||
def check_short_ttl(args):
|
||||
check(args.short_ttl_url, "--short-ttl-url is required for the expiry check")
|
||||
short = Client(args.short_ttl_url)
|
||||
main = Client(args.base_url)
|
||||
actor = short.user("ttl")
|
||||
main.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
time.sleep(3.5)
|
||||
main.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
say("SESSION_TTL_SECONDS=2 session expired and was rejected by the main API")
|
||||
|
||||
|
||||
def drill(args, client, actor, chat, message, service, expected_message):
|
||||
started = time.monotonic()
|
||||
compose(args, "stop", service)
|
||||
wait_for(client, 503, 45)
|
||||
detection = time.monotonic() - started
|
||||
client.call("GET", "/health/live", 200, schema="Health")
|
||||
client.call("GET", "/health/ready", 503)
|
||||
client.call("GET", "/api/v1/users/me", 503, actor=actor)
|
||||
say("%s down: live=200, ready=503, protected=503 (%s; detected in %.1fs)"
|
||||
% (service, expected_message, detection))
|
||||
|
||||
started = time.monotonic()
|
||||
compose(args, "start", service)
|
||||
wait_for(client, 200, 60)
|
||||
recovery = time.monotonic() - started
|
||||
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
page = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
||||
actor=actor, schema="MessagePage")["json"]
|
||||
check(any(item == message for item in page["items"]),
|
||||
service + " recovery lost a confirmed message")
|
||||
say("%s recovered in %.1fs; same token and confirmed message intact" % (service, recovery))
|
||||
|
||||
|
||||
def check_failure_drills(args):
|
||||
client = Client(args.base_url)
|
||||
actor = client.user("drill")
|
||||
chat = client.call("POST", "/api/v1/chats", 201, actor=actor,
|
||||
body={"title": "Failure drill", "member_ids": [actor["id"]]},
|
||||
schema="Chat")["json"]
|
||||
message = client.message(actor, chat, "drill-persist-" + uuid.uuid4().hex)["json"]
|
||||
drill(args, client, actor, chat, message, "redis", "bounded unavailable error, no anonymous access")
|
||||
drill(args, client, actor, chat, message, "postgres", "bounded unavailable error, session retained")
|
||||
|
||||
|
||||
def _basic(username, password):
|
||||
import base64
|
||||
return base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Lab 2 grade 4 evidence checks")
|
||||
parser.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080"))
|
||||
parser.add_argument("--short-ttl-url", default=os.environ.get("SHORT_TTL_URL", "http://localhost:8081"))
|
||||
parser.add_argument("--compose-project", default=os.environ.get("COMPOSE_PROJECT_NAME", "mtusi2026lab2"))
|
||||
parser.add_argument("--compose-file", default="compose.yaml")
|
||||
parser.add_argument("--skip-failure", action="store_true", help="skip Redis/PostgreSQL stop drills")
|
||||
args = parser.parse_args()
|
||||
|
||||
check_wrong_password(args)
|
||||
check_logout(args)
|
||||
check_x_user_id(args)
|
||||
check_concurrent_registration(args)
|
||||
check_chat_rollback(args)
|
||||
check_short_ttl(args)
|
||||
if not args.skip_failure:
|
||||
check_failure_drills(args)
|
||||
|
||||
print("PASS: grade 4 checks completed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Failure as error:
|
||||
print("FAIL:", str(error))
|
||||
raise SystemExit(1)
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
# AGENT: Restart hook for tests/resilience.py. It intentionally uses `restart`,
|
||||
# never `down -v`, `flushall` or a re-seed: PostgreSQL data and the Redis AOF
|
||||
# stay on their named volumes. Exit 0 means "the restart was requested", the
|
||||
# test then waits for /health/ready and reuses the existing bearer token.
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
COMPOSE_FILE_PATH="${COMPOSE_FILE:-compose.yaml}"
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
echo "restart.sh: .env is missing; copy .env.example to .env first" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
compose() {
|
||||
docker compose --env-file .env -f "$COMPOSE_FILE_PATH" "$@"
|
||||
}
|
||||
|
||||
# State stores first, then the API, so the app reconnects to a live database.
|
||||
# The API itself retries PostgreSQL for up to 30 seconds during startup.
|
||||
compose restart postgres redis
|
||||
compose restart api
|
||||
@@ -0,0 +1,53 @@
|
||||
-- Synthetic history for the index evidence. Never run this against real data:
|
||||
-- it deletes only messages of the dedicated seed chat and re-creates them.
|
||||
\set ON_ERROR_STOP on
|
||||
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', 'seed_10k', 'Seed 10k',
|
||||
'not-a-login-hash', '2026-01-01T00:00:00Z')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'Seed 10k history', '2026-01-01T00:00:00Z')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id)
|
||||
VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa')
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
DELETE FROM messages WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(),
|
||||
'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
|
||||
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
|
||||
'seed message ' || g,
|
||||
'2026-01-01T00:00:00Z'::timestamptz + (g || ' seconds')::interval
|
||||
FROM generate_series(1, 10000) AS g;
|
||||
|
||||
-- Remove index entries left by the previous seed run; without this the plan
|
||||
-- reads dead entries and the buffer count reflects history, not page depth.
|
||||
VACUUM (ANALYZE) messages;
|
||||
|
||||
SELECT count(*) AS seeded_messages
|
||||
FROM messages
|
||||
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
|
||||
|
||||
-- First page: the same predicate the API sends with an empty cursor.
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
|
||||
AND (created_at, id) > ('0001-01-01T00:00:00Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
-- Deep page: the cursor points at message 5000, so a correct plan seeks there
|
||||
-- instead of scanning the first 5000 rows.
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
|
||||
AND (created_at, id) > ('2026-01-01T01:23:20Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
# AGENT: Seeds exactly 10,000 synthetic messages into a dedicated chat and runs
|
||||
# EXPLAIN (ANALYZE, BUFFERS) using the API's keyset predicate. Output goes to
|
||||
# stdout so it can be stored in evidence/explain.txt.
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
echo "seed_and_explain.sh: .env is missing" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1091
|
||||
. ./.env
|
||||
set +a
|
||||
|
||||
docker compose --env-file .env exec -T postgres \
|
||||
psql -v ON_ERROR_STOP=1 -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" \
|
||||
< scripts/seed_10k.sql
|
||||
Reference in New Issue
Block a user