Skip to content

The agent that grows with you

test(docker): the fail-closed dashboard guard no longer passes when every docker exec stalls

kshitijk4poor34411c1a68a0
diff --git a/tests/docker/conftest.py b/tests/docker/conftest.py
index 79817e698da..073189c57b6 100644
--- a/tests/docker/conftest.py
+++ b/tests/docker/conftest.py
@@ -45,7 +45,8 @@ def pytest_collection_modifyitems(config, items):
for item in items:
if "tests/docker/" not in str(item.fspath).replace(os.sep, "/"):
continue
- if not docker_ok:
+ # Modules that set DOCKER_DAEMON_REQUIRED = False drive the helpers with stand-ins.
+ if not docker_ok and getattr(getattr(item, "module", None), "DOCKER_DAEMON_REQUIRED", True):
item.add_marker(skip_docker)
@@ -237,21 +238,29 @@ def poll_container(
``deadline_s`` elapses.
Returns ``(success, last_stdout)``. Useful for waiting on a process
- to appear, a port to open, a file to contain a string, etc.
+ to appear, a port to open, a file to contain a string, etc. Raises
+ ``AssertionError`` when no attempt completed (every ``docker exec``
+ stalled): that is no verdict, so a negative ``assert not ok`` must not pass.
"""
end = time.monotonic() + deadline_s
last = ""
- while time.monotonic() < end:
+ completed = False
+ while (remaining := end - time.monotonic()) > 0:
try:
- r = docker_exec_sh(container, probe, user=user, timeout=10)
- except subprocess.TimeoutExpired:
+ r = docker_exec_sh(container, probe, user=user, timeout=min(10.0, remaining))
+ except subprocess.TimeoutExpired as exc:
# A stalled ``docker exec`` (busy daemon on a loaded runner) is one failed attempt,
- # not the verdict: keep polling until the deadline like any other non-zero probe.
+ # not the verdict: keep polling until the deadline. Keep the last real stdout if any.
+ if not completed:
+ last = f"docker exec stalled: {exc}"
continue
+ completed = True
last = r.stdout
if r.returncode == 0:
return True, last
time.sleep(interval_s)
+ if not completed:
+ raise AssertionError(f"No probe of {container} completed within {deadline_s}s; {last}")
return False, last
@@ -266,7 +275,8 @@ def wait_for_path(
"""Poll ``test -<kind> <path>`` inside the container until success or timeout.
``kind`` is the ``test`` flag: ``'f'`` for file, ``'d'`` for directory,
- ``'e'`` for existence. Returns ``True`` on success, ``False`` on timeout.
+ ``'e'`` for existence. Returns ``True`` on success, ``False`` on timeout;
+ raises ``AssertionError`` if no probe completed.
"""
return poll_container(
container, f"test -{kind} {path}",
diff --git a/tests/docker/test_dashboard.py b/tests/docker/test_dashboard.py
index ce59de958d8..76710a0837e 100644
--- a/tests/docker/test_dashboard.py
+++ b/tests/docker/test_dashboard.py
@@ -97,9 +97,9 @@ except urllib.error.HTTPError as h:
)
end = time.monotonic() + deadline_s
last_err = ""
- while time.monotonic() < end:
+ while (remaining := end - time.monotonic()) > 0:
try:
- r = docker_exec_sh(container, probe, timeout=10)
+ r = docker_exec_sh(container, probe, timeout=min(10.0, remaining))
except subprocess.TimeoutExpired as exc:
last_err = f"docker exec stalled: {exc}" # a busy daemon: retry until the deadline
continue
diff --git a/tests/docker/test_poll_stalled_exec.py b/tests/docker/test_poll_stalled_exec.py
index 263489991fd..ecc5c0cc082 100644
--- a/tests/docker/test_poll_stalled_exec.py
+++ b/tests/docker/test_poll_stalled_exec.py
@@ -1,7 +1,7 @@
"""A stalled ``docker exec`` is one failed poll attempt, never the poll's verdict.
Driven without Docker: ``docker_exec_sh`` is replaced by a scripted stand-in and the poll's clock
-is fake, so the timeout branches run on every host.
+is fake, so the timeout branches run on every host (DOCKER_DAEMON_REQUIRED opts out of the skip).
"""
from __future__ import annotations
@@ -14,6 +14,8 @@ import pytest
from tests.docker import conftest as docker_conftest
from tests.docker import test_dashboard
+DOCKER_DAEMON_REQUIRED = False
+
class _Clock:
"""``time`` stand-in: each probe attempt and each sleep advance the fake clock."""
@@ -52,7 +54,28 @@ def test_stalled_exec_is_retried_until_the_deadline(monkeypatch, module):
clock.now = 0.0
monkeypatch.setattr(module, "docker_exec_sh", _scripted_exec(clock, ["stall"]))
if module is docker_conftest:
- assert module.poll_container("c", "probe", deadline_s=30.0) == (False, "")
+ with pytest.raises(AssertionError, match="docker exec stalled"):
+ module.poll_container("c", "probe", deadline_s=12.0)
else:
with pytest.raises(AssertionError, match="docker exec stalled"):
- module._http_probe("c", "/api/status", deadline_s=30.0)
+ module._http_probe("c", "/api/status", deadline_s=12.0)
+ assert clock.now <= 12.0 # each attempt gets only the time left, so the deadline holds
+
+
+def test_fail_closed_dashboard_guard_needs_a_completed_probe(monkeypatch):
+ """Every exec stalling proves nothing about the dashboard, so the negative guard must not pass."""
+ clock = _Clock()
+ monkeypatch.setattr(docker_conftest, "time", clock)
+ monkeypatch.setattr(docker_conftest, "docker_exec_sh", _scripted_exec(clock, ["stall"]))
+ monkeypatch.setattr(test_dashboard, "start_container", lambda *args, **kwargs: "c")
+ with pytest.raises(AssertionError, match="No probe of c completed"):
+ test_dashboard.test_dashboard_insecure_env_var_no_longer_bypasses_gate("image", "c")
+
+
+def test_stall_after_a_completed_probe_keeps_its_output(monkeypatch):
+ """A late stall must not replace the last real probe output that positive callers print."""
+ clock = _Clock()
+ monkeypatch.setattr(docker_conftest, "time", clock)
+ failed = SimpleNamespace(returncode=1, stdout="HOME=/root", stderr="")
+ monkeypatch.setattr(docker_conftest, "docker_exec_sh", _scripted_exec(clock, [failed, "stall"]))
+ assert docker_conftest.poll_container("c", "probe", deadline_s=12.0) == (False, "HOME=/root")