#!/usr/bin/env python3 """Run the grade 3 checks and keep small sanitized, reproducible evidence files.""" from datetime import datetime, timezone import json import os from pathlib import Path import platform import subprocess import sys ROOT = Path(__file__).resolve().parents[1] EVIDENCE = ROOT / 'evidence' BASE = os.getenv('BASE_URL', 'http://127.0.0.1:8084') ENV = dict(os.environ, BASE_URL=BASE, PYTHONUTF8='1', PYTHONIOENCODING='utf-8') def run(name, args): print('Running', name, flush=True) proc = subprocess.run(args, cwd=ROOT, env=ENV, text=True, encoding='utf-8', errors='replace', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = '$ ' + ' '.join(args).replace(sys.executable, 'python') + '\n' + proc.stdout output = '\n'.join(line.rstrip() for line in output.splitlines()).rstrip() + '\n' (EVIDENCE / (name + '.txt')).write_text(output, encoding='utf-8') print(proc.stdout, end='', flush=True) if proc.returncode: raise SystemExit('Check failed: ' + name) def main(): EVIDENCE.mkdir(exist_ok=True) run('check', [sys.executable, 'scripts/check_contract.py']) run('compose', ['docker', 'compose', '--env-file', '.env', 'config', '--quiet']) run('smoke', [sys.executable, 'tests/smoke.py', '--base-url', BASE]) run('idempotency', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_idempotency.py']) run('limits', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_limits.py']) run('attachments-grade3', [sys.executable, 'student-tests/grade3.py']) run('persistence', [sys.executable, 'tests/resilience.py', '--mode', 'persistence', '--base-url', BASE, '--action-script', 'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh']) run('failover', [sys.executable, 'tests/resilience.py', '--mode', 'failover', '--base-url', BASE, '--action-script', 'scripts/stop-one.cmd' if os.name == 'nt' else 'scripts/stop-one.sh']) run('restore', [sys.executable, 'scripts/labctl.py', 'restore']) run('smoke-after', [sys.executable, 'tests/smoke.py', '--base-url', BASE]) run('processes', [sys.executable, 'scripts/labctl.py', 'state']) info = json.loads(subprocess.check_output(['docker', 'info', '--format', '{{json .}}'], env=ENV, text=True)) environment = dict(verified_at_utc=datetime.now(timezone.utc).isoformat(), host_os=platform.platform(), client_python=platform.python_version(), docker={k: info[k] for k in ('ServerVersion', 'OperatingSystem', 'KernelVersion', 'NCPU', 'MemTotal', 'Architecture')}, images={}) environment['app_python'] = subprocess.check_output( ['docker', 'compose', 'exec', '-T', 'api', 'python', '--version'], text=True, env=ENV).strip() for name in ('postgres:17.6-bookworm', 'redis:7.4.5-bookworm', 'haproxy:3.0.16-alpine', 'minio/minio:RELEASE.2025-04-22T22-12-26Z'): environment['images'][name] = json.loads(subprocess.check_output( ['docker', 'image', 'inspect', name, '--format', '{{json .RepoDigests}}'], text=True, env=ENV)) (EVIDENCE / 'environment.json').write_text(json.dumps(environment, indent=2) + '\n', encoding='utf-8') print('PASS: all required grade 3 scenarios completed.') if __name__ == '__main__': main()