forked from mtusi2026/mtusi2026lab4
69 lines
2.9 KiB
Python
69 lines
2.9 KiB
Python
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def call(name, args):
|
|
result = subprocess.run(args, cwd=ROOT, capture_output=True, text=True, encoding='utf-8', env=os.environ | {'PYTHONUTF8': '1'})
|
|
(ROOT / 'evidence' / name).write_text(result.stdout + result.stderr, encoding='utf-8')
|
|
print(name + ': ' + str(result.returncode), flush=True)
|
|
if result.returncode:
|
|
print(result.stdout + result.stderr)
|
|
raise SystemExit(result.returncode)
|
|
|
|
|
|
def snapshot(name):
|
|
ids = subprocess.check_output(['docker', 'compose', 'ps', '-q'], cwd=ROOT, text=True).split()
|
|
rows = json.loads(subprocess.check_output(['docker', 'inspect', *ids], text=True))
|
|
safe = [{'name': r['Name'], 'id': r['Id'][:12], 'started': r['State']['StartedAt'],
|
|
'image': r['Config']['Image'], 'memory_limit': r['HostConfig']['Memory'],
|
|
'cpus': r['HostConfig']['NanoCpus']/1e9} for r in rows]
|
|
(ROOT / 'evidence' / name).write_text(json.dumps(safe, indent=2), encoding='utf-8')
|
|
|
|
|
|
def ready():
|
|
for _ in range(90):
|
|
try:
|
|
with urllib.request.urlopen('http://localhost:8080/health/ready', timeout=3) as response:
|
|
if response.status == 200:
|
|
return
|
|
except Exception:
|
|
pass
|
|
time.sleep(1)
|
|
raise RuntimeError('Readiness timeout')
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--full', action='store_true')
|
|
args = parser.parse_args()
|
|
call('contract.txt', [sys.executable, '-X', 'utf8', 'scripts/check_contract.py'])
|
|
call('smoke.txt', [sys.executable, '-X', 'utf8', 'tests/smoke.py'])
|
|
if not args.full:
|
|
return
|
|
call('acceptance.txt', [sys.executable, '-X', 'utf8', 'student-tests/acceptance.py'])
|
|
call('consistency.txt', [sys.executable, '-X', 'utf8', 'student-tests/consistency.py'])
|
|
call('audit.txt', [sys.executable, '-X', 'utf8', 'scripts/audit.py'])
|
|
call('runtime-packages.txt', ['docker', 'compose', 'exec', '-T', 'api', 'pip', 'freeze'])
|
|
snapshot('before-restart.json')
|
|
suffix = 'cmd' if os.name == 'nt' else 'sh'
|
|
call('persistence.txt', [sys.executable, '-X', 'utf8', 'tests/resilience.py', '--mode', 'persistence', '--action-script', 'scripts/restart.' + suffix])
|
|
snapshot('after-restart.json')
|
|
for repeat in (1, 2):
|
|
call('failover-' + str(repeat) + '.txt', [sys.executable, '-X', 'utf8', 'tests/resilience.py', '--mode', 'failover', '--action-script', 'scripts/stop-one.' + suffix])
|
|
call('restore-' + str(repeat) + '.txt', ['docker', 'compose', 'up', '-d', '--no-deps', 'api'])
|
|
ready()
|
|
call('smoke-final.txt', [sys.executable, '-X', 'utf8', 'tests/smoke.py'])
|
|
call('summarize.txt', [sys.executable, '-X', 'utf8', 'scripts/summarize.py'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|