Files
mtusi2026lab2/scripts/lab.py
T

348 lines
15 KiB
Python

import argparse
from datetime import datetime, timezone
import hashlib
import json
import os
from pathlib import Path
import platform
import secrets
import shutil
import socket
import subprocess
import sys
import time
from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener
import venv
ROOT = Path(__file__).resolve().parents[1]
LOG = []
LOCAL_HTTP = build_opener(ProxyHandler({}))
def settings():
path = ROOT / ".env"
result = {}
if path.exists():
for line in path.read_text(encoding="utf-8-sig").splitlines():
key, separator, value = line.partition("=")
if separator and not key.strip().startswith("#"):
result[key.strip()] = value.strip()
return result
def environment(args):
result = os.environ.copy()
result.update(PYTHONUTF8="1", PYTHONIOENCODING="utf-8", PYTHONDONTWRITEBYTECODE="1",
COMPOSE_PROJECT_NAME=args.project, HTTP_PORT=str(args.port),
SESSION_TTL_SECONDS=str(args.ttl), LAB2_PROJECT=args.project,
LAB2_PORT=str(args.port), LAB2_TTL=str(args.ttl))
for key in ("NO_PROXY", "no_proxy"):
result[key] = ",".join(filter(None, [result.get(key), "localhost", "127.0.0.1"]))
return result
def say(value):
line = str(value).replace(str(ROOT), ".")
print(line, flush=True)
LOG.append(line)
def run(args, command, quiet=False):
command = [str(item) for item in command]
if not quiet:
say("$ " + subprocess.list2cmdline(command))
with subprocess.Popen(command, cwd=ROOT, env=environment(args), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace") as process:
lines = []
for line in process.stdout:
lines.append(line)
if not quiet:
say(line.rstrip())
code = process.wait()
if code:
raise RuntimeError(f"{command[0]} exited with code {code}")
return "".join(lines)
def compose(args, *command, quiet=False):
return run(args, ["docker", "compose", "--project-name", args.project, "--env-file", ".env", *command], quiet)
def interpreter():
return ROOT / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
def evidence(name, value):
path = ROOT / "evidence" / name
path.parent.mkdir(exist_ok=True)
content = json.dumps(value, ensure_ascii=False, indent=2) if not isinstance(value, str) else value
path.write_text(content.rstrip() + "\n", encoding="utf-8", newline="\n")
def setup(args):
if not (ROOT / ".env").exists():
shutil.copyfile(ROOT / ".env.example", ROOT / ".env")
folder = ROOT / "secrets"
folder.mkdir(exist_ok=True)
password_file = folder / "postgres_password"
if not password_file.exists():
password_file.write_text(secrets.token_hex(32) + "\n", encoding="ascii")
password = password_file.read_text(encoding="ascii").strip()
values = {"database_url": f"postgresql://messenger:{password}@db:5432/messenger",
"cursor_key": secrets.token_hex(32)}
for name, value in values.items():
path = folder / name
if not path.exists():
path.write_text(value + "\n", encoding="ascii")
if os.name != "nt":
path.chmod(0o644)
if os.name != "nt":
folder.chmod(0o700)
password_file.chmod(0o644)
if not interpreter().exists():
venv.EnvBuilder(with_pip=True).create(ROOT / ".venv")
lockfiles = [ROOT / "requirements.lock", ROOT / "requirements-validation.lock"]
fingerprint = hashlib.sha256(b"".join(path.read_bytes() for path in lockfiles)).hexdigest()
marker = ROOT / ".venv/.lab-dependencies"
if not marker.exists() or marker.read_text(encoding="ascii") != fingerprint:
run(args, [interpreter(), "-X", "utf8", "-m", "pip", "install", "--disable-pip-version-check",
"--no-input", "--progress-bar", "off", "--quiet", "-r", lockfiles[0], "-r", lockfiles[1]])
marker.write_text(fingerprint, encoding="ascii")
say("Environment ready; existing settings and secrets preserved")
def base_url(args):
return f"http://127.0.0.1:{args.port}"
def wait_ready(args, timeout=90):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
with LOCAL_HTTP.open(base_url(args) + "/health/ready", timeout=4) as response:
if response.status == 200 and json.load(response) == {"status": "ok"}:
return
except (URLError, OSError, ValueError):
pass
time.sleep(0.3)
raise RuntimeError("API readiness timeout")
def build(args):
compose(args, "config", "--quiet")
compose(args, "build", "api")
def start(args):
current = compose(args, "ps", "--all", "--quiet", "api", quiet=True).strip()
if not current:
with socket.socket() as probe:
try:
probe.bind(("127.0.0.1", args.port))
except OSError:
raise RuntimeError(f"Port {args.port} is busy; stop its container or use --port") from None
compose(args, "up", "-d", "--no-build", "db", "migrate", "api")
wait_ready(args)
say("API ready: " + base_url(args))
def states(args):
result = {}
for service in ("api", "db"):
container = compose(args, "ps", "--all", "--quiet", service, quiet=True).strip()
if not container:
raise RuntimeError("Missing container: " + service)
data = json.loads(run(args, ["docker", "inspect", container], quiet=True))[0]
result[service] = {
"id": data["Id"], "image": data["Image"], "started_at": data["State"]["StartedAt"],
"running": data["State"]["Running"], "user": data["Config"]["User"],
"volumes": [mount["Name"] for mount in data["Mounts"] if mount["Type"] == "volume"],
}
return result
def restart(args):
before = states(args)
if not all(item["running"] for item in before.values()):
raise RuntimeError("Both API and DB must be running before restart")
compose(args, "restart", "--timeout", "10", "db", "api")
after = states(args)
for service in before:
if before[service]["started_at"] == after[service]["started_at"]:
raise RuntimeError("Container was not restarted: " + service)
if before[service]["volumes"] != after[service]["volumes"]:
raise RuntimeError("Volume changed during restart")
evidence("restart.json", {"project": args.project, "before": before, "after": after})
say("PASS: API and PostgreSQL restarted, volume preserved")
def contract_tests(args):
run(args, [interpreter(), "-X", "utf8", "scripts/check_contract.py"])
run(args, [interpreter(), "-X", "utf8", "scripts/validate_openapi.py"])
compose(args, "config", "--quiet")
def api_tests(args):
wait_ready(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
os.environ["BASE_URL"] = base_url(args)
run(args, [interpreter(), "-X", "utf8", "-m", "unittest", "discover", "-s", "student-tests", "-v"])
def client(args):
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
for key in ("NO_PROXY", "no_proxy"):
os.environ[key] = environment(args)[key]
return Client(base_url(args))
def database_stats(args):
sql = """SELECT json_build_object(
'users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),
'members',(SELECT count(*) FROM chat_members),'messages',(SELECT count(*) FROM messages),
'sessions',(SELECT count(*) FROM sessions),'migrations',(SELECT count(*) FROM schema_migrations),
'expiry_fingerprint',(SELECT md5(COALESCE(string_agg(user_id::text || created_at::text || expires_at::text,
',' ORDER BY token_hash),'')) FROM sessions),
'passwords_are_argon2id',(SELECT bool_and(password_hash LIKE '$argon2id$%') FROM users),
'session_hashes_only',(SELECT bool_and(token_hash ~ '^[0-9a-f]{64}$') FROM sessions))"""
result = compose(args, "exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-At", "-c", sql, quiet=True)
return json.loads(result)
def restart_check(args):
from urllib.parse import urlencode
c = client(args)
alice, bob, eve, chat = c.bootstrap()
path = "/api/v1/chats/" + chat["id"] + "/messages"
c.message(alice, chat, "first")
last = c.message(bob, chat, "second")["json"]
page = c.call("GET", path + "?limit=1", actor=alice, schema="MessagePage")["json"]
revoked = c.user("revoked")
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=revoked)
hook = ROOT / "scripts" / ("restart.cmd" if os.name == "nt" else "restart.sh")
run(args, [interpreter(), "-X", "utf8", "tests/resilience.py", "--mode", "persistence",
"--action-script", hook, "--base-url", base_url(args)])
continued = c.call("GET", path + "?" + urlencode({"cursor": page["next_cursor"]}),
actor=alice, schema="MessagePage")["json"]
if continued != {"items": [last], "next_cursor": None}:
raise RuntimeError("Old cursor did not survive restart")
c.call("GET", "/api/v1/users/me", 401, actor=revoked)
c.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve)
before = database_stats(args)
compose(args, "run", "--rm", "--no-deps", "migrate")
compose(args, "run", "--rm", "--no-deps", "migrate")
after = database_stats(args)
if before != after or after["migrations"] != 1:
raise RuntimeError("Migration replay changed existing data")
if not after["passwords_are_argon2id"] or not after["session_hashes_only"]:
raise RuntimeError("Unexpected credential representation in DB")
result = {"public_persistence": "PASS", "cursor_after_restart": "PASS",
"logout_after_restart": "PASS", "migration_replay": "PASS", "database": after}
evidence("persistence.json", result)
say("PASS: old session/cursor, durable logout and repeatable migrations")
return result
def ttl_check(args):
test = argparse.Namespace(project=args.project + "-ttl", port=args.test_port, ttl=2,
test_port=args.test_port, action="start")
try:
start(test)
c = client(test)
actor = c.user("expiry")
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
before = database_stats(test)["expiry_fingerprint"]
compose(test, "restart", "--timeout", "5", "api")
wait_ready(test)
after = database_stats(test)["expiry_fingerprint"]
if before != after:
raise RuntimeError("Session expiry changed during restart")
time.sleep(2.2)
c.call("GET", "/api/v1/users/me", 401, actor=actor)
registration = actor["registration"]
import base64
basic = base64.b64encode((registration["username"] + ":" + registration["password"]).encode()).decode()
c.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": "Basic " + basic}, schema="Session")
say("PASS: TTL=2, expired token rejected after API restart; fresh login works")
result = {"ttl_seconds": 2, "expiry_unchanged_on_restart": True, "before_restart": 200,
"after_expiry_and_restart": 401, "new_login": 201}
evidence("expiry.json", result)
return result
finally:
compose(test, "stop", "--timeout", "10", "api", "db")
def dependency_check(args):
c = client(args)
actor = c.user("dbcheck")
try:
compose(args, "stop", "--timeout", "10", "db")
c.call("GET", "/health/live", schema="Health")
c.call("GET", "/health/ready", 503)
c.call("GET", "/api/v1/users/me", 503, actor=actor)
finally:
compose(args, "start", "db")
wait_ready(args)
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
say("PASS: DB outage -> live=200, ready/business=503; session works after recovery")
return {"live_without_db": 200, "ready_without_db": 503, "business_without_db": 503, "recovered": 200}
def verify(args):
summary = {"date_utc": datetime.now(timezone.utc).isoformat(), "platform": platform.platform(),
"source_commit": run(args, ["git", "rev-parse", "HEAD"], quiet=True).strip(),
"worktree_dirty_at_start": bool(run(args, ["git", "status", "--porcelain"], quiet=True).strip()),
"project": args.project, "status": "FAIL"}
try:
setup(args)
contract_tests(args)
build(args)
start(args)
api_tests(args)
summary["persistence"] = restart_check(args)
summary["expiry"] = ttl_check(args)
summary["dependency"] = dependency_check(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
summary["containers"] = states(args)
summary["docker"] = run(args, ["docker", "version", "--format", "{{.Server.Version}}"], quiet=True).strip()
summary["resources"] = json.loads(run(
args, ["docker", "info", "--format", '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}'], quiet=True))
summary["status"] = "PASS"
say("PASS: full lab2 verification")
finally:
evidence("verification.txt", "\n".join(LOG))
evidence("summary.json", summary)
def main():
saved = settings()
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["setup", "build", "start", "test", "restart",
"restart-check", "stop", "verify"])
parser.add_argument("--project", default=os.environ.get("LAB2_PROJECT", saved.get("COMPOSE_PROJECT_NAME", "mtusi-lab2")))
parser.add_argument("--port", type=int, default=int(os.environ.get("LAB2_PORT", saved.get("HTTP_PORT", "8080"))))
parser.add_argument("--ttl", type=int, default=int(os.environ.get("LAB2_TTL", saved.get("SESSION_TTL_SECONDS", "3600"))))
parser.add_argument("--test-port", type=int, default=18082)
args = parser.parse_args()
if not 1 <= args.port <= 65535 or not 1 <= args.test_port <= 65535 or not 1 <= args.ttl <= 86400:
parser.error("Invalid port or session TTL")
if args.action in ("setup", "build", "start", "test", "restart-check"):
setup(args)
actions = {"setup": lambda item: None, "build": build, "start": start,
"test": lambda item: (contract_tests(item), api_tests(item)),
"restart": restart, "restart-check": restart_check, "verify": verify,
"stop": lambda item: compose(item, "stop", "--timeout", "10", "api", "db")}
actions[args.action](args)
if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, ValueError) as error:
print("FAIL: " + str(error), file=sys.stderr)
raise SystemExit(1)