Files
mtusi2026lab5/scripts/verify.py
T

87 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Run reproducible acceptance checks against an already running local lab5 stand.
The default checks do not stop services. --faults explicitly includes the grade 4
fault suite; each fault restores services without deleting volumes or secrets.
"""
import argparse
import os
from pathlib import Path
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, Failure
def wait_ready(url):
client = Client(url)
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
client.call('GET', '/health/ready', schema='Health')
return
except Failure:
time.sleep(0.5)
raise Failure('API did not become ready before verification block')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--base-url', default=os.environ.get('BASE_URL', 'http://127.0.0.1:8080'))
parser.add_argument('--faults', action='store_true', help='Also run disruptive grade 4 fault checks')
parser.add_argument('--only', nargs='+', choices=['offline', 'unit', 'smoke', 'tests', 'lab5-checks',
'persistence', 'failover', 'postgres-pause', 'cleanup', 'stack'])
parser.add_argument('--output-prefix', default='', help='Keep a separate set of UTF-8 evidence logs')
options = parser.parse_args()
if Path(options.output_prefix).name != options.output_prefix or any(c in options.output_prefix for c in '/\\'):
parser.error('output prefix must be a filename prefix')
checks = [
('offline', ['scripts/check_contract.py']),
('unit', ['docker', 'compose', 'run', '--rm', '--no-deps', '--entrypoint', 'python',
'-v', './unit-tests:/app/unit-tests:ro', 'worker', '-m', 'unittest', 'discover', '-s', 'unit-tests', '-v']),
('smoke', ['tests/smoke.py', '--base-url', options.base_url]),
('tests', ['-m', 'unittest', 'discover', '-s', 'student-tests', '-v']),
('cleanup', ['docker', 'compose', '--profile', 'tools', 'run', '--rm', '--no-deps', 'cleanup']),
('stack', ['scripts/stack_evidence.py']),
]
if options.faults or (options.only and 'lab5-checks' in options.only):
checks.insert(4, ('lab5-checks', ['scripts/lab5_checks.py', '--base-url', options.base_url]))
for name, mode, hook in (
('persistence', 'persistence', 'restart'), ('failover', 'failover', 'stop-one')
):
if options.faults or (options.only and name in options.only):
script = 'scripts/%s.%s' % (hook, 'cmd' if os.name == 'nt' else 'sh')
checks.insert(-1, (name, ['tests/resilience.py', '--mode', mode, '--action-script', script,
'--base-url', options.base_url]))
if options.faults or (options.only and 'postgres-pause' in options.only):
checks.insert(-1, ('postgres-pause', ['scripts/check_postgres_pause.py', '--base-url', options.base_url]))
(ROOT / 'evidence').mkdir(exist_ok=True)
for name, args in checks:
if options.only and name not in options.only:
continue
if name != 'offline':
wait_ready(options.base_url)
invocation = args if name in ('unit', 'cleanup') else [sys.executable, '-X', 'utf8', *args]
result = subprocess.run(invocation, cwd=ROOT,
env=dict(os.environ, BASE_URL=options.base_url), capture_output=True)
if name == 'failover':
restored = subprocess.run(['docker', 'compose', 'up', '-d', '--wait'], cwd=ROOT,
capture_output=True)
if restored.returncode:
print('Failed to restore API replica after failover', flush=True)
return restored.returncode
content = result.stdout.decode('utf-8', errors='replace') + result.stderr.decode('utf-8', errors='replace')
(ROOT / 'evidence' / (options.output_prefix + name + '.txt')).write_text(content, encoding='utf-8')
print(name + ': ' + ('PASS' if result.returncode == 0 else 'FAIL'), flush=True)
if result.returncode:
print(content[-6000:], flush=True)
return result.returncode
return 0
if __name__ == '__main__':
sys.exit(main())