Files
mtusi2026lab7/scripts/audit.py
T
2026-11-07 13:15:01 +03:00

71 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Sequential lab3-5 verification; capture actual output and exit status, fail fast."""
from datetime import datetime, timezone
import argparse
import json
from pathlib import Path
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
DESTINATION = ROOT / "evidence/audit"
CHECKS = [
("contracts", ["make", "check", "validate", "PYTHON=.venv/bin/python"]),
("compose", ["docker", "compose", "config", "--quiet"]),
("smoke", ["make", "test"]),
("attachments", ["make", "test-attachments"]),
("idempotency", [sys.executable, "student-tests/idempotency_concurrency.py"]),
("regressions", [sys.executable, "student-tests/review_regressions.py"]),
("private-bucket", ["make", "test-private"]),
("s3-permissions", ["make", "test-s3-permissions"]),
("bootstrap", ["docker", "compose", "run", "--rm", "--no-deps", "s3-init"]),
("upload-failover", ["make", "test-upload-failover"]),
("public-failover", ["make", "test-failover", "ACTION_SCRIPT=scripts/stop-one.sh"]),
("restore-replicas", ["docker", "compose", "up", "-d", "--wait", "api-a", "api-b", "load_balancer"]),
("s3-outage", ["make", "test-s3-outage"]),
("persistence", [sys.executable, "scripts/verify-persistence.py"]),
("reconcile", ["make", "test-reconcile"]),
("reconcile-audit", ["make", "test-reconcile-audit"]),
("upload-memory", ["make", "test-upload-memory"]),
("reconcile-benchmark", ["make", "test-reconcile-benchmark"]),
("final-smoke", ["make", "test"]),
("environment", [sys.executable, "scripts/record-environment.py"]),
]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--resume-from', choices=[name for name, _ in CHECKS])
args = parser.parse_args()
DESTINATION.mkdir(parents=True, exist_ok=True)
results = []
checks = CHECKS
if args.resume_from:
offset = [name for name, _ in CHECKS].index(args.resume_from)
previous = json.loads((DESTINATION / 'results.json').read_text())['checks']
results = previous[:offset]
if [item['name'] for item in results] != [name for name, _ in CHECKS[:offset]] or any(item['exit_code'] for item in results):
parser.error('resume requires successful evidence for every earlier step')
checks = CHECKS[offset:]
for name, command in checks:
print(f"RUN {name}", flush=True)
started = time.monotonic()
with (DESTINATION / f"{name}.txt").open("w") as stream:
result = subprocess.run(command, cwd=ROOT, stdout=stream, stderr=subprocess.STDOUT)
results.append({"name": name, "command": command, "exit_code": result.returncode,
"seconds": round(time.monotonic() - started, 3)})
(DESTINATION / "results.json").write_text(json.dumps({
"recorded_at_utc": datetime.now(timezone.utc).isoformat(),
"checks": results, "complete": len(results) == len(CHECKS),
}, indent=2) + "\n")
print(f"{'PASS' if result.returncode == 0 else 'FAIL'} {name}: exit={result.returncode}", flush=True)
if result.returncode:
print((DESTINATION / f"{name}.txt").read_text()[-8000:], flush=True)
return result.returncode
return 0
if __name__ == "__main__":
raise SystemExit(main())