ЛР 3 — БПИ2303 — Щербаков — на 3

This commit is contained in:
2026-11-13 13:27:43 +03:00
parent 0ab3a341aa
commit fd086f679e
30 changed files with 1450 additions and 24 deletions
+18
View File
@@ -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.")
+4
View File
@@ -0,0 +1,4 @@
@echo off
cd /d "%~dp0.."
docker compose --env-file .env restart postgres api1 api2 lb
exit /b %errorlevel%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env restart postgres api1 api2 lb
+4
View File
@@ -0,0 +1,4 @@
@echo off
cd /d "%~dp0.."
python scripts/stop_one.py
exit /b %errorlevel%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
exec "${PYTHON:-python3}" scripts/stop_one.py
+19
View File
@@ -0,0 +1,19 @@
"""Stop exactly the process identified by TARGET_INSTANCE_ID; never remove volumes."""
import os
from pathlib import Path
import re
import subprocess
ROOT = Path(__file__).resolve().parents[1]
target = os.environ.get("TARGET_INSTANCE_ID", "")
if not re.fullmatch(r"api[12]-[0-9a-f]{12}", target):
raise SystemExit("TARGET_INSTANCE_ID must be the exact X-Instance-Id from a running API")
service = target.split("-", 1)[0]
compose = ["docker", "compose", "--env-file", ".env"]
# Compare to the real API process, rather than trusting a supplied service name.
code = "from urllib.request import urlopen; print(urlopen('http://127.0.0.1:8080/health/live',timeout=2).headers['X-Instance-Id'])"
actual = subprocess.check_output(compose + ["exec", "-T", service, "python", "-c", code], cwd=ROOT, text=True).strip()
if actual != target:
raise SystemExit("Target process changed; refusing to stop another instance")
subprocess.run(compose + ["stop", "--timeout", "6", service], cwd=ROOT, check=True)
print("Stopped " + service + " instance=" + target)
+150
View File
@@ -0,0 +1,150 @@
"""Reproduce grade-3 evidence against the lab3 Compose stack; no credentials in output."""
import argparse
from datetime import datetime, timezone
import json
import os
from pathlib import Path
import subprocess
import sys
import threading
import time
from urllib.parse import quote
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:
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 state():
result = {}
for service in ("api1", "api2", "postgres", "lb"):
cid = command(COMPOSE + ["ps", "-a", "-q", service], True).strip()
data = json.loads(command(["docker", "inspect", cid], True))[0]
result[service] = {"id": cid[:12], "status": data["State"]["Status"],
"started_at": data["State"]["StartedAt"],
"volumes": [m["Name"] for m in data["Mounts"] if m["Type"] == "volume"],
"ports": data["HostConfig"]["PortBindings"],
"memory_limit": data["HostConfig"]["Memory"],
"nano_cpus": data["HostConfig"]["NanoCpus"]}
return result
def wait_replicas(c, actor):
deadline, seen = time.monotonic() + 60, set()
while time.monotonic() < deadline:
try:
seen.add(c.call("GET", "/api/v1/users/me", actor=actor, schema="User")["headers"]["X-Instance-Id"])
if len(seen) >= 2:
return seen
except Failure:
pass
time.sleep(0.1)
raise AssertionError("Two replicas did not become ready")
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--base-url", default="http://127.0.0.1:8080")
p.add_argument("--output")
args = p.parse_args()
if args.output:
sys.stdout = open(ROOT / args.output, "w", encoding="utf-8", buffering=1)
sys.stderr = sys.stdout
os.environ.update(BASE_URL=args.base_url, NO_PROXY="localhost,127.0.0.1", no_proxy="localhost,127.0.0.1")
print("UTC " + datetime.now(timezone.utc).isoformat(), flush=True)
print("COMMAND python scripts/verify.py --base-url " + args.base_url, flush=True)
command(["docker", "version", "--format", "Client={{.Client.Version}} Server={{.Server.Version}}"])
command(["docker", "info", "--format", "Docker CPUs={{.NCPU}} memory_bytes={{.MemTotal}} OS={{.OperatingSystem}}"])
command(COMPOSE + ["config", "--quiet"])
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"])
c = Client(args.base_url, timeout=2)
a, b, _, chat = c.bootstrap()
seen = wait_replicas(c, a)
print("OBSERVED_INSTANCES " + json.dumps(sorted(seen)), flush=True)
first = c.message(a, chat, "survives restart", key="restart-key")["json"]
second = c.message(b, chat, "cursor after restart")["json"]
path = "/api/v1/chats/" + chat["id"] + "/messages"
page = c.call("GET", path + "?limit=1", actor=a, schema="MessagePage")["json"]
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 message_keys),(SELECT count(*) FROM schema_migrations)"
before_counts = sql(counts)
for _ in range(2):
command(COMPOSE + ["run", "--rm", "--no-deps", "migrate"])
assert sql(counts) == before_counts
print("PASS: repeated migrations preserve all table counts " + before_counts, flush=True)
before = state()
print("BEFORE_RESTART " + json.dumps(before), 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])
after = state()
print("AFTER_RESTART " + json.dumps(after), flush=True)
for service in before:
assert before[service]["started_at"] != after[service]["started_at"]
assert before[service]["volumes"] == after[service]["volumes"]
assert c.message(a, chat, "survives restart", key="restart-key")["json"] == first
rest = c.call("GET", path + "?limit=1&cursor=" + quote(page["next_cursor"]), actor=a, schema="MessagePage")["json"]
assert page["items"] == [first] and rest["items"] == [second]
print("PASS: four containers restarted; volume, sessions, cursor and idempotency result preserved", flush=True)
stopped = set()
hook = "scripts/stop-one.cmd" if os.name == "nt" else "scripts/stop-one.sh"
# Run the unchanged public test until it has selected each real replica.
# Read concurrently with its POST stream to also measure read availability.
for attempt in range(6):
if stopped == {"api1", "api2"}:
break
command(COMPOSE + ["up", "-d", "--wait", "api1", "api2"], True)
wait_replicas(c, a)
done, reads = threading.Event(), {"ok": 0, "errors": 0}
def read_during_failure():
reader = Client(args.base_url, timeout=2)
while not done.is_set():
try:
result = reader.call("GET", path, actor=b, schema="MessagePage")["json"]
assert first in result["items"]
reads["ok"] += 1
except Failure:
reads["errors"] += 1
done.wait(0.1)
worker = threading.Thread(target=read_during_failure)
worker.start()
try:
print("FAILOVER_RUN " + str(attempt + 1), flush=True)
command([sys.executable, "tests/resilience.py", "--mode", "failover", "--action-script", hook, "--base-url", args.base_url])
current = state()
down = {s for s in ("api1", "api2") if current[s]["status"] == "exited"}
assert len(down) == 1
stopped.update(down)
print("AFTER_FAILOVER " + json.dumps(current), flush=True)
assert reads["ok"] >= 5
print("CONCURRENT_READS " + json.dumps(reads), flush=True)
assert c.message(a, chat, "survives restart", key="restart-key")["json"] == first
finally:
done.set()
worker.join(timeout=5)
command(COMPOSE + ["up", "-d", "--wait", "api1", "api2"], True)
assert stopped == {"api1", "api2"}, "Both replicas must be tested"
wait_replicas(c, a)
assert sql("SELECT count(*) FROM chats c WHERE NOT EXISTS (SELECT 1 FROM memberships m WHERE m.chat_id=c.id)") == "0"
print("PASS: each API stopped separately; reads/writes and original session survive; both replicas restored", flush=True)
print("PASS: grade-3 verification complete", flush=True)
if __name__ == "__main__":
main()