87 lines
3.9 KiB
Python
87 lines
3.9 KiB
Python
"""What this module covers: the cluster choice of an indicator recomputation: how `POST /reportes/indicadores/<d>/recalcular`
|
|
validates it, how the queue carries it in the pending marker, and how the worker applies it to one run only.
|
|
|
|
Fixtures: `queue`, a `RecomputationQueue` over a temporary folder.
|
|
"""
|
|
import json
|
|
import os
|
|
|
|
import pytest
|
|
from flask import Flask
|
|
|
|
from webapi.blueprints.reports import reports_bp
|
|
from webapi.recomputation import RecomputationQueue, _cluster_override
|
|
|
|
|
|
@pytest.fixture
|
|
def queue(tmp_path, monkeypatch):
|
|
"""A queue over an empty temporary folder, which the route also uses through INDICADORES_DIR."""
|
|
monkeypatch.setenv("INDICADORES_DIR", str(tmp_path))
|
|
return RecomputationQueue(tmp_path)
|
|
|
|
|
|
@pytest.fixture
|
|
def client(queue):
|
|
"""The reports blueprint alone, over the same folder as `queue`."""
|
|
app = Flask(__name__)
|
|
app.register_blueprint(reports_bp, url_prefix="/reportes")
|
|
return app.test_client()
|
|
|
|
|
|
def test_a_request_carries_its_cluster_choice_to_the_worker(queue):
|
|
"""Pins down: the marker keeps the choice, `requested_cluster` reads it back, and a plain request has none.
|
|
|
|
Fails if `request_run` drops the choice or the marker stops being readable after a plain request.
|
|
"""
|
|
queue.request_run("tea", {"mode": "local"})
|
|
assert queue.requested_cluster("tea") == {"mode": "local"}
|
|
queue.request_run("tea")
|
|
assert queue.requested_cluster("tea") is None
|
|
assert queue.requested_cluster("cacu") is None
|
|
|
|
|
|
def test_a_marker_written_as_plain_text_still_counts_as_pending(queue):
|
|
"""Pins down: a marker left by an older version (a bare timestamp) is pending and has no cluster choice.
|
|
|
|
Fails if the worker starts to crash on, or ignore, markers that are not JSON.
|
|
"""
|
|
queue._pending.mkdir(parents=True)
|
|
(queue._pending / "tea").write_text("2026-01-01T00:00:00+00:00", encoding="utf-8")
|
|
assert queue.pending() == ["tea"] and queue.requested_cluster("tea") is None
|
|
|
|
|
|
def test_the_route_validates_and_queues_the_choice(client, queue):
|
|
"""Pins down: a valid choice is queued (202), a bad mode, a non-object body and `remote` without a scheduler are
|
|
400s that queue nothing, and no body keeps working.
|
|
|
|
Fails if the route queues unchecked input, or stops accepting the body-less call the UI used before.
|
|
"""
|
|
ok = client.post("/reportes/indicadores/tea/recalcular", json={"cluster": {"mode": "local"}})
|
|
assert ok.status_code == 202 and queue.requested_cluster("tea") == {"mode": "local"}
|
|
for bad in ({"cluster": {"mode": "nope"}}, ["x"]):
|
|
assert client.post("/reportes/indicadores/tea/recalcular", json=bad).status_code == 400
|
|
os.environ.pop("DASK_SCHEDULER_ADDRESS", None)
|
|
assert client.post("/reportes/indicadores/tea/recalcular", json={"cluster": {"mode": "remote"}}).status_code == 400
|
|
assert client.post("/reportes/indicadores/tea/recalcular").status_code == 202
|
|
|
|
|
|
def test_a_run_applies_the_choice_and_then_restores_the_environment(monkeypatch):
|
|
"""Pins down: during the run the choice is in the environment (`local` drops the scheduler), and afterwards the
|
|
process environment is exactly as before, even when the run fails.
|
|
|
|
Fails if one run's choice leaks into the next runs of the worker.
|
|
"""
|
|
monkeypatch.setenv("DASK_SCHEDULER_ADDRESS", "tcp://scheduler:1")
|
|
monkeypatch.delenv("EEG_ANALYSIS_CLUSTER", raising=False)
|
|
before = dict(os.environ)
|
|
with _cluster_override({"mode": "local"}):
|
|
assert os.environ["EEG_ANALYSIS_CLUSTER"] == "local" and "DASK_SCHEDULER_ADDRESS" not in os.environ
|
|
assert dict(os.environ) == before
|
|
with pytest.raises(RuntimeError):
|
|
with _cluster_override({"mode": "remote", "scheduler": "tcp://other:2"}):
|
|
assert os.environ["DASK_SCHEDULER_ADDRESS"] == "tcp://other:2"
|
|
raise RuntimeError("boom")
|
|
assert dict(os.environ) == before
|
|
with _cluster_override(None):
|
|
assert dict(os.environ) == before
|