-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathtest_create.py
More file actions
221 lines (173 loc) · 7.03 KB
/
Copy pathtest_create.py
File metadata and controls
221 lines (173 loc) · 7.03 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
from time import sleep
from typing import Any, cast
from uuid import uuid4
import httpx
import pytest
from e2b import CommandExitException, Sandbox, SandboxState
from e2b.api.client.models import (
NewSandbox,
SandboxAutoResumeConfig,
)
from e2b.exceptions import InvalidArgumentException
from e2b.sandbox.sandbox_api import SandboxQuery
@pytest.mark.skip_debug()
def test_start(sandbox_factory):
sbx = sandbox_factory(timeout=5)
assert sbx.is_running()
assert sbx._envd_version is not None
@pytest.mark.skip_debug()
def test_metadata(sandbox_factory):
sbx = sandbox_factory(timeout=5, metadata={"test-key": "test-value"})
paginator = Sandbox.list(query=SandboxQuery(metadata={"test-key": "test-value"}))
sandboxes = paginator.next_items()
for sbx_info in sandboxes:
if sbx.sandbox_id == sbx_info.sandbox_id:
assert sbx_info.metadata is not None
assert sbx_info.metadata["test-key"] == "test-value"
break
else:
assert False, "Sandbox not found"
@pytest.mark.skip_debug()
def test_mcp_gateway_start_failure_kills_created_sandbox(template):
metadata = {"mcp_gateway_cleanup_test_id": str(uuid4())}
query = SandboxQuery(state=[SandboxState.RUNNING], metadata=metadata)
remaining_sandboxes = []
try:
# The base template has no mcp-gateway binary, so gateway startup
# reliably fails after the sandbox has been allocated.
with pytest.raises(CommandExitException):
Sandbox.create(
template,
timeout=60,
metadata=metadata,
mcp=cast(Any, {"invalid_server": {}}),
)
remaining_sandboxes = Sandbox.list(query=query).next_items()
assert remaining_sandboxes == []
finally:
try:
remaining_sandboxes = Sandbox.list(query=query).next_items()
except Exception:
pass
for sandbox in remaining_sandboxes:
Sandbox.kill(sandbox.sandbox_id)
def test_create_payload_serializes_auto_resume_enabled():
body = NewSandbox(
template_id="template-id",
auto_pause=True,
auto_resume=SandboxAutoResumeConfig(enabled=True),
)
assert body.to_dict()["autoPause"] is True
assert body.to_dict()["autoResume"] == {"enabled": True}
def test_create_payload_deserializes_auto_resume_enabled():
body = NewSandbox.from_dict(
{
"templateID": "template-id",
"autoPause": False,
"autoResume": {"enabled": False},
}
)
assert isinstance(body.auto_resume, SandboxAutoResumeConfig)
assert body.auto_resume.to_dict() == {"enabled": False}
@pytest.mark.skip_debug()
def test_filesystem_only_auto_pause_rejects_auto_resume():
# A filesystem-only auto-pause snapshot can only be resumed explicitly, so
# combining keep_memory=False with auto_resume is rejected client-side.
with pytest.raises(InvalidArgumentException):
Sandbox.create(
timeout=3,
lifecycle={
"on_timeout": {"action": "pause", "keep_memory": False},
"auto_resume": True,
},
)
@pytest.mark.skip_debug()
def test_keep_memory_not_allowed_with_kill():
# The discriminated union forbids keep_memory on action="kill" at type-check
# time; the runtime guard rejects it for callers that bypass the type
# (cast(Any, ...) feeds the deliberately type-invalid input).
with pytest.raises(InvalidArgumentException):
Sandbox.create(
timeout=3,
lifecycle=cast(
Any, {"on_timeout": {"action": "kill", "keep_memory": False}}
),
)
@pytest.mark.skip_debug()
def test_invalid_on_timeout_type_does_not_crash(sandbox_factory):
# An untyped/invalid on_timeout (e.g. None) must not crash create; it falls
# back to kill semantics like a missing on_timeout (the sandbox just starts).
sbx = sandbox_factory(timeout=10, lifecycle=cast(Any, {"on_timeout": None}))
assert sbx.is_running()
@pytest.mark.skip_debug()
def test_keep_memory_none_defaults_to_full_memory(sandbox_factory):
# An explicit None keep_memory must default to full memory (not filesystem-only):
# the timeout auto-pause then resumes the SAME sandbox in place (memory restore),
# so the boot id is unchanged. A changed boot id would mean None was wrongly
# treated as filesystem-only (cold boot).
sbx = sandbox_factory(
timeout=60,
lifecycle={"on_timeout": {"action": "pause", "keep_memory": None}},
)
boot_before = sbx.files.read("/proc/sys/kernel/random/boot_id").strip()
sbx.set_timeout(0) # force the timeout auto-pause now
for _ in range(150):
if not sbx.is_running():
break
sleep(0.2)
assert not sbx.is_running()
resumed = sbx.connect()
assert resumed.sandbox_id == sbx.sandbox_id # same sandbox
boot_after = resumed.files.read("/proc/sys/kernel/random/boot_id").strip()
assert boot_after == boot_before # memory restore in place, not a cold boot
@pytest.mark.skip_debug()
def test_auto_pause_filesystem_only_reboots(sandbox_factory):
# keep_memory=False makes the timeout auto-pause filesystem-only, so resuming
# cold-boots the sandbox from disk.
sandbox = sandbox_factory(
timeout=3,
lifecycle={"on_timeout": {"action": "pause", "keep_memory": False}},
)
marker = "auto-pause-fs-only"
sandbox.files.write("/home/user/auto-pause-marker.txt", marker)
boot_before = sandbox.files.read("/proc/sys/kernel/random/boot_id").strip()
sleep(5)
assert sandbox.get_info().state == SandboxState.PAUSED
# A filesystem-only snapshot cannot auto-resume on traffic; connect resumes
# it by cold-booting.
resumed = sandbox.connect()
persisted = resumed.files.read("/home/user/auto-pause-marker.txt").strip()
assert persisted == marker
boot_after = resumed.files.read("/proc/sys/kernel/random/boot_id").strip()
assert boot_after != boot_before
@pytest.mark.skip_debug()
def test_auto_pause_without_auto_resume_requires_connect(sandbox_factory):
sandbox = sandbox_factory(
timeout=3,
lifecycle={"on_timeout": "pause", "auto_resume": False},
)
sleep(5)
assert sandbox.get_info().state == SandboxState.PAUSED
assert not sandbox.is_running()
sandbox.connect()
assert sandbox.get_info().state == SandboxState.RUNNING
assert sandbox.is_running()
@pytest.mark.skip_debug()
def test_auto_resume_wakes_on_http_request(sandbox_factory):
sandbox = sandbox_factory(
timeout=3,
lifecycle={"on_timeout": "pause", "auto_resume": True},
)
cmd = sandbox.commands.run("python3 -m http.server 8000", background=True)
try:
sleep(5)
url = f"https://{sandbox.get_host(8000)}"
res = httpx.get(url, timeout=15.0)
assert res.status_code == 200
assert sandbox.get_info().state == SandboxState.RUNNING
assert sandbox.is_running()
finally:
try:
cmd.kill()
except Exception:
pass