-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_interfaces.py
More file actions
304 lines (244 loc) · 11.4 KB
/
Copy pathtest_interfaces.py
File metadata and controls
304 lines (244 loc) · 11.4 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
295
296
297
298
299
300
301
302
303
304
"""Tests for Devbox interface classes.
Tests the command, file, and network interface helper classes that provide
structured access to devbox operations.
"""
from __future__ import annotations
from types import SimpleNamespace
from pathlib import Path
from unittest.mock import Mock
import httpx
import pytest
from tests.sdk.conftest import MockExecutionView
from runloop_api_client.sdk import Devbox
class TestCommandInterface:
"""Tests for _CommandInterface."""
def test_exec_without_callbacks(self, mock_client: Mock, execution_view: MockExecutionView) -> None:
"""Test exec without streaming callbacks."""
mock_client.devboxes.execute_async.return_value = execution_view
mock_client.devboxes.executions.await_completed.return_value = execution_view
devbox = Devbox(mock_client, "dbx_123")
result = devbox.cmd.exec("echo hello")
assert result.exit_code == 0
assert result.stdout(num_lines=10) == "output"
call_kwargs = mock_client.devboxes.execute_async.call_args[1]
assert call_kwargs["command"] == "echo hello"
assert "polling_config" not in call_kwargs
assert "timeout" not in call_kwargs
mock_client.devboxes.executions.await_completed.assert_not_called()
def test_exec_with_stdout_callback(self, mock_client: Mock, mock_stream: Mock) -> None:
"""Test exec with stdout callback."""
execution_async = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="running",
)
execution_completed = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="completed",
exit_status=0,
stdout="output",
stderr="",
)
mock_client.devboxes.execute_async.return_value = execution_async
mock_client.devboxes.executions.await_completed.return_value = execution_completed
mock_client.devboxes.executions.stream_stdout_updates.return_value = mock_stream
stdout_calls: list[str] = []
devbox = Devbox(mock_client, "dbx_123")
result = devbox.cmd.exec("echo hello", stdout=stdout_calls.append)
assert result.exit_code == 0
mock_client.devboxes.execute_async.assert_called_once()
mock_client.devboxes.executions.await_completed.assert_called_once()
def test_exec_with_stderr_callback(self, mock_client: Mock, mock_stream: Mock) -> None:
"""Test exec with stderr callback."""
execution_async = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="running",
)
execution_completed = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="completed",
exit_status=0,
stdout="",
stderr="error",
)
mock_client.devboxes.execute_async.return_value = execution_async
mock_client.devboxes.executions.await_completed.return_value = execution_completed
mock_client.devboxes.executions.stream_stderr_updates.return_value = mock_stream
stderr_calls: list[str] = []
devbox = Devbox(mock_client, "dbx_123")
result = devbox.cmd.exec("echo hello", stderr=stderr_calls.append)
assert result.exit_code == 0
mock_client.devboxes.execute_async.assert_called_once()
def test_exec_with_output_callback(self, mock_client: Mock, mock_stream: Mock) -> None:
"""Test exec with output callback."""
execution_async = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="running",
)
execution_completed = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="completed",
exit_status=0,
stdout="output",
stderr="",
)
mock_client.devboxes.execute_async.return_value = execution_async
mock_client.devboxes.executions.await_completed.return_value = execution_completed
mock_client.devboxes.executions.stream_stdout_updates.return_value = mock_stream
mock_client.devboxes.executions.stream_stderr_updates.return_value = mock_stream
output_calls: list[str] = []
devbox = Devbox(mock_client, "dbx_123")
result = devbox.cmd.exec("echo hello", output=output_calls.append)
assert result.exit_code == 0
mock_client.devboxes.execute_async.assert_called_once()
def test_exec_with_all_callbacks(self, mock_client: Mock, mock_stream: Mock) -> None:
"""Test exec with all callbacks."""
execution_async = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="running",
)
execution_completed = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="completed",
exit_status=0,
stdout="output",
stderr="error",
)
mock_client.devboxes.execute_async.return_value = execution_async
mock_client.devboxes.executions.await_completed.return_value = execution_completed
mock_client.devboxes.executions.stream_stdout_updates.return_value = mock_stream
mock_client.devboxes.executions.stream_stderr_updates.return_value = mock_stream
stdout_calls: list[str] = []
stderr_calls: list[str] = []
output_calls: list[str] = []
devbox = Devbox(mock_client, "dbx_123")
result = devbox.cmd.exec(
"echo hello",
stdout=stdout_calls.append,
stderr=stderr_calls.append,
output=output_calls.append,
)
assert result.exit_code == 0
mock_client.devboxes.execute_async.assert_called_once()
def test_exec_async_returns_execution(self, mock_client: Mock, mock_stream: Mock) -> None:
"""Test exec_async returns Execution object."""
execution_async = SimpleNamespace(
execution_id="exn_123",
devbox_id="dbx_123",
status="running",
)
mock_client.devboxes.execute_async.return_value = execution_async
mock_client.devboxes.executions.stream_stdout_updates.return_value = mock_stream
devbox = Devbox(mock_client, "dbx_123")
execution = devbox.cmd.exec_async("long-running command")
assert execution.execution_id == "exn_123"
assert execution.devbox_id == "dbx_123"
mock_client.devboxes.execute_async.assert_called_once()
class TestFileInterface:
"""Tests for _FileInterface."""
def test_read(self, mock_client: Mock) -> None:
"""Test file read."""
mock_client.devboxes.read_file_contents.return_value = "file content"
devbox = Devbox(mock_client, "dbx_123")
result = devbox.file.read(file_path="/path/to/file")
assert result == "file content"
call_kwargs = mock_client.devboxes.read_file_contents.call_args[1]
assert call_kwargs["file_path"] == "/path/to/file"
assert "timeout" not in call_kwargs
def test_write_string(self, mock_client: Mock) -> None:
"""Test file write with string."""
execution_detail = SimpleNamespace()
mock_client.devboxes.write_file_contents.return_value = execution_detail
devbox = Devbox(mock_client, "dbx_123")
result = devbox.file.write(file_path="/path/to/file", contents="content")
assert result == execution_detail
call_kwargs = mock_client.devboxes.write_file_contents.call_args[1]
assert call_kwargs["file_path"] == "/path/to/file"
assert call_kwargs["contents"] == "content"
assert "timeout" not in call_kwargs
def test_write_bytes(self, mock_client: Mock) -> None:
"""Test file write with bytes."""
execution_detail = SimpleNamespace()
mock_client.devboxes.write_file_contents.return_value = execution_detail
devbox = Devbox(mock_client, "dbx_123")
result = devbox.file.write(file_path="/path/to/file", contents="content")
assert result == execution_detail
call_kwargs = mock_client.devboxes.write_file_contents.call_args[1]
assert call_kwargs["file_path"] == "/path/to/file"
assert call_kwargs["contents"] == "content"
assert "timeout" not in call_kwargs
def test_download(self, mock_client: Mock) -> None:
"""Test file download."""
mock_response = Mock(spec=httpx.Response)
mock_response.read.return_value = b"file content"
mock_client.devboxes.download_file.return_value = mock_response
devbox = Devbox(mock_client, "dbx_123")
result = devbox.file.download(path="/path/to/file")
assert result == b"file content"
call_kwargs = mock_client.devboxes.download_file.call_args[1]
assert call_kwargs["path"] == "/path/to/file"
assert "timeout" not in call_kwargs
def test_upload(self, mock_client: Mock, tmp_path: Path) -> None:
"""Test file upload."""
execution_detail = SimpleNamespace()
mock_client.devboxes.upload_file.return_value = execution_detail
devbox = Devbox(mock_client, "dbx_123")
# Create a temporary file for upload
temp_file = tmp_path / "test_file.txt"
temp_file.write_text("test content")
result = devbox.file.upload(path="/remote/path", file=temp_file)
assert result == execution_detail
call_kwargs = mock_client.devboxes.upload_file.call_args[1]
assert call_kwargs["path"] == "/remote/path"
assert call_kwargs["file"] is not None # File object from temp_path
assert "timeout" not in call_kwargs
class TestNetworkInterface:
"""Tests for _NetworkInterface."""
def test_create_ssh_key(self, mock_client: Mock) -> None:
"""Test create SSH key."""
ssh_key_response = SimpleNamespace(public_key="ssh-rsa ...")
mock_client.devboxes.create_ssh_key.return_value = ssh_key_response
devbox = Devbox(mock_client, "dbx_123")
result = devbox.net.create_ssh_key(
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
assert result == ssh_key_response
mock_client.devboxes.create_ssh_key.assert_called_once_with(
"dbx_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)
def test_remove_tunnel(self, mock_client: Mock) -> None:
"""Test remove tunnel."""
mock_client.devboxes.remove_tunnel.return_value = object()
devbox = Devbox(mock_client, "dbx_123")
result = devbox.net.remove_tunnel(
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_client.devboxes.remove_tunnel.assert_called_once_with(
"dbx_123",
extra_headers={"X-Custom": "value"},
extra_query={"param": "value"},
extra_body={"key": "value"},
timeout=30.0,
idempotency_key="key-123",
)