forked from mtusi2026/mtusi2026lab3
Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit test-project restart/outage adapter. Never removes volumes/data."""
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__)
|
||||
p.add_argument("--project", required=True)
|
||||
p.add_argument("--file", action="append", type=Path, required=True)
|
||||
p.add_argument("--api-service", default="api")
|
||||
p.add_argument("--database-service", default="postgres")
|
||||
p.add_argument("--cache-service", default="redis")
|
||||
p.add_argument("--evidence", type=Path)
|
||||
p.add_argument("action", choices=("restart-stack", "stop-db", "start-db", "stop-cache", "start-cache", "restart-cache"))
|
||||
args = p.parse_args()
|
||||
if not args.project.startswith(("lab2-test-", "highload-auth-retest-", "highload-lab2-test-")):
|
||||
print("Use an explicitly named disposable test project (lab2-test-*).", file=sys.stderr)
|
||||
return 2
|
||||
base = ["docker", "compose", "--project-name", args.project]
|
||||
for file in args.file:
|
||||
base.extend(("--file", str(file.resolve())))
|
||||
|
||||
def call(argv):
|
||||
result = subprocess.run(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=70)
|
||||
if result.returncode:
|
||||
raise RuntimeError("Docker action failed; output suppressed")
|
||||
return result.stdout
|
||||
|
||||
config = json.loads(call(base + ["config", "--format", "json"]))
|
||||
services = config["services"]
|
||||
stores = [args.database_service]
|
||||
if args.cache_service in services:
|
||||
stores.append(args.cache_service)
|
||||
elif "cache" in args.action:
|
||||
print("Cache service is not configured; adapt --cache-service.", file=sys.stderr)
|
||||
return 2
|
||||
targets = [args.api_service] + stores if args.action == "restart-stack" else [
|
||||
args.database_service if args.action.endswith("db") else args.cache_service]
|
||||
if any(name not in services for name in targets):
|
||||
return 2
|
||||
|
||||
def snapshot():
|
||||
result = {}
|
||||
for service in targets:
|
||||
ident = call(base + ["ps", "--all", "--quiet", service]).strip()
|
||||
if not ident or "\n" in ident:
|
||||
raise RuntimeError("expected one existing container per test service")
|
||||
raw = json.loads(call(["docker", "inspect", ident]))[0]
|
||||
if raw["Config"]["Labels"].get("com.docker.compose.project") != args.project:
|
||||
raise RuntimeError("container project mismatch")
|
||||
result[service] = {"id": ident, "started_at": raw["State"]["StartedAt"],
|
||||
"running": raw["State"]["Running"],
|
||||
"volumes": [mount["Name"] for mount in raw["Mounts"] if mount["Type"] == "volume"]}
|
||||
return result
|
||||
|
||||
before = snapshot()
|
||||
operation = "restart" if args.action.startswith("restart") else args.action.split("-")[0]
|
||||
call(base + [operation] + targets)
|
||||
proof = {"action": args.action, "project": args.project, "before": before, "after": snapshot()}
|
||||
if args.evidence:
|
||||
args.evidence.parent.mkdir(parents=True, exist_ok=True)
|
||||
with args.evidence.open("a") as output:
|
||||
output.write(json.dumps(proof) + "\n")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except (OSError, ValueError, KeyError, RuntimeError, subprocess.SubprocessError):
|
||||
print("Test infrastructure action failed; private details suppressed.", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Copy and implement one explicit test adapter; never prints credentials."""
|
||||
import sys
|
||||
|
||||
# Expected metrics stdout: {"sql_queries": 1234, "cache_hits": 56}.
|
||||
# Counts must come from the real observed implementation, not constants here.
|
||||
# Other hooks: evict only this test namespace; hold/release a real SQL lock or
|
||||
# all APPLICATION pool connections; switch baseline/protected implementation;
|
||||
# back up the source and restore only to the explicitly empty test target.
|
||||
# See README.md for contracts and cleanup obligations. No destructive defaults.
|
||||
print("BLOCKED: implement the selected adapter for the chosen application/storage.", file=sys.stderr)
|
||||
raise SystemExit(2)
|
||||
Reference in New Issue
Block a user