hermes-agent/tests/tools/test_docker_network_config.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

116 lines
4.5 KiB
Python

"""Regression tests for the Docker terminal network toggle.
Ported from NanoClaw PR #2713's opt-in egress lockdown idea. Hermes already
has DockerEnvironment(network=False), but the terminal config path did not
expose it, so operators could not request networkless Docker execution from
config.yaml.
"""
import tools.terminal_tool as terminal_tool
from tools.environments import docker as docker_env
def test_terminal_env_config_reads_docker_network_toggle(monkeypatch):
monkeypatch.setenv("TERMINAL_DOCKER_NETWORK", "false")
config = terminal_tool._get_env_config()
assert config["docker_network"] is False
def test_sibling_container_config_sites_carry_docker_network():
"""Every container_config dict that carries docker_run_as_host_user must
also carry docker_network — otherwise that code path silently falls back
to networked containers while the terminal path honors the lockdown
(the probe/exec asymmetry reported on issue #46358).
"""
import ast
import inspect
import tools.code_execution_tool as code_execution_tool
import tools.file_tools as file_tools
for module in (terminal_tool, file_tools, code_execution_tool):
tree = ast.parse(inspect.getsource(module))
sites = 0
for node in ast.walk(tree):
if not isinstance(node, ast.Dict):
continue
keys = {k.value for k in node.keys if isinstance(k, ast.Constant)}
if "docker_run_as_host_user" in keys:
sites += 1
assert "docker_network" in keys, (
f"{module.__name__} builds a container_config with "
f"docker_run_as_host_user but without docker_network "
f"(line {node.lineno})"
)
assert sites >= 1, f"expected at least one container_config site in {module.__name__}"
def _reuse_guard_harness(monkeypatch, *, existing_mode: str, network: bool):
"""Drive DockerEnvironment through the cross-process reuse path with a
fake existing container whose NetworkMode is *existing_mode*.
Returns the list of docker commands issued.
"""
commands = []
def fake_run(cmd, *args, **kwargs):
commands.append(cmd)
class Result:
returncode = 0
stderr = ""
stdout = ""
if len(cmd) > 1 and cmd[1] == "ps":
# Matches the egress-aware reuse probe: with egress off the
# format string is ID\tState\tEgressLabel and docker renders a
# missing label as "<no value>".
Result.stdout = "existing-container-id\trunning\t<no value>\n"
elif len(cmd) > 1 and cmd[1] == "inspect":
Result.stdout = f"{existing_mode}\n"
elif len(cmd) > 1 and cmd[1] == "run":
Result.stdout = "fresh-container-id\n"
return Result()
monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")
monkeypatch.setattr(docker_env.subprocess, "run", fake_run)
monkeypatch.setattr(docker_env.DockerEnvironment, "_storage_opt_supported", lambda self: False)
docker_env.DockerEnvironment(
image="python:3.11",
cwd="/workspace",
timeout=60,
task_id="reuse-guard-test",
network=network,
persist_across_processes=True,
)
return commands
def test_reuse_rejects_networked_container_when_lockdown_requested(monkeypatch):
commands = _reuse_guard_harness(monkeypatch, existing_mode="bridge", network=False)
assert any(cmd[1:3] == ["rm", "-f"] for cmd in commands), (
"bridge-networked container must be removed when docker_network=false"
)
run_cmd = next(cmd for cmd in commands if len(cmd) > 2 and cmd[1:3] == ["run", "-d"])
assert "--network=none" in run_cmd
def test_reuse_keeps_airgapped_container_when_lockdown_requested(monkeypatch):
commands = _reuse_guard_harness(monkeypatch, existing_mode="none", network=False)
assert not any(cmd[1] == "rm" for cmd in commands)
assert not any(cmd[1] == "run" for cmd in commands), "matching container must be reused"
def test_reuse_skips_inspect_when_network_enabled(monkeypatch):
commands = _reuse_guard_harness(monkeypatch, existing_mode="none", network=True)
# Default-network config never churns containers, even air-gapped ones
# (operators may have created them via docker_extra_args).
assert not any(cmd[1] == "inspect" for cmd in commands)
assert not any(cmd[1] == "rm" for cmd in commands)
assert not any(cmd[1] == "run" for cmd in commands)