Files
mtusi2026lab3/scripts/verify.py

61 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Run public checks and all grade 4 demonstrations, preserving sanitized evidence."""
import json
import os
from pathlib import Path
import platform
import subprocess
import sys
from datetime import datetime, timezone
ROOT = Path(__file__).resolve().parents[1]
EVIDENCE = ROOT / 'evidence'
ENV = dict(os.environ, PYTHONUTF8='1', PYTHONIOENCODING='utf-8')
BASE = os.environ.get('BASE_URL', 'http://127.0.0.1:8080')
ENV['BASE_URL'] = BASE
PYTHON = sys.executable
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(PYTHON, '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', [PYTHON, 'scripts/check_contract.py'])
run('compose', ['docker', 'compose', '--env-file', '.env', 'config', '--quiet'])
run('smoke', [PYTHON, 'tests/smoke.py', '--base-url', BASE])
run('idempotency', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_idempotency.py'])
run('migration-repeat', ['docker', 'compose', 'run', '--rm', '--no-deps', 'migrate'])
run('persistence', [PYTHON, 'tests/resilience.py', '--mode', 'persistence', '--base-url', BASE,
'--action-script', 'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh'])
run('restore-before-failover', [PYTHON, 'scripts/labctl.py', 'restore'])
run('failover', [PYTHON, '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-after-failover', [PYTHON, 'scripts/labctl.py', 'restore'])
run('load-grade4', [PYTHON, 'student-tests/test_recovery.py'])
run('smoke-after', [PYTHON, 'tests/smoke.py', '--base-url', BASE])
run('processes', [PYTHON, 'scripts/labctl.py', 'state'])
info = json.loads(subprocess.check_output(['docker', 'info', '--format', '{{json .}}'], 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={})
for name in ('postgres:17.6-bookworm', 'redis:7.4.5-bookworm', 'haproxy:3.0.16-alpine'):
environment['images'][name] = json.loads(subprocess.check_output(
['docker', 'image', 'inspect', name, '--format', '{{json .RepoDigests}}'], text=True))
(EVIDENCE / 'environment.json').write_text(json.dumps(environment, indent=2) + '\n', encoding='utf-8')
print('PASS: public checks and every grade 4 scenario complete.')
if __name__ == '__main__':
main()