Runs API: a run's state, steps, live progress, info and warnings apart, the failure's own message; cancel; history by job and domain; execution options and health; POST /tea/eegs/analyze
test_runs.py: 17 passed on SQLite and Postgres (compose test stack). test_conectar.py fails on an identity-merge count, as it does on tea/parity without this change.
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
"""Run shaping and the API's Dagster-backed run and execution routes."""
|
||||
|
||||
import time
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
import conftest
|
||||
from persistance.cluster import Progress
|
||||
from webapi.runs import STATE, graphql, shape
|
||||
|
||||
PAYLOAD = {
|
||||
"runId": "run",
|
||||
"jobName": "analyze",
|
||||
"status": "SUCCESS",
|
||||
"startTime": 1.0,
|
||||
"endTime": 4.0,
|
||||
"tags": [{"key": "domain", "value": "cacu"}],
|
||||
}
|
||||
|
||||
|
||||
def test_shape_splits_info_and_warnings():
|
||||
def event(level, message, step_key=None, kind="LogMessageEvent"):
|
||||
return {"__typename": kind, "level": level, "message": message, "stepKey": step_key}
|
||||
|
||||
run = shape(
|
||||
PAYLOAD,
|
||||
[
|
||||
event("INFO", "starting"),
|
||||
event("WARNING", "skipped", "analyze_studies"),
|
||||
event("DEBUG", "debug"),
|
||||
event("INFO", "engine", kind="EngineEvent"),
|
||||
event("INFO", "finished", "analyze_studies"),
|
||||
],
|
||||
)
|
||||
assert run.info == ["starting", "analyze_studies: finished"]
|
||||
assert run.warnings == ["analyze_studies: skipped"]
|
||||
assert run.error is None
|
||||
|
||||
warning_only = shape(PAYLOAD, [event("WARNING", "skipped")])
|
||||
assert warning_only.state == "succeeded"
|
||||
assert warning_only.error is None
|
||||
|
||||
steps = [
|
||||
{"stepKey": "waiting", "status": "SKIPPED", "startTime": None, "endTime": None},
|
||||
{"stepKey": "second", "status": "SUCCESS", "startTime": 2, "endTime": 4},
|
||||
{"stepKey": "first", "status": "SUCCESS", "startTime": 0, "endTime": 1},
|
||||
]
|
||||
assert [step.key for step in shape(PAYLOAD | {"stepStats": steps}, []).steps] == [
|
||||
"first", "second", "waiting",
|
||||
] # fmt: skip
|
||||
|
||||
capped = shape(
|
||||
PAYLOAD, [event(level, str(i)) for i in range(201) for level in ("INFO", "WARNING")]
|
||||
)
|
||||
assert capped.info == [str(i) for i in range(1, 201)]
|
||||
assert capped.warnings == [str(i) for i in range(200)]
|
||||
|
||||
|
||||
def test_shape_failure_message_is_the_ops_own_exception():
|
||||
failed = PAYLOAD | {"status": "FAILURE"}
|
||||
step_failure = {
|
||||
"__typename": "ExecutionStepFailureEvent",
|
||||
"error": {
|
||||
"message": "DagsterExecutionStepExecutionError: invocation failed",
|
||||
"causes": [
|
||||
{"message": "intermediate cause"},
|
||||
{"message": " LookupError: no EEG x|2026-01-01\nmore details "},
|
||||
],
|
||||
},
|
||||
}
|
||||
assert shape(failed, [step_failure]).error == "LookupError: no EEG x|2026-01-01"
|
||||
run_failure = {"__typename": "RunFailureEvent", "error": {"message": "x" * 501}}
|
||||
assert shape(failed, [step_failure, run_failure]).error == "x" * 500
|
||||
assert shape(failed, []).error == "the run failed"
|
||||
|
||||
|
||||
def test_shape_progress_tag():
|
||||
progress = Progress(
|
||||
phase="analyzing", done=3, total=6, elapsed_s=4, eta_s=4, mode="sync",
|
||||
scheduler=None, updated_at="2026-01-01T00:00:00Z",
|
||||
) # fmt: skip
|
||||
|
||||
def with_tag(value):
|
||||
return PAYLOAD | {"tags": [{"key": "isssteleon/progress", "value": value}]}
|
||||
|
||||
run = shape(with_tag(progress.model_dump_json()), [])
|
||||
assert run.progress is not None
|
||||
assert run.progress.done == 3
|
||||
assert run.progress.phase == "analyzing"
|
||||
assert shape(with_tag("{bad json"), []).progress is None
|
||||
for status, state in STATE.items():
|
||||
run = shape(PAYLOAD | {"status": status}, [])
|
||||
assert (run.status, run.state) == (status, state)
|
||||
assert shape(PAYLOAD | {"status": "UNKNOWN"}, []).state == "running"
|
||||
|
||||
|
||||
def _ok(response):
|
||||
assert response.status_code == 200, response.text
|
||||
return response.json()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def failed_run(api):
|
||||
run_id = _ok(api.post("/models/cacu/analyze"))["run_id"]
|
||||
deadline = time.monotonic() + 120
|
||||
while (run := _ok(api.get(f"/runs/{run_id}")))["state"] != "failed":
|
||||
assert time.monotonic() < deadline, run
|
||||
time.sleep(1)
|
||||
return run
|
||||
|
||||
|
||||
def test_unknown_run_is_404(api):
|
||||
assert api.get("/runs/does-not-exist").status_code == 404
|
||||
assert api.post("/runs/does-not-exist/cancel").status_code == 404
|
||||
|
||||
|
||||
def test_failed_run_reports_its_error(failed_run):
|
||||
assert failed_run["status"] == "FAILURE"
|
||||
assert "analyze" in failed_run["error"]
|
||||
assert isinstance(failed_run["info"], list)
|
||||
assert isinstance(failed_run["warnings"], list)
|
||||
assert any(
|
||||
step["key"] == "analyze_studies" and step["status"] == "FAILURE"
|
||||
for step in failed_run["steps"]
|
||||
)
|
||||
assert failed_run["domain"] == "cacu"
|
||||
assert failed_run["job"] == "analyze"
|
||||
|
||||
|
||||
def test_history_filters_by_job_and_domain(api, failed_run):
|
||||
run_id = failed_run["id"]
|
||||
history = _ok(api.get("/runs", params={"job": "analyze", "domain": "cacu"}))
|
||||
assert run_id in {run["id"] for run in history}
|
||||
for params in ({"job": "train"}, {"domain": "tea"}):
|
||||
assert run_id not in {run["id"] for run in _ok(api.get("/runs", params=params))}
|
||||
|
||||
another_id = _ok(api.post("/models/cacu/analyze"))["run_id"]
|
||||
page = _ok(api.get("/runs", params={"limit": 1}))
|
||||
assert [run["id"] for run in page] == [another_id]
|
||||
page = _ok(api.get("/runs", params={"limit": 1, "before": another_id}))
|
||||
assert [run["id"] for run in page] == [run_id]
|
||||
|
||||
|
||||
def test_cancel_a_finished_run_is_409(api, failed_run):
|
||||
response = api.post(f"/runs/{failed_run['id']}/cancel")
|
||||
assert response.status_code == 409
|
||||
assert response.json()["detail"] == "the run already ended: failed"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_with_dagster_address(platform, request, monkeypatch):
|
||||
"""Capture the Dagster port chosen by the shared API fixture for its first process."""
|
||||
ports = []
|
||||
free_port = conftest._free_port
|
||||
|
||||
def record_port():
|
||||
port = free_port()
|
||||
ports.append(port)
|
||||
return port
|
||||
|
||||
monkeypatch.setattr(conftest, "_free_port", record_port)
|
||||
api = request.getfixturevalue("api")
|
||||
monkeypatch.setenv("DAGSTER_HOST", "127.0.0.1")
|
||||
monkeypatch.setenv("DAGSTER_PORT", str(ports[0]))
|
||||
return api
|
||||
|
||||
|
||||
def test_run_requests_validate_the_execution_choice(api_with_dagster_address):
|
||||
api = api_with_dagster_address
|
||||
path = "/models/tea/analyze"
|
||||
for execution in (
|
||||
{"mode": "remote", "scheduler": "tcp://evil:8786"},
|
||||
{"mode": "local", "scheduler": "tcp://x:8786"},
|
||||
):
|
||||
assert api.post(path, json={"execution": execution}).status_code == 422
|
||||
|
||||
run_id = _ok(api.post(path, json={"execution": {"mode": "sync"}}))["run_id"]
|
||||
data = graphql(
|
||||
"""
|
||||
query RunConfig($id: ID!) {
|
||||
runOrError(runId: $id) { ... on Run { runConfigYaml } }
|
||||
}
|
||||
""",
|
||||
{"id": run_id},
|
||||
)
|
||||
config = yaml.safe_load(data["runOrError"]["runConfigYaml"])
|
||||
assert config["ops"]["analyze_studies"]["config"]["execution"]["mode"] == "sync"
|
||||
|
||||
|
||||
def test_execution_options_and_health(api):
|
||||
options = _ok(api.get("/execution/options"))
|
||||
assert options["default_mode"] == "sync"
|
||||
assert options["schedulers"] == []
|
||||
health = _ok(api.get("/execution/health", params={"mode": "local"}))
|
||||
assert health["reachable"] is True
|
||||
assert health["workers"] >= 1
|
||||
assert api.get("/execution/health", params={"mode": "remote"}).status_code == 422
|
||||
assert api.get("/execution/health?mode=sync&reference=..%2Fx").status_code == 422
|
||||
|
||||
|
||||
def test_tea_eegs_analyze_validates_scope(api):
|
||||
response = api.post("/tea/eegs/analyze", json={"scope": {"mode": "first_n"}})
|
||||
assert response.status_code == 422
|
||||
Reference in New Issue
Block a user