|
1 | 1 | """Unit tests for the /health REST API endpoint.""" |
2 | 2 |
|
3 | 3 | import pytest |
4 | | -from llama_stack_client import APIConnectionError |
| 4 | +from llama_stack_client import APIConnectionError, APIStatusError |
5 | 5 | from pytest_mock import MockerFixture |
6 | 6 |
|
7 | 7 | from app.endpoints.health import ( |
8 | 8 | HealthStatus, |
| 9 | + check_default_model_available, |
9 | 10 | get_providers_health_statuses, |
10 | 11 | liveness_probe_get_method, |
11 | 12 | readiness_probe_get_method, |
@@ -207,3 +208,189 @@ async def test_get_providers_health_statuses_connection_error( |
207 | 208 | assert ( |
208 | 209 | result[0].message == "Failed to initialize health check: Connection error." |
209 | 210 | ) |
| 211 | + |
| 212 | + |
| 213 | +class TestCheckDefaultModelAvailable: |
| 214 | + """Test cases for the check_default_model_available function.""" |
| 215 | + |
| 216 | + @pytest.mark.asyncio |
| 217 | + async def test_model_available(self, mocker: MockerFixture) -> None: |
| 218 | + """Test returns True when the default model is found in the registry.""" |
| 219 | + mock_config = mocker.patch("app.endpoints.health.configuration") |
| 220 | + mock_config.inference.default_model = ( |
| 221 | + "publishers/google/models/gemini-2.5-flash" |
| 222 | + ) |
| 223 | + mock_config.inference.default_provider = "google-vertex" |
| 224 | + |
| 225 | + mock_lsc = mocker.patch("app.endpoints.health.AsyncLlamaStackClientHolder") |
| 226 | + mock_client = mocker.AsyncMock() |
| 227 | + mock_lsc.return_value.get_client.return_value = mock_client |
| 228 | + |
| 229 | + mock_model = mocker.Mock() |
| 230 | + mock_model.id = "google-vertex/publishers/google/models/gemini-2.5-flash" |
| 231 | + mock_client.models.list.return_value = [mock_model] |
| 232 | + |
| 233 | + available, reason = await check_default_model_available() |
| 234 | + |
| 235 | + assert available is True |
| 236 | + assert "is available" in reason |
| 237 | + |
| 238 | + @pytest.mark.asyncio |
| 239 | + async def test_model_not_found(self, mocker: MockerFixture) -> None: |
| 240 | + """Test returns False when the default model is missing from the registry.""" |
| 241 | + mock_config = mocker.patch("app.endpoints.health.configuration") |
| 242 | + mock_config.inference.default_model = ( |
| 243 | + "publishers/google/models/gemini-2.5-flash" |
| 244 | + ) |
| 245 | + mock_config.inference.default_provider = "google-vertex" |
| 246 | + |
| 247 | + mock_lsc = mocker.patch("app.endpoints.health.AsyncLlamaStackClientHolder") |
| 248 | + mock_client = mocker.AsyncMock() |
| 249 | + mock_lsc.return_value.get_client.return_value = mock_client |
| 250 | + |
| 251 | + mock_model = mocker.Mock() |
| 252 | + mock_model.id = "some-other-provider/some-other-model" |
| 253 | + mock_client.models.list.return_value = [mock_model] |
| 254 | + |
| 255 | + available, reason = await check_default_model_available() |
| 256 | + |
| 257 | + assert available is False |
| 258 | + assert "not found in model registry" in reason |
| 259 | + |
| 260 | + @pytest.mark.asyncio |
| 261 | + async def test_no_inference_config(self, mocker: MockerFixture) -> None: |
| 262 | + """Test returns True when no inference configuration exists.""" |
| 263 | + mock_config = mocker.patch("app.endpoints.health.configuration") |
| 264 | + mock_config.inference = None |
| 265 | + |
| 266 | + available, reason = await check_default_model_available() |
| 267 | + |
| 268 | + assert available is True |
| 269 | + assert reason == "No inference configuration" |
| 270 | + |
| 271 | + @pytest.mark.asyncio |
| 272 | + async def test_no_default_model_configured(self, mocker: MockerFixture) -> None: |
| 273 | + """Test returns True when no default model is configured.""" |
| 274 | + mock_config = mocker.patch("app.endpoints.health.configuration") |
| 275 | + mock_config.inference.default_model = None |
| 276 | + mock_config.inference.default_provider = None |
| 277 | + |
| 278 | + available, reason = await check_default_model_available() |
| 279 | + |
| 280 | + assert available is True |
| 281 | + assert reason == "No default model configured" |
| 282 | + |
| 283 | + @pytest.mark.asyncio |
| 284 | + async def test_connection_error(self, mocker: MockerFixture) -> None: |
| 285 | + """Test returns False when model list call fails with connection error.""" |
| 286 | + mock_config = mocker.patch("app.endpoints.health.configuration") |
| 287 | + mock_config.inference.default_model = ( |
| 288 | + "publishers/google/models/gemini-2.5-flash" |
| 289 | + ) |
| 290 | + mock_config.inference.default_provider = "google-vertex" |
| 291 | + |
| 292 | + mock_lsc = mocker.patch("app.endpoints.health.AsyncLlamaStackClientHolder") |
| 293 | + mock_client = mocker.AsyncMock() |
| 294 | + mock_lsc.return_value.get_client.return_value = mock_client |
| 295 | + mock_client.models.list.side_effect = APIConnectionError(request=mocker.Mock()) |
| 296 | + |
| 297 | + available, reason = await check_default_model_available() |
| 298 | + |
| 299 | + assert available is False |
| 300 | + assert "Failed to check model availability" in reason |
| 301 | + |
| 302 | + @pytest.mark.asyncio |
| 303 | + async def test_api_status_error(self, mocker: MockerFixture) -> None: |
| 304 | + """Test returns False when model list call fails with API status error.""" |
| 305 | + mock_config = mocker.patch("app.endpoints.health.configuration") |
| 306 | + mock_config.inference.default_model = ( |
| 307 | + "publishers/google/models/gemini-2.5-flash" |
| 308 | + ) |
| 309 | + mock_config.inference.default_provider = "google-vertex" |
| 310 | + |
| 311 | + mock_lsc = mocker.patch("app.endpoints.health.AsyncLlamaStackClientHolder") |
| 312 | + mock_client = mocker.AsyncMock() |
| 313 | + mock_lsc.return_value.get_client.return_value = mock_client |
| 314 | + |
| 315 | + mock_response = mocker.Mock() |
| 316 | + mock_response.status_code = 500 |
| 317 | + mock_response.headers = {} |
| 318 | + mock_client.models.list.side_effect = APIStatusError( |
| 319 | + message="Internal error", |
| 320 | + response=mock_response, |
| 321 | + body=None, |
| 322 | + ) |
| 323 | + |
| 324 | + available, reason = await check_default_model_available() |
| 325 | + |
| 326 | + assert available is False |
| 327 | + assert "API error checking model availability" in reason |
| 328 | + |
| 329 | + |
| 330 | +@pytest.mark.asyncio |
| 331 | +async def test_readiness_probe_fails_when_model_not_available( |
| 332 | + mocker: MockerFixture, |
| 333 | +) -> None: |
| 334 | + """Test readiness returns 503 when providers are healthy but default model is missing.""" |
| 335 | + mock_authorization_resolvers(mocker) |
| 336 | + |
| 337 | + mock_get_providers = mocker.patch( |
| 338 | + "app.endpoints.health.get_providers_health_statuses" |
| 339 | + ) |
| 340 | + mock_get_providers.return_value = [ |
| 341 | + ProviderHealthStatus( |
| 342 | + provider_id="provider1", |
| 343 | + status=HealthStatus.OK.value, |
| 344 | + message="Provider is healthy", |
| 345 | + ) |
| 346 | + ] |
| 347 | + |
| 348 | + mock_check_model = mocker.patch( |
| 349 | + "app.endpoints.health.check_default_model_available" |
| 350 | + ) |
| 351 | + mock_check_model.return_value = ( |
| 352 | + False, |
| 353 | + "Default model google-vertex/publishers/google/models/gemini-2.5-flash " |
| 354 | + "not found in model registry", |
| 355 | + ) |
| 356 | + |
| 357 | + mock_response = mocker.Mock() |
| 358 | + auth: AuthTuple = ("test_user_id", "test_user", True, "test_token") |
| 359 | + |
| 360 | + response = await readiness_probe_get_method(auth=auth, response=mock_response) |
| 361 | + |
| 362 | + assert response.ready is False |
| 363 | + assert "not found in model registry" in response.reason |
| 364 | + assert mock_response.status_code == 503 |
| 365 | + |
| 366 | + |
| 367 | +@pytest.mark.asyncio |
| 368 | +async def test_readiness_probe_succeeds_with_healthy_providers_and_model( |
| 369 | + mocker: MockerFixture, |
| 370 | +) -> None: |
| 371 | + """Test readiness returns 200 when providers are healthy and default model is available.""" |
| 372 | + mock_authorization_resolvers(mocker) |
| 373 | + |
| 374 | + mock_get_providers = mocker.patch( |
| 375 | + "app.endpoints.health.get_providers_health_statuses" |
| 376 | + ) |
| 377 | + mock_get_providers.return_value = [ |
| 378 | + ProviderHealthStatus( |
| 379 | + provider_id="provider1", |
| 380 | + status=HealthStatus.OK.value, |
| 381 | + message="Provider is healthy", |
| 382 | + ) |
| 383 | + ] |
| 384 | + |
| 385 | + mock_check_model = mocker.patch( |
| 386 | + "app.endpoints.health.check_default_model_available" |
| 387 | + ) |
| 388 | + mock_check_model.return_value = (True, "Default model is available") |
| 389 | + |
| 390 | + mock_response = mocker.Mock() |
| 391 | + auth: AuthTuple = ("test_user_id", "test_user", True, "test_token") |
| 392 | + |
| 393 | + response = await readiness_probe_get_method(auth=auth, response=mock_response) |
| 394 | + |
| 395 | + assert response.ready is True |
| 396 | + assert response.reason == "All providers are healthy" |
0 commit comments