""" FILE: deploy/smoke.py DESCRIPTION: Nicht-schreibender Smoke-Test gegen einen laufenden Mindnet-Stack, aus Sicht des Hosts über die veröffentlichten Ports. Nur Standardbibliothek, läuft daher direkt auf dem Runner (python3), ohne venv. - API /healthz: Status ok, erwartetes Collection-Präfix - API /admin/stats: Vektorgröße 768; API erreicht Qdrant (Collection-Counts) - UI /_stcore/health --require-data (Prod): alle Collections vorhanden und Notes nicht leer. VERSION: 1.0.0 STATUS: Active USAGE: smoke.py --api http://127.0.0.1:8012 --ui http://127.0.0.1:8512 --prefix mindnet_dev [--require-data] """ from __future__ import annotations import argparse import json import sys import time import urllib.error import urllib.request EXPECTED_VECTOR_SIZE = 768 class SmokeError(RuntimeError): pass def _get(url: str, timeout: float = 15) -> bytes: try: with urllib.request.urlopen(url, timeout=timeout) as res: return res.read() except urllib.error.HTTPError as e: raise SmokeError(f"{url}: HTTP {e.code}") from None except (urllib.error.URLError, OSError) as e: raise SmokeError(f"{url} nicht erreichbar ({getattr(e, 'reason', e)})") from None def _get_json(url: str) -> dict: try: return json.loads(_get(url)) except ValueError: raise SmokeError(f"{url} liefert kein JSON") from None def check_api(api: str, prefix: str, require_data: bool) -> list[str]: """Gibt Warnungen zurück; harte Fehler als SmokeError.""" health = _get_json(f"{api}/healthz") if health.get("status") != "ok": raise SmokeError(f"/healthz meldet {health.get('status')!r}") if health.get("prefix") != prefix: raise SmokeError(f"API nutzt Präfix {health.get('prefix')!r}, erwartet {prefix!r}") stats = _get_json(f"{api}/admin/stats") size = (stats.get("config") or {}).get("vector_size") if size != EXPECTED_VECTOR_SIZE: raise SmokeError(f"API-Vektorgröße {size}, erwartet {EXPECTED_VECTOR_SIZE}") warnings = [] collections = stats.get("collections") or {} for kind in ("notes", "chunks", "edges"): entry = collections.get(kind) or {} count = entry.get("count", -1) if count < 0: # -1: Collection fehlt oder Qdrant ist aus dem API-Container nicht erreichbar. msg = f"Collection {entry.get('name', kind)!r} nicht lesbar (fehlt oder Qdrant nicht erreichbar)" if require_data: raise SmokeError(msg) warnings.append(msg) if require_data and (collections.get("notes") or {}).get("count", 0) <= 0: raise SmokeError("Notes-Collection ist leer") return warnings def check_ui(ui: str) -> None: body = _get(f"{ui}/_stcore/health").decode("utf-8", "replace").strip() if body != "ok": raise SmokeError(f"UI-Health meldet {body!r}") def run(api: str, ui: str, prefix: str, require_data: bool = False, attempts: int = 1, delay: float = 5) -> int: api, ui = api.rstrip("/"), ui.rstrip("/") for attempt in range(1, attempts + 1): try: warnings = check_api(api, prefix, require_data) check_ui(ui) except SmokeError as e: if attempt == attempts: print(f"smoke: FEHLER: {e}", file=sys.stderr) return 1 time.sleep(delay) continue for w in warnings: print(f"smoke: WARNUNG: {w}") print(f"smoke: OK (api={api}, ui={ui}, prefix={prefix})") return 0 return 1 def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("USAGE:")[0]) parser.add_argument("--api", required=True) parser.add_argument("--ui", required=True) parser.add_argument("--prefix", required=True) parser.add_argument("--require-data", action="store_true") parser.add_argument("--attempts", type=int, default=3) args = parser.parse_args(argv) return run(args.api, args.ui, args.prefix, args.require_data, args.attempts) if __name__ == "__main__": sys.exit(main())