forked from mtusi2026/mtusi2026lab2
ЛР 2 — БПИ-2304 — Саламатин — на 4
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""Run against a separate API configured with SESSION_TTL_SECONDS=2."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, check
|
||||
|
||||
|
||||
def main():
|
||||
c = Client(os.environ["EXPIRY_BASE_URL"])
|
||||
deadline = time.monotonic() + 30
|
||||
while True:
|
||||
try:
|
||||
c.call("GET", "/health/ready", schema="Health")
|
||||
break
|
||||
except AssertionError:
|
||||
check(time.monotonic() < deadline, "TTL API did not become ready")
|
||||
time.sleep(0.25)
|
||||
actor = c.user("expiry")
|
||||
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
time.sleep(2.3)
|
||||
response = c.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
report = {"session_ttl_seconds": 2, "wait_seconds": 2.3,
|
||||
"before_expiry_status": 200, "after_expiry_status": response["status"],
|
||||
"result": "PASS"}
|
||||
(ROOT / "evidence" / "expiry.json").write_text(json.dumps(report, indent=2) + "\n")
|
||||
print("PASS: TTL=2s, initial 200, after 2.3s -> 401; normal API TTL unchanged")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Seed an isolated synthetic chat, measure the actual keyset SQL, keep raw plans."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from urllib.parse import quote
|
||||
from uuid import UUID
|
||||
|
||||
import psycopg
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from app.config import secret
|
||||
from app.security import Cursors, stamp
|
||||
from client import Client, check
|
||||
|
||||
|
||||
def walk(node):
|
||||
yield node
|
||||
for child in node.get("Plans", []):
|
||||
yield from walk(child)
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
actor = c.user("explain")
|
||||
chat = c.call("POST", "/api/v1/chats", 201, actor=actor,
|
||||
body={"title": "EXPLAIN 10000", "member_ids": [actor["id"]]}, schema="Chat")["json"]
|
||||
base = datetime.now(timezone.utc)
|
||||
with psycopg.connect(secret("DATABASE_URL"), connect_timeout=2) as conn:
|
||||
conn.execute("""INSERT INTO messages(id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(), %s, %s, 'synthetic-' || i,
|
||||
%s + i * interval '1 microsecond'
|
||||
FROM generate_series(1, 10000) AS i""", (UUID(chat["id"]), UUID(actor["id"]), base))
|
||||
conn.execute("UPDATE chats SET last_message_at=%s WHERE id=%s",
|
||||
(base + timedelta(microseconds=10000), UUID(chat["id"])))
|
||||
conn.execute("ANALYZE messages")
|
||||
report = {"messages_in_chat": 10000, "page_limit": 50,
|
||||
"index": "messages_chat_created_id_idx", "cases": {}}
|
||||
text_plans = []
|
||||
cursors = Cursors(secret("CURSOR_KEY").encode())
|
||||
with psycopg.connect(secret("DATABASE_URL"), connect_timeout=2) as conn:
|
||||
for label, offset in (("first", 0), ("middle", 5000), ("late", 9900)):
|
||||
sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id=%s"
|
||||
params = [UUID(chat["id"])]
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=50"
|
||||
if offset:
|
||||
boundary = conn.execute("SELECT created_at, id FROM messages WHERE chat_id=%s AND text=%s",
|
||||
(UUID(chat["id"]), "synthetic-" + str(offset))).fetchone()
|
||||
sql += " AND (created_at, id) > (%s, %s)"
|
||||
params.extend(boundary)
|
||||
token = cursors.encode("messages:" + chat["id"],
|
||||
{"created_at": stamp(boundary[0]), "id": str(boundary[1])})
|
||||
path += "&cursor=" + quote(token, safe="")
|
||||
sql += " ORDER BY created_at, id LIMIT %s"
|
||||
params.append(51)
|
||||
data = conn.execute("EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) " + sql, params).fetchone()[0][0]
|
||||
nodes = list(walk(data["Plan"]))
|
||||
check(not any(x["Node Type"] == "Seq Scan" for x in nodes), "Unexpected full scan in keyset query")
|
||||
check(any(x.get("Index Name") == report["index"] for x in nodes), "History index not used")
|
||||
check(data["Plan"]["Actual Rows"] == 51, "LIMIT was not respected")
|
||||
page = c.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
check(len(page["items"]) == 50 and page["next_cursor"], "HTTP page differs from measured SQL")
|
||||
expected = ["synthetic-" + str(i) for i in range(offset + 1, offset + 51)]
|
||||
check([x["text"] for x in page["items"]] == expected, "HTTP keyset boundary differs")
|
||||
report["cases"][label] = {"after_message": offset, "sql": sql, "plan": data}
|
||||
text_plans.append(label.upper() + "\n" + sql + "\n" + "\n".join(
|
||||
row[0] for row in conn.execute("EXPLAIN (ANALYZE, BUFFERS) " + sql, params).fetchall()))
|
||||
(ROOT / "evidence" / "explain.json").write_text(json.dumps(report, indent=2) + "\n")
|
||||
(ROOT / "evidence" / "explain.txt").write_text("\n\n".join(text_plans) + "\n")
|
||||
print("PASS: 10000 messages, first/middle/late pages use history index; 51 SQL rows for 50 HTTP items")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Create local synthetic credentials once without printing secret values."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main():
|
||||
directory = ROOT / "secrets"
|
||||
directory.mkdir(exist_ok=True)
|
||||
for name in ("postgres_password", "redis_password", "cursor_key"):
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
path.write_text(secrets.token_hex(32) + "\n", encoding="utf-8", newline="\n")
|
||||
else:
|
||||
# Shell command substitution on Linux must not retain a Windows CR.
|
||||
value = path.read_text(encoding="utf-8").strip()
|
||||
path.write_text(value + "\n", encoding="utf-8", newline="\n")
|
||||
pg_password = (directory / "postgres_password").read_text().strip()
|
||||
redis_password = (directory / "redis_password").read_text().strip()
|
||||
for name, value in {
|
||||
"database_url": f"postgresql://messenger:{pg_password}@postgres:5432/messenger",
|
||||
"cache_url": f"redis://:{redis_password}@redis:6379/0",
|
||||
}.items():
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
path.write_text(value + "\n", encoding="utf-8", newline="\n")
|
||||
env = ROOT / ".env"
|
||||
if not env.exists():
|
||||
env.write_bytes((ROOT / ".env.example").read_bytes())
|
||||
print("PASS: local .env and secret files exist; existing values preserved")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --env-file .env restart --timeout 5 api postgres redis
|
||||
@@ -0,0 +1,114 @@
|
||||
"""One reproducible local check: public tests, failures, expiry and SQL plans."""
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
EVIDENCE = ROOT / "evidence"
|
||||
|
||||
|
||||
def command(args, output=None, timeout=300):
|
||||
result = subprocess.run(args, cwd=ROOT, capture_output=True, text=True,
|
||||
encoding="utf-8", errors="replace", timeout=timeout)
|
||||
if output:
|
||||
(EVIDENCE / output).write_text(result.stdout + result.stderr, encoding="utf-8")
|
||||
if result.returncode:
|
||||
raise RuntimeError("Command failed; see " + (output or "local Docker status"))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def compose(*args, **kwargs):
|
||||
return command(["docker", "compose", "--env-file", ".env", *args], **kwargs)
|
||||
|
||||
|
||||
def checks(*args, output):
|
||||
return compose("run", "--rm", "--no-deps", "checks", *args, output=output)
|
||||
|
||||
|
||||
def snapshot():
|
||||
result = {}
|
||||
for service in ("api", "postgres", "redis"):
|
||||
identifier = compose("ps", "--all", "--quiet", service)
|
||||
data = json.loads(command(["docker", "inspect", identifier]))[0]
|
||||
result[service] = {"id": data["Id"], "started_at": data["State"]["StartedAt"],
|
||||
"image_id": data["Image"], "user": data["Config"]["User"],
|
||||
"volumes": [item["Name"] for item in data["Mounts"] if item["Type"] == "volume"],
|
||||
"memory_limit_bytes": data["HostConfig"]["Memory"],
|
||||
"nano_cpus": data["HostConfig"]["NanoCpus"]}
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--skip-build", action="store_true")
|
||||
parser.add_argument("--published-base-url", help="Optional host URL for a second public smoke through the published port")
|
||||
args = parser.parse_args()
|
||||
EVIDENCE.mkdir(exist_ok=True)
|
||||
command([os.sys.executable, "scripts/init_local.py"])
|
||||
compose("config", "--quiet")
|
||||
if not args.skip_build:
|
||||
print("Building runtime and check images", flush=True)
|
||||
compose("build", "api", "checks", output="build.txt", timeout=600)
|
||||
compose("up", "-d", "--wait", "--wait-timeout", "90", output="startup.txt", timeout=150)
|
||||
print("Running contract, public smoke and student integration tests", flush=True)
|
||||
checks("make", "check", output="contract-check.txt")
|
||||
checks("make", "test", output="smoke.txt")
|
||||
checks("make", "test-student", output="student-tests.txt")
|
||||
print("Running unchanged public persistence test", flush=True)
|
||||
before = snapshot()
|
||||
checks("sh", "-ec", "chmod +x scripts/restart.sh; make test-persistence ACTION_SCRIPT=scripts/restart.sh",
|
||||
output="persistence.txt")
|
||||
after = snapshot()
|
||||
for service in before:
|
||||
assert before[service]["id"] == after[service]["id"], "Unexpected container replacement"
|
||||
assert before[service]["started_at"] != after[service]["started_at"], "Service was not restarted"
|
||||
assert before[service]["volumes"] == after[service]["volumes"], "Volumes changed"
|
||||
(EVIDENCE / "restart.json").write_text(json.dumps({"before": before, "after": after}, indent=2) + "\n")
|
||||
print("Testing short expiry in a separate API process", flush=True)
|
||||
project = os.environ.get("COMPOSE_PROJECT_NAME", "mtusi-lab2")
|
||||
# Read the configured project name from normalized compose metadata, not local paths.
|
||||
project = json.loads(compose("config", "--format", "json"))["name"]
|
||||
ttl_name = project + "-ttl-check"
|
||||
existing = command(["docker", "ps", "-a", "--filter", "name=^/" + ttl_name + "$", "--format", "{{.ID}}"])
|
||||
if existing:
|
||||
raise RuntimeError("TTL test container name already exists; it was not removed")
|
||||
created = False
|
||||
try:
|
||||
compose("run", "-d", "--no-deps", "--name", ttl_name, "-e", "SESSION_TTL_SECONDS=2", "api")
|
||||
created = True
|
||||
compose("run", "--rm", "--no-deps", "-e", "EXPIRY_BASE_URL=http://" + ttl_name + ":8080",
|
||||
"checks", "python", "scripts/check_expiry.py", output="expiry.txt")
|
||||
finally:
|
||||
if created:
|
||||
command(["docker", "stop", "--time", "5", ttl_name])
|
||||
command(["docker", "rm", ttl_name])
|
||||
print("Measuring first, middle and late history pages on 10000 messages", flush=True)
|
||||
checks("make", "explain", output="explain-run.txt")
|
||||
counts_before = compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-Atc",
|
||||
"SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM messages)")
|
||||
compose("run", "--rm", "--no-deps", "migrate", output="migrations-repeat.txt")
|
||||
counts_after = compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-Atc",
|
||||
"SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM messages)")
|
||||
assert counts_before == counts_after, "Migration replay changed business data"
|
||||
checks("make", "test", output="smoke-final.txt")
|
||||
if args.published_base_url:
|
||||
command([os.sys.executable, "tests/smoke.py", "--base-url", args.published_base_url],
|
||||
output="published-smoke.txt")
|
||||
versions = compose("exec", "-T", "api", "python", "-c",
|
||||
"import importlib.metadata as m,json,platform; print(json.dumps({'python':platform.python_version(),"
|
||||
"'packages':{p:m.version(p) for p in ['fastapi','pydantic','uvicorn','psycopg','psycopg-pool','redis']}}))")
|
||||
summary = {"result": "PASS", "utc": datetime.now(timezone.utc).isoformat(),
|
||||
"checks": ["contract", "public_smoke", "8_student_tests", "all_stores_restart", "short_expiry",
|
||||
"explain_10000", "migration_replay", "final_smoke"],
|
||||
"versions": json.loads(versions), "migration_counts_before": counts_before,
|
||||
"migration_counts_after": counts_after, "deployment": after,
|
||||
"published_base_url": args.published_base_url}
|
||||
(EVIDENCE / "verification.json").write_text(json.dumps(summary, indent=2) + "\n")
|
||||
print("PASS: all grade-4 checks; summaries saved under evidence/", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user