forked from mtusi2026/mtusi2026lab3
Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run public persistence and lab3 replay with Docker lifecycle evidence."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "student-tests"))
|
||||
import stack # noqa: E402
|
||||
|
||||
TARGETS = ["api", "postgres", "redis", "lb"]
|
||||
|
||||
|
||||
def run_case(name, argv, base, env, output, summary):
|
||||
before = stack.snapshot(base, env, TARGETS)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
process = subprocess.run(argv, cwd=ROOT, env=env, capture_output=True,
|
||||
text=True, timeout=180, check=False)
|
||||
(output / (name + ".stdout.log")).write_text(process.stdout)
|
||||
(output / (name + ".stderr.log")).write_text(process.stderr)
|
||||
exit_code = process.returncode
|
||||
except subprocess.TimeoutExpired as exc:
|
||||
def decoded(value):
|
||||
return value.decode(errors="replace") if isinstance(value, bytes) else (value or "")
|
||||
(output / (name + ".stdout.log")).write_text(decoded(exc.stdout))
|
||||
(output / (name + ".stderr.log")).write_text(decoded(exc.stderr))
|
||||
exit_code = -1
|
||||
after = stack.snapshot(base, env, TARGETS)
|
||||
proof = {"exit_code": exit_code, "seconds": round(time.monotonic() - started, 3),
|
||||
"before": before, "after": after}
|
||||
try:
|
||||
stack.record_lifecycle(output / "lifecycle.jsonl", "restart", env["COMPOSE_PROJECT_NAME"],
|
||||
before, after, source=name)
|
||||
proof["docker_transition"] = "PASS"
|
||||
except stack.CheckFailure as exc:
|
||||
proof["docker_transition"] = "FAIL"
|
||||
proof["transition_error"] = str(exc)
|
||||
proof["status"] = "PASS" if exit_code == 0 and proof["docker_transition"] == "PASS" else "FAIL"
|
||||
summary[name] = proof
|
||||
print(proof["status"] + " " + name + " (HTTP exit " + str(exit_code) + ")", flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--project", required=True, help="explicit Compose project, e.g. highload-lab3")
|
||||
parser.add_argument("--compose-file", action="append", help="Compose file(s); defaults to HighLoad/compose.yaml")
|
||||
parser.add_argument("--base-url", default="http://127.0.0.1:8080", help="public LB URL")
|
||||
parser.add_argument("--output", required=True)
|
||||
args = parser.parse_args()
|
||||
output = Path(args.output).resolve()
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
env = os.environ.copy()
|
||||
env["COMPOSE_PROJECT_NAME"] = args.project
|
||||
env["COMPOSE_FILE"] = os.pathsep.join(args.compose_file or [str(ROOT / "compose.yaml")])
|
||||
env["BASE_URL"] = args.base_url
|
||||
base = stack.compose_base(env)
|
||||
config = output / "lab3-restart-config.json"
|
||||
config.write_text(json.dumps({"base_url": args.base_url,
|
||||
"hooks": {"restart": [str(ROOT / "scripts/restart.sh")]}}))
|
||||
summary = {"project": args.project, "base_url": args.base_url,
|
||||
"scope": "public LB and Docker Compose service containers",
|
||||
"results": {}}
|
||||
try:
|
||||
run_case("public-persistence",
|
||||
["make", "test-persistence", "ACTION_SCRIPT=scripts/restart.sh",
|
||||
"BASE_URL=" + args.base_url], base, env, output, summary["results"])
|
||||
run_case("lab3-idempotency-restart",
|
||||
[sys.executable, "student-tests/lab3/run.py", "--config", str(config),
|
||||
"--output", str(output / "lab3-run"), "--scenario", "restart"],
|
||||
base, env, output, summary["results"])
|
||||
except (OSError, stack.CheckFailure, subprocess.SubprocessError) as exc:
|
||||
summary["error"] = type(exc).__name__ + ": " + str(exc)
|
||||
summary["status"] = "PASS" if len(summary["results"]) == 2 and all(
|
||||
item["status"] == "PASS" for item in summary["results"].values()) else "FAIL"
|
||||
path = output / "summary.json"
|
||||
path.write_text(json.dumps(summary, ensure_ascii=False, indent=2) + "\n")
|
||||
print(summary["status"] + ": " + str(path), flush=True)
|
||||
return 0 if summary["status"] == "PASS" else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user