Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/js-sdk/tests/api/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from 'vitest'

import { Sandbox } from '../../src'
import { isDebug, sandboxTest } from '../setup.js'
import { isDebug, sandboxTest, template } from '../setup.js'

sandboxTest.skipIf(isDebug)('list sandboxes', async ({ sandbox }) => {
const sandboxes = await Sandbox.list()
Expand All @@ -15,11 +15,11 @@ sandboxTest.skipIf(isDebug)('list sandboxes', async ({ sandbox }) => {
sandboxTest.skipIf(isDebug)('list sandboxes with metadata filter', async () => {
const uniqueId = Date.now().toString()
// Create an extra sandbox with a uniqueId
const extraSbx = await Sandbox.create({ })
const extraSbx = await Sandbox.create(template)
try {
const sbx = await Sandbox.create({metadata: {uniqueId: uniqueId}})
const sbx = await Sandbox.create(template, { metadata: { uniqueId: uniqueId } })
try {
const sandboxes = await Sandbox.list({query:{metadata: {uniqueId}}})
const sandboxes = await Sandbox.list({ query: { metadata: { uniqueId } } })
assert.equal(sandboxes.length, 1)
assert.equal(sandboxes[0].sandboxId, sbx.sandboxId)
} finally {
Expand All @@ -34,7 +34,7 @@ sandboxTest.skipIf(isDebug)('list sandboxes empty filter', async ({ sandbox }) =
const sandboxes = await Sandbox.list()
assert.isAtLeast(sandboxes.length, 1)
assert.include(
sandboxes.map((s) => s.sandboxId),
sandbox.sandboxId
sandboxes.map((s) => s.sandboxId),
sandbox.sandboxId
)
})
16 changes: 16 additions & 0 deletions packages/js-sdk/tests/cmdHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CommandHandle, CommandExitError } from '../src/index.js'
import { assert } from 'vitest'

export function catchCmdExitErrorInBackground(cmd: CommandHandle) {
let disabled = false

cmd.wait().catch((res: CommandExitError) => {
if (!disabled) {
assert.equal(res.exitCode, 0, `command failed with exit code ${res.exitCode}: ${res.stderr}`)
}
})

return () => {
disabled = true
}
}
6 changes: 4 additions & 2 deletions packages/js-sdk/tests/runtimes/browser/run.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { expect, inject, test } from 'vitest'
import { render } from 'vitest-browser-react'
import { waitFor } from '@testing-library/react'
import React from 'react'
import { useEffect, useState } from 'react'

import { Sandbox } from '../../../src'
import { waitFor } from '@testing-library/react'
import { template } from '../../template'

function E2BTest() {
const [text, setText] = useState<string>()

useEffect(() => {
const getText = async () => {
const sandbox = await Sandbox.create({apiKey: inject('E2B_API_KEY')})
const sandbox = await Sandbox.create(template, { apiKey: inject('E2B_API_KEY') })

try {
await sandbox.commands.run('echo "Hello World" > hello.txt')
Expand Down
3 changes: 2 additions & 1 deletion packages/js-sdk/tests/runtimes/bun/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect, test } from 'bun:test'

import { Sandbox } from '../../../src'
import { template } from '../../template'

test(
'Bun test',
async () => {
const sbx = await Sandbox.create('base', { timeoutMs: 5_000 })
const sbx = await Sandbox.create(template, { timeoutMs: 5_000 })
try {
const isRunning = await sbx.isRunning()
expect(isRunning).toBeTruthy()
Expand Down
3 changes: 2 additions & 1 deletion packages/js-sdk/tests/runtimes/deno/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { load } from 'https://deno.land/std@0.224.0/dotenv/mod.ts'
await load({ envPath: '.env', export: true })

import { Sandbox } from '../../../dist/index.mjs'
import { template } from '../../template'


Deno.test('Deno test', async () => {
const sbx = await Sandbox.create('base', { timeoutMs: 5_000 })
const sbx = await Sandbox.create(template, { timeoutMs: 5_000 })
try {
const isRunning = await sbx.isRunning()
assert(isRunning)
Expand Down
11 changes: 8 additions & 3 deletions packages/js-sdk/tests/sandbox/closed_port.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { assert, test } from 'vitest'

import { Sandbox } from '../../src/index.js'
import { isDebug, template, wait } from '../setup.js'
import { catchCmdExitErrorInBackground } from '../cmdHelper.js'

test.skipIf(isDebug)('closed port in SDK', async () => {
const sbx = await Sandbox.create(template, { timeoutMs: 60_000 })
const goodPort = 8000

await sbx.commands.run(`python -m http.server ${goodPort}`, {
const cmd = await sbx.commands.run(`python -m http.server ${goodPort}`, {
background: true,
})

await wait(1000)
const disable = catchCmdExitErrorInBackground(cmd)

const goodHost = sbx.getHost(goodPort)
// leave this here as a helper to visit host in browser
Expand Down Expand Up @@ -44,16 +45,19 @@ test.skipIf(isDebug)('closed port in SDK', async () => {
assert.equal(resp.message, 'The sandbox is running but port is not open')
assert.equal(cleanedSbxId, resp.sandboxId)
assert.equal(resp.port, badPort)
disable()
})

test.skipIf(isDebug)('closed port in browser ', async () => {
const sbx = await Sandbox.create(template, { timeoutMs: 60_000 })
const goodPort = 8000

await sbx.commands.run(`python -m http.server ${goodPort}`, {
const cmd = await sbx.commands.run(`python -m http.server ${goodPort}`, {
background: true,
})

const disable = catchCmdExitErrorInBackground(cmd)

await wait(1000)

const goodHost = sbx.getHost(goodPort)
Expand Down Expand Up @@ -87,4 +91,5 @@ test.skipIf(isDebug)('closed port in browser ', async () => {
assert.equal(res.status, 502)
const resp_text = await res.text()
assert(resp_text.includes('<title>Closed Port Error</title>'))
disable()
})
4 changes: 2 additions & 2 deletions packages/js-sdk/tests/sandbox/commands/envVars.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from 'vitest'

import { sandboxTest, isDebug } from '../../setup.js'
import { sandboxTest, isDebug, template } from '../../setup.js'
import { Sandbox } from '../../../src'

sandboxTest.skipIf(isDebug)('env vars', async ({ sandbox }) => {
Expand All @@ -11,7 +11,7 @@ sandboxTest.skipIf(isDebug)('env vars', async ({ sandbox }) => {
})

sandboxTest.skipIf(isDebug)('env vars on sandbox', async () => {
const sandbox = await Sandbox.create({ envs: { FOO: 'bar' } })
const sandbox = await Sandbox.create(template, { envs: { FOO: 'bar' } })

try {
const cmd = await sandbox.commands.run('echo "$FOO"')
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/tests/sandbox/commands/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sandboxTest('run with multiline string', async ({ sandbox }) => {
})

sandboxTest('run with timeout', async ({ sandbox }) => {
const cmd = await sandbox.commands.run('echo "Hello, World!"', { timeoutMs: 1000 })
const cmd = await sandbox.commands.run('echo "Hello, World!"', { timeoutMs: 4000 })
Comment thread
ValentaTomas marked this conversation as resolved.

assert.equal(cmd.exitCode, 0)
})
Expand Down
5 changes: 4 additions & 1 deletion packages/js-sdk/tests/sandbox/host.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { assert } from 'vitest'

import { isDebug, sandboxTest, wait } from '../setup.js'

import { catchCmdExitErrorInBackground } from '../cmdHelper.js'
sandboxTest(
'ping server in running sandbox',
async ({ sandbox }) => {
const cmd = await sandbox.commands.run('python -m http.server 8000', {
background: true,
})

const disable = catchCmdExitErrorInBackground(cmd)

try {
await wait(1000)

Expand All @@ -25,6 +27,7 @@ sandboxTest(
await wait(500)
}
assert.equal(res.status, 200)
disable()
} finally {
try {
await cmd.kill()
Expand Down
9 changes: 5 additions & 4 deletions packages/js-sdk/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Sandbox } from '../src'
import { test as base } from 'vitest'

export const template = 'base'
import { template } from './template'

interface SandboxFixture {
sandbox: Sandbox
}

export const sandboxTest = base.extend<SandboxFixture>({
sandbox: [
async ({}, use) => {
async ({ }, use) => {
const sandbox = await Sandbox.create(template)
try {
await use(sandbox)
Expand All @@ -25,7 +24,7 @@ export const sandboxTest = base.extend<SandboxFixture>({
}
}
},
{ auto: true },
{ auto: false },
Comment thread
ValentaTomas marked this conversation as resolved.
],
})

Expand All @@ -35,3 +34,5 @@ export const isIntegrationTest = process.env.E2B_INTEGRATION_TEST !== undefined
export async function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

export { template }
1 change: 1 addition & 0 deletions packages/js-sdk/tests/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const template = 'base'
4 changes: 2 additions & 2 deletions packages/python-sdk/tests/async/api_async/test_sbx_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ async def test_list_sandboxes(async_sandbox: AsyncSandbox):


@pytest.mark.skip_debug()
async def test_list_sandboxes_with_filter(async_sandbox: AsyncSandbox):
async def test_list_sandboxes_with_filter(template):
unique_id = "".join(random.choices(string.ascii_letters, k=5))
sbx = await AsyncSandbox.create(metadata={"unique_id": unique_id})
sbx = await AsyncSandbox.create(template=template, metadata={"unique_id": unique_id})
try:
# There's an extra sandbox created by the test runner
sandboxes = await AsyncSandbox.list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async def test_command_envs(async_sandbox: AsyncSandbox):


@pytest.mark.skip_debug()
async def test_sandbox_envs(template: str):
async def test_sandbox_envs(template):
Comment thread
ValentaTomas marked this conversation as resolved.
try:
sbx = await AsyncSandbox.create(template, envs={"FOO": "bar"})
cmd = await sbx.commands.run("echo $FOO")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from e2b import AsyncSandbox


async def test_ping_server(async_sandbox: AsyncSandbox, debug):
async def test_ping_server(async_sandbox: AsyncSandbox, debug, helpers):
cmd = await async_sandbox.commands.run(
"python -m http.server 8000",
background=True,
)

disable = helpers.catch_cmd_exit_error_in_background(cmd)

try:
host = async_sandbox.get_host(8000)

Expand All @@ -23,5 +25,6 @@ async def test_ping_server(async_sandbox: AsyncSandbox, debug):
break
await asyncio.sleep(0.5)
assert status_code == 200
disable()
finally:
await cmd.kill()
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
from e2b import AsyncSandbox


async def test_port_closed(template):
async def test_port_closed(template, helpers):
sbx = await AsyncSandbox.create(template, timeout=60)
try:
assert await sbx.is_running()

good_port = 8002
# Start a Python HTTP server on port 8002
await sbx.commands.run(
cmd = await sbx.commands.run(
f"python -m http.server {good_port}",
background=True,
)

disable = helpers.catch_cmd_exit_error_in_background(cmd)

await asyncio.sleep(1) # Wait for server to start

# Test good port (8002)
Expand Down Expand Up @@ -50,5 +53,6 @@ async def test_port_closed(template):
assert resp["message"] == "The sandbox is running but port is not open"
assert cleaned_sbx_id == resp["sandboxId"]
assert resp["port"] == bad_port
disable()
finally:
await sbx.kill()
47 changes: 46 additions & 1 deletion packages/python-sdk/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import asyncio

import pytest
import pytest_asyncio
import os

from logging import warning

from e2b import Sandbox, AsyncSandbox
from e2b import (
Sandbox,
AsyncSandbox,
AsyncCommandHandle,
CommandExitException,
CommandHandle,
)


@pytest.fixture()
Expand Down Expand Up @@ -54,3 +62,40 @@ def skip_by_debug(request, debug):
if request.node.get_closest_marker("skip_debug"):
if debug:
pytest.skip("skipped because E2B_DEBUG is set")


class Helpers:
@staticmethod
def catch_cmd_exit_error_in_background(cmd: AsyncCommandHandle):
disabled = False

async def wait_for_exit():
try:
await cmd.wait()
except CommandExitException as e:
if not disabled:
assert (
False
), f"command failed with exit code {e.exit_code}: {e.stderr}"

asyncio.create_task(wait_for_exit())

def disable():
nonlocal disabled
disabled = True

return disable

@staticmethod
def check_cmd_exit_error(cmd: CommandHandle):
try:
cmd.wait()
except CommandExitException as e:
assert False, f"command failed with exit code {e.exit_code}: {e.stderr}"
except Exception as e:
raise e


@pytest.fixture
def helpers():
return Helpers
4 changes: 2 additions & 2 deletions packages/python-sdk/tests/sync/api_sync/test_sbx_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def test_list_sandboxes(sandbox: Sandbox):


@pytest.mark.skip_debug()
def test_list_sandboxes_with_filter(sandbox: Sandbox):
def test_list_sandboxes_with_filter(template):
unique_id = "".join(random.choices(string.ascii_letters, k=5))
Sandbox(metadata={"unique_id": unique_id})
Sandbox(template=template, metadata={"unique_id": unique_id})
sandboxes = Sandbox.list(query=SandboxQuery(metadata={"unique_id": unique_id}))
assert len(sandboxes) == 1
assert sandboxes[0].metadata["unique_id"] == unique_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_command_envs(sandbox: Sandbox):


@pytest.mark.skip_debug()
def test_sandbox_envs(template: str):
def test_sandbox_envs(template):
Comment thread
ValentaTomas marked this conversation as resolved.
sandbox = Sandbox(template, envs={"FOO": "bar"})
try:
cmd = sandbox.commands.run("echo $FOO")
Expand Down
Loading