"""Run the persistence hook and record actual service restart timestamps.""" import json import os from pathlib import Path import subprocess ROOT = Path(__file__).resolve().parents[1] SERVICES = ('postgres', 's3', 'broker', 'openbao', 'prometheus', 'loki', 'tempo', 'api-a', 'api-b') def started(service): container = subprocess.check_output(['docker', 'compose', 'ps', '-q', service], cwd=ROOT, text=True).strip() if not container: raise RuntimeError(f'{service} is not running') return subprocess.check_output(['docker', 'inspect', '--format', '{{.State.StartedAt}}', container], text=True).strip() before = {service: started(service) for service in SERVICES} result = subprocess.run(['make', 'test-persistence', 'ACTION_SCRIPT=scripts/restart.sh'], cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) print(result.stdout, end='') if result.returncode: raise SystemExit(result.returncode) after = {service: started(service) for service in SERVICES} for service in SERVICES: if after[service] <= before[service]: raise RuntimeError(f'{service} did not restart') evidence = {'status': 'PASS', 'command': 'make test-persistence ACTION_SCRIPT=scripts/restart.sh', 'services': {service: {'before': before[service], 'after': after[service]} for service in SERVICES}, 'test_output': result.stdout.strip()} path = ROOT / 'evidence' / 'lab7-persistence.json' path.write_text(json.dumps(evidence, ensure_ascii=False, indent=2) + '\n') print('PASS: restart timestamps recorded in evidence/lab7-persistence.json')