-
Notifications
You must be signed in to change notification settings - Fork 886
Expand file tree
/
Copy pathtest_stacktrace.py
More file actions
354 lines (293 loc) · 11.6 KB
/
test_stacktrace.py
File metadata and controls
354 lines (293 loc) · 11.6 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
import traceback
from types import SimpleNamespace
from typing import List, Optional
from uuid import uuid4
import pytest
import linecache
from e2b import AsyncTemplate, CopyItem, wait_for_timeout
from e2b.template.types import TemplateBuildStatus
import e2b.template_async.main as template_async_main
import e2b.template_async.build_api as build_api_mod
non_existent_path = "nonexistent/path"
# map template alias -> failed step index
failure_map: dict[str, Optional[int]] = {
"from_image": 0,
"from_template": 0,
"from_dockerfile": 0,
"from_image_registry": 0,
"from_aws_registry": 0,
"from_gcp_registry": 0,
"copy": None,
"copy_items": None,
"remove": 1,
"rename": 1,
"make_dir": 1,
"make_symlink": 1,
"run_cmd": 1,
"set_workdir": 1,
"set_user": 1,
"pip_install": 1,
"npm_install": 1,
"apt_install": 1,
"git_clone": 1,
"set_start_cmd": 1,
"add_mcp_server": None,
"beta_dev_container_prebuild": 1,
"beta_set_dev_container_start": 1,
}
@pytest.fixture(autouse=True)
def mock_template_build(monkeypatch):
async def mock_request_build(
client, name: str, tags: Optional[List[str]], cpu_count: int, memory_mb: int
):
return SimpleNamespace(template_id=name, build_id=str(uuid4()), tags=tags or [])
async def mock_trigger_build(client, template_id: str, build_id: str, template):
return None
async def mock_get_build_status(
client, template_id: str, build_id: str, logs_offset: int
):
step = failure_map[template_id]
reason = SimpleNamespace(
message="Mocked API build error",
log_entries=[],
step=str(step) if step is not None else None,
)
return SimpleNamespace(
status=TemplateBuildStatus.ERROR,
log_entries=[],
reason=reason,
)
monkeypatch.setattr(template_async_main, "request_build", mock_request_build)
monkeypatch.setattr(template_async_main, "trigger_build", mock_trigger_build)
monkeypatch.setattr(build_api_mod, "get_build_status", mock_get_build_status)
async def _expect_to_throw_and_check_trace(func, expected_method: str):
try:
await func()
assert False, "Expected AsyncTemplate.build to raise an exception"
except Exception as e: # noqa: BLE001 - we want to assert on the traceback regardless of type
tb = e.__traceback__
saw_this_file = False
saw_expected_method = False
while tb is not None:
traceback_file = tb.tb_frame.f_code.co_filename
if traceback_file == __file__:
saw_this_file = True
caller_line = linecache.getline(traceback_file, tb.tb_lineno)
if caller_line and f".{expected_method}(" in caller_line:
saw_expected_method = True
break
tb = tb.tb_next
assert saw_this_file, traceback.format_exc()
assert saw_expected_method, traceback.format_exc()
@pytest.mark.skip_debug()
async def test_traces_on_from_image(async_build):
template = AsyncTemplate().from_image("e2b.dev/this-image-does-not-exist")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="from_image", skip_cache=True), "from_image"
)
@pytest.mark.skip_debug()
async def test_traces_on_from_template(async_build):
template = AsyncTemplate().from_template("this-template-does-not-exist")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="from_template", skip_cache=True),
"from_template",
)
@pytest.mark.skip_debug()
async def test_traces_on_from_dockerfile(async_build):
template = AsyncTemplate().from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="from_dockerfile", skip_cache=True),
"from_dockerfile",
)
@pytest.mark.skip_debug()
async def test_traces_on_from_image_registry(async_build):
template = AsyncTemplate().from_image(
"registry.example.com/nonexistent:latest",
username="test",
password="test",
)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="from_image_registry", skip_cache=True),
"from_image",
)
@pytest.mark.skip_debug()
async def test_traces_on_from_aws_registry(async_build):
template = AsyncTemplate().from_aws_registry(
"123456789.dkr.ecr.us-east-1.amazonaws.com/nonexistent:latest",
access_key_id="test",
secret_access_key="test",
region="us-east-1",
)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="from_aws_registry"), "from_aws_registry"
)
@pytest.mark.skip_debug()
async def test_traces_on_from_gcp_registry(async_build):
template = AsyncTemplate().from_gcp_registry(
"gcr.io/nonexistent-project/nonexistent:latest",
service_account_json={
"type": "service_account",
},
)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="from_gcp_registry"), "from_gcp_registry"
)
@pytest.mark.skip_debug()
async def test_traces_on_copy(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().copy(non_existent_path, non_existent_path)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="copy"), "copy"
)
@pytest.mark.skip_debug()
async def test_traces_on_copyItems(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().copy_items(
[CopyItem(src=non_existent_path, dest=non_existent_path)]
)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="copy_items"), "copy_items"
)
@pytest.mark.skip_debug()
async def test_traces_on_copy_absolute_path():
await _expect_to_throw_and_check_trace(
lambda: (
AsyncTemplate().from_base_image().copy("/absolute/path", "/absolute/path")
),
"copy",
)
@pytest.mark.skip_debug()
async def test_traces_on_copyItems_absolute_path():
await _expect_to_throw_and_check_trace(
lambda: (
AsyncTemplate()
.from_base_image()
.copy_items([CopyItem(src="/absolute/path", dest="/absolute/path")])
),
"copy_items",
)
@pytest.mark.skip_debug()
async def test_traces_on_remove(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().remove(non_existent_path)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="remove"), "remove"
)
@pytest.mark.skip_debug()
async def test_traces_on_rename(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().rename(non_existent_path, "/tmp/dest.txt")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="rename"), "rename"
)
@pytest.mark.skip_debug()
async def test_traces_on_make_dir(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.set_user("root").skip_cache().make_dir("/root/.bashrc")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="make_dir"), "make_dir"
)
@pytest.mark.skip_debug()
async def test_traces_on_make_symlink(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().make_symlink(".bashrc", ".bashrc")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="make_symlink"), "make_symlink"
)
@pytest.mark.skip_debug()
async def test_traces_on_run_cmd(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().run_cmd(f"cat {non_existent_path}")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="run_cmd"), "run_cmd"
)
@pytest.mark.skip_debug()
async def test_traces_on_set_workdir(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.set_user("root").skip_cache().set_workdir("/root/.bashrc")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="set_workdir"), "set_workdir"
)
@pytest.mark.skip_debug()
async def test_traces_on_set_user(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().set_user("; exit 1")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="set_user"), "set_user"
)
@pytest.mark.skip_debug()
async def test_traces_on_pip_install(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().pip_install("nonexistent-package")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="pip_install"), "pip_install"
)
@pytest.mark.skip_debug()
async def test_traces_on_npm_install(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().npm_install("nonexistent-package")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="npm_install"), "npm_install"
)
@pytest.mark.skip_debug()
async def test_traces_on_apt_install(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().apt_install("nonexistent-package")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="apt_install"), "apt_install"
)
@pytest.mark.skip_debug()
async def test_traces_on_git_clone(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.skip_cache().git_clone("https://github.com/repo.git")
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="git_clone"), "git_clone"
)
@pytest.mark.skip_debug()
async def test_traces_on_start_cmd(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.set_start_cmd(
f"./{non_existent_path}", wait_for_timeout(10_000)
)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="set_start_cmd"), "set_start_cmd"
)
@pytest.mark.skip_debug()
async def test_traces_on_add_mcp_server():
# needs mcp-gateway as base template, without it no mcp servers can be added
await _expect_to_throw_and_check_trace(
lambda: AsyncTemplate().from_base_image().skip_cache().add_mcp_server("exa"),
"add_mcp_server",
)
@pytest.mark.skip_debug()
async def test_traces_on_dev_container_prebuild(async_build):
template = AsyncTemplate()
template = template.from_template("devcontainer")
template = template.skip_cache().beta_dev_container_prebuild(non_existent_path)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="beta_dev_container_prebuild"),
"beta_dev_container_prebuild",
)
@pytest.mark.skip_debug()
async def test_traces_on_set_dev_container_start(async_build):
template = AsyncTemplate()
template = template.from_template("devcontainer")
template = template.beta_set_dev_container_start(non_existent_path)
await _expect_to_throw_and_check_trace(
lambda: async_build(template, name="beta_set_dev_container_start"),
"beta_set_dev_container_start",
)