-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_core.py
More file actions
294 lines (245 loc) · 11.9 KB
/
test_core.py
File metadata and controls
294 lines (245 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""Tests for core AsyncDevbox functionality.
Tests the primary AsyncDevbox class including initialization, async CRUD
operations, snapshot creation, blueprint launching, and async execution methods.
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from tests.sdk.conftest import MockDevboxView
from runloop_api_client.sdk import AsyncDevbox
from runloop_api_client.lib.polling import PollingConfig
from runloop_api_client.sdk.async_devbox import (
_AsyncFileInterface,
_AsyncCommandInterface,
_AsyncNetworkInterface,
)
class TestAsyncDevbox:
"""Tests for AsyncDevbox class."""
def test_init(self, mock_async_client: AsyncMock) -> None:
"""Test AsyncDevbox initialization."""
devbox = AsyncDevbox(mock_async_client, "dev_123")
assert devbox.id == "dev_123"
def test_repr(self, mock_async_client: AsyncMock) -> None:
"""Test AsyncDevbox string representation."""
devbox = AsyncDevbox(mock_async_client, "dev_123")
assert repr(devbox) == "<AsyncDevbox id='dev_123'>"
@pytest.mark.asyncio
async def test_context_manager_enter_exit(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test context manager behavior with successful shutdown."""
mock_async_client.devboxes.shutdown = AsyncMock(return_value=devbox_view)
async with AsyncDevbox(mock_async_client, "dev_123") as devbox:
assert devbox.id == "dev_123"
call_kwargs = mock_async_client.devboxes.shutdown.call_args[1]
assert "timeout" not in call_kwargs
@pytest.mark.asyncio
async def test_context_manager_exception_handling(self, mock_async_client: AsyncMock) -> None:
"""Test context manager handles exceptions during shutdown."""
mock_async_client.devboxes.shutdown = AsyncMock(side_effect=RuntimeError("Shutdown failed"))
with pytest.raises(ValueError, match="Test error"):
async with AsyncDevbox(mock_async_client, "dev_123"):
raise ValueError("Test error")
# Shutdown should be called even when body raises exception
mock_async_client.devboxes.shutdown.assert_called_once()
@pytest.mark.asyncio
async def test_get_info(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test get_info method."""
mock_async_client.devboxes.retrieve = AsyncMock(return_value=devbox_view)
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.get_info(
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
)
assert result == devbox_view
mock_async_client.devboxes.retrieve.assert_called_once_with(
"dev_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
)
@pytest.mark.asyncio
async def test_await_running(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test await_running method."""
mock_async_client.devboxes.await_running = AsyncMock(return_value=devbox_view)
polling_config = PollingConfig(timeout_seconds=60.0)
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.await_running(polling_config=polling_config)
assert result == devbox_view
mock_async_client.devboxes.await_running.assert_called_once_with(
"dev_123",
polling_config=polling_config,
)
@pytest.mark.asyncio
async def test_await_suspended(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test await_suspended method."""
mock_async_client.devboxes.await_suspended = AsyncMock(return_value=devbox_view)
polling_config = PollingConfig(timeout_seconds=60.0)
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.await_suspended(polling_config=polling_config)
assert result == devbox_view
mock_async_client.devboxes.await_suspended.assert_called_once_with(
"dev_123",
polling_config=polling_config,
)
@pytest.mark.asyncio
async def test_shutdown(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test shutdown method."""
mock_async_client.devboxes.shutdown = AsyncMock(return_value=devbox_view)
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.shutdown(
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
assert result == devbox_view
mock_async_client.devboxes.shutdown.assert_called_once_with(
"dev_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
@pytest.mark.asyncio
async def test_suspend(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test suspend method."""
mock_async_client.devboxes.suspend = AsyncMock(return_value=devbox_view)
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.suspend(
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
assert result == devbox_view
mock_async_client.devboxes.suspend.assert_called_once_with(
"dev_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
@pytest.mark.asyncio
async def test_resume(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test resume method."""
mock_async_client.devboxes.resume = AsyncMock(return_value=devbox_view)
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.resume(
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
assert result == devbox_view
mock_async_client.devboxes.resume.assert_called_once_with(
"dev_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
@pytest.mark.asyncio
async def test_keep_alive(self, mock_async_client: AsyncMock) -> None:
"""Test keep_alive method."""
mock_async_client.devboxes.keep_alive = AsyncMock(return_value=object())
devbox = AsyncDevbox(mock_async_client, "dev_123")
result = await devbox.keep_alive(
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
assert result is not None # Verify return value is propagated
mock_async_client.devboxes.keep_alive.assert_called_once_with(
"dev_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
@pytest.mark.asyncio
async def test_snapshot_disk(self, mock_async_client: AsyncMock) -> None:
"""Test snapshot_disk waits for completion."""
snapshot_data = SimpleNamespace(id="snap_123")
snapshot_status = SimpleNamespace(status="completed")
mock_async_client.devboxes.snapshot_disk_async = AsyncMock(return_value=snapshot_data)
mock_async_client.devboxes.disk_snapshots.await_completed = AsyncMock(return_value=snapshot_status)
devbox = AsyncDevbox(mock_async_client, "dev_123")
polling_config = PollingConfig(timeout_seconds=60.0)
snapshot = await devbox.snapshot_disk(
name="test-snapshot",
metadata={"key": "value"},
polling_config=polling_config,
extra_headers={"X-Custom": "value"},
)
assert snapshot.id == "snap_123"
mock_async_client.devboxes.snapshot_disk_async.assert_called_once()
call_kwargs = mock_async_client.devboxes.snapshot_disk_async.call_args[1]
assert "commit_message" not in call_kwargs
assert call_kwargs["metadata"] == {"key": "value"}
assert call_kwargs["name"] == "test-snapshot"
assert call_kwargs["extra_headers"] == {"X-Custom": "value"}
assert "polling_config" not in call_kwargs
assert "timeout" not in call_kwargs
mock_async_client.devboxes.disk_snapshots.await_completed.assert_called_once()
call_kwargs2 = mock_async_client.devboxes.disk_snapshots.await_completed.call_args[1]
assert call_kwargs2["polling_config"] == polling_config
assert "timeout" not in call_kwargs2
@pytest.mark.asyncio
async def test_snapshot_disk_async(self, mock_async_client: AsyncMock) -> None:
"""Test snapshot_disk_async returns immediately."""
snapshot_data = SimpleNamespace(id="snap_123")
mock_async_client.devboxes.snapshot_disk_async = AsyncMock(return_value=snapshot_data)
devbox = AsyncDevbox(mock_async_client, "dev_123")
snapshot = await devbox.snapshot_disk_async(
name="test-snapshot",
metadata={"key": "value"},
extra_headers={"X-Custom": "value"},
)
assert snapshot.id == "snap_123"
mock_async_client.devboxes.snapshot_disk_async.assert_called_once()
call_kwargs = mock_async_client.devboxes.snapshot_disk_async.call_args[1]
assert "commit_message" not in call_kwargs
assert call_kwargs["metadata"] == {"key": "value"}
assert call_kwargs["name"] == "test-snapshot"
assert call_kwargs["extra_headers"] == {"X-Custom": "value"}
assert "polling_config" not in call_kwargs
assert "timeout" not in call_kwargs
@pytest.mark.asyncio
async def test_close(self, mock_async_client: AsyncMock, devbox_view: MockDevboxView) -> None:
"""Test close method calls shutdown."""
mock_async_client.devboxes.shutdown = AsyncMock(return_value=devbox_view)
devbox = AsyncDevbox(mock_async_client, "dev_123")
await devbox.close()
mock_async_client.devboxes.shutdown.assert_called_once()
call_kwargs = mock_async_client.devboxes.shutdown.call_args[1]
assert "timeout" not in call_kwargs
def test_cmd_property(self, mock_async_client: AsyncMock) -> None:
"""Test cmd property returns AsyncCommandInterface."""
devbox = AsyncDevbox(mock_async_client, "dev_123")
cmd = devbox.cmd
assert isinstance(cmd, _AsyncCommandInterface)
assert cmd._devbox is devbox
def test_file_property(self, mock_async_client: AsyncMock) -> None:
"""Test file property returns AsyncFileInterface."""
devbox = AsyncDevbox(mock_async_client, "dev_123")
file_interface = devbox.file
assert isinstance(file_interface, _AsyncFileInterface)
assert file_interface._devbox is devbox
def test_net_property(self, mock_async_client: AsyncMock) -> None:
"""Test net property returns AsyncNetworkInterface."""
devbox = AsyncDevbox(mock_async_client, "dev_123")
net = devbox.net
assert isinstance(net, _AsyncNetworkInterface)
assert net._devbox is devbox