Files
mtusi2026lab6/student-tests/check_stack.py

119 lines
7.7 KiB
Python

import json
import platform
import sys
from common import call, check, compose_output, docker, new_user, seen_instances
def main():
info = json.loads(docker("info", "--format", "{{json .}}"))
print(f"host: {platform.system()} {platform.release()}, {platform.processor() or platform.machine()}; "
f"client Python {platform.python_version()}")
print(f"Docker {docker('version', '--format', '{{.Server.Version}}')}, "
f"{compose_output('version').strip()}; Docker VM: {info.get('NCPU')} CPU, "
f"{int(info.get('MemTotal', 0)) / 2**30:.1f} GiB RAM, {info.get('OperatingSystem')}")
print()
print(compose_output("ps", "-a", "--format", "table {{.Service}}\t{{.ID}}\t{{.Status}}\t{{.Ports}}").strip())
for one_shot in ("migrate", "s3-init"):
status = compose_output("ps", "-a", "--format", "{{.Status}}", one_shot).strip()
check(status.startswith("Exited (0)"), f"{one_shot} service must finish with exit code 0")
config = json.loads(compose_output("config", "--format", "json"))
services = config["services"]
containers = compose_output("ps", "-q").split()
rows = [json.loads(line) for line in docker("inspect", "--format", "{{json .}}", *containers).splitlines()]
print()
print("container image cpus memory host ports networks")
api_count = 0
for row in sorted(rows, key=lambda r: r["Name"]):
labels = row["Config"]["Labels"]
service = labels.get("com.docker.compose.service")
limits = row["HostConfig"]
cpus = limits.get("NanoCpus", 0) / 1e9
memory = limits.get("Memory", 0) // 2**20
ports = sorted({f"{b['HostIp']}:{b['HostPort']}" for bindings in (row["NetworkSettings"]["Ports"] or {}).values()
for b in (bindings or [])})
networks = sorted(name.split("_", 1)[-1] for name in row["NetworkSettings"]["Networks"])
print(f"{row['Name'].lstrip('/'):<28} {row['Config']['Image']:<24} {cpus:>4.2f} {memory:>4} MiB "
f"{','.join(ports) or '-':<23} {','.join(networks)}")
check(cpus > 0 and memory > 0, f"{service}: no CPU/RAM limit")
if service == "api":
api_count += 1
check(not ports, "api must not publish ports: everything goes through the balancer")
if service == "lb":
check(ports and all(p.startswith("127.0.0.1:") for p in ports), "lb must listen on 127.0.0.1 only")
if service in ("postgres", "redis", "s3", "rabbitmq"):
check(not ports and networks == ["data"], f"{service} must be only in the internal data network")
if service in ("janitor", "worker", "dispatcher"):
# lab6: + внутренняя сеть telemetry (только до Collector), портов нет
check(not ports and networks == ["data", "telemetry"], f"{service}: networks data+telemetry, no ports")
if service == "otel-collector":
check(not ports and networks == ["telemetry"], "otel-collector: only the internal telemetry network, no ports")
if service in ("prometheus", "loki", "tempo", "grafana"):
check(ports and all(p.startswith("127.0.0.1:") for p in ports), f"{service}: ports only on 127.0.0.1")
check("data" not in networks and "edge" not in networks, f"{service} must not reach app data/edge networks")
check(api_count >= 2, "need at least 2 api replicas")
running = {r["Config"]["Labels"].get("com.docker.compose.service") for r in rows if r["State"]["Running"]}
check({"rabbitmq", "worker", "dispatcher"} <= running, "lab5: broker, worker and dispatcher must be running")
check(any(v.get("target") == "/var/lib/rabbitmq" and v.get("type") == "volume"
for v in services["rabbitmq"].get("volumes", [])), "rabbitmq needs a persistent volume")
check(services["rabbitmq"].get("hostname") == "rabbitmq", "rabbitmq needs a fixed hostname (node data dir)")
check(all("container_name" not in s for s in services.values()), "container_name prevents scaling")
check(config["networks"]["data"].get("internal") is True, "data network must be internal")
# lab6: наблюдаемость
check(config["networks"]["telemetry"].get("internal") is True, "telemetry network must be internal")
check({"otel-collector", "prometheus", "loki", "tempo", "grafana"} <= running,
"lab6: collector, prometheus, loki, tempo and grafana must be running")
for name, target in (("prometheus", "/prometheus"), ("loki", "/loki"), ("tempo", "/var/tempo"),
("grafana", "/var/lib/grafana")):
check(any(v.get("target") == target and v.get("type") == "volume" for v in services[name].get("volumes", [])),
f"{name} needs a persistent volume at {target}")
for name, service in services.items():
image = service.get("image", "")
check(not image.endswith(":latest") and (":" in image or not image), f"{name}: image version must be pinned")
check("data" not in services["lb"]["networks"], "lb must not reach postgres/redis")
print()
for name, service in sorted(services.items()):
secrets = sorted(s["source"] for s in service.get("secrets", []))
print(f"secrets of {name}: {secrets or '-'}")
check(not services["lb"].get("secrets"), "lb gets no secrets")
check(not services["otel-collector"].get("secrets"), "otel-collector gets no secrets")
check({s["source"] for s in services["grafana"].get("secrets", [])} == {"grafana_admin_password"},
"grafana: only its admin password")
for name in ("api", "worker", "dispatcher", "janitor", "prometheus", "loki", "tempo"):
check("grafana_admin_password" not in {s["source"] for s in services[name].get("secrets", [])},
f"{name} must not get the Grafana password")
check(sorted(s["source"] for s in services["migrate"]["secrets"]) == ["database_url"], "migrate needs only database_url")
for name in ("api", "janitor", "worker"):
names = {s["source"] for s in services[name]["secrets"]}
check(not names & {"s3_root_user", "s3_root_password", "s3_config"}, f"{name} must not get S3 admin keys")
check({"s3_access_key_id", "s3_secret_access_key"} <= names, f"{name} needs the limited S3 key")
broker_secrets = {"broker_url_dispatcher", "broker_url_worker", "rabbitmq_definitions"}
for name in ("api", "janitor", "lb", "migrate"):
names = {s["source"] for s in services[name].get("secrets", [])}
check(not names & broker_secrets, f"{name} must not get broker credentials (API uses the outbox)")
check({s["source"] for s in services["dispatcher"]["secrets"]} == {"database_url", "broker_url_dispatcher"},
"dispatcher: only database_url and its own broker user")
check(not {s["source"] for s in services["dispatcher"]["secrets"]} & {"s3_access_key_id", "s3_secret_access_key"},
"dispatcher needs no S3 key")
user = new_user("stack")
served = seen_instances(user["token"])
print()
print(f"through http://127.0.0.1:8080 one session is served by {len(served)} replicas: {sorted(served)}")
check(len(served) >= 2, "requests are not balanced")
health = call("GET", "/health/ready", 200)
print(f"/health/ready -> {health.status} from {health.instance}")
print(f"PASS: {api_count} api replicas behind one entry point, no direct API ports, limits on every "
f"container, postgres/redis/s3/rabbitmq only in the internal data network, worker/dispatcher/janitor in "
f"data+telemetry, collector only in the internal telemetry network, prometheus/loki/tempo/grafana on "
f"127.0.0.1 with persistent volumes, pinned images, S3 admin keys only in s3 and s3-init, broker "
f"credentials only in dispatcher/worker")
if __name__ == "__main__":
sys.exit(main())