Skip to content

Commit

Permalink
Merge branch 'main' into return-error-indicating-that-the-sandbox-is-…
Browse files Browse the repository at this point in the history
…not-running-when-e2b-1327-test
  • Loading branch information
0div authored Jan 18, 2025
2 parents a9c283e + 7beccd7 commit 399f929
Show file tree
Hide file tree
Showing 48 changed files with 1,378 additions and 983 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,4 @@ cython_debug/
# SDK reference artifacts
sdk_ref/
sdkRefRoutes.json
spec/openapi_generated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ This page covers how to set and use environment variables in a sandbox, and defa
## Default environment variables
### Knowing if you are inside a sandbox

<Note>
Sometimes it's useful to know if you are inside a sandbox. Upon creating a sandbox, an environment variable `E2B_SANDBOX` is set to `true`.
</Note>
Sometimes it's useful to know if the code is running inside a sandbox. Upon creating a sandbox, an environment variable `E2B_SANDBOX` is automatically set to `true`.
You can try it out by running the following code in the sandbox:

<CodeGroup>
```js
Expand All @@ -15,7 +14,7 @@ const result = await sandbox.commands.run('echo $E2B_SANDBOX')
```
```python
sandbox = Sandbox()
result = sandbox.run_code("import os; os.getenv('E2B_SANDBOX')")
result = sandbox.commands.run("echo $E2B_SANDBOX")
```
</CodeGroup>

Expand Down
5 changes: 3 additions & 2 deletions packages/js-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
"dev": "tsup --watch",
"example": "tsx example.mts",
"test": "vitest run",
"generate": "openapi-typescript ../../spec/openapi.yml -x api_key --support-array-length --alphabetize --output src/api/schema.gen.ts",
"generate": "python ./../../spec/remove_extra_tags.py sandboxes templates && openapi-typescript ../../spec/openapi_generated.yml -x api_key --support-array-length --alphabetize --output src/api/schema.gen.ts",
"generate-envd-api": "openapi-typescript ../../spec/envd/envd.yaml -x api_key --support-array-length --alphabetize --output src/envd/schema.gen.ts",
"generate-ref": "./scripts/generate_sdk_ref.sh",
"check-deps": "knip",
"update-deps": "ncu -u && pnpm i",
"postPublish": "./scripts/post-publish.sh || true",
"test:bun": "bun test tests/runtimes/bun --env-file=.env",
"test:deno": "deno test tests/runtimes/deno/ --allow-net --allow-read --allow-env --unstable-sloppy-imports --trace-leaks"
"test:deno": "deno test tests/runtimes/deno/ --allow-net --allow-read --allow-env --unstable-sloppy-imports --trace-leaks",
"test:integration": "E2B_INTEGRATION_TEST=1 vitest run tests/integration/**"
},
"devDependencies": {
"@testing-library/react": "^16.0.1",
Expand Down
151 changes: 124 additions & 27 deletions packages/js-sdk/src/api/schema.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions packages/js-sdk/tests/integration/stress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { test } from 'vitest'

import Sandbox from '../../src/index.js'
import { isIntegrationTest, wait } from '../setup.js'

const heavyArray = new ArrayBuffer(256 * 1024 * 1024) // 256 MiB = 256 * 1024 * 1024 bytes
const view = new Uint8Array(heavyArray)
for (let i = 0; i < view.length; i++) {
view[i] = Math.floor(Math.random() * 256)
}

const integrationTestTemplate = 'integration-test-v1'
const sanboxCount = 10

test.skipIf(!isIntegrationTest)(
'stress test heavy file writes and reads',
async () => {
const promises: Array<Promise<string | void>> = []
for (let i = 0; i < sanboxCount; i++) {
promises.push(
Sandbox.create(integrationTestTemplate, { timeoutMs: 60 })
.then((sbx) => {
console.log(sbx.sandboxId)
return sbx.files
.write('heavy-file', heavyArray)
.then(() => sbx.files.read('heavy-file'))
})
.catch(console.error)
)
}
await wait(10_000)
await Promise.all(promises)
}
)

test.skipIf(!isIntegrationTest)('stress requests to nextjs app', async ({}) => {
const hostPromises: Array<Promise<string | void>> = []

for (let i = 0; i < sanboxCount; i++) {
hostPromises.push(
Sandbox.create(integrationTestTemplate, { timeoutMs: 60_000 }).then(
(sbx) => {
console.log('created sandbox', sbx.sandboxId)
return new Promise((resolve, reject) => {
try {
resolve(sbx.getHost(3000))
} catch (e) {
console.error('error getting sbx host', e)
reject(e)
}
})
}
)
)
}

await wait(10_000)
const hosts = await Promise.all(hostPromises)

const fetchPromises: Array<Promise<string | void>> = []

for (let i = 0; i < 100; i++) {
for (const host of hosts) {
fetchPromises.push(
new Promise((resolve) => {
fetch('https://' + host)
.then((res) => {
console.log(`response for ${host}: ${res.status}`)
})
.then(resolve)
})
)
}
}

await Promise.all(fetchPromises)
})
5 changes: 5 additions & 0 deletions packages/js-sdk/tests/integration/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Integration test template

# Build the template

`$ e2b template build -c "cd /basic-nextjs-app/ && sudo npm run dev"`
8 changes: 8 additions & 0 deletions packages/js-sdk/tests/integration/template/e2b.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM e2bdev/code-interpreter:latest

# Create a basic Next.js app
RUN npx -y create-next-app@latest test --yes --ts --use-npm

# Install dependencies
RUN cd basic-nextjs-app && npm install

18 changes: 18 additions & 0 deletions packages/js-sdk/tests/integration/template/e2b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This is a config for E2B sandbox template.
# You can use template ID (2e2z80zhv34yumbrybvn) or template name (integration-test-v1) to create a sandbox:

# Python SDK
# from e2b import Sandbox, AsyncSandbox
# sandbox = Sandbox("integration-test-v1") # Sync sandbox
# sandbox = await AsyncSandbox.create("integration-test-v1") # Async sandbox

# JS SDK
# import { Sandbox } from 'e2b'
# const sandbox = await Sandbox.create('integration-test-v1')

team_id = "b9c07023-d095-4bdc-9634-e25d5530ba47"
memory_mb = 1_024
start_cmd = "npm run dev"
dockerfile = "e2b.Dockerfile"
template_name = "integration-test-v1"
template_id = "2e2z80zhv34yumbrybvn"
Loading

0 comments on commit 399f929

Please sign in to comment.