"""Cover the cluster route with cluster_client and monkeypatched network probes. Fails if an unapproved scheduler is probed or health results use the wrong HTTP status. """ from unittest.mock import Mock import distributed import pytest from flask import Flask from tea.pipeline import cluster_status from webapi.blueprints.governance import governance_bp CLUSTER_URL = "/governance/tea/cluster" @pytest.fixture def cluster_client(monkeypatch): """Mount the governance blueprint with clean config so machine defaults cannot affect routing.""" for variable in ("DASK_SCHEDULER_ADDRESS", "TEA_DASK_SCHEDULERS"): monkeypatch.delenv(variable, raising=False) app = Flask(__name__) app.register_blueprint(governance_bp, url_prefix="/governance") return app.test_client() def test_rejects_scheduler_outside_allow_list(cluster_client, monkeypatch): """Pin allow-list enforcement. Fail if an arbitrary scheduler reaches a network probe.""" monkeypatch.setenv("DASK_SCHEDULER_ADDRESS", "tcp://default:8786") monkeypatch.setenv("TEA_DASK_SCHEDULERS", "tcp://backup:8786") probe = Mock() monkeypatch.setattr(cluster_status.ClusterStatus, "check", probe) response = cluster_client.get(CLUSTER_URL, query_string={"scheduler": "tcp://other:8786"}) assert response.status_code == 400 assert response.json["error"] probe.assert_not_called() @pytest.mark.parametrize("mode", ["invalid", "remote"]) def test_rejects_invalid_cluster_choice(cluster_client, mode): """Pin validation errors. Fail if an unknown mode or addressless remote request succeeds.""" response = cluster_client.get(CLUSTER_URL, query_string={"mode": mode}) assert response.status_code == 400 assert response.json["error"] def test_local_mode_reports_reachable(cluster_client): """Pin local status serialization. Fail if local checks start reporting remote details.""" response = cluster_client.get(CLUSTER_URL, query_string={"mode": "local"}) assert response.status_code == 200 assert response.json["mode"] == "local" assert response.json["reachable"] is True assert response.json["scheduler"] is None assert response.json["workers"] is None @pytest.mark.parametrize("scheduler", ["tcp://backup:8786", "tcp://default:8786"]) def test_unreachable_approved_scheduler_returns_health(cluster_client, monkeypatch, scheduler): """Pin HTTP 200 on connection failure. Fail if either approval source rejects its address.""" connect = Mock(side_effect=OSError("connection refused")) monkeypatch.setattr(distributed, "Client", connect) monkeypatch.setattr(cluster_status, "urlopen", Mock(side_effect=OSError)) monkeypatch.setenv("DASK_SCHEDULER_ADDRESS", "tcp://default:8786") monkeypatch.setenv("TEA_DASK_SCHEDULERS", "tcp://first:8786, tcp://backup:8786 ") response = cluster_client.get(CLUSTER_URL, query_string={"scheduler": scheduler}) assert response.status_code == 200 assert response.json["mode"] == "remote" assert response.json["scheduler"] == scheduler assert response.json["reachable"] is False assert response.json["error"] == "connection refused" connect.assert_called_once_with(scheduler, timeout=5)