mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-11 09:55:59 +00:00
f401e7baa6
* Fix stale AIO sandbox cache reuse * Address AIO sandbox review feedback * Distinguish sandbox health check failures * Keep local discovery recoverable when the runtime check fails LocalContainerBackend.discover() shares _is_container_running, which now raises on transient daemon errors instead of returning False. Discovery has no exception handling in _discover_or_create_with_lock(_async), so a brief Docker hiccup turned a recoverable "could not verify, create instead" into a hard acquire failure. Catch the check failure inside discover() and return None so an unverifiable container is simply not adopted, restoring the pre-change fall-through while keeping raise-on-unknown semantics protecting the destroy path. Reported by fancy-agent on PR #3494. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Narrow the not-found match in container inspect error handling A bare "not found" substring also matches transient failures like "command not found" or "context not found", which would misclassify a check error as "container definitely gone" and bypass the raise-on-unknown contract. Keep Docker's specific "No such object"/"No such container" phrases, and only trust a generic "not found" (Apple Container) when the message names the inspected container or refers to a container/object. Reported by WillemJiang on PR #3494. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
336 lines
11 KiB
Python
336 lines
11 KiB
Python
import logging
|
|
import os
|
|
import subprocess
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from deerflow.community.aio_sandbox.local_backend import (
|
|
LocalContainerBackend,
|
|
_format_container_command_for_log,
|
|
_format_container_mount,
|
|
_redact_container_command_for_log,
|
|
_resolve_docker_bind_host,
|
|
)
|
|
|
|
|
|
def test_format_container_mount_uses_mount_syntax_for_docker_windows_paths():
|
|
args = _format_container_mount("docker", "D:/deer-flow/backend/.deer-flow/threads", "/mnt/threads", False)
|
|
|
|
assert args == [
|
|
"--mount",
|
|
"type=bind,src=D:/deer-flow/backend/.deer-flow/threads,dst=/mnt/threads",
|
|
]
|
|
|
|
|
|
def test_format_container_mount_marks_docker_readonly_mounts():
|
|
args = _format_container_mount("docker", "/host/path", "/mnt/path", True)
|
|
|
|
assert args == [
|
|
"--mount",
|
|
"type=bind,src=/host/path,dst=/mnt/path,readonly",
|
|
]
|
|
|
|
|
|
def test_format_container_mount_keeps_volume_syntax_for_apple_container():
|
|
args = _format_container_mount("container", "/host/path", "/mnt/path", True)
|
|
|
|
assert args == [
|
|
"-v",
|
|
"/host/path:/mnt/path:ro",
|
|
]
|
|
|
|
|
|
def test_redact_container_command_for_log_redacts_env_values():
|
|
redacted = _redact_container_command_for_log(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"-e",
|
|
"API_KEY=secret-value",
|
|
"--env=TOKEN=token-value",
|
|
"--name",
|
|
"sandbox",
|
|
"image",
|
|
]
|
|
)
|
|
|
|
assert "API_KEY=<redacted>" in redacted
|
|
assert "--env=TOKEN=<redacted>" in redacted
|
|
assert "secret-value" not in " ".join(redacted)
|
|
assert "token-value" not in " ".join(redacted)
|
|
|
|
|
|
def test_redact_container_command_for_log_keeps_inherited_env_names():
|
|
redacted = _redact_container_command_for_log(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"-e",
|
|
"API_KEY",
|
|
"--env=TOKEN",
|
|
"--name",
|
|
"sandbox",
|
|
"image",
|
|
]
|
|
)
|
|
|
|
assert redacted == [
|
|
"docker",
|
|
"run",
|
|
"-e",
|
|
"API_KEY",
|
|
"--env=TOKEN",
|
|
"--name",
|
|
"sandbox",
|
|
"image",
|
|
]
|
|
|
|
|
|
def test_format_container_command_for_log_uses_windows_quoting(monkeypatch):
|
|
monkeypatch.setattr(os, "name", "nt")
|
|
|
|
command = _format_container_command_for_log(["docker", "run", "--name", "sandbox one", "image"])
|
|
|
|
assert command == 'docker run --name "sandbox one" image'
|
|
|
|
|
|
def test_start_container_logs_redacted_env_values(monkeypatch, caplog):
|
|
backend = LocalContainerBackend(
|
|
image="sandbox:latest",
|
|
base_port=8080,
|
|
container_prefix="sandbox",
|
|
config_mounts=[],
|
|
environment={"API_KEY": "secret-value", "NORMAL": "visible-value"},
|
|
)
|
|
monkeypatch.setattr(backend, "_runtime", "docker")
|
|
|
|
captured_cmd: list[str] = []
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
captured_cmd.extend(cmd)
|
|
return SimpleNamespace(stdout="container-id\n", stderr="", returncode=0)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
with caplog.at_level(logging.INFO, logger="deerflow.community.aio_sandbox.local_backend"):
|
|
backend._start_container("sandbox-test", 18080)
|
|
|
|
joined_cmd = " ".join(captured_cmd)
|
|
assert "API_KEY=secret-value" in joined_cmd
|
|
assert "NORMAL=visible-value" in joined_cmd
|
|
|
|
log_output = "\n".join(record.getMessage() for record in caplog.records)
|
|
assert "API_KEY=<redacted>" in log_output
|
|
assert "NORMAL=<redacted>" in log_output
|
|
assert "secret-value" not in log_output
|
|
assert "visible-value" not in log_output
|
|
|
|
|
|
def _capture_start_container_command(monkeypatch, backend: LocalContainerBackend, runtime: str = "docker") -> list[str]:
|
|
monkeypatch.setattr(backend, "_runtime", runtime)
|
|
captured_cmd: list[str] = []
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
captured_cmd.extend(cmd)
|
|
return SimpleNamespace(stdout="container-id\n", stderr="", returncode=0)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
backend._start_container("sandbox-test", 18080)
|
|
return captured_cmd
|
|
|
|
|
|
def test_resolve_docker_bind_host_defaults_loopback_for_localhost(monkeypatch):
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_BIND_HOST", raising=False)
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_HOST", raising=False)
|
|
|
|
assert _resolve_docker_bind_host() == "127.0.0.1"
|
|
|
|
|
|
def test_resolve_docker_bind_host_keeps_dood_compatibility(monkeypatch):
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_BIND_HOST", raising=False)
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_HOST", "host.docker.internal")
|
|
|
|
assert _resolve_docker_bind_host() == "0.0.0.0"
|
|
|
|
|
|
def test_resolve_docker_bind_host_uses_ipv6_loopback_for_ipv6_sandbox_host(monkeypatch):
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_BIND_HOST", raising=False)
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_HOST", "[::1]")
|
|
|
|
assert _resolve_docker_bind_host() == "[::1]"
|
|
|
|
|
|
def test_resolve_docker_bind_host_logs_selected_bind_reason(caplog):
|
|
with caplog.at_level(logging.DEBUG, logger="deerflow.community.aio_sandbox.local_backend"):
|
|
assert _resolve_docker_bind_host(sandbox_host="localhost", bind_host="") == "127.0.0.1"
|
|
|
|
messages = "\n".join(record.getMessage() for record in caplog.records)
|
|
assert "Docker sandbox bind: 127.0.0.1 (loopback default)" in messages
|
|
|
|
|
|
def test_resolve_docker_bind_host_allows_explicit_override(monkeypatch):
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_HOST", "localhost")
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_BIND_HOST", "192.0.2.10")
|
|
|
|
assert _resolve_docker_bind_host() == "192.0.2.10"
|
|
|
|
|
|
def test_start_container_binds_local_docker_port_to_loopback_by_default(monkeypatch):
|
|
backend = LocalContainerBackend(
|
|
image="sandbox:latest",
|
|
base_port=8080,
|
|
container_prefix="sandbox",
|
|
config_mounts=[],
|
|
environment={},
|
|
)
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_HOST", raising=False)
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_BIND_HOST", raising=False)
|
|
|
|
captured_cmd = _capture_start_container_command(monkeypatch, backend)
|
|
|
|
assert captured_cmd[captured_cmd.index("-p") + 1] == "127.0.0.1:18080:8080"
|
|
|
|
|
|
def test_start_container_keeps_broad_bind_for_dood_sandbox_host(monkeypatch):
|
|
backend = LocalContainerBackend(
|
|
image="sandbox:latest",
|
|
base_port=8080,
|
|
container_prefix="sandbox",
|
|
config_mounts=[],
|
|
environment={},
|
|
)
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_HOST", "host.docker.internal")
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_BIND_HOST", raising=False)
|
|
|
|
captured_cmd = _capture_start_container_command(monkeypatch, backend)
|
|
|
|
assert captured_cmd[captured_cmd.index("-p") + 1] == "0.0.0.0:18080:8080"
|
|
|
|
|
|
def test_start_container_binds_ipv6_sandbox_host_to_ipv6_loopback(monkeypatch):
|
|
backend = LocalContainerBackend(
|
|
image="sandbox:latest",
|
|
base_port=8080,
|
|
container_prefix="sandbox",
|
|
config_mounts=[],
|
|
environment={},
|
|
)
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_HOST", "[::1]")
|
|
monkeypatch.delenv("DEER_FLOW_SANDBOX_BIND_HOST", raising=False)
|
|
|
|
captured_cmd = _capture_start_container_command(monkeypatch, backend)
|
|
|
|
assert captured_cmd[captured_cmd.index("-p") + 1] == "[::1]:18080:8080"
|
|
|
|
|
|
def test_start_container_keeps_apple_container_port_format(monkeypatch):
|
|
backend = LocalContainerBackend(
|
|
image="sandbox:latest",
|
|
base_port=8080,
|
|
container_prefix="sandbox",
|
|
config_mounts=[],
|
|
environment={},
|
|
)
|
|
monkeypatch.setenv("DEER_FLOW_SANDBOX_BIND_HOST", "127.0.0.1")
|
|
|
|
captured_cmd = _capture_start_container_command(monkeypatch, backend, runtime="container")
|
|
|
|
assert captured_cmd[captured_cmd.index("-p") + 1] == "18080:8080"
|
|
|
|
|
|
def _backend_for_inspect_tests() -> LocalContainerBackend:
|
|
backend = LocalContainerBackend(
|
|
image="sandbox:latest",
|
|
base_port=8080,
|
|
container_prefix="sandbox",
|
|
config_mounts=[],
|
|
environment={},
|
|
)
|
|
backend._runtime = "docker"
|
|
return backend
|
|
|
|
|
|
def test_is_container_running_false_when_container_missing(monkeypatch):
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
return SimpleNamespace(stdout="", stderr="Error: No such object: sandbox-missing", returncode=1)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
assert backend._is_container_running("sandbox-missing") is False
|
|
|
|
|
|
def test_is_container_running_raises_on_runtime_error(monkeypatch):
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
return SimpleNamespace(stdout="", stderr="Cannot connect to the Docker daemon", returncode=1)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
with pytest.raises(RuntimeError, match="Failed to inspect container sandbox-busy"):
|
|
backend._is_container_running("sandbox-busy")
|
|
|
|
|
|
def test_is_container_running_raises_on_timeout(monkeypatch):
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
raise subprocess.TimeoutExpired(cmd=cmd, timeout=kwargs["timeout"])
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
with pytest.raises(RuntimeError, match="Timed out checking container sandbox-timeout"):
|
|
backend._is_container_running("sandbox-timeout")
|
|
|
|
|
|
def test_discover_returns_none_when_runtime_check_fails(monkeypatch):
|
|
"""A transient daemon error during discovery must fall through to create, not fail acquire."""
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
return SimpleNamespace(stdout="", stderr="Cannot connect to the Docker daemon", returncode=1)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
assert backend.discover("sandbox-blip") is None
|
|
|
|
|
|
def test_discover_returns_none_when_runtime_check_times_out(monkeypatch):
|
|
"""An inspect timeout during discovery must not propagate out of discover()."""
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
raise subprocess.TimeoutExpired(cmd=cmd, timeout=kwargs["timeout"])
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
assert backend.discover("sandbox-timeout") is None
|
|
|
|
|
|
def test_is_container_running_false_on_apple_container_not_found(monkeypatch):
|
|
"""Apple Container's generic "not found" is trusted when it names the container."""
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
return SimpleNamespace(stdout="", stderr='Error: not found: "sandbox-apple"', returncode=1)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
assert backend._is_container_running("sandbox-apple") is False
|
|
|
|
|
|
def test_is_container_running_raises_on_unrelated_not_found_error(monkeypatch):
|
|
"""Transient errors whose text contains "not found" must not be misread as a dead container."""
|
|
backend = _backend_for_inspect_tests()
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
return SimpleNamespace(stdout="", stderr="Error: credential helper not found in $PATH", returncode=1)
|
|
|
|
monkeypatch.setattr("subprocess.run", fake_run)
|
|
|
|
with pytest.raises(RuntimeError, match="Failed to inspect container sandbox-busy"):
|
|
backend._is_container_running("sandbox-busy")
|