"""Test: Rechner von Hand aus dem Pool nehmen und geplante Wartung über den Zeitplan.""" import json import os import subprocess import sys import time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from common import KEY, P, ask, check, failures, ground_truth, http, status, where # noqa: E402 BIN = "../../target/debug/omega-proxy" def cli(*args): r = subprocess.run([BIN, "-c", "proxy.toml", *args], capture_output=True, text=True) out = (r.stdout + r.stderr).strip().splitlines() return r.returncode, out[-1] if out else "" def node(name): return next(n for n in status() if n["name"] == name) def wait(cond, secs): end = time.time() + secs while time.time() < end: if cond(): return True time.sleep(0.5) return cond() print("1) Von Hand aus dem Pool: keine neuen Anfragen, Modelle werden entladen") st, _, _ = ask("llamacpp", "qwen3-0.6b") check(st == 200 and where("llamacpp", "qwen3-0.6b") == ["spark1"], "llamacpp/qwen3-0.6b auf spark1 geladen (nur spark1 hat llama.cpp)") code, out = cli("--as", "admin", "pool", "out", "spark1", "--reason", "Treiber-Update") print(f" {out}") check(code == 0, "spark1 aus dem Pool genommen") check(wait(lambda: (node("spark1")["out_of_pool"] or {}).get("reason") == "Treiber-Update", 10), "Statusseite zeigt den Grund") check(wait(lambda: not ground_truth()[1]["spark1"], 20), "Modelle auf spark1 entladen") st, _, body = ask("llamacpp", "qwen3-0.6b") print(f" {st} {json.loads(body).get('error', {}).get('message', body)[:140]}") check(st == 503 and b"nicht im Pool" in body, "Modell nur auf spark1: 503 mit Grund") st, b = http(f"{P}/llamacpp/v1/models", headers={"Authorization": f"Bearer {KEY}"}) check(st == 200 and not json.loads(b)["data"], "Modellliste von llama.cpp leer, solange spark1 nicht im Pool ist") for _ in range(3): st, _, _ = ask("ollama", "smollm2:135m") check(st == 200 and where("ollama", "smollm2:135m") and "spark1" not in where("ollama", "smollm2:135m"), f"Ollama-Anfragen gehen an andere Rechner: {where('ollama', 'smollm2:135m')}") code, out = cli("--as", "admin", "pool", "list") check("spark1" in out and "Treiber-Update" in out, f"pool list: {out}") print("2) Gepinnte Modelle ruhen außerhalb des Pools") code, out = cli("--as", "admin", "schedule", "pin", "llamacpp/qwen3-0.6b", "--note", "Dienst") check(code == 0, f"Pin auf spark1 angelegt, obwohl spark1 gerade von Hand herausgenommen ist: {out}") pin_id = out.split()[1].rstrip(":") check(wait(lambda: node("spark1")["suspended_pins"] == ["llamacpp/qwen3-0.6b"], 10), "Pin ruht") check(not ground_truth()[1]["spark1"], "und wird nicht geladen") print("3) Wieder aufnehmen: der Pin wird geladen, Anfragen gehen wieder") code, out = cli("--as", "admin", "pool", "in", "spark1") check(code == 0, out) check(wait(lambda: "llamacpp:qwen3-0.6b" in ground_truth()[1]["spark1"], 40), "gepinntes Modell wieder geladen") st, _, _ = ask("llamacpp", "qwen3-0.6b") check(st == 200, "Anfrage an llama.cpp geht wieder") code, out = cli("--as", "admin", "pool", "in", "spark1") check(code != 0, f"zweites Aufnehmen abgelehnt: {out}") print("4) Wartung im Zeitplan: Pins auf dem Rechner sind dann nicht möglich") code, out = cli("--as", "admin", "schedule", "maintenance", "spark1", "--until", "+1h", "--note", "Firmware") print(f" {out}") check(code != 0 and "fällt in die Wartung von spark1" in out, "Wartung gegen den dauerhaften Pin abgelehnt") cli("--as", "admin", "schedule", "remove", pin_id) code, out = cli("--as", "admin", "schedule", "maintenance", "spark2", "--until", "+20s", "--note", "Firmware") check(code == 0, f"Wartung für spark2 angelegt: {out}") code, out = cli("--as", "admin", "schedule", "pin", "ollama/smollm2:135m", "--node", "spark2", "--until", "+10m") check(code != 0 and "Wartung" in out, f"Pin auf spark2 während der Wartung abgelehnt: {out}") check(wait(lambda: (node("spark2")["out_of_pool"] or {}).get("manual") is False, 10), "spark2 ist laut Zeitplan in Wartung") check("Firmware" in node("spark2")["out_of_pool"]["reason"], "mit Anmerkung als Grund") for _ in range(3): ask("ollama", "qwen3:0.6b") check("spark2" not in where("ollama", "qwen3:0.6b"), f"keine Anfrage an spark2: {where('ollama', 'qwen3:0.6b')}") code, out = cli("--as", "admin", "pool", "in", "spark2") check(code != 0, f"Wartung laut Zeitplan lässt sich nicht von Hand beenden: {out}") check(wait(lambda: node("spark2")["out_of_pool"] is None, 35), "nach der Wartung wieder im Pool") print("ERGEBNIS:", "alles bestanden" if not failures else f"{len(failures)} Fehler: {failures}")