forked from mtusi2026/mtusi2026lab5
72 lines
4.0 KiB
Python
72 lines
4.0 KiB
Python
"""Reproducible lab5 acceptance on a fresh isolated Compose project."""
|
|
import json
|
|
import hashlib
|
|
import os
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
evidence = ROOT / 'evidence'
|
|
evidence.mkdir(exist_ok=True)
|
|
project = 'mtusilab5-' + uuid.uuid4().hex[:10]
|
|
with socket.socket() as listener:
|
|
listener.bind(('127.0.0.1', 0))
|
|
port = listener.getsockname()[1]
|
|
results = []
|
|
code_manifest = {}
|
|
for directory in ('src', 'migrations', 'infra'):
|
|
for path in sorted((ROOT/directory).rglob('*')):
|
|
if path.is_file() and '__pycache__' not in path.parts:
|
|
code_manifest[str(path.relative_to(ROOT))] = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
for name in ('Dockerfile','compose.yaml','requirements.txt','lab.json'):
|
|
code_manifest[name] = hashlib.sha256((ROOT/name).read_bytes()).hexdigest()
|
|
with tempfile.TemporaryDirectory(prefix='mtusi-lab5-') as temporary:
|
|
envfile = Path(temporary) / '.env'
|
|
subprocess.run([sys.executable, 'scripts/setup-env.py', '--output', str(envfile)], cwd=ROOT, check=True)
|
|
env = dict(os.environ, ENV_FILE=str(envfile), COMPOSE_ENV_FILES=str(envfile),
|
|
COMPOSE_PROJECT_NAME=project, APP_PORT=str(port),
|
|
BASE_URL=f'http://127.0.0.1:{port}')
|
|
compose = ['docker', 'compose', '--env-file', str(envfile), '-p', project]
|
|
def step(name, command):
|
|
started = time.monotonic()
|
|
path = evidence / ('lab5-' + name + '.txt')
|
|
with path.open('w') as output:
|
|
process = subprocess.run(command, cwd=ROOT, env=env, stdout=output, stderr=subprocess.STDOUT)
|
|
result = dict(step=name, result='PASS' if process.returncode == 0 else 'FAIL',
|
|
seconds=round(time.monotonic() - started, 2), evidence=path.name)
|
|
results.append(result)
|
|
print(json.dumps(result), flush=True)
|
|
if process.returncode:
|
|
raise RuntimeError(name + ' failed; see ' + str(path))
|
|
try:
|
|
step('quality', ['make', 'lint', 'check', 'validate', 'PYTHON=' + sys.executable])
|
|
step('clean-start', compose + ['up', '-d', '--build', '--wait'])
|
|
step('public-smoke', [sys.executable, 'course-tests/smoke.py', '--base-url', env['BASE_URL']])
|
|
step('regression', ['make','test-attachments','test-idempotency','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
|
step('migration', compose + ['exec','-T','worker','python','-c', (ROOT/'tests/lab5_migration.py').read_text()])
|
|
step('limits', compose + ['exec','-T','worker','python','-c', (ROOT/'tests/lab5_limits.py').read_text()])
|
|
step('faults', [sys.executable,'tests/lab5.py','--faults','--project',project,'--base-url',env['BASE_URL']])
|
|
step('persistence', ['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
|
step('failover', ['make','test-attachment-failover','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
|
step('review', ['make','test-api-regressions','PYTHON='+sys.executable,'BASE_URL='+env['BASE_URL']])
|
|
step('final-smoke', [sys.executable,'course-tests/smoke.py','--base-url',env['BASE_URL']])
|
|
finally:
|
|
with (evidence/'lab5-container-status.txt').open('w') as output:
|
|
subprocess.run(compose+['ps','-a'],cwd=ROOT,env=env,stdout=output,stderr=subprocess.STDOUT)
|
|
subprocess.run(compose+['down','--volumes','--remove-orphans'],cwd=ROOT,env=env,
|
|
stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,check=True)
|
|
(evidence/'lab5-summary.json').write_text(json.dumps(dict(project=project,
|
|
fresh_volumes=True, code_manifest=code_manifest, results=results),ensure_ascii=False,indent=2)+'\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|