Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Test sandbox closed port error (#597)
0dive905d9be341f
diff --git a/packages/js-sdk/tests/sandbox/closed_port.test.ts b/packages/js-sdk/tests/sandbox/closed_port.test.tsnew file mode 100644index 000000000..21874f628--- /dev/null+++ b/packages/js-sdk/tests/sandbox/closed_port.test.ts@@ -0,0 +1,90 @@+import { assert, test } from 'vitest'++import { Sandbox } from '../../src/index.js'+import { isDebug, template, wait } from '../setup.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}`, {+ background: true,+ })++ await wait(1000)++ const goodHost = sbx.getHost(goodPort)+ // leave this here as a helper to visit host in browser+ console.log('goodHost', 'https://' + goodHost)++ let res2 = await fetch(`${isDebug ? 'http' : 'https'}://${goodHost}`)++ for (let i = 0; i < 10; i++) {+ if (res2.status === 200) {+ break+ }++ res2 = await fetch(`${isDebug ? 'http' : 'https'}://${goodHost}`)+ await wait(500)+ }+ assert.equal(res2.status, 200)++ const badPort = 3000+ const badHost = sbx.getHost(badPort)+ // leave this here as a helper to visit host in browser+ console.log('badHost', 'https://' + badHost)++ // Test as non-browser user agent+ const res = await fetch(`${isDebug ? 'http' : 'https'}://${badHost}`)+ assert.equal(res.status, 502)+ const resp_text = await res.text()+ const resp = JSON.parse(resp_text)+ const [cleanedSbxId] = sbx.sandboxId.split('-')++ assert.equal(resp.error, 'The sandbox is running but port is not open')+ assert.equal(cleanedSbxId, resp.sandboxId)+ assert.equal(resp.port, `:${badPort}`)+})++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}`, {+ background: true,+ })++ await wait(1000)++ const goodHost = sbx.getHost(goodPort)+ // leave this here as a helper to visit host in browser+ console.log('goodHost', 'https://' + goodHost)++ let res2 = await fetch(`${isDebug ? 'http' : 'https'}://${goodHost}`)++ for (let i = 0; i < 10; i++) {+ if (res2.status === 200) {+ break+ }++ res2 = await fetch(`${isDebug ? 'http' : 'https'}://${goodHost}`)+ await wait(500)+ }+ assert.equal(res2.status, 200)++ const badPort = 3000+ const badHost = sbx.getHost(badPort)+ // leave this here as a helper to visit host in browser+ console.log('badHost', 'https://' + badHost)++ // Test as browser user agent+ const res = await fetch(`${isDebug ? 'http' : 'https'}://${badHost}`, {+ headers: {+ 'User-Agent':+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',+ },+ })+ assert.equal(res.status, 502)+ const resp_text = await res.text()+ assert(resp_text.includes('<title>Closed Port Error</title>'))+})diff --git a/packages/python-sdk/tests/async/sandbox_async/test_port_closed.py b/packages/python-sdk/tests/async/sandbox_async/test_port_closed.pynew file mode 100644index 000000000..e91b70e5b--- /dev/null+++ b/packages/python-sdk/tests/async/sandbox_async/test_port_closed.py@@ -0,0 +1,54 @@+import pytest+import asyncio+import json+import httpx+from e2b import AsyncSandbox+++async def test_port_closed(template):+ sbx = await AsyncSandbox.create(template, timeout=60)+ try:+ assert await sbx.is_running()++ good_port = 8000+ # Start a Python HTTP server on port 8000+ await sbx.commands.run(+ f"python -m http.server {good_port}",+ background=True,+ )+ await asyncio.sleep(1) # Wait for server to start++ # Test good port (8000)+ good_host = sbx.get_host(good_port)+ async with httpx.AsyncClient() as client:+ for _ in range(10):+ try:+ response = await client.get(f"https://{good_host}")+ if response.status_code == 200:+ break+ except httpx.RequestError:+ pass+ await asyncio.sleep(0.5)+ assert response.status_code == 200++ # Test bad port (3000)+ bad_port = 3000+ bad_host = sbx.get_host(bad_port)+ async with httpx.AsyncClient() as client:+ for _ in range(10):+ try:+ response = await client.get(f"https://{bad_host}")+ if response.status_code == 502:+ break+ except httpx.RequestError:+ pass+ await asyncio.sleep(0.5)+ assert response.status_code == 502+ resp_text = response.text+ resp = json.loads(resp_text)+ cleaned_sbx_id = sbx.sandbox_id.split("-")[0]+ assert resp["error"] == "The sandbox is running but port is not open"+ assert cleaned_sbx_id == resp["sandboxId"]+ assert resp["port"] == f":{bad_port}"+ finally:+ await sbx.kill()diff --git a/packages/python-sdk/tests/sync/sandbox_sync/test_port_closed.py b/packages/python-sdk/tests/sync/sandbox_sync/test_port_closed.pynew file mode 100644index 000000000..3338f68f4--- /dev/null+++ b/packages/python-sdk/tests/sync/sandbox_sync/test_port_closed.py@@ -0,0 +1,54 @@+import pytest+import json+import httpx+import time+from e2b import Sandbox+++def test_port_closed(template):+ sbx = Sandbox(template, timeout=60)+ try:+ assert sbx.is_running()++ good_port = 8000+ # Start a Python HTTP server on port 8000+ sbx.commands.run(+ f"python -m http.server {good_port}",+ background=True,+ )+ time.sleep(1) # Wait for server to start++ # Test good port (8000)+ good_host = sbx.get_host(good_port)+ with httpx.Client() as client:+ for _ in range(10):+ try:+ response = client.get(f"https://{good_host}")+ if response.status_code == 200:+ break+ except httpx.RequestError:+ pass+ time.sleep(0.5)+ assert response.status_code == 200++ # Test bad port (3000)+ bad_port = 3000+ bad_host = sbx.get_host(bad_port)+ with httpx.Client() as client:+ for _ in range(10):+ try:+ response = client.get(f"https://{bad_host}")+ if response.status_code == 502:+ break+ except httpx.RequestError:+ pass+ time.sleep(0.5)+ assert response.status_code == 502+ resp_text = response.text+ resp = json.loads(resp_text)+ cleaned_sbx_id = sbx.sandbox_id.split("-")[0]+ assert resp["error"] == "The sandbox is running but port is not open"+ assert cleaned_sbx_id == resp["sandboxId"]+ assert resp["port"] == f":{bad_port}"+ finally:+ sbx.kill()