Files
mindnet/tests/test_container_connectivity.py
T
LarsandClaude Opus 5.5 8f9f83aa79 Add reachability checks and develop/main container pipeline (inactive)
- preflight --live: Ollama/Qdrant reachable from the container, required
  Ollama models present, 768 dimensions; clear abort instead of traceback.
  Static check rejects unset or container-local service URLs.
- deploy/smoke.py: non-writing post-start check via published ports.
- deploy/deploy.sh: test (no network), build, preflight, up --wait, smoke;
  prod rolls back to the last good image tag.
- Workflow templates deploy-dev.yml (push develop) and deploy-prod.yml
  (push main) following the Kansho pattern; not under .gitea/workflows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 16:01:37 +01:00

259 lines
9.5 KiB
Python

"""
FILE: tests/test_container_connectivity.py
DESCRIPTION: Prüft die Erreichbarkeitsprüfungen der Container-Bereitstellung gegen einen
lokalen Fake-Server (Ollama, Qdrant, Mindnet-API, Streamlit-UI):
- deploy/entrypoint.py: preflight --live (Dienste, Modelle, Dimensionen)
- deploy/smoke.py: Smoke-Test nach dem Start
Läuft ohne Docker und ohne echte Dienste (auch mit --network none).
"""
import importlib.util
import json
import threading
import urllib.error
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
import pytest
DEPLOY = Path(__file__).resolve().parents[1] / "deploy"
def _load(name):
spec = importlib.util.spec_from_file_location(f"mindnet_{name}", DEPLOY / f"{name}.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
ep = _load("entrypoint")
smoke = _load("smoke")
class FakeServices(ThreadingHTTPServer):
"""Ein Server für alle Dienste; Zustand pro Test über Attribute steuerbar."""
def __init__(self):
super().__init__(("127.0.0.1", 0), _Handler)
self.models = ["nomic-embed-text:latest", "phi3:mini"]
self.embedding_dim = 768
self.collections = {"mindnet_dev_notes": 768, "mindnet_dev_chunks": 768}
self.api_key = None
self.seen_headers = []
self.health = {"status": "ok", "prefix": "mindnet_dev"}
self.vector_size = 768
self.counts = {"notes": 12, "chunks": 80, "edges": 40}
self.ui_body = b"ok"
@property
def url(self):
return f"http://127.0.0.1:{self.server_address[1]}"
class _Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def _send(self, status, body):
data = body if isinstance(body, bytes) else json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_POST(self):
srv = self.server
self.rfile.read(int(self.headers.get("Content-Length") or 0))
if self.path == "/api/embeddings":
return self._send(200, {"embedding": [0.1] * srv.embedding_dim})
self._send(404, {})
def do_GET(self):
srv = self.server
srv.seen_headers.append(dict(self.headers))
if self.path.startswith("/collections") and srv.api_key and self.headers.get("api-key") != srv.api_key:
return self._send(401, {})
if self.path == "/api/tags":
return self._send(200, {"models": [{"name": m} for m in srv.models]})
if self.path == "/collections":
return self._send(200, {"result": {"collections": []}})
if self.path.startswith("/collections/"):
name = self.path.rsplit("/", 1)[1]
if name not in srv.collections:
return self._send(404, {})
size = srv.collections[name]
return self._send(200, {"result": {"config": {"params": {"vectors": {"size": size}}}}})
if self.path == "/healthz":
return self._send(200, srv.health)
if self.path == "/admin/stats":
collections = {k: {"name": f"mindnet_dev_{k}", "count": v} for k, v in srv.counts.items()}
return self._send(200, {"collections": collections, "config": {"vector_size": srv.vector_size}})
if self.path == "/_stcore/health":
return self._send(200, srv.ui_body)
self._send(404, {})
@pytest.fixture
def services():
srv = FakeServices()
thread = threading.Thread(target=srv.serve_forever, daemon=True)
thread.start()
yield srv
srv.shutdown()
srv.server_close()
@pytest.fixture
def live_env(monkeypatch, services):
for key in ("QDRANT_API_KEY", "MINDNET_EXPECTED_VECTOR_DIM", "MINDNET_EXPECTED_EMBEDDING_MODEL"):
monkeypatch.delenv(key, raising=False)
monkeypatch.chdir(DEPLOY.parent)
monkeypatch.setenv("MINDNET_OLLAMA_URL", services.url)
monkeypatch.setenv("QDRANT_URL", services.url)
monkeypatch.setenv("COLLECTION_PREFIX", "mindnet_dev")
monkeypatch.setenv("MINDNET_LLM_MODEL", "phi3:mini")
return monkeypatch
def _closed_port_url():
srv = ThreadingHTTPServer(("127.0.0.1", 0), BaseHTTPRequestHandler)
port = srv.server_address[1]
srv.server_close()
return f"http://127.0.0.1:{port}"
# --- Dienst-URLs (statisch) -------------------------------------------------------------
@pytest.mark.parametrize("url", ["", "http://127.0.0.1:11434", "http://localhost:6333", "http://0.0.0.0:6333"])
def test_service_urls_reject_missing_or_container_local(monkeypatch, url):
monkeypatch.setenv("QDRANT_URL", "http://qdrant:6333")
monkeypatch.setenv("MINDNET_OLLAMA_URL", url)
with pytest.raises(ep.PreflightError, match="MINDNET_OLLAMA_URL"):
ep.check_service_urls()
def test_service_urls_accept_host_gateway_and_compose_service(monkeypatch):
monkeypatch.setenv("MINDNET_OLLAMA_URL", "http://host.docker.internal:11434")
monkeypatch.setenv("QDRANT_URL", "http://qdrant:6333")
ep.check_service_urls()
def test_required_models_come_from_ollama_profiles_and_chat_model(live_env):
live_env.setenv("MINDNET_LLM_MODEL", "llama3:8b")
assert {"nomic-embed-text", "phi3:mini", "llama3:8b"} <= ep.required_ollama_models()
# --- preflight --live ----------------------------------------------------------------------
def test_live_passes_when_everything_matches(live_env, capsys):
ep.check_live()
out = capsys.readouterr().out
assert "Ollama erreichbar" in out and "Qdrant erreichbar" in out
def test_live_reports_missing_ollama_model(live_env, services):
services.models = ["nomic-embed-text:latest"]
with pytest.raises(ep.PreflightError, match="phi3:mini"):
ep.check_live()
def test_live_does_not_accept_other_tag_as_latest(live_env, services):
services.models = ["nomic-embed-text:v1.5", "phi3:mini"]
with pytest.raises(ep.PreflightError, match="nomic-embed-text"):
ep.check_live()
def test_live_rejects_wrong_embedding_dimension(live_env, services):
services.embedding_dim = 384
with pytest.raises(ep.PreflightError, match="384 Dimensionen"):
ep.check_live()
def test_live_rejects_wrong_collection_dimension(live_env, services):
services.collections["mindnet_dev_chunks"] = 384
with pytest.raises(ep.PreflightError, match="mindnet_dev_chunks"):
ep.check_live()
def test_live_accepts_missing_collections(live_env, services, capsys):
services.collections = {}
ep.check_live()
assert "existiert noch nicht" in capsys.readouterr().out
def test_live_sends_qdrant_api_key(live_env, services):
services.api_key = "secret"
with pytest.raises(ep.PreflightError, match="HTTP 401"):
ep.check_live()
live_env.setenv("QDRANT_API_KEY", "secret")
ep.check_live()
@pytest.mark.parametrize("var", ["MINDNET_OLLAMA_URL", "QDRANT_URL"])
def test_live_unreachable_service_is_a_clear_error(live_env, var):
live_env.setenv(var, _closed_port_url())
with pytest.raises(ep.PreflightError, match="nicht erreichbar"):
ep.check_live()
def test_unreachable_host_gateway_hints_at_loopback_binding(monkeypatch):
def refuse(*args, **kwargs):
raise urllib.error.URLError(ConnectionRefusedError(111, "Connection refused"))
monkeypatch.setattr(ep.urllib.request, "urlopen", refuse)
with pytest.raises(ep.PreflightError, match="nur an 127.0.0.1"):
ep._fetch_json("http://host.docker.internal:11434/api/tags")
def test_preflight_live_exits_1_instead_of_traceback(live_env, monkeypatch, tmp_path):
root = tmp_path / "vault"
(root / "_system" / "dictionary").mkdir(parents=True)
(root / "_system" / "dictionary" / "edge_vocabulary.md").write_text("x", encoding="utf-8")
for key, value in {"MINDNET_ENV": "dev", "VECTOR_DIM": "768", "MINDNET_VAULT_ROOT": str(root),
"OPENROUTER_API_KEY": "", "GOOGLE_API_KEY": "", "MINDNET_EMBEDDING_MODEL": ""}.items():
monkeypatch.setenv(key, value)
monkeypatch.setattr(ep, "_is_read_only_mount", lambda path: True)
monkeypatch.setattr(ep, "check_service_urls", lambda: None) # Fake-Server läuft auf 127.0.0.1
assert ep.main(["entrypoint.py", "preflight", "--live"]) == 0
monkeypatch.setenv("QDRANT_URL", _closed_port_url())
assert ep.main(["entrypoint.py", "preflight", "--live"]) == 1
# --- smoke.py ---------------------------------------------------------------------------------
def _smoke(services, **kwargs):
return smoke.run(services.url, services.url, "mindnet_dev", attempts=1, **kwargs)
def test_smoke_ok(services, capsys):
assert _smoke(services) == 0
assert "smoke: OK" in capsys.readouterr().out
@pytest.mark.parametrize("change", [
lambda s: s.health.update(prefix="mindnet"),
lambda s: s.health.update(status="degraded"),
lambda s: setattr(s, "vector_size", 384),
lambda s: setattr(s, "ui_body", b"starting"),
])
def test_smoke_fails_on_wrong_state(services, change):
change(services)
assert _smoke(services) == 1
def test_smoke_unreachable_api():
url = _closed_port_url()
assert smoke.run(url, url, "mindnet_dev", attempts=1) == 1
def test_smoke_missing_collection_warns_in_dev_and_fails_with_require_data(services, capsys):
services.counts["edges"] = -1
assert _smoke(services) == 0
assert "WARNUNG" in capsys.readouterr().out
assert _smoke(services, require_data=True) == 1
def test_smoke_require_data_rejects_empty_notes(services):
services.counts["notes"] = 0
assert _smoke(services) == 0
assert _smoke(services, require_data=True) == 1