mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-17 19:44:45 +08:00
fix(sandbox):deer-flow-provisioner container fails to start in local execution mode (#889)
This commit is contained in:
95
backend/tests/test_docker_sandbox_mode_detection.py
Normal file
95
backend/tests/test_docker_sandbox_mode_detection.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""Regression tests for docker sandbox mode detection logic."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[2]
|
||||
SCRIPT_PATH = REPO_ROOT / "scripts" / "docker.sh"
|
||||
|
||||
|
||||
def _detect_mode_with_config(config_content: str) -> str:
|
||||
"""Write config content into a temp project root and execute detect_sandbox_mode."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
tmp_root = Path(tmpdir)
|
||||
(tmp_root / "config.yaml").write_text(config_content)
|
||||
|
||||
command = (
|
||||
f"source '{SCRIPT_PATH}' && "
|
||||
f"PROJECT_ROOT='{tmp_root}' && "
|
||||
"detect_sandbox_mode"
|
||||
)
|
||||
|
||||
output = subprocess.check_output(
|
||||
["bash", "-lc", command],
|
||||
text=True,
|
||||
).strip()
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def test_detect_mode_defaults_to_local_when_config_missing():
|
||||
"""No config file should default to local mode."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
command = (
|
||||
f"source '{SCRIPT_PATH}' && "
|
||||
f"PROJECT_ROOT='{tmpdir}' && "
|
||||
"detect_sandbox_mode"
|
||||
)
|
||||
output = subprocess.check_output(["bash", "-lc", command], text=True).strip()
|
||||
|
||||
assert output == "local"
|
||||
|
||||
|
||||
def test_detect_mode_local_provider():
|
||||
"""Local sandbox provider should map to local mode."""
|
||||
config = """
|
||||
sandbox:
|
||||
use: src.sandbox.local:LocalSandboxProvider
|
||||
""".strip()
|
||||
|
||||
assert _detect_mode_with_config(config) == "local"
|
||||
|
||||
|
||||
def test_detect_mode_aio_without_provisioner_url():
|
||||
"""AIO sandbox without provisioner_url should map to aio mode."""
|
||||
config = """
|
||||
sandbox:
|
||||
use: src.community.aio_sandbox:AioSandboxProvider
|
||||
""".strip()
|
||||
|
||||
assert _detect_mode_with_config(config) == "aio"
|
||||
|
||||
|
||||
def test_detect_mode_provisioner_with_url():
|
||||
"""AIO sandbox with provisioner_url should map to provisioner mode."""
|
||||
config = """
|
||||
sandbox:
|
||||
use: src.community.aio_sandbox:AioSandboxProvider
|
||||
provisioner_url: http://provisioner:8002
|
||||
""".strip()
|
||||
|
||||
assert _detect_mode_with_config(config) == "provisioner"
|
||||
|
||||
|
||||
def test_detect_mode_ignores_commented_provisioner_url():
|
||||
"""Commented provisioner_url should not activate provisioner mode."""
|
||||
config = """
|
||||
sandbox:
|
||||
use: src.community.aio_sandbox:AioSandboxProvider
|
||||
# provisioner_url: http://provisioner:8002
|
||||
""".strip()
|
||||
|
||||
assert _detect_mode_with_config(config) == "aio"
|
||||
|
||||
|
||||
def test_detect_mode_unknown_provider_falls_back_to_local():
|
||||
"""Unknown sandbox provider should default to local mode."""
|
||||
config = """
|
||||
sandbox:
|
||||
use: custom.module:UnknownProvider
|
||||
""".strip()
|
||||
|
||||
assert _detect_mode_with_config(config) == "local"
|
||||
121
backend/tests/test_provisioner_kubeconfig.py
Normal file
121
backend/tests/test_provisioner_kubeconfig.py
Normal file
@@ -0,0 +1,121 @@
|
||||
"""Regression tests for provisioner kubeconfig path handling."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _load_provisioner_module():
|
||||
"""Load docker/provisioner/app.py as an importable test module."""
|
||||
repo_root = Path(__file__).resolve().parents[2]
|
||||
module_path = repo_root / "docker" / "provisioner" / "app.py"
|
||||
spec = importlib.util.spec_from_file_location("provisioner_app_test", module_path)
|
||||
assert spec is not None
|
||||
assert spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_wait_for_kubeconfig_rejects_directory(tmp_path):
|
||||
"""Directory mount at kubeconfig path should fail fast with clear error."""
|
||||
provisioner_module = _load_provisioner_module()
|
||||
kubeconfig_dir = tmp_path / "config_dir"
|
||||
kubeconfig_dir.mkdir()
|
||||
|
||||
provisioner_module.KUBECONFIG_PATH = str(kubeconfig_dir)
|
||||
|
||||
try:
|
||||
provisioner_module._wait_for_kubeconfig(timeout=1)
|
||||
raise AssertionError("Expected RuntimeError for directory kubeconfig path")
|
||||
except RuntimeError as exc:
|
||||
assert "directory" in str(exc)
|
||||
|
||||
|
||||
def test_wait_for_kubeconfig_accepts_file(tmp_path):
|
||||
"""Regular file mount should pass readiness wait."""
|
||||
provisioner_module = _load_provisioner_module()
|
||||
kubeconfig_file = tmp_path / "config"
|
||||
kubeconfig_file.write_text("apiVersion: v1\n")
|
||||
|
||||
provisioner_module.KUBECONFIG_PATH = str(kubeconfig_file)
|
||||
|
||||
# Should return immediately without raising.
|
||||
provisioner_module._wait_for_kubeconfig(timeout=1)
|
||||
|
||||
|
||||
def test_init_k8s_client_rejects_directory_path(tmp_path):
|
||||
"""KUBECONFIG_PATH that resolves to a directory should be rejected."""
|
||||
provisioner_module = _load_provisioner_module()
|
||||
kubeconfig_dir = tmp_path / "config_dir"
|
||||
kubeconfig_dir.mkdir()
|
||||
|
||||
provisioner_module.KUBECONFIG_PATH = str(kubeconfig_dir)
|
||||
|
||||
try:
|
||||
provisioner_module._init_k8s_client()
|
||||
raise AssertionError("Expected RuntimeError for directory kubeconfig path")
|
||||
except RuntimeError as exc:
|
||||
assert "expected a file" in str(exc)
|
||||
|
||||
|
||||
def test_init_k8s_client_uses_file_kubeconfig(tmp_path, monkeypatch):
|
||||
"""When file exists, provisioner should load kubeconfig file path."""
|
||||
provisioner_module = _load_provisioner_module()
|
||||
kubeconfig_file = tmp_path / "config"
|
||||
kubeconfig_file.write_text("apiVersion: v1\n")
|
||||
|
||||
called: dict[str, object] = {}
|
||||
|
||||
def fake_load_kube_config(config_file: str):
|
||||
called["config_file"] = config_file
|
||||
|
||||
monkeypatch.setattr(
|
||||
provisioner_module.k8s_config,
|
||||
"load_kube_config",
|
||||
fake_load_kube_config,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
provisioner_module.k8s_client,
|
||||
"CoreV1Api",
|
||||
lambda *args, **kwargs: "core-v1",
|
||||
)
|
||||
|
||||
provisioner_module.KUBECONFIG_PATH = str(kubeconfig_file)
|
||||
|
||||
result = provisioner_module._init_k8s_client()
|
||||
|
||||
assert called["config_file"] == str(kubeconfig_file)
|
||||
assert result == "core-v1"
|
||||
|
||||
|
||||
def test_init_k8s_client_falls_back_to_incluster_when_missing(
|
||||
tmp_path, monkeypatch
|
||||
):
|
||||
"""When kubeconfig file is missing, in-cluster config should be attempted."""
|
||||
provisioner_module = _load_provisioner_module()
|
||||
missing_path = tmp_path / "missing-config"
|
||||
|
||||
calls: dict[str, int] = {"incluster": 0}
|
||||
|
||||
def fake_load_incluster_config():
|
||||
calls["incluster"] += 1
|
||||
|
||||
monkeypatch.setattr(
|
||||
provisioner_module.k8s_config,
|
||||
"load_incluster_config",
|
||||
fake_load_incluster_config,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
provisioner_module.k8s_client,
|
||||
"CoreV1Api",
|
||||
lambda *args, **kwargs: "core-v1",
|
||||
)
|
||||
|
||||
provisioner_module.KUBECONFIG_PATH = str(missing_path)
|
||||
|
||||
result = provisioner_module._init_k8s_client()
|
||||
|
||||
assert calls["incluster"] == 1
|
||||
assert result == "core-v1"
|
||||
Reference in New Issue
Block a user