forked from mtusi2026/mtusi2026lab2
70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Controlled Redis outage check for the local Compose stack; restores Redis in finally."""
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
|
|
def request_status(url, timeout=15):
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=timeout) as response:
|
|
return response.status, response.read()
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code, exc.read()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--base-url', default='http://localhost:8080')
|
|
parser.add_argument('--timeout', type=int, default=45)
|
|
#parser.add_argument('--outage-timeout', type=int, default=30)
|
|
parser.add_argument('--compose-file', default='.env')
|
|
parser.add_argument('--target', choices=('redis', 'postgres'), default='redis')
|
|
args = parser.parse_args()
|
|
compose = ['docker', 'compose', '--env-file', args.compose_file]
|
|
stopped = False
|
|
try:
|
|
code, body = request_status(args.base_url.rstrip('/') + '/health/ready')
|
|
if code != 200:
|
|
raise RuntimeError('API must be ready before the test; got HTTP %s' % code)
|
|
stopped = True
|
|
subprocess.run(compose + ['stop', args.target], check=True)
|
|
deadline = time.monotonic() + 30
|
|
outage_status = None
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
outage_status, body = request_status(args.base_url.rstrip('/') + '/health/ready')
|
|
if outage_status == 503:
|
|
break
|
|
except (TimeoutError, OSError):
|
|
pass
|
|
time.sleep(0.25)
|
|
if outage_status != 503:
|
|
raise RuntimeError('readiness did not return HTTP 503 while %s was stopped (last=%r)' % (args.target, outage_status))
|
|
print('PASS: /health/ready returned HTTP 503 while %s was unavailable' % args.target)
|
|
finally:
|
|
if stopped:
|
|
subprocess.run(compose + ['start', args.target], check=False)
|
|
deadline = time.monotonic() + args.timeout
|
|
recovered = False
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
code, body = request_status(args.base_url.rstrip('/') + '/health/ready')
|
|
if code == 200:
|
|
recovered = True
|
|
break
|
|
except (TimeoutError, OSError):
|
|
pass
|
|
time.sleep(0.5)
|
|
if recovered:
|
|
print('PASS: readiness recovered to HTTP 200 after %s restart' % args.target)
|
|
else:
|
|
raise RuntimeError('%s did not recover to ready state; inspect docker compose logs %s api' % (args.target, args.target))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|