Files
mindnet/tests/test_container_preflight.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

282 lines
11 KiB
Python

"""
FILE: tests/test_container_preflight.py
DESCRIPTION: Prüft Container-Preflight (deploy/entrypoint.py) und statisch die
Sicherheitszusagen der Compose-/Docker-Dateien. Läuft ohne Docker.
"""
import importlib.util
import re
from pathlib import Path
import pytest
import yaml
ROOT = Path(__file__).resolve().parents[1]
DEPLOY = ROOT / "deploy"
SYSTEMD_PORTS = {8001, 8002, 8501, 8502}
def _load_entrypoint():
spec = importlib.util.spec_from_file_location("mindnet_entrypoint", DEPLOY / "entrypoint.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
ep = _load_entrypoint()
@pytest.fixture
def env(monkeypatch):
for key in ("MINDNET_ENV", "COLLECTION_PREFIX", "VECTOR_DIM", "MINDNET_EMBEDDING_MODEL",
"OPENROUTER_API_KEY", "GOOGLE_API_KEY", "MINDNET_ALLOW_CLOUD_EGRESS",
"MINDNET_VAULT_ROOT", "MINDNET_VAULT_SENTINEL", "MINDNET_VAULT_READONLY",
"MINDNET_VAULT_REQUIRED", "MINDNET_EXPECTED_VECTOR_DIM"):
monkeypatch.delenv(key, raising=False)
monkeypatch.chdir(ROOT)
return monkeypatch
# --- Umgebung und Präfix ---------------------------------------------------------
@pytest.mark.parametrize("menv, prefix", [("", "mindnet"), ("staging", "mindnet"), ("prod", ""),
("dev", "mindnet"), ("prod", "mindnet_dev")])
def test_environment_rejects_ambiguous_setups(env, menv, prefix):
env.setenv("MINDNET_ENV", menv)
env.setenv("COLLECTION_PREFIX", prefix)
with pytest.raises(ep.PreflightError):
ep.check_environment()
@pytest.mark.parametrize("menv, prefix", [("dev", "mindnet_dev"), ("prod", "mindnet")])
def test_environment_accepts_explicit_setups(env, menv, prefix):
env.setenv("MINDNET_ENV", menv)
env.setenv("COLLECTION_PREFIX", prefix)
assert ep.check_environment() == menv
# --- Embeddings: keine stille Rückkehr zu 384 Dimensionen -----------------------
@pytest.mark.parametrize("dim", [None, "384", "3072"])
def test_embeddings_reject_wrong_or_missing_dim(env, dim):
if dim:
env.setenv("VECTOR_DIM", dim)
with pytest.raises(ep.PreflightError, match="VECTOR_DIM"):
ep.check_embeddings()
def test_embeddings_accept_repository_profile(env):
env.setenv("VECTOR_DIM", "768")
ep.check_embeddings()
def test_embeddings_reject_minilm_override(env):
env.setenv("VECTOR_DIM", "768")
env.setenv("MINDNET_EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
with pytest.raises(ep.PreflightError, match="MINDNET_EMBEDDING_MODEL"):
ep.check_embeddings()
@pytest.mark.parametrize("profile", [
{"provider": "ollama", "model": "all-minilm", "dimensions": 384},
{"provider": "ollama", "model": "nomic-embed-text", "dimensions": 384},
{"provider": "ollama", "model": "nomic-embed-text"},
])
def test_embeddings_reject_bad_profile(env, tmp_path, profile):
env.setenv("VECTOR_DIM", "768")
path = tmp_path / "profiles.yaml"
path.write_text(yaml.safe_dump({"profiles": {"embedding_expert": profile}}), encoding="utf-8")
with pytest.raises(ep.PreflightError, match="embedding_expert"):
ep.check_embeddings(str(path))
# --- Cloud-Egress ------------------------------------------------------------------
def test_cloud_keys_require_explicit_ack(env):
env.setenv("OPENROUTER_API_KEY", "sk-test")
with pytest.raises(ep.PreflightError, match="MINDNET_ALLOW_CLOUD_EGRESS"):
ep.check_cloud_egress()
env.setenv("MINDNET_ALLOW_CLOUD_EGRESS", "true")
ep.check_cloud_egress()
def test_no_cloud_keys_needs_no_ack(env):
ep.check_cloud_egress()
# --- Vault / NAS -----------------------------------------------------------------------
def _vault(tmp_path, with_sentinel=True):
root = tmp_path / "vault"
root.mkdir()
if with_sentinel:
sentinel = root / "_system" / "dictionary" / "edge_vocabulary.md"
sentinel.parent.mkdir(parents=True)
sentinel.write_text("# vocab", encoding="utf-8")
return root
def test_vault_missing_directory(env, tmp_path):
env.setenv("MINDNET_VAULT_ROOT", str(tmp_path / "nope"))
with pytest.raises(ep.PreflightError, match="fehlt"):
ep.check_vault()
def test_vault_empty_mountpoint_is_detected(env, tmp_path):
# Ausgefallenes NAS: Mountpunkt existiert, ist aber leer.
env.setenv("MINDNET_VAULT_ROOT", str(_vault(tmp_path, with_sentinel=False)))
with pytest.raises(ep.PreflightError, match="Sentinel"):
ep.check_vault()
def test_vault_must_be_read_only(env, tmp_path):
env.setenv("MINDNET_VAULT_ROOT", str(_vault(tmp_path)))
env.setattr(ep, "_is_read_only_mount", lambda path: False)
with pytest.raises(ep.PreflightError, match="schreibgeschützt"):
ep.check_vault()
env.setattr(ep, "_is_read_only_mount", lambda path: True)
ep.check_vault()
def test_read_only_check_fails_closed_without_statvfs(env, tmp_path):
env.delattr(ep.os, "statvfs", raising=False)
assert ep._is_read_only_mount(tmp_path) is False
def test_unknown_mode_is_rejected():
assert ep.main(["entrypoint.py", "shell"]) == 2
# --- Statische Prüfung der Deployment-Dateien ---------------------------------------
def _compose(name):
return yaml.safe_load((DEPLOY / name).read_text(encoding="utf-8"))
def _host_port(mapping):
m = re.search(r":(?:\$\{[A-Z_]+:-)?(\d+)\}?:\d+$", mapping)
return int(m.group(1))
COMPOSE = {"dev": _compose("compose.dev.yml"), "prod": _compose("compose.prod.yml")}
APP_SERVICES = ("api", "ui")
def test_compose_projects_are_separate():
assert COMPOSE["dev"]["name"] == "mindnet-dev"
assert COMPOSE["prod"]["name"] == "mindnet-prod"
assert set(COMPOSE["dev"]["networks"]).isdisjoint(COMPOSE["prod"]["networks"])
def test_compose_ports_do_not_collide():
ports = {e: {_host_port(p) for s in c["services"].values() for p in s.get("ports", [])}
for e, c in COMPOSE.items()}
assert ports["dev"].isdisjoint(ports["prod"])
assert not (ports["dev"] | ports["prod"]) & SYSTEMD_PORTS
@pytest.mark.parametrize("envname", ["dev", "prod"])
def test_compose_app_services_hardened(envname):
for name in APP_SERVICES:
svc = COMPOSE[envname]["services"][name]
assert svc["read_only"] is True
assert svc["cap_drop"] == ["ALL"]
assert "no-new-privileges:true" in svc["security_opt"]
assert svc["environment"]["VECTOR_DIM"] == "768"
assert svc["environment"]["MINDNET_VAULT_READONLY"] == "true"
assert svc["healthcheck"]["test"][-1] == name
@pytest.mark.parametrize("envname", ["dev", "prod"])
def test_compose_vault_is_read_only_bind_without_autocreate(envname):
mounts = [v for v in COMPOSE[envname]["services"]["api"]["volumes"]
if isinstance(v, dict) and v.get("target") == "/vault"]
assert len(mounts) == 1
assert mounts[0]["read_only"] is True
assert mounts[0]["bind"]["create_host_path"] is False
assert all(":/vault" not in v for v in COMPOSE[envname]["services"]["ui"]["volumes"] if isinstance(v, str))
@pytest.mark.parametrize("envname", ["dev", "prod"])
def test_compose_api_only_on_loopback(envname):
assert all(p.startswith("127.0.0.1:") for p in COMPOSE[envname]["services"]["api"]["ports"])
def test_prod_uses_existing_qdrant_and_dev_its_own():
assert "qdrant" not in COMPOSE["prod"]["services"]
dev_qdrant = COMPOSE["dev"]["services"]["qdrant"]
assert "@sha256:" in dev_qdrant["image"]
for name in APP_SERVICES:
dev_env = COMPOSE["dev"]["services"][name]["environment"]
assert dev_env["QDRANT_URL"] == "http://qdrant:6333"
assert dev_env["COLLECTION_PREFIX"].endswith("_dev")
def test_dockerfile_pinned_and_non_root():
text = (DEPLOY / "Dockerfile").read_text(encoding="utf-8")
assert re.search(r"ARG PYTHON_IMAGE=python:3\.12-slim-bookworm@sha256:[0-9a-f]{64}", text)
runtime = text.split("AS runtime", 1)[1].split("AS test", 1)[0]
assert "USER mindnet:mindnet" in runtime
assert "--require-hashes" in text
def test_lock_excludes_torch_and_minilm_and_is_hashed():
lock = (DEPLOY / "requirements.lock").read_text(encoding="utf-8")
pins = re.findall(r"^([a-z0-9][a-z0-9._-]*)==", lock, re.M)
assert pins and not {"torch", "sentence-transformers", "transformers"} & set(pins)
entries = re.split(r"\n(?=[a-z0-9])", lock.strip())
assert all("--hash=sha256:" in e for e in entries if re.match(r"[a-z0-9]", e))
def test_dockerignore_is_allowlist_without_env_files():
lines = [l.strip() for l in (ROOT / ".dockerignore").read_text(encoding="utf-8").splitlines()
if l.strip() and not l.startswith("#")]
assert lines[0] == "**"
assert "config/*.env" in lines and "deploy/env/*.env" in lines
assert not any(l.startswith("!") and ("vault" in l or ".env" in l) for l in lines)
def test_prod_env_example_blocks_cloud_egress():
values = dict(l.split("=", 1) for l in (DEPLOY / "env" / "prod.env.example").read_text(encoding="utf-8").splitlines()
if "=" in l and not l.startswith("#"))
assert values["OPENROUTER_API_KEY"] == "" and values["GOOGLE_API_KEY"] == ""
assert values["MINDNET_ALLOW_CLOUD_EGRESS"] == "false"
# --- Gitea-Workflows und Deploy-Skript (Muster wie Kansho: develop -> dev, main -> prod) ----
def _workflow(name):
data = yaml.safe_load((DEPLOY / "gitea-workflows" / name).read_text(encoding="utf-8"))
return data, data.get("on", data.get(True)) # PyYAML liest den Schlüssel 'on' als True
@pytest.mark.parametrize("name, branch, envname", [("deploy-dev.yml", "develop", "dev"),
("deploy-prod.yml", "main", "prod")])
def test_workflow_maps_branch_to_environment(name, branch, envname):
wf, on = _workflow(name)
assert on["push"]["branches"] == [branch]
job = wf["jobs"]["deploy"]
assert job["runs-on"] == "linux_host"
assert job["env"]["MINDNET_ENV_FILE"].startswith("/") and job["env"]["MINDNET_ENV_FILE"].endswith(f"{envname}.env")
assert any(s.get("run") == f"bash deploy/deploy.sh {envname}" for s in job["steps"])
def test_prod_workflow_shares_concurrency_with_systemd_deploy():
wf, _ = _workflow("deploy-prod.yml")
systemd = yaml.safe_load((ROOT / ".gitea" / "workflows" / "deploy.yml").read_text(encoding="utf-8"))
assert wf["concurrency"]["group"] == systemd["concurrency"]["group"]
def test_deploy_script_checks_before_touching_running_stack():
script = (DEPLOY / "deploy.sh").read_text(encoding="utf-8")
assert "\r" not in script
steps = script.split('step "1/5', 1)[1] # ohne Kopfkommentar und Funktionsdefinitionen
order = [steps.index(s) for s in ("--network none", "preflight --live", "up -d --wait --no-build && smoke")]
assert order == sorted(order)
assert "RUN_DEPS=(--no-deps)" in script and "SMOKE_ARGS=(--require-data)" in script
assert "last_good_tag" in script
@pytest.mark.parametrize("envname", ["dev", "prod"])
def test_compose_reads_env_file_from_host(envname):
assert COMPOSE[envname]["x-hardening"]["env_file"] == [f"${{MINDNET_ENV_FILE:-env/{envname}.env}}"]