fix: narrow exception handling in list_nodes, move mock imports to top
- Remove redundant httpx.ConnectError from nodes except clause (it's a subclass of HTTPError so the tuple catch was redundant) - Narrow services except clause from bare Exception to httpx.HTTPError, add logger.warning with coordinator_url for debuggability - Move `from unittest.mock import MagicMock, patch` from mid-file to the top-of-file import block with the other stdlib/third-party imports
This commit is contained in:
parent
c2de9e53da
commit
47cb9f661f
2 changed files with 4 additions and 3 deletions
|
|
@ -117,7 +117,7 @@ def list_nodes() -> list:
|
||||||
r = httpx.get(f"{coordinator_url}/api/nodes", timeout=5.0)
|
r = httpx.get(f"{coordinator_url}/api/nodes", timeout=5.0)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
coord_nodes: list[dict] = r.json()
|
coord_nodes: list[dict] = r.json()
|
||||||
except (httpx.HTTPError, httpx.ConnectError) as exc:
|
except httpx.HTTPError as exc:
|
||||||
logger.warning("Coordinator unreachable: %s", exc)
|
logger.warning("Coordinator unreachable: %s", exc)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
@ -125,7 +125,8 @@ def list_nodes() -> list:
|
||||||
sr = httpx.get(f"{coordinator_url}/api/services", timeout=5.0)
|
sr = httpx.get(f"{coordinator_url}/api/services", timeout=5.0)
|
||||||
sr.raise_for_status()
|
sr.raise_for_status()
|
||||||
services_data: list[dict] = sr.json()
|
services_data: list[dict] = sr.json()
|
||||||
except Exception:
|
except httpx.HTTPError:
|
||||||
|
logger.warning("Services API unreachable for %s, skipping", coordinator_url)
|
||||||
services_data = []
|
services_data = []
|
||||||
|
|
||||||
# Build per-node, per-GPU running services map
|
# Build per-node, per-GPU running services map
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from pathlib import Path
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
|
|
@ -47,7 +48,6 @@ def test_list_nodes_returns_empty_when_no_coordinator(client):
|
||||||
assert r.json() == []
|
assert r.json() == []
|
||||||
|
|
||||||
|
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
|
|
||||||
|
|
||||||
def _fake_nodes_response(nodes_json: list, services_json: list | None = None):
|
def _fake_nodes_response(nodes_json: list, services_json: list | None = None):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue