forked from mtusi2026/mtusi2026lab2
115 lines
6.1 KiB
Python
115 lines
6.1 KiB
Python
"""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()
|