58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""SIGTERM must stop the container in <= 10s and refuse new requests."""
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
|
|
def _http_ok(url):
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=2) as resp:
|
|
return 200 <= resp.status < 300
|
|
except Exception: # noqa: BLE001
|
|
return False
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument('--container', default=os.environ.get('CONTAINER', 'mtusi-messenger-lab1'))
|
|
ap.add_argument('--base-url', default=os.environ.get('BASE_URL', 'http://localhost:8080'))
|
|
args = ap.parse_args()
|
|
|
|
if not _http_ok(args.base_url + '/health/live'):
|
|
print('FAIL: server is not alive before SIGTERM')
|
|
return 1
|
|
|
|
start = time.monotonic()
|
|
subprocess.run(['docker', 'kill', '--signal', 'SIGTERM', args.container],
|
|
check=True)
|
|
deadline = start + 12
|
|
while time.monotonic() < deadline:
|
|
state = subprocess.run(
|
|
['docker', 'inspect', '-f', '{{.State.Running}}', args.container],
|
|
capture_output=True, text=True).stdout.strip()
|
|
if state == 'false':
|
|
break
|
|
time.sleep(0.15)
|
|
else:
|
|
print('FAIL: container did not stop within 12 s')
|
|
return 1
|
|
|
|
elapsed = time.monotonic() - start
|
|
if elapsed > 10.0:
|
|
print('FAIL: SIGTERM took %.2f s (>10)' % elapsed)
|
|
return 1
|
|
if _http_ok(args.base_url + '/health/live'):
|
|
print('FAIL: server still accepts requests after SIGTERM')
|
|
return 1
|
|
print('PASS: SIGTERM stopped container in %.2f s; port refused afterwards'
|
|
% elapsed)
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main()) |