Files
mtusi2026lab3/scripts/final-checks-lab3.py
T
2026-11-06 18:54:28 +03:00

77 lines
2.7 KiB
Python

"""Финальные регрессии и безопасный снимок инфраструктуры без Config.Env."""
import json
import subprocess
import sys
from pathlib import Path
from replicas import compose, containers, docker
ROOT = Path(__file__).resolve().parents[1]
DEST = ROOT / "evidence"
def check(name, command, stdin=None):
result = subprocess.run(command, cwd=ROOT, input=stdin, text=True, capture_output=True)
(DEST / ("lab3-final-" + name + ".txt")).write_text(result.stdout + result.stderr)
print(name, "PASS" if result.returncode == 0 else "FAIL", flush=True)
if result.returncode:
raise RuntimeError((result.stdout + result.stderr)[-3000:])
for name in ["smoke", "lab3", "regressions", "dependencies"]:
check(name, [sys.executable, "tests/" + name + ".py"])
for name in [
"database",
"passwords",
"settings",
"callbacks",
"idempotency_db",
"readiness",
"error_headers",
]:
check(
name,
["docker", "exec", "-i", containers()[0], "python"],
(ROOT / "tests" / (name + ".py")).read_text(),
)
snapshot = {}
all_ids = []
for service in ["api", "lb", "postgres", "redis", "migration"]:
ids = compose("ps", "--all", "-q", service).split()
snapshot[service] = []
for cid in ids:
# Only explicitly selected infrastructure fields; credentials are excluded.
obj = json.loads(docker("inspect", "--format", "{{json .}}", cid))
state = obj["State"]
record = {
"id": cid[:12],
"name": obj["Name"],
"image_id": obj["Image"],
"status": state["Status"],
"exit_code": state["ExitCode"],
"cpu_limit": obj["HostConfig"]["NanoCpus"] / 1e9,
"memory_limit_bytes": obj["HostConfig"]["Memory"],
"ports": obj["NetworkSettings"]["Ports"],
"read_only_root": obj["HostConfig"]["ReadonlyRootfs"],
}
snapshot[service].append(record)
assert record["cpu_limit"] > 0 and record["memory_limit_bytes"] > 0
if service == "api":
assert not any(record["ports"].values()), "API port is published"
if state["Running"]:
all_ids.append(cid)
assert len(snapshot["api"]) == 2
snapshot["post_test_usage"] = [
json.loads(line)
for line in docker("stats", "--no-stream", "--format", "{{json .}}", *all_ids).splitlines()
]
snapshot["note"] = (
"CPU/memory usage snapshot after tests, not a peak load measurement; cgroup CPU deltas under load are in research results."
)
(DEST / "lab3-infrastructure.json").write_text(
json.dumps(snapshot, ensure_ascii=False, indent=2) + "\n"
)
print("PASS: 2 API, internal ports, CPU/RAM limits on all containers", flush=True)