service_smoke.py
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""Minimal local smoke test for the FastAPI ACR service."""
from __future__ import annotations
import json
import subprocess
import time
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
BASE = "http://127.0.0.1:8000"
def fetch_json(path: str):
with urlopen(BASE + path, timeout=10) as resp:
return json.loads(resp.read().decode("utf-8"))
def main():
cmd = [
"/usr/local/miniconda3/bin/python",
"-m",
"uvicorn",
"src.service.app:app",
"--host",
"127.0.0.1",
"--port",
"8000",
]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
try:
last_error = None
for _ in range(20):
time.sleep(0.5)
try:
health = fetch_json("/health")
ready = fetch_json("/ready")
config = fetch_json("/config")
cache = fetch_json("/cache")
print(json.dumps({
"status": "ok",
"health": health,
"ready": ready,
"config": config,
"cache": cache,
}, indent=2, ensure_ascii=False))
return
except (URLError, HTTPError, ConnectionError) as exc:
last_error = str(exc)
raise SystemExit(json.dumps({
"status": "failed",
"reason": "service_not_ready_in_time",
"last_error": last_error,
}, indent=2, ensure_ascii=False))
finally:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=5)
if __name__ == "__main__":
main()