forked from mtusi2026/mtusi2026lab2
ЛР 2 — БПИ2303 — Щербаков — на 3
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
"""Generate local synthetic credentials without printing or overwriting them."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
directory = root / "secrets"
|
||||
directory.mkdir(exist_ok=True)
|
||||
password_file = directory / "postgres_password"
|
||||
if not password_file.exists():
|
||||
password_file.write_text(secrets.token_hex(32), encoding="ascii")
|
||||
url = directory / "database_url"
|
||||
if not url.exists():
|
||||
password = password_file.read_text(encoding="ascii").strip()
|
||||
url.write_text(f"postgresql://messenger:{password}@postgres:5432/messenger", encoding="ascii")
|
||||
env = root / ".env"
|
||||
if not env.exists():
|
||||
env.write_bytes((root / ".env.example").read_bytes())
|
||||
print("Local .env and secrets ready; existing files preserved.")
|
||||
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
docker compose --env-file .env restart postgres api
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --env-file .env restart postgres api
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Reproducible grade-3 checks against this Compose project (synthetic data)."""
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure
|
||||
|
||||
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
||||
|
||||
|
||||
def command(args, capture=False):
|
||||
result = subprocess.run(args, cwd=ROOT, text=True, encoding="utf-8", errors="replace",
|
||||
stdout=subprocess.PIPE if capture else sys.stdout,
|
||||
stderr=subprocess.STDOUT, timeout=180)
|
||||
if result.returncode:
|
||||
# Do not dump Docker config, environment or request credentials.
|
||||
raise RuntimeError("Command failed: " + args[0])
|
||||
return result.stdout
|
||||
|
||||
|
||||
def sql(query):
|
||||
return command(COMPOSE + ["exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-Atc", query], True).strip()
|
||||
|
||||
|
||||
def wait_ready(url):
|
||||
c = Client(url, timeout=5)
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
c.call("GET", "/health/ready", schema="Health")
|
||||
return c
|
||||
except Failure:
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError("Readiness deadline exceeded")
|
||||
|
||||
|
||||
def container_state():
|
||||
result = {}
|
||||
for service in ("api", "postgres"):
|
||||
cid = command(COMPOSE + ["ps", "-q", service], True).strip()
|
||||
data = json.loads(command(["docker", "inspect", cid], True))[0]
|
||||
result[service] = {"id": cid[:12], "started_at": data["State"]["StartedAt"],
|
||||
"health": data["State"].get("Health", {}).get("Status"),
|
||||
"volumes": [m["Name"] for m in data["Mounts"] if m["Type"] == "volume"],
|
||||
"memory_limit": data["HostConfig"]["Memory"],
|
||||
"nano_cpus": data["HostConfig"]["NanoCpus"]}
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://127.0.0.1:18082")
|
||||
parser.add_argument("--output", help="UTF-8 evidence log, without credentials")
|
||||
args = parser.parse_args()
|
||||
if args.output:
|
||||
sys.stdout = open(ROOT / args.output, "w", encoding="utf-8", buffering=1)
|
||||
sys.stderr = sys.stdout
|
||||
os.environ["NO_PROXY"] = "localhost,127.0.0.1"
|
||||
os.environ["no_proxy"] = "localhost,127.0.0.1"
|
||||
os.environ["BASE_URL"] = args.base_url
|
||||
print("UTC", datetime.now(timezone.utc).isoformat(), flush=True)
|
||||
c = wait_ready(args.base_url)
|
||||
command([sys.executable, "scripts/check_contract.py"])
|
||||
command([sys.executable, "tests/smoke.py", "--base-url", args.base_url])
|
||||
command([sys.executable, "-m", "unittest", "discover", "-s", "student-tests", "-v"])
|
||||
# Compare all relevant counts; a rolled-back chat must not remain as an orphan.
|
||||
assert sql("SELECT count(*) FROM chats c WHERE NOT EXISTS (SELECT 1 FROM memberships m WHERE m.chat_id=c.id)") == "0"
|
||||
print("PASS: no orphan chats after invalid compound operations", flush=True)
|
||||
counts = "SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM memberships),(SELECT count(*) FROM messages),(SELECT count(*) FROM sessions),(SELECT count(*) FROM settings),(SELECT count(*) FROM schema_migrations)"
|
||||
before = sql(counts)
|
||||
command(COMPOSE + ["exec", "-T", "api", "python", "-m", "src.database"])
|
||||
command(COMPOSE + ["exec", "-T", "api", "python", "-m", "src.database"])
|
||||
assert sql(counts) == before
|
||||
print("PASS: migrations repeated twice; all table counts unchanged: " + before, flush=True)
|
||||
assert sql("SELECT bool_and(password_hash LIKE 'scrypt$16384$8$1$%') FROM users") == "t"
|
||||
assert sql("SELECT bool_and(length(token_hash)=64) FROM sessions") == "t"
|
||||
print("PASS: password KDF and token digests stored, no plaintext fields", flush=True)
|
||||
|
||||
alice, bob, _, chat = c.bootstrap()
|
||||
first = c.message(alice, chat, "cursor before restart")["json"]
|
||||
second = c.message(bob, chat, "cursor after restart")["json"]
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
page = c.call("GET", path + "?limit=1", actor=alice, schema="MessagePage")["json"]
|
||||
from urllib.parse import quote
|
||||
states = container_state()
|
||||
print("BEFORE_RESTART " + json.dumps(states), flush=True)
|
||||
hook = "scripts/restart.cmd" if os.name == "nt" else "scripts/restart.sh"
|
||||
command([sys.executable, "tests/resilience.py", "--mode", "persistence", "--action-script", hook, "--base-url", args.base_url])
|
||||
wait_ready(args.base_url)
|
||||
after = container_state()
|
||||
print("AFTER_RESTART " + json.dumps(after), flush=True)
|
||||
for service in states:
|
||||
assert states[service]["started_at"] != after[service]["started_at"]
|
||||
assert states[service]["volumes"] == after[service]["volumes"]
|
||||
resumed = c.call("GET", path + "?limit=1&cursor=" + quote(page["next_cursor"]), actor=alice, schema="MessagePage")["json"]
|
||||
assert page["items"] == [first] and resumed["items"] == [second]
|
||||
c.call("POST", "/api/v1/users", 409, body=alice["registration"])
|
||||
print("PASS: both containers restarted; named volume, session, users and signed cursor preserved", flush=True)
|
||||
|
||||
# Separate short-lived API, same persistent PostgreSQL; main API TTL is unchanged.
|
||||
name = "mtusi2026lab2-expiry-check"
|
||||
command(COMPOSE + ["run", "-d", "--no-deps", "--name", name, "-e", "SESSION_TTL_SECONDS=2", "-p", "127.0.0.1:18083:8080", "api"], True)
|
||||
try:
|
||||
short = wait_ready("http://127.0.0.1:18083")
|
||||
actor = short.user("expiry")
|
||||
short.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
time.sleep(2.3)
|
||||
short.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
short.call("DELETE", "/api/v1/auth/sessions/current", 401, actor=actor)
|
||||
print("PASS: SESSION_TTL_SECONDS=2; valid then expired token rejected", flush=True)
|
||||
finally:
|
||||
command(["docker", "rm", "-f", name], True)
|
||||
print("PASS: grade-3 verification complete", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user