forked from mtusi2026/mtusi2026lab2
75 lines
4.2 KiB
Python
75 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Run the grade 4 checks and preserve small sanitized evidence files."""
|
|
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, BASE_URL=os.environ.get('BASE_URL', 'http://127.0.0.1:8080'), PYTHONIOENCODING='utf-8')
|
|
python = sys.executable
|
|
if hasattr(sys.stdout, 'reconfigure'):
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
|
|
|
|
def run(name, command):
|
|
print('Running', name, flush=True)
|
|
process = subprocess.run(command, cwd=ROOT, env=env, text=True, encoding='utf-8', errors='replace',
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
log = '$ ' + ' '.join(command).replace(python, 'python') + '\n' + process.stdout
|
|
log = '\n'.join(line.rstrip() for line in log.splitlines()).rstrip() + '\n'
|
|
(EVIDENCE / (name + '.txt')).write_text(log, encoding='utf-8')
|
|
print(process.stdout, end='', flush=True)
|
|
if process.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', env['BASE_URL']])
|
|
run('own-api', [python, 'student-tests/test_api.py'])
|
|
run('database', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_database.py'])
|
|
run('persistence', [python, 'tests/resilience.py', '--mode', 'persistence', '--action-script',
|
|
'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh', '--base-url', env['BASE_URL']])
|
|
run('expiry', [python, 'scripts/check_expiry.py'])
|
|
run('outages', [python, 'student-tests/test_outages.py'])
|
|
run('demo', [python, 'scripts/demo.py'])
|
|
run('durability-postgres', ['docker', 'compose', 'exec', '-T', 'db', 'psql', '-U', 'messenger',
|
|
'-d', 'messenger', '-c', 'SHOW fsync;', '-c', 'SHOW synchronous_commit;',
|
|
'-c', 'SHOW full_page_writes;'])
|
|
run('durability-redis', ['docker', 'compose', 'exec', '-T', 'redis', 'redis-cli',
|
|
'CONFIG', 'GET', 'appendonly', 'appendfsync'])
|
|
output = subprocess.check_output(['docker', 'compose', 'exec', '-T', 'api', 'python',
|
|
'scripts/explain_history.py'], cwd=ROOT, text=True)
|
|
plan = json.loads(output)
|
|
(EVIDENCE / 'explain.json').write_text(json.dumps(plan, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
|
|
print('PASS: EXPLAIN on', plan['messages'], 'messages uses messages_history.', flush=True)
|
|
run('resources', ['docker', 'stats', '--no-stream', '--format',
|
|
'{{.Name}} CPU={{.CPUPerc}} RAM={{.MemUsage}}',
|
|
'mtusi-lab2-osokin-api-1', 'mtusi-lab2-osokin-db-1', 'mtusi-lab2-osokin-redis-1'])
|
|
environment = dict(verified_at_utc=datetime.now(timezone.utc).isoformat(), os=platform.platform(),
|
|
client_python=platform.python_version())
|
|
environment['docker'] = json.loads(subprocess.check_output(
|
|
['docker', 'info', '--format', '{{json .}}'], text=True))
|
|
# Whitelist machine properties; never persist Docker client config/proxy details.
|
|
environment['docker'] = {k: environment['docker'][k] for k in
|
|
('ServerVersion', 'OperatingSystem', 'KernelVersion', 'NCPU', 'MemTotal', 'Architecture')}
|
|
environment['images'] = {}
|
|
environment['runtime_python'] = subprocess.check_output(
|
|
['docker', 'compose', 'exec', '-T', 'api', 'python', '--version'], cwd=ROOT, text=True).strip()
|
|
for image in ('postgres:17.6-bookworm', 'redis:7.4.5-bookworm'):
|
|
environment['images'][image] = json.loads(subprocess.check_output(
|
|
['docker', 'image', 'inspect', image, '--format', '{{json .RepoDigests}}'], text=True))
|
|
(EVIDENCE / 'environment.json').write_text(json.dumps(environment, indent=2) + '\n', encoding='utf-8')
|
|
print('PASS: all grade 4 scenarios completed; no grade 5 track was run.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|