Skip to content

Commit 72a899c

Browse files
committed
update test setup conf
1 parent abe6a02 commit 72a899c

File tree

5 files changed

+103
-113
lines changed

5 files changed

+103
-113
lines changed

js/tests/basic.test.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
import { expect } from 'vitest'
2-
import { isDebug, sandboxTest } from './setup'
3-
import { Sandbox } from '../src'
2+
import { isDebug, sandboxTest, secureSandboxTest } from './setup'
43

54
sandboxTest('basic', async ({ sandbox }) => {
65
const result = await sandbox.runCode('x =1; x')
76

87
expect(result.text).toEqual('1')
98
})
109

11-
sandboxTest.skipIf(isDebug)('secure access', async ({ template }) => {
12-
const sandbox = await Sandbox.create(template, {
13-
network: {
14-
allowPublicTraffic: false,
15-
},
16-
})
17-
10+
secureSandboxTest.skipIf(isDebug)('secure access', async ({ sandbox }) => {
1811
const result = await sandbox.runCode('x =1; x')
1912

2013
expect(result.text).toEqual('1')
21-
22-
await sandbox.kill()
2314
})

js/tests/contexts.test.ts

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect } from 'vitest'
22

3-
import { isDebug, sandboxTest } from './setup'
4-
import Sandbox from '../src'
3+
import { isDebug, sandboxTest, secureSandboxTest } from './setup'
54

65
sandboxTest('create context with no options', async ({ sandbox }) => {
76
const context = await sandbox.createCodeContext()
@@ -63,14 +62,9 @@ sandboxTest('restart context', async ({ sandbox }) => {
6362
expect(execution.error?.value).toBe("name 'x' is not defined")
6463
})
6564

66-
sandboxTest.skipIf(isDebug)(
65+
secureSandboxTest.skipIf(isDebug)(
6766
'create context (secure traffic)',
68-
async ({ template }) => {
69-
const sandbox = await Sandbox.create(template, {
70-
network: {
71-
allowPublicTraffic: false,
72-
},
73-
})
67+
async ({ sandbox }) => {
7468
const context = await sandbox.createCodeContext()
7569

7670
const contexts = await sandbox.listCodeContexts()
@@ -79,19 +73,12 @@ sandboxTest.skipIf(isDebug)(
7973
expect(lastContext.id).toBe(context.id)
8074
expect(lastContext.language).toBe(context.language)
8175
expect(lastContext.cwd).toBe(context.cwd)
82-
83-
await sandbox.kill()
8476
}
8577
)
8678

87-
sandboxTest.skipIf(isDebug)(
79+
secureSandboxTest.skipIf(isDebug)(
8880
'remove context (secure traffic)',
89-
async ({ template }) => {
90-
const sandbox = await Sandbox.create(template, {
91-
network: {
92-
allowPublicTraffic: false,
93-
},
94-
})
81+
async ({ sandbox }) => {
9582
const context = await sandbox.createCodeContext()
9683

9784
await sandbox.removeCodeContext(context.id)
@@ -103,14 +90,9 @@ sandboxTest.skipIf(isDebug)(
10390
}
10491
)
10592

106-
sandboxTest.skipIf(isDebug)(
93+
secureSandboxTest.skipIf(isDebug)(
10794
'list contexts (secure traffic)',
108-
async ({ template }) => {
109-
const sandbox = await Sandbox.create(template, {
110-
network: {
111-
allowPublicTraffic: false,
112-
},
113-
})
95+
async ({ sandbox }) => {
11496
const contexts = await sandbox.listCodeContexts()
11597

11698
// default contexts should include python and javascript
@@ -121,14 +103,9 @@ sandboxTest.skipIf(isDebug)(
121103
}
122104
)
123105

124-
sandboxTest.skipIf(isDebug)(
106+
secureSandboxTest.skipIf(isDebug)(
125107
'restart context (secure traffic)',
126-
async ({ template }) => {
127-
const sandbox = await Sandbox.create(template, {
128-
network: {
129-
allowPublicTraffic: false,
130-
},
131-
})
108+
async ({ sandbox }) => {
132109
const context = await sandbox.createCodeContext()
133110

134111
// set a variable in the context
@@ -144,7 +121,5 @@ sandboxTest.skipIf(isDebug)(
144121
expect(execution.error).toBeDefined()
145122
expect(execution.error?.name).toBe('NameError')
146123
expect(execution.error?.value).toBe("name 'x' is not defined")
147-
148-
await sandbox.kill()
149124
}
150125
)

js/tests/setup.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
import { Sandbox } from '../src'
21
import { test as base } from 'vitest'
3-
4-
const timeoutMs = 60_000
5-
6-
const template = process.env.E2B_TESTS_TEMPLATE || 'code-interpreter-v1'
2+
import { Sandbox, SandboxOpts } from '../src'
73

84
interface SandboxFixture {
95
sandbox: Sandbox
106
template: string
7+
sandboxTestId: string
8+
sandboxOpts: Partial<SandboxOpts>
119
}
1210

11+
const template = process.env.E2B_TESTS_TEMPLATE || 'code-interpreter-v1'
12+
1313
export const sandboxTest = base.extend<SandboxFixture>({
14-
sandbox: [
14+
template,
15+
sandboxTestId: [
1516
// eslint-disable-next-line no-empty-pattern
1617
async ({}, use) => {
18+
const id = `test-${generateRandomString()}`
19+
await use(id)
20+
},
21+
{ auto: true },
22+
],
23+
sandboxOpts: {},
24+
sandbox: [
25+
async ({ sandboxTestId, sandboxOpts }, use) => {
1726
const sandbox = await Sandbox.create(template, {
18-
timeoutMs,
27+
metadata: { sandboxTestId },
28+
...sandboxOpts,
1929
})
2030
try {
2131
await use(sandbox)
@@ -31,13 +41,30 @@ export const sandboxTest = base.extend<SandboxFixture>({
3141
}
3242
}
3343
},
34-
{ auto: true },
44+
{ auto: false },
3545
],
36-
template,
3746
})
3847

3948
export const isDebug = process.env.E2B_DEBUG !== undefined
49+
export const isIntegrationTest = process.env.E2B_INTEGRATION_TEST !== undefined
50+
51+
export const secureSandboxTest = sandboxTest.extend({
52+
sandboxOpts: {
53+
secure: true,
54+
network: {
55+
allowPublicTraffic: false,
56+
},
57+
},
58+
})
59+
60+
function generateRandomString(length: number = 8): string {
61+
return Math.random()
62+
.toString(36)
63+
.substring(2, length + 2)
64+
}
4065

4166
export async function wait(ms: number) {
4267
return new Promise((resolve) => setTimeout(resolve, ms))
4368
}
69+
70+
export { template }

python/tests/conftest.py

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import pytest
2-
import pytest_asyncio
3-
import os
41
import asyncio
2+
import os
53

6-
from logging import warning
7-
8-
from e2b_code_interpreter.code_interpreter_async import AsyncSandbox
9-
from e2b_code_interpreter.code_interpreter_sync import Sandbox
4+
import pytest
105

11-
timeout = 60
6+
from e2b_code_interpreter import (
7+
AsyncSandbox,
8+
Sandbox,
9+
)
10+
import uuid
1211

1312

14-
# Override the event loop so it never closes during test execution
15-
# This helps with pytest-xdist and prevents "Event loop is closed" errors
1613
@pytest.fixture(scope="session")
17-
def event_loop():
18-
"""Create a session-scoped event loop for all async tests."""
19-
try:
20-
loop = asyncio.get_running_loop()
21-
except RuntimeError:
22-
loop = asyncio.new_event_loop()
23-
yield loop
24-
loop.close()
14+
def sandbox_test_id():
15+
return f"test_{uuid.uuid4()}"
2516

2617

2718
@pytest.fixture()
@@ -30,66 +21,72 @@ def template():
3021

3122

3223
@pytest.fixture()
33-
def sandbox(template, debug, network):
34-
sandbox = Sandbox.create(template, timeout=timeout, debug=debug, network=network)
24+
def sandbox_factory(request, template, sandbox_test_id):
25+
def factory(*, template_name: str = template, **kwargs):
26+
kwargs.setdefault("secure", False)
27+
kwargs.setdefault("timeout", 5)
28+
29+
metadata = kwargs.setdefault("metadata", dict())
30+
metadata.setdefault("sandbox_test_id", sandbox_test_id)
31+
32+
sandbox = Sandbox.create(template_name, **kwargs)
33+
34+
request.addfinalizer(lambda: sandbox.kill())
35+
36+
return sandbox
37+
38+
return factory
3539

40+
41+
@pytest.fixture()
42+
def sandbox(sandbox_factory):
43+
return sandbox_factory()
44+
45+
46+
# override the event loop so it never closes
47+
# this helps us with the global-scoped async http transport
48+
@pytest.fixture(scope="session")
49+
def event_loop():
3650
try:
37-
yield sandbox
38-
finally:
39-
try:
40-
sandbox.kill()
41-
except: # noqa: E722
42-
if not debug:
43-
warning(
44-
"Failed to kill sandbox — this is expected if the test runs with local envd."
45-
)
51+
loop = asyncio.get_running_loop()
52+
except RuntimeError:
53+
loop = asyncio.new_event_loop()
54+
yield loop
55+
loop.close()
4656

4757

4858
@pytest.fixture
49-
def async_sandbox_factory(request, template, debug, network, event_loop):
50-
"""Factory for creating async sandboxes with proper cleanup."""
51-
52-
async def factory(template_override=None, **kwargs):
53-
template_name = template_override or template
54-
kwargs.setdefault("timeout", timeout)
55-
kwargs.setdefault("debug", debug)
56-
kwargs.setdefault("network", network)
59+
def async_sandbox_factory(request, template, sandbox_test_id, event_loop):
60+
async def factory(*, template_name: str = template, **kwargs):
61+
kwargs.setdefault("timeout", 5)
62+
63+
metadata = kwargs.setdefault("metadata", dict())
64+
metadata.setdefault("sandbox_test_id", sandbox_test_id)
65+
5766
sandbox = await AsyncSandbox.create(template_name, **kwargs)
5867

5968
def kill():
6069
async def _kill():
61-
try:
62-
await sandbox.kill()
63-
except: # noqa: E722
64-
if not debug:
65-
warning(
66-
"Failed to kill sandbox — this is expected if the test runs with local envd."
67-
)
70+
await sandbox.kill()
6871

6972
event_loop.run_until_complete(_kill())
7073

7174
request.addfinalizer(kill)
75+
7276
return sandbox
7377

7478
return factory
7579

7680

7781
@pytest.fixture
7882
async def async_sandbox(async_sandbox_factory):
79-
"""Default async sandbox fixture."""
8083
return await async_sandbox_factory()
8184

8285

8386
@pytest.fixture
8487
def debug():
8588
return os.getenv("E2B_DEBUG") is not None
8689

87-
88-
@pytest.fixture
89-
def network():
90-
return None
91-
92-
9390
@pytest.fixture(autouse=True)
9491
def skip_by_debug(request, debug):
9592
if request.node.get_closest_marker("skip_debug"):

python/tests/sync/test_contexts.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def test_restart_context(sandbox: Sandbox):
6666

6767
# Secure traffic tests (public traffic disabled)
6868
@pytest.mark.skip_debug
69-
def test_create_context_secure_traffic(sandbox):
70-
sandbox = sandbox(network={"allow_public_traffic": False})
69+
def test_create_context_secure_traffic(sandbox_factory):
70+
sandbox = sandbox_factory(secure=True, network={"allow_public_traffic": False})
7171
context = sandbox.create_code_context()
7272

7373
contexts = sandbox.list_code_contexts()
@@ -79,8 +79,8 @@ def test_create_context_secure_traffic(sandbox):
7979

8080

8181
@pytest.mark.skip_debug
82-
def test_remove_context_secure_traffic(sandbox):
83-
sandbox = sandbox(network={"allow_public_traffic": False})
82+
def test_remove_context_secure_traffic(sandbox_factory):
83+
sandbox = sandbox_factory(secure=True, network={"allow_public_traffic": False})
8484
context = sandbox.create_code_context()
8585

8686
sandbox.remove_code_context(context.id)
@@ -90,8 +90,8 @@ def test_remove_context_secure_traffic(sandbox):
9090

9191

9292
@pytest.mark.skip_debug
93-
def test_list_contexts_secure_traffic(sandbox):
94-
sandbox = sandbox(network={"allow_public_traffic": False})
93+
def test_list_contexts_secure_traffic(sandbox_factory):
94+
sandbox = sandbox_factory(secure=True, network={"allow_public_traffic": False})
9595
contexts = sandbox.list_code_contexts()
9696

9797
# default contexts should include python and javascript
@@ -101,8 +101,8 @@ def test_list_contexts_secure_traffic(sandbox):
101101

102102

103103
@pytest.mark.skip_debug
104-
def test_restart_context_secure_traffic(sandbox):
105-
sandbox = sandbox(network={"allow_public_traffic": False})
104+
def test_restart_context_secure_traffic(sandbox_factory):
105+
sandbox = sandbox_factory(secure=True, network={"allow_public_traffic": False})
106106
context = sandbox.create_code_context()
107107

108108
# set a variable in the context

0 commit comments

Comments
 (0)