-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sdk.py
More file actions
425 lines (360 loc) · 12.5 KB
/
Copy pathtest_sdk.py
File metadata and controls
425 lines (360 loc) · 12.5 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from __future__ import annotations
import json
import httpx
import pytest
from createos import (
APIError,
AuthenticationError,
Client,
CommandStreamEvent,
CreateSandboxRequest,
ExecStreamEventType,
ForkSandboxRequest,
ManagedProcessConnectEvent,
ProtocolError,
RequestOptions,
RunCommandRequest,
SandboxStatus,
)
def envelope(data, status_code=200, headers=None):
return httpx.Response(
status_code,
json={"status": "success", "data": data},
headers=headers,
)
def mock_client(handler):
return httpx.Client(transport=httpx.MockTransport(handler))
def test_sandbox_access_token_lifecycle_and_scoped_credential():
seen = []
responses = [
envelope({"id": "sb-1", "status": "running"}),
envelope(
{
"token": "skp_sb_first",
"enabled": True,
"created_at": "2026-09-18T10:00:00Z",
}
),
envelope(
{
"enabled": True,
"token_hint": "skp_sb...irst",
"created_at": "2026-09-18T10:00:00Z",
}
),
envelope(
{
"result": {"stdout": "hello\n", "stderr": "", "exit_code": 0},
"exec_ms": 1,
}
),
envelope(
{
"token": "skp_sb_second",
"enabled": True,
"created_at": "2026-09-18T10:00:00Z",
"rotated_at": "2026-09-18T11:00:00Z",
}
),
envelope({"enabled": False}),
]
def handler(request):
seen.append(
(request.method, request.url.path, request.headers["x-api-key"])
)
return responses.pop(0)
client = Client(
api_key="owner",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandbox = client.get_sandbox("sb-1")
created = sandbox.create_access_token()
assert created.token == "skp_sb_first" and created.created_at.year == 2026
assert sandbox.get_access_token().token_hint == "skp_sb...irst"
worker = sandbox.with_access_token(created.token)
assert worker is not sandbox and worker.files is not sandbox.files
assert (
worker.run_command(
RunCommandRequest(command="echo", arguments=["hello"])
).result.standard_output
== "hello\n"
)
assert sandbox.rotate_access_token().rotated_at.hour == 11
assert sandbox.disable_access_token().enabled is False
assert seen == [
("GET", "/v1/sandboxes/sb-1", "owner"),
("POST", "/v1/sandboxes/sb-1/access-token", "owner"),
("GET", "/v1/sandboxes/sb-1/access-token", "owner"),
("POST", "/v1/sandboxes/sb-1/exec", "skp_sb_first"),
("POST", "/v1/sandboxes/sb-1/access-token/rotate", "owner"),
("DELETE", "/v1/sandboxes/sb-1/access-token", "owner"),
]
with pytest.raises(ValueError):
sandbox.with_access_token(" ")
def test_health_omits_auth_and_whoami_sends_it():
seen = []
def handler(request):
seen.append(request)
if request.url.path == "/healthz":
return envelope({"up": True})
return envelope({"user_id": "user-1", "stats": {"total": 1}})
client = Client(
api_key="secret",
base_url="https://example.test",
http_client=mock_client(handler),
)
assert client.health().up is True
assert client.who_am_i().user_id == "user-1"
assert "x-api-key" not in seen[0].headers
assert seen[1].headers["x-api-key"] == "secret"
def test_injected_client_credentials_are_removed():
seen = []
def handler(request):
seen.append(request)
if request.url.path == "/healthz":
return envelope({"up": True})
return envelope({"user_id": "user-1", "stats": {"total": 1}})
supplied = httpx.Client(
transport=httpx.MockTransport(handler),
headers={
"X-Api-Key": "injected",
"Authorization": "Bearer injected",
"Cookie": "session=injected",
"X-CSRF-Token": "injected",
},
auth=httpx.BasicAuth("injected", "secret"),
)
client = Client(
api_key="sdk-key",
base_url="https://example.test",
http_client=supplied,
)
assert client.health().up is True
assert client.who_am_i().user_id == "user-1"
for request in seen:
assert "authorization" not in request.headers
assert "cookie" not in request.headers
assert "x-csrf-token" not in request.headers
assert "x-api-key" not in seen[0].headers
assert seen[1].headers["x-api-key"] == "sdk-key"
def test_error_body_is_read_only_up_to_limit():
class CountingStream(httpx.SyncByteStream):
chunks = 0
def __iter__(self):
for _ in range(8):
self.chunks += 1
yield b"x" * (1 << 20)
source = CountingStream()
supplied = mock_client(lambda _: httpx.Response(404, stream=source))
client = Client(
api_key="sdk-key",
base_url="https://example.test",
http_client=supplied,
)
with pytest.raises(APIError) as caught:
client.get_sandbox("missing")
assert len(caught.value.body) == 4 << 20
assert source.chunks <= 4
def test_client_uses_createos_api_key_environment_variable(monkeypatch):
seen = []
def handler(request):
seen.append(request)
return envelope({"user_id": "user-1", "stats": {"total": 1}})
monkeypatch.setenv("CREATEOS_API_KEY", "new-key")
with Client(
base_url="https://example.test", http_client=mock_client(handler)
) as client:
client.who_am_i()
assert seen[0].headers["x-api-key"] == "new-key"
monkeypatch.delenv("CREATEOS_API_KEY")
with Client(
base_url="https://example.test", http_client=mock_client(handler)
) as client:
with pytest.raises(AuthenticationError):
client.who_am_i()
assert len(seen) == 1
def test_create_initializes_state_services_and_clear_wire_fields():
requests = []
def handler(request):
requests.append(request)
return envelope(
{
"id": "sb-1",
"status": "paused",
"name": "demo",
"ip": "10.0.0.2",
"shape": "s-4vcpu-4gb",
"mem_mib": 4096,
"disk_mib": 8192,
"ingress_url_template": "https://<port>-sb-1.example.test",
}
)
client = Client(
api_key="key",
base_url="https://example.test/api",
http_client=mock_client(handler),
)
sandbox = client.create_sandbox(
CreateSandboxRequest(
shape="s-4vcpu-4gb", name="demo", ingress_enabled=True
)
)
assert requests[0].url.path == "/api/v1/sandboxes"
assert sandbox.id == "sb-1"
assert sandbox.status is SandboxStatus.PAUSED
assert sandbox.files and sandbox.processes and sandbox.computer.mouse
assert sandbox.preview_url(/sitelet?url=https%3A%2F%2Fgithub.com%2FNodeOps-app%2Fcreateos-python-sdk%2Fblob%2Ffix%2Fpython-version-0.1.3%2Ftests%2F8080) == "https://8080-sb-1.example.test"
def test_projected_stream_response_aliases_match_go_contract():
command = CommandStreamEvent.from_dict({"type": "exit", "exitCode": 17})
process = ManagedProcessConnectEvent.from_dict(
{
"type": "exit",
"exitCode": 23,
"oldestAvailableSeq": 9,
}
)
assert command.exit_code == 17
assert command.to_dict()["exitCode"] == 17
assert process.exit_code == 23
assert process.oldest_available_sequence == 9
assert process.to_dict()["oldestAvailableSeq"] == 9
def test_destroy_uses_response_status_without_a_fallback():
def handler(request):
if request.method == "GET":
return envelope({"id": "sb-1", "status": "running"})
return envelope({"id": "sb-1", "status": "destroyed"})
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandbox = client.get_sandbox("sb-1")
sandbox.destroy()
assert sandbox.status is SandboxStatus.DESTROYED
def test_destroy_rejects_a_response_without_status():
def handler(request):
if request.method == "GET":
return envelope({"id": "sb-1", "status": "running"})
return envelope({"id": "sb-1"})
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandbox = client.get_sandbox("sb-1")
with pytest.raises(ProtocolError, match="DestroyedResponse"):
sandbox.destroy()
assert sandbox.status is SandboxStatus.RUNNING
def test_fork_serializes_explicit_false():
bodies = []
def handler(request):
if request.method == "GET":
return envelope({"id": "source", "status": "running"})
bodies.append(json.loads(request.content))
return envelope({"id": "fork-1", "status": "paused"})
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandbox = client.get_sandbox("source")
clone = sandbox.fork(ForkSandboxRequest(ingress_enabled=False))
assert clone.id == "fork-1"
assert bodies[-1] == {"ingress_enabled": False}
def test_api_error_is_inspectable():
def handler(request):
return httpx.Response(
404,
json={"status": "error", "message": "not found", "code": 42},
headers={"X-Request-ID": "req-1"},
)
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
with pytest.raises(APIError) as caught:
client.get_sandbox("missing")
assert caught.value.status_code == 404
assert caught.value.code == 42
assert caught.value.request_id == "req-1"
def test_list_sandboxes_walks_pages():
offsets = []
def handler(request):
offset = int(request.url.params["offset"])
offsets.append(offset)
rows = [
{"id": f"sb-{index}", "status": "running"}
for index in range(offset, min(offset + 2, 3))
]
return envelope(
{
"data": rows,
"pagination": {
"total": 3,
"offset": offset,
"count": len(rows),
},
}
)
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandboxes = client.list_sandboxes()
assert [item.id for item in sandboxes] == ["sb-0", "sb-1", "sb-2"]
assert offsets == [0, 2]
def test_command_stream_projects_frames_and_accepts_sse():
payload = (
b': keepalive\ndata: {"stdout":"hi","stderr":"warn"}\n{"exit_code":0}\n'
)
def handler(request):
if request.method == "GET":
return envelope({"id": "sb-1", "status": "running"})
return httpx.Response(200, content=payload)
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandbox = client.get_sandbox("sb-1")
with sandbox.stream_command(RunCommandRequest(command="echo")) as stream:
events = list(stream)
assert [event.type for event in events] == [
ExecStreamEventType.STDOUT,
ExecStreamEventType.STDERR,
ExecStreamEventType.EXIT,
]
assert events[-1].exit_code == 0
def test_file_transfers_use_operation_timeout():
requests = []
def handler(request):
requests.append(request)
if request.url.path == "/v1/sandboxes/sb-1":
return envelope({"id": "sb-1", "status": "running"})
if request.method == "PUT":
assert request.content == b"contents"
return envelope({"bytes": 8, "path": "/workspace/file.txt"})
return httpx.Response(200, content=b"contents")
client = Client(
api_key="key",
base_url="https://example.test",
http_client=mock_client(handler),
)
sandbox = client.get_sandbox("sb-1")
sandbox.files.upload(
"/workspace/file.txt",
b"contents",
RequestOptions(timeout=300),
)
with sandbox.files.download(
"/workspace/file.txt",
RequestOptions(timeout=600),
) as download:
assert download.read() == b"contents"
upload_timeout = requests[1].extensions["timeout"]
download_timeout = requests[2].extensions["timeout"]
assert set(upload_timeout.values()) == {300}
assert set(download_timeout.values()) == {600}