Files
awoooi/apps/api/tests/test_health_ollama_provider_chain.py
Your Name 9bac5718da
All checks were successful
CD Pipeline / tests (push) Successful in 6m8s
Code Review / ai-code-review (push) Successful in 10s
CD Pipeline / build-and-deploy (push) Successful in 4m38s
CD Pipeline / post-deploy-checks (push) Successful in 1m42s
feat(health): expose ollama provider chain
2026-05-24 11:44:37 +08:00

69 lines
2.9 KiB
Python

from __future__ import annotations
import pytest
from src.api.v1 import health
def _set_ollama_settings(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(health.settings, "OLLAMA_URL", "http://gcp-a:11434")
monkeypatch.setattr(health.settings, "OLLAMA_SECONDARY_URL", "http://gcp-b:11434")
monkeypatch.setattr(health.settings, "OLLAMA_FALLBACK_URL", "http://local-111:11434")
@pytest.mark.asyncio
async def test_ollama_provider_chain_reports_fallback_when_primary_down(monkeypatch: pytest.MonkeyPatch) -> None:
_set_ollama_settings(monkeypatch)
async def fake_http_check(name: str, _url: str, _path: str) -> health.ComponentHealth:
if name == "ollama_gcp_a":
return health.ComponentHealth(status="down", error="timeout")
if name == "ollama_gcp_b":
return health.ComponentHealth(status="up", latency_ms=42.0)
return health.ComponentHealth(status="up", latency_ms=9.0)
monkeypatch.setattr(health, "_http_health_check", fake_http_check)
aggregate, details = await health.check_ollama_provider_chain()
assert aggregate.status == "degraded"
assert aggregate.latency_ms == 42.0
assert aggregate.error == "primary unavailable; fallback active: ollama_gcp_b"
assert details["ollama_gcp_a"].status == "down"
assert details["ollama_gcp_b"].status == "up"
assert details["ollama_local"].status == "up"
@pytest.mark.asyncio
async def test_ollama_provider_chain_reports_all_endpoints_when_down(monkeypatch: pytest.MonkeyPatch) -> None:
_set_ollama_settings(monkeypatch)
async def fake_http_check(name: str, _url: str, _path: str) -> health.ComponentHealth:
return health.ComponentHealth(status="down", error=f"{name} timeout")
monkeypatch.setattr(health, "_http_health_check", fake_http_check)
aggregate, details = await health.check_ollama_provider_chain()
assert aggregate.status == "down"
assert "ollama_gcp_a=ollama_gcp_a timeout" in (aggregate.error or "")
assert "ollama_gcp_b=ollama_gcp_b timeout" in (aggregate.error or "")
assert "ollama_local=ollama_local timeout" in (aggregate.error or "")
assert set(details) == {"ollama_gcp_a", "ollama_gcp_b", "ollama_local"}
def test_overall_status_uses_aggregate_ollama_not_endpoint_details() -> None:
components = {
"api": health.ComponentHealth(status="up"),
"postgresql": health.ComponentHealth(status="up"),
"redis": health.ComponentHealth(status="up"),
"ollama": health.ComponentHealth(status="degraded"),
"openclaw": health.ComponentHealth(status="up"),
"signoz": health.ComponentHealth(status="up"),
"ollama_gcp_a": health.ComponentHealth(status="down"),
"ollama_gcp_b": health.ComponentHealth(status="up"),
"ollama_local": health.ComponentHealth(status="up"),
}
assert health._determine_overall_status(components) == "degraded"