forked from mtusi2026/mtusi2026lab6
49 lines
2.2 KiB
Python
49 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Sequential public tests with safe container-state evidence; no credentials."""
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def states():
|
|
ids = subprocess.check_output(['docker', 'compose', 'ps', '-aq'], text=True).split()
|
|
rows = json.loads(subprocess.check_output(['docker', 'inspect', *ids], text=True))
|
|
return {r['Config']['Labels']['com.docker.compose.service']:
|
|
{'id': r['Id'][:12], 'started_at': r['State']['StartedAt'], 'status': r['State']['Status']}
|
|
for r in rows}
|
|
|
|
|
|
def main():
|
|
results, stopped = [], set()
|
|
try:
|
|
for mode in ['persistence'] + ['failover'] * 6:
|
|
before = states()
|
|
hook = 'restart' if mode == 'persistence' else 'stop-one'
|
|
result = subprocess.run([sys.executable, 'tests/resilience.py', '--mode', mode,
|
|
'--action-script', 'scripts/' + hook + '.sh'],
|
|
text=True, capture_output=True, timeout=200)
|
|
after = states()
|
|
results.append(dict(mode=mode, returncode=result.returncode,
|
|
output=result.stdout + result.stderr, before=before, after=after))
|
|
(ROOT / 'evidence/resilience.json').write_text(json.dumps(results, indent=2) + '\n')
|
|
print(result.stdout + result.stderr, flush=True)
|
|
assert result.returncode == 0, 'public resilience failed'
|
|
if mode == 'persistence':
|
|
for service in ('postgres', 's3', 'broker', 'worker', 'outbox', 'api-a', 'api-b'):
|
|
assert before[service]['started_at'] != after[service]['started_at'], service
|
|
else:
|
|
stopped.update(s for s in ('api-a', 'api-b') if after[s]['status'] == 'exited')
|
|
subprocess.run(['docker', 'compose', 'up', '-d', '--no-deps', '--wait', 'api-a', 'api-b'], check=True)
|
|
if len(stopped) == 2:
|
|
break
|
|
assert len(stopped) == 2, 'did not exercise both API replicas'
|
|
finally:
|
|
subprocess.run(['docker', 'compose', 'up', '-d', '--no-deps', '--wait', 'api-a', 'api-b'], check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|