|
1 | | -from app.endpoints.health import readiness_probe_get_method, liveness_probe_get_method |
| 1 | +from unittest.mock import Mock |
2 | 2 |
|
| 3 | +from app.endpoints.health import ( |
| 4 | + readiness_probe_get_method, |
| 5 | + liveness_probe_get_method, |
| 6 | + get_providers_health_statuses, |
| 7 | +) |
| 8 | +from models.responses import ProviderHealthStatus, ReadinessResponse |
| 9 | +from llama_stack.providers.datatypes import HealthStatus |
3 | 10 |
|
4 | | -def test_readiness_probe(mocker): |
5 | | - """Test the readiness endpoint handler.""" |
6 | | - response = readiness_probe_get_method() |
| 11 | + |
| 12 | +def test_readiness_probe_fails_due_to_unhealthy_providers(mocker): |
| 13 | + """Test the readiness endpoint handler fails when providers are unhealthy.""" |
| 14 | + # Mock get_providers_health_statuses to return an unhealthy provider |
| 15 | + mock_get_providers_health_statuses = mocker.patch( |
| 16 | + "app.endpoints.health.get_providers_health_statuses" |
| 17 | + ) |
| 18 | + mock_get_providers_health_statuses.return_value = [ |
| 19 | + ProviderHealthStatus( |
| 20 | + provider_id="test_provider", |
| 21 | + status=HealthStatus.ERROR.value, |
| 22 | + message="Provider is down", |
| 23 | + ) |
| 24 | + ] |
| 25 | + |
| 26 | + # Mock the Response object |
| 27 | + mock_response = Mock() |
| 28 | + |
| 29 | + response = readiness_probe_get_method(mock_response) |
| 30 | + |
| 31 | + assert response.ready is False |
| 32 | + assert "test_provider" in response.reason |
| 33 | + assert "Providers not healthy" in response.reason |
| 34 | + assert mock_response.status_code == 503 |
| 35 | + |
| 36 | + |
| 37 | +def test_readiness_probe_success_when_all_providers_healthy(mocker): |
| 38 | + """Test the readiness endpoint handler succeeds when all providers are healthy.""" |
| 39 | + # Mock get_providers_health_statuses to return healthy providers |
| 40 | + mock_get_providers_health_statuses = mocker.patch( |
| 41 | + "app.endpoints.health.get_providers_health_statuses" |
| 42 | + ) |
| 43 | + mock_get_providers_health_statuses.return_value = [ |
| 44 | + ProviderHealthStatus( |
| 45 | + provider_id="provider1", |
| 46 | + status=HealthStatus.OK.value, |
| 47 | + message="Provider is healthy", |
| 48 | + ), |
| 49 | + ProviderHealthStatus( |
| 50 | + provider_id="provider2", |
| 51 | + status=HealthStatus.NOT_IMPLEMENTED.value, |
| 52 | + message="Provider does not implement health check", |
| 53 | + ), |
| 54 | + ] |
| 55 | + |
| 56 | + # Mock the Response object |
| 57 | + mock_response = Mock() |
| 58 | + |
| 59 | + response = readiness_probe_get_method(mock_response) |
7 | 60 | assert response is not None |
| 61 | + assert isinstance(response, ReadinessResponse) |
8 | 62 | assert response.ready is True |
9 | | - assert response.reason == "service is ready" |
| 63 | + assert response.reason == "All providers are healthy" |
| 64 | + # Should return empty list since no providers are unhealthy |
| 65 | + assert len(response.providers) == 0 |
10 | 66 |
|
11 | 67 |
|
12 | | -def test_liveness_probe(mocker): |
| 68 | +def test_liveness_probe(): |
13 | 69 | """Test the liveness endpoint handler.""" |
14 | 70 | response = liveness_probe_get_method() |
15 | 71 | assert response is not None |
16 | 72 | assert response.alive is True |
| 73 | + |
| 74 | + |
| 75 | +class TestProviderHealthStatus: |
| 76 | + """Test cases for the ProviderHealthStatus model.""" |
| 77 | + |
| 78 | + def test_provider_health_status_creation(self): |
| 79 | + """Test creating a ProviderHealthStatus instance.""" |
| 80 | + status = ProviderHealthStatus( |
| 81 | + provider_id="test_provider", status="ok", message="All good" |
| 82 | + ) |
| 83 | + assert status.provider_id == "test_provider" |
| 84 | + assert status.status == "ok" |
| 85 | + assert status.message == "All good" |
| 86 | + |
| 87 | + def test_provider_health_status_optional_fields(self): |
| 88 | + """Test creating a ProviderHealthStatus with minimal fields.""" |
| 89 | + status = ProviderHealthStatus(provider_id="test_provider", status="ok") |
| 90 | + assert status.provider_id == "test_provider" |
| 91 | + assert status.status == "ok" |
| 92 | + assert status.message is None |
| 93 | + |
| 94 | + |
| 95 | +class TestGetProvidersHealthStatuses: |
| 96 | + """Test cases for the get_providers_health_statuses function.""" |
| 97 | + |
| 98 | + def test_get_providers_health_statuses(self, mocker): |
| 99 | + """Test get_providers_health_statuses with healthy providers.""" |
| 100 | + # Mock the imports |
| 101 | + mock_get_llama_stack_client = mocker.patch( |
| 102 | + "app.endpoints.health.get_llama_stack_client" |
| 103 | + ) |
| 104 | + mock_configuration = mocker.patch("app.endpoints.health.configuration") |
| 105 | + |
| 106 | + # Mock the client and its methods |
| 107 | + mock_client = mocker.Mock() |
| 108 | + mock_get_llama_stack_client.return_value = mock_client |
| 109 | + |
| 110 | + # Mock providers.list() to return providers with health |
| 111 | + mock_provider_1 = mocker.Mock() |
| 112 | + mock_provider_1.provider_id = "provider1" |
| 113 | + mock_provider_1.health = { |
| 114 | + "status": HealthStatus.OK.value, |
| 115 | + "message": "All good", |
| 116 | + } |
| 117 | + |
| 118 | + mock_provider_2 = mocker.Mock() |
| 119 | + mock_provider_2.provider_id = "provider2" |
| 120 | + mock_provider_2.health = { |
| 121 | + "status": HealthStatus.NOT_IMPLEMENTED.value, |
| 122 | + "message": "Provider does not implement health check", |
| 123 | + } |
| 124 | + |
| 125 | + mock_provider_3 = mocker.Mock() |
| 126 | + mock_provider_3.provider_id = "unhealthy_provider" |
| 127 | + mock_provider_3.health = { |
| 128 | + "status": HealthStatus.ERROR.value, |
| 129 | + "message": "Connection failed", |
| 130 | + } |
| 131 | + |
| 132 | + mock_client.providers.list.return_value = [ |
| 133 | + mock_provider_1, |
| 134 | + mock_provider_2, |
| 135 | + mock_provider_3, |
| 136 | + ] |
| 137 | + |
| 138 | + # Mock configuration |
| 139 | + mock_llama_stack_config = mocker.Mock() |
| 140 | + mock_configuration.llama_stack_configuration = mock_llama_stack_config |
| 141 | + |
| 142 | + result = get_providers_health_statuses() |
| 143 | + |
| 144 | + assert len(result) == 3 |
| 145 | + assert result[0].provider_id == "provider1" |
| 146 | + assert result[0].status == HealthStatus.OK.value |
| 147 | + assert result[0].message == "All good" |
| 148 | + assert result[1].provider_id == "provider2" |
| 149 | + assert result[1].status == HealthStatus.NOT_IMPLEMENTED.value |
| 150 | + assert result[1].message == "Provider does not implement health check" |
| 151 | + assert result[2].provider_id == "unhealthy_provider" |
| 152 | + assert result[2].status == HealthStatus.ERROR.value |
| 153 | + assert result[2].message == "Connection failed" |
| 154 | + |
| 155 | + def test_get_providers_health_statuses_connection_error(self, mocker): |
| 156 | + """Test get_providers_health_statuses when connection fails.""" |
| 157 | + # Mock the imports |
| 158 | + mock_get_llama_stack_client = mocker.patch( |
| 159 | + "app.endpoints.health.get_llama_stack_client" |
| 160 | + ) |
| 161 | + mock_configuration = mocker.patch("app.endpoints.health.configuration") |
| 162 | + |
| 163 | + # Mock configuration |
| 164 | + mock_llama_stack_config = mocker.Mock() |
| 165 | + mock_configuration.llama_stack_configuration = mock_llama_stack_config |
| 166 | + |
| 167 | + # Mock get_llama_stack_client to raise an exception |
| 168 | + mock_get_llama_stack_client.side_effect = Exception("Connection error") |
| 169 | + |
| 170 | + result = get_providers_health_statuses() |
| 171 | + |
| 172 | + assert len(result) == 1 |
| 173 | + assert result[0].provider_id == "unknown" |
| 174 | + assert result[0].status == HealthStatus.ERROR.value |
| 175 | + assert ( |
| 176 | + result[0].message == "Failed to initialize health check: Connection error" |
| 177 | + ) |
0 commit comments