#!/usr/bin/env python3 """Dependency failure and recovery; only this lab's services are controlled.""" import json from pathlib import Path import subprocess import sys import time ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'tests')) from client import Client, Failure c=Client(timeout=8) a=c.user('ready') compose=[str(ROOT/'scripts/compose.sh')] def cmd(*args): return subprocess.check_output(args,text=True,stderr=subprocess.DEVNULL).strip() api=cmd(*compose,'ps','-q','api').splitlines() def probe(container,path): code='import urllib.request,urllib.error;\ntry: r=urllib.request.urlopen("http://127.0.0.1:8080'+path+'",timeout=7)\nexcept urllib.error.HTTPError as e: r=e\nprint(r.code)' return int(cmd('docker','exec',container,'python','-c',code)) report={} try: cmd(*compose,'stop','postgres') report['postgres_stopped']=[{'container':x,'live':probe(x,'/health/live'),'ready':probe(x,'/health/ready')} for x in api] assert all(x['live']==200 and x['ready']==503 for x in report['postgres_stopped']) finally: cmd(*compose,'start','postgres') for _ in range(60): try: c.call('GET','/api/v1/users/me',actor=a,schema='User') break except Failure: time.sleep(.5) else: raise AssertionError('PostgreSQL/session failed to recover') try: cmd(*compose,'stop','redis') report['redis_stopped']=[{'container':x,'ready':probe(x,'/health/ready')} for x in api] assert all(x['ready']==200 for x in report['redis_stopped']) c.call('GET','/api/v1/users/me',actor=a,schema='User') report['redis_optional_profile_fallback']=200 finally: cmd(*compose,'start','redis') report['result']='PASS' (ROOT/'evidence/readiness.json').write_text(json.dumps(report,indent=2)+'\n') print(json.dumps(report,indent=2))