|
5 | 5 | import pytest
|
6 | 6 |
|
7 | 7 | from vec_inf.client import ModelStatus, ModelType, VecInfClient
|
| 8 | +from vec_inf.client._exceptions import ServerError, SlurmJobError |
8 | 9 |
|
9 | 10 |
|
10 | 11 | @pytest.fixture
|
@@ -128,3 +129,84 @@ def test_wait_until_ready():
|
128 | 129 | assert result.server_status == ModelStatus.READY
|
129 | 130 | assert result.base_url == "http://gpu123:8080/v1"
|
130 | 131 | assert mock_status.call_count == 2
|
| 132 | + |
| 133 | + |
| 134 | +def test_shutdown_model_success(): |
| 135 | + """Test model shutdown success.""" |
| 136 | + client = VecInfClient() |
| 137 | + with patch("vec_inf.client.api.run_bash_command") as mock_command: |
| 138 | + mock_command.return_value = ("", "") |
| 139 | + result = client.shutdown_model(12345) |
| 140 | + |
| 141 | + assert result is True |
| 142 | + mock_command.assert_called_once_with("scancel 12345") |
| 143 | + |
| 144 | + |
| 145 | +def test_shutdown_model_failure(): |
| 146 | + """Test model shutdown failure.""" |
| 147 | + client = VecInfClient() |
| 148 | + with patch("vec_inf.client.api.run_bash_command") as mock_command: |
| 149 | + mock_command.return_value = ("", "Error: Job not found") |
| 150 | + with pytest.raises( |
| 151 | + SlurmJobError, match="Failed to shutdown model: Error: Job not found" |
| 152 | + ): |
| 153 | + client.shutdown_model(12345) |
| 154 | + |
| 155 | + |
| 156 | +def test_wait_until_ready_timeout(): |
| 157 | + """Test timeout in wait_until_ready.""" |
| 158 | + client = VecInfClient() |
| 159 | + |
| 160 | + with patch.object(client, "get_status") as mock_status: |
| 161 | + mock_response = MagicMock() |
| 162 | + mock_response.server_status = ModelStatus.LAUNCHING |
| 163 | + mock_status.return_value = mock_response |
| 164 | + |
| 165 | + with ( |
| 166 | + patch("time.sleep"), |
| 167 | + pytest.raises(ServerError, match="Timed out waiting for model"), |
| 168 | + ): |
| 169 | + client.wait_until_ready(12345, timeout_seconds=1, poll_interval_seconds=0.5) |
| 170 | + |
| 171 | + |
| 172 | +def test_wait_until_ready_failed_status(): |
| 173 | + """Test wait_until_ready when model fails.""" |
| 174 | + client = VecInfClient() |
| 175 | + |
| 176 | + with patch.object(client, "get_status") as mock_status: |
| 177 | + mock_response = MagicMock() |
| 178 | + mock_response.server_status = ModelStatus.FAILED |
| 179 | + mock_response.failed_reason = "Out of memory" |
| 180 | + mock_status.return_value = mock_response |
| 181 | + |
| 182 | + with pytest.raises(ServerError, match="Model failed to start: Out of memory"): |
| 183 | + client.wait_until_ready(12345) |
| 184 | + |
| 185 | + |
| 186 | +def test_wait_until_ready_failed_no_reason(): |
| 187 | + """Test wait_until_ready when model fails without reason.""" |
| 188 | + client = VecInfClient() |
| 189 | + |
| 190 | + with patch.object(client, "get_status") as mock_status: |
| 191 | + mock_response = MagicMock() |
| 192 | + mock_response.server_status = ModelStatus.FAILED |
| 193 | + mock_response.failed_reason = None |
| 194 | + mock_status.return_value = mock_response |
| 195 | + |
| 196 | + with pytest.raises(ServerError, match="Model failed to start: Unknown error"): |
| 197 | + client.wait_until_ready(12345) |
| 198 | + |
| 199 | + |
| 200 | +def test_wait_until_ready_shutdown(): |
| 201 | + """Test wait_until_ready when model is shutdown.""" |
| 202 | + client = VecInfClient() |
| 203 | + |
| 204 | + with patch.object(client, "get_status") as mock_status: |
| 205 | + mock_response = MagicMock() |
| 206 | + mock_response.server_status = ModelStatus.SHUTDOWN |
| 207 | + mock_status.return_value = mock_response |
| 208 | + |
| 209 | + with pytest.raises( |
| 210 | + ServerError, match="Model was shutdown before it became ready" |
| 211 | + ): |
| 212 | + client.wait_until_ready(12345) |
0 commit comments