Files
mtusi2026lab3/scripts/verify.py

113 lines
5.7 KiB
Python

"""Build and verify the complete local lab3 stack without deleting volumes."""
import argparse
from datetime import datetime, timezone
import json
import os
from pathlib import Path
import subprocess
import sys
from ops import ROOT, command, compose, containers, summary
def saved(name, args, timeout=300):
result = subprocess.run(args, cwd=ROOT, capture_output=True, text=True,
encoding="utf-8", errors="replace", timeout=timeout)
(ROOT / "evidence" / name).write_text(result.stdout + result.stderr, encoding="utf-8")
if result.returncode:
raise RuntimeError("Check failed; see evidence/" + name)
return result.stdout
def checks(name, *args):
return saved(name, ["docker", "compose", "--env-file", ".env", "run", "--rm", "--no-deps", "checks", *args])
def main():
p = argparse.ArgumentParser()
p.add_argument("--skip-build", action="store_true")
p.add_argument("--base-url", default=None)
p.add_argument("--skip-experiments", action="store_true")
args = p.parse_args()
(ROOT / "evidence").mkdir(exist_ok=True)
command([sys.executable, "-X", "utf8", "scripts/init_local.py"])
compose("config", "--quiet")
if not args.skip_build:
print("Building API and check images", flush=True)
saved("build.txt", ["docker", "compose", "--env-file", ".env", "build", "api", "checks"], 900)
saved("startup.txt", ["docker", "compose", "--env-file", ".env", "up", "-d", "--wait", "--wait-timeout", "120"], 240)
config = json.loads(compose("config", "--format", "json"))
base_url = args.base_url or "http://127.0.0.1:" + str(config["services"]["lb"]["ports"][0]["published"])
print("Checking contract, HTTP API and student tests", flush=True)
checks("contract-check.txt", "make", "check")
checks("smoke.txt", "make", "test")
checks("student-tests.txt", "make", "test-student")
before = summary()
checks("persistence.txt", "sh", "-ec", "chmod +x scripts/restart.sh; make test-persistence ACTION_SCRIPT=scripts/restart.sh")
after = summary()
by_id = {r["id"]: r for r in after}
for old in before:
new = by_id[old["id"]]
assert old["started_at"] != new["started_at"]
assert old["volumes"] == new["volumes"]
(ROOT / "evidence" / "restart.json").write_text(json.dumps({"before": before, "after": after}, indent=2) + "\n")
print("Running public failover scenario", flush=True)
try:
checks("failover.txt", "sh", "-ec", "chmod +x scripts/stop-one.sh; make test-failover ACTION_SCRIPT=scripts/stop-one.sh")
finally:
compose("start", "api")
# Wait for both replicas through LB before starting subsequent checks.
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
from experiments import wait_instances
client = Client(base_url)
actor = client.user("verify")
wait_instances(client, actor, 2)
print("Checking short session expiry and indexed history", flush=True)
ttl_name = config["name"] + "-ttl-check"
assert not command(["docker", "ps", "-a", "--filter", "name=^/" + ttl_name + "$", "--format", "{{.ID}}"])
created = False
try:
try:
compose("run", "-d", "--no-deps", "--name", ttl_name,
"-e", "SESSION_TTL_SECONDS=2", "api", timeout=45)
except subprocess.TimeoutExpired:
# A detached Compose command on Desktop can time out after Docker
# already started its container. Inspect the unique name rather
# than create another process or treat an unknown startup as PASS.
row = json.loads(command(["docker", "inspect", ttl_name]))[0]
labels = row["Config"]["Labels"]
assert labels["com.docker.compose.project"] == config["name"]
assert labels["com.docker.compose.service"] == "api"
assert "SESSION_TTL_SECONDS=2" in row["Config"]["Env"]
assert row["State"]["Running"], "TTL container did not start"
created = True
checks("expiry.txt", "env", "EXPIRY_BASE_URL=http://" + ttl_name + ":8080", "python", "scripts/check_expiry.py")
finally:
if created:
command(["docker", "stop", "--time", "6", ttl_name])
command(["docker", "rm", ttl_name])
checks("explain-run.txt", "make", "explain")
checks("smoke-final.txt", "make", "test")
saved("published-smoke.txt", [sys.executable, "-X", "utf8", "tests/smoke.py", "--base-url", base_url])
if not args.skip_experiments:
print("Measuring failure of each replica and sequential replacement under load", flush=True)
saved("experiments.txt", [sys.executable, "-X", "utf8", "scripts/experiments.py", "--base-url", base_url], 360)
deployed = summary()
assert len([r for r in deployed if r["service"] == "api" and r["status"] == "running"]) == 2
assert all(not r["published_ports"] for r in deployed if r["service"] != "lb")
assert all(r["memory_bytes"] and r["nano_cpus"] for r in deployed)
versions = command(["docker", "info", "--format", "{{json .}}"])
info = json.loads(versions)
report = {"result": "PASS", "utc": datetime.now(timezone.utc).isoformat(), "base_url": base_url,
"checks": ["public_smoke", "student_tests", "full_restart", "public_failover", "expiry", "index_10000", "published_smoke"]
+ ([] if args.skip_experiments else ["each_replica_failure", "sequential_replacement_under_load"]),
"docker": {k: info[k] for k in ("ServerVersion", "OperatingSystem", "Architecture", "NCPU", "MemTotal")},
"deployment": deployed}
(ROOT / "evidence" / "verification.json").write_text(json.dumps(report, indent=2) + "\n")
print("PASS: lab3 checks completed; evidence/verification.json", flush=True)
if __name__ == "__main__":
main()