197 lines
7.7 KiB
Python
197 lines
7.7 KiB
Python
"""Cover cluster probes using clean_environment, loopback_port and monkeypatch.
|
|
|
|
Fails if mode resolution, connection handling or independent probe results regress.
|
|
"""
|
|
|
|
import socket
|
|
from contextlib import nullcontext
|
|
from time import perf_counter
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import distributed
|
|
import pytest
|
|
|
|
from tea.pipeline import cluster_status
|
|
from tea.pipeline.cluster_status import ClusterStatus
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean_environment(monkeypatch):
|
|
"""Clear cluster configuration so machine settings cannot change probe targets or results."""
|
|
for variable in (
|
|
"DASK_SCHEDULER_ADDRESS",
|
|
"TEA_DASK_EXPECTED_WORKERS",
|
|
"TEA_DASK_DASHBOARD_URL",
|
|
"TEA_DASK_DASHBOARD_PORT",
|
|
):
|
|
monkeypatch.delenv(variable, raising=False)
|
|
|
|
|
|
@pytest.fixture
|
|
def loopback_port():
|
|
"""Reserve a loopback port so live probes cannot connect to another service."""
|
|
try:
|
|
port = socket.socket()
|
|
except PermissionError as exc:
|
|
pytest.skip(f"TCP sockets are unavailable: {exc}")
|
|
with port:
|
|
try:
|
|
port.bind(("127.0.0.1", 0))
|
|
except PermissionError as exc:
|
|
pytest.skip(f"Loopback binding is unavailable: {exc}")
|
|
yield port.getsockname()[1]
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["auto", "local", "sync"])
|
|
def test_non_remote_mode_does_not_connect(monkeypatch, mode):
|
|
"""Pin the full non-remote response. Fail if a client starts or a probe reports a value."""
|
|
client = Mock()
|
|
monkeypatch.setattr(distributed, "Client", client)
|
|
if mode != "auto":
|
|
monkeypatch.setenv("DASK_SCHEDULER_ADDRESS", "tcp://scheduler:8786")
|
|
|
|
assert ClusterStatus().check(mode) == {
|
|
"mode": "local" if mode == "auto" else mode,
|
|
"scheduler": None,
|
|
"reachable": True,
|
|
"workers": None,
|
|
"expected_workers": None,
|
|
"versions_ok": None,
|
|
"version_error": None,
|
|
"dashboard_ok": None,
|
|
"dashboard_url": None,
|
|
"reference": None,
|
|
"latency_ms": None,
|
|
"error": None,
|
|
}
|
|
client.assert_not_called()
|
|
|
|
|
|
def test_unreachable_scheduler_is_bounded(monkeypatch, loopback_port):
|
|
"""Pin failure on a closed TCP port. Fail if it raises or ignores the connection timeout."""
|
|
monkeypatch.setenv("TEA_DASK_DASHBOARD_PORT", str(loopback_port))
|
|
start = perf_counter()
|
|
status = ClusterStatus().check(scheduler=f"tcp://127.0.0.1:{loopback_port}")
|
|
|
|
assert perf_counter() - start < 9
|
|
assert status["mode"] == "remote"
|
|
assert status["reachable"] is False
|
|
assert status["error"]
|
|
assert status["workers"] is None
|
|
assert status["versions_ok"] is None
|
|
assert status["dashboard_ok"] is False
|
|
|
|
|
|
@pytest.mark.usefixtures("loopback_port")
|
|
def test_reachable_scheduler_reports_one_worker(monkeypatch):
|
|
"""Pin health on a real Dask worker. Fail if the client miscounts it or skips version checks."""
|
|
with distributed.LocalCluster(
|
|
n_workers=1, threads_per_worker=1, processes=False, dashboard_address=":0"
|
|
) as cluster:
|
|
monkeypatch.setenv("DASK_SCHEDULER_ADDRESS", cluster.scheduler_address)
|
|
monkeypatch.setenv("TEA_DASK_EXPECTED_WORKERS", "2")
|
|
port = cluster.scheduler.services["dashboard"].port
|
|
monkeypatch.setenv("TEA_DASK_DASHBOARD_URL", f"http://127.0.0.1:{port}/status")
|
|
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
|
|
|
status = ClusterStatus().check()
|
|
|
|
assert status["scheduler"] == cluster.scheduler_address
|
|
assert status["reachable"] is True
|
|
assert status["workers"] == 1
|
|
assert status["expected_workers"] == 2
|
|
assert status["versions_ok"] is True
|
|
assert status["dashboard_ok"] is True
|
|
assert status["latency_ms"] >= 0
|
|
assert status["error"] is None
|
|
|
|
|
|
@pytest.mark.parametrize("dashboard_healthy", [True, False])
|
|
def test_version_mismatch_and_dashboard_are_independent(monkeypatch, dashboard_healthy):
|
|
"""Pin zero-worker and mismatch responses. Fail if either prevents an independent dashboard probe."""
|
|
client = Mock()
|
|
client.scheduler_info.return_value = {"workers": {}}
|
|
client.get_versions.side_effect = ValueError("version mismatch")
|
|
connect = Mock(return_value=client)
|
|
monkeypatch.setattr(distributed, "Client", connect)
|
|
if dashboard_healthy:
|
|
dashboard_url = "https://dashboard.example/status?view=all"
|
|
health_url = "https://dashboard.example/health"
|
|
monkeypatch.setenv("TEA_DASK_DASHBOARD_URL", dashboard_url)
|
|
open_health = Mock(return_value=nullcontext(SimpleNamespace(status=200)))
|
|
else:
|
|
dashboard_url = "http://scheduler:8787/status"
|
|
health_url = "http://scheduler:8787/health"
|
|
open_health = Mock(side_effect=TimeoutError)
|
|
monkeypatch.setattr(cluster_status, "urlopen", open_health)
|
|
|
|
status = ClusterStatus().check("remote", "tcp://scheduler:8786", "cohort")
|
|
|
|
assert status["reachable"] is True
|
|
assert status["error"] is None
|
|
assert status["workers"] == 0
|
|
assert status["versions_ok"] is False
|
|
assert status["version_error"] == "version mismatch"
|
|
assert status["dashboard_ok"] is dashboard_healthy
|
|
assert status["dashboard_url"] == dashboard_url
|
|
assert status["reference"] is None
|
|
connect.assert_called_once_with("tcp://scheduler:8786", timeout=5)
|
|
client.get_versions.assert_called_once_with(check=True)
|
|
client.submit.assert_not_called()
|
|
client.close.assert_called_once_with()
|
|
open_health.assert_called_once_with(health_url, timeout=3)
|
|
|
|
|
|
def test_warning_only_version_mismatch_is_reported(monkeypatch):
|
|
"""Pin mismatches returned by Dask without an exception. Fail if they count as compatible."""
|
|
client = Mock()
|
|
client.scheduler_info.return_value = {"workers": {"worker": {}}}
|
|
matching = {"packages": {"distributed": "2026.8.0"}}
|
|
client.get_versions.return_value = {
|
|
"scheduler": matching,
|
|
"client": matching,
|
|
"workers": {"worker": {"packages": {"distributed": "2026.9.0"}}},
|
|
}
|
|
monkeypatch.setattr(distributed, "Client", Mock(return_value=client))
|
|
monkeypatch.setattr(cluster_status, "urlopen", Mock(side_effect=TimeoutError))
|
|
|
|
status = ClusterStatus().check(scheduler="tcp://scheduler:8786")
|
|
|
|
assert status["versions_ok"] is False
|
|
assert "Mismatched versions found" in status["version_error"]
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["cohort-1", "../escape", ".."])
|
|
def test_reference_submission_requires_safe_name(monkeypatch, name):
|
|
"""Pin worker-side reference lookup. Fail if an unsafe name is submitted or a valid count is lost."""
|
|
client = Mock()
|
|
client.scheduler_info.return_value = {"workers": {"worker": {}}}
|
|
client.submit.return_value.result.return_value = 3
|
|
monkeypatch.setattr(distributed, "Client", Mock(return_value=client))
|
|
monkeypatch.setattr(cluster_status, "urlopen", Mock(side_effect=TimeoutError))
|
|
|
|
status = ClusterStatus().check(scheduler="tcp://scheduler:8786", reference_name=name)
|
|
|
|
if name == "cohort-1":
|
|
client.submit.assert_called_once_with(cluster_status._count_edfs, name, pure=False)
|
|
assert status["reference"] == {"name": name, "edfs": 3}
|
|
else:
|
|
client.submit.assert_not_called()
|
|
assert status["reference"] is None
|
|
|
|
|
|
def test_local_workers_can_be_capped_by_the_environment(monkeypatch):
|
|
"""Pins down: `TEA_LOCAL_WORKERS` fixes the size of a local cluster, an explicit count still wins, and a value
|
|
below one means one worker.
|
|
|
|
Fails if a local run on a shared machine can no longer be limited from the environment.
|
|
"""
|
|
from tea.pipeline.dask_runner import local_workers
|
|
|
|
monkeypatch.setenv("TEA_LOCAL_WORKERS", "1")
|
|
assert local_workers() == 1
|
|
monkeypatch.setenv("TEA_LOCAL_WORKERS", "0")
|
|
assert local_workers() == 1
|
|
assert local_workers(n_workers=3) == 3
|