Wait for actual replica health after deliberate failure
This commit is contained in:
@@ -7,6 +7,16 @@ ROOT=Path(__file__).resolve().parents[1]
|
||||
def docker(*args):
|
||||
return subprocess.check_output(['docker',*args],cwd=ROOT,text=True,encoding='utf-8',stderr=subprocess.PIPE).strip()
|
||||
def replicas(): return docker('compose','ps','-q','api').splitlines()
|
||||
|
||||
def ready_replicas(count=2,timeout=60):
|
||||
deadline=time.monotonic()+timeout
|
||||
while time.monotonic()<deadline:
|
||||
ids=replicas()
|
||||
if len(ids)==count:
|
||||
states=[json.loads(docker('inspect','--format','{{json .State}}',cid)) for cid in ids]
|
||||
if all(s['Running'] and s.get('Health',{}).get('Status')=='healthy' for s in states): return
|
||||
time.sleep(.5)
|
||||
raise AssertionError('Actual replica health did not recover within timeout')
|
||||
def instance(cid):
|
||||
return docker('exec',cid,'python','-c',"import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live',timeout=2).headers['X-Instance-Id'])")
|
||||
def state():
|
||||
|
||||
@@ -11,7 +11,7 @@ import time
|
||||
import uuid
|
||||
|
||||
import httpx
|
||||
from stack import ROOT, docker, instance, ready, replicas, state
|
||||
from stack import ROOT, docker, instance, ready, ready_replicas, replicas, state
|
||||
|
||||
OUT = ROOT / 'evidence'
|
||||
OUT.mkdir(exist_ok=True)
|
||||
@@ -49,7 +49,8 @@ def history(c, headers, path):
|
||||
|
||||
|
||||
def restore(c):
|
||||
docker('compose', 'up', '-d', '--no-deps', '--wait', '--scale', 'api=2', 'api')
|
||||
docker('compose', 'up', '-d', '--no-deps', '--scale', 'api=2', 'api')
|
||||
ready_replicas()
|
||||
ready(c)
|
||||
deadline = time.monotonic() + 20
|
||||
while time.monotonic() < deadline:
|
||||
@@ -121,7 +122,8 @@ def fault_scenario(c, uid, headers, target, rolling=False):
|
||||
assert json.loads(docker('inspect', '--format', '{{json .State.Running}}', cid)) is False
|
||||
if rolling:
|
||||
docker('rm', cid)
|
||||
docker('compose', 'up', '-d', '--no-deps', '--no-recreate', '--wait', '--scale', 'api=2', 'api')
|
||||
docker('compose', 'up', '-d', '--no-deps', '--no-recreate', '--scale', 'api=2', 'api')
|
||||
ready_replicas()
|
||||
ready(c)
|
||||
event['replacement_ready_at'] = time.monotonic() - start
|
||||
events.append(event)
|
||||
|
||||
Reference in New Issue
Block a user