Skip to content

Commit 72129ee

Browse files
authored
Merge pull request #115 from VectorInstitute/test_api
Add API tests for model shut down and wait_until_ready function
2 parents 09aa9ce + bc4c9a8 commit 72129ee

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/vec_inf/client/test_api.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from vec_inf.client import ModelStatus, ModelType, VecInfClient
8+
from vec_inf.client._exceptions import ServerError, SlurmJobError
89

910

1011
@pytest.fixture
@@ -128,3 +129,84 @@ def test_wait_until_ready():
128129
assert result.server_status == ModelStatus.READY
129130
assert result.base_url == "http://gpu123:8080/v1"
130131
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

Comments
 (0)