From 37855b97a6320d0473ef0f0396e205175a586d66 Mon Sep 17 00:00:00 2001 From: 0div Date: Thu, 16 Jan 2025 09:38:43 -0800 Subject: [PATCH 1/9] add host test for sandbox not found error --- packages/js-sdk/tests/sandbox/host.test.ts | 43 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index f499f7d3e..42d39094e 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -1,12 +1,15 @@ import { assert } from 'vitest' -import { isDebug, sandboxTest, wait } from '../setup.js' +import Sandbox from '../../src/index.js' +import { isDebug, sandboxTest, template, wait } from '../setup.js' -sandboxTest('ping server in sandbox', async ({ sandbox }) => { - const cmd = await sandbox.commands.run('python -m http.server 8000', { background: true }) +sandboxTest('ping server in running sandbox', async ({ sandbox }) => { + const cmd = await sandbox.commands.run('python -m http.server 8000', { + background: true, + }) try { - await wait(1000) + await wait(10_000) const host = sandbox.getHost(8000) @@ -21,3 +24,35 @@ sandboxTest('ping server in sandbox', async ({ sandbox }) => { } } }) + +sandboxTest('ping server in non-running sandbox', async () => { + const sbx = await Sandbox.create(template, { timeoutMs: 60_000 }) + + const cmd = await sbx.commands.run('python -m http.server 8000', { + background: true, + }) + + try { + await wait(10_000) + + const host = sbx.getHost(8000) + + const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + + assert.equal(res.status, 200) + + await sbx.kill() + + const res2 = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + assert.equal(res2.status, 502) + + const text = await res2.text() + assert.equal(text, 'Sandbox does not exist.') + } finally { + try { + await cmd.kill() + } catch (e) { + console.error(e) + } + } +}) From 91770ae3eb425cf6e196ed5ee37bca9147c8d0f1 Mon Sep 17 00:00:00 2001 From: 0div Date: Thu, 16 Jan 2025 09:40:01 -0800 Subject: [PATCH 2/9] skip to not block wokflow --- packages/js-sdk/tests/sandbox/host.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index 42d39094e..b6a72aac0 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -25,7 +25,8 @@ sandboxTest('ping server in running sandbox', async ({ sandbox }) => { } }) -sandboxTest('ping server in non-running sandbox', async () => { +// TODO: unskip when we have this new error handling deployed +sandboxTest.skip('ping server in non-running sandbox', async () => { const sbx = await Sandbox.create(template, { timeoutMs: 60_000 }) const cmd = await sbx.commands.run('python -m http.server 8000', { From 55ecbaf05bfa62082f7c84e7dfd5c8470909a44a Mon Sep 17 00:00:00 2001 From: 0div <98087403+0div@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:33:29 +0100 Subject: [PATCH 3/9] Update packages/js-sdk/tests/sandbox/host.test.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jakub Novák --- packages/js-sdk/tests/sandbox/host.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index b6a72aac0..42d39094e 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -25,8 +25,7 @@ sandboxTest('ping server in running sandbox', async ({ sandbox }) => { } }) -// TODO: unskip when we have this new error handling deployed -sandboxTest.skip('ping server in non-running sandbox', async () => { +sandboxTest('ping server in non-running sandbox', async () => { const sbx = await Sandbox.create(template, { timeoutMs: 60_000 }) const cmd = await sbx.commands.run('python -m http.server 8000', { From c00f73f063077067d3e18e82e56ee2d199d690c2 Mon Sep 17 00:00:00 2001 From: 0div Date: Thu, 16 Jan 2025 13:48:01 -0800 Subject: [PATCH 4/9] skip ifdebug --- .../tests/sandbox/commands/sendStdin.test.ts | 4 +++- packages/js-sdk/tests/sandbox/connect.test.ts | 24 ++++++++++++++++++- packages/js-sdk/tests/sandbox/host.test.ts | 9 ++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts b/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts index 550c04a15..5b447d90c 100644 --- a/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts +++ b/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts @@ -1,5 +1,5 @@ import { assert } from 'vitest' -import { sandboxTest } from '../../setup.js' +import { sandboxTest, wait } from '../../setup.js' sandboxTest('send stdin to process', async ({ sandbox }) => { const text = 'Hello, World!' @@ -29,6 +29,8 @@ sandboxTest('send special characters to stdin', async ({ sandbox }) => { await sandbox.commands.sendStdin(cmd.pid, text) + await wait(5_000) + await cmd.kill() assert.equal(cmd.stdout, text) diff --git a/packages/js-sdk/tests/sandbox/connect.test.ts b/packages/js-sdk/tests/sandbox/connect.test.ts index 7311713a3..d5eb1b213 100644 --- a/packages/js-sdk/tests/sandbox/connect.test.ts +++ b/packages/js-sdk/tests/sandbox/connect.test.ts @@ -1,4 +1,4 @@ -import { test, assert } from 'vitest' +import { assert, test } from 'vitest' import { Sandbox } from '../../src' import { isDebug, template } from '../setup.js' @@ -19,3 +19,25 @@ test('connect', async () => { } } }) + +test('connect to non-running sandbox', async () => { + const sbx = await Sandbox.create(template, { timeoutMs: 10_000 }) + let isKilled = false + + try { + const isRunning = await sbx.isRunning() + assert.isTrue(isRunning) + await sbx.kill() + isKilled = true + + const sbxConnection = await Sandbox.connect(sbx.sandboxId) + const isRunning2 = await sbxConnection.isRunning() + assert.isFalse(isRunning2) + + await sbxConnection.commands.run('echo "hello"') + } finally { + if (!isKilled) { + await sbx.kill() + } + } +}) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index b6a72aac0..b77c8dcc3 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -3,7 +3,7 @@ import { assert } from 'vitest' import Sandbox from '../../src/index.js' import { isDebug, sandboxTest, template, wait } from '../setup.js' -sandboxTest('ping server in running sandbox', async ({ sandbox }) => { +sandboxTest.skip('ping server in running sandbox', async ({ sandbox }) => { const cmd = await sandbox.commands.run('python -m http.server 8000', { background: true, }) @@ -25,16 +25,15 @@ sandboxTest('ping server in running sandbox', async ({ sandbox }) => { } }) -// TODO: unskip when we have this new error handling deployed -sandboxTest.skip('ping server in non-running sandbox', async () => { - const sbx = await Sandbox.create(template, { timeoutMs: 60_000 }) +sandboxTest.skipIf(isDebug)('ping server in non-running sandbox', async () => { + const sbx = await Sandbox.create(template, { timeoutMs: 120_000 }) const cmd = await sbx.commands.run('python -m http.server 8000', { background: true, }) try { - await wait(10_000) + await wait(20_000) const host = sbx.getHost(8000) From ad3d8fbf9f1c41d93cf6cc8279944357c553d0c5 Mon Sep 17 00:00:00 2001 From: 0div Date: Thu, 16 Jan 2025 13:49:56 -0800 Subject: [PATCH 5/9] skip ifdebug --- packages/js-sdk/tests/sandbox/host.test.ts | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index b77c8dcc3..cd8dc7b4f 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -3,27 +3,30 @@ import { assert } from 'vitest' import Sandbox from '../../src/index.js' import { isDebug, sandboxTest, template, wait } from '../setup.js' -sandboxTest.skip('ping server in running sandbox', async ({ sandbox }) => { - const cmd = await sandbox.commands.run('python -m http.server 8000', { - background: true, - }) +sandboxTest.skipIf(isDebug)( + 'ping server in running sandbox', + async ({ sandbox }) => { + const cmd = await sandbox.commands.run('python -m http.server 8000', { + background: true, + }) - try { - await wait(10_000) + try { + await wait(10_000) - const host = sandbox.getHost(8000) + const host = sandbox.getHost(8000) - const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) - assert.equal(res.status, 200) - } finally { - try { - await cmd.kill() - } catch (e) { - console.error(e) + assert.equal(res.status, 200) + } finally { + try { + await cmd.kill() + } catch (e) { + console.error(e) + } } } -}) +) sandboxTest.skipIf(isDebug)('ping server in non-running sandbox', async () => { const sbx = await Sandbox.create(template, { timeoutMs: 120_000 }) From a9c283e6d30d2000ed7c9655f01eff66ff9dfb9f Mon Sep 17 00:00:00 2001 From: 0div Date: Thu, 16 Jan 2025 19:29:13 -0800 Subject: [PATCH 6/9] edits based on feedback --- packages/js-sdk/tests/sandbox/host.test.ts | 48 +++++++++++----------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index cd8dc7b4f..6ba8788ed 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -1,7 +1,6 @@ import { assert } from 'vitest' -import Sandbox from '../../src/index.js' -import { isDebug, sandboxTest, template, wait } from '../setup.js' +import { isDebug, sandboxTest, wait } from '../setup.js' sandboxTest.skipIf(isDebug)( 'ping server in running sandbox', @@ -28,34 +27,35 @@ sandboxTest.skipIf(isDebug)( } ) -sandboxTest.skipIf(isDebug)('ping server in non-running sandbox', async () => { - const sbx = await Sandbox.create(template, { timeoutMs: 120_000 }) - - const cmd = await sbx.commands.run('python -m http.server 8000', { - background: true, - }) +sandboxTest.skipIf(isDebug)( + 'ping server in non-running sandbox', + async ({ sandbox }) => { + const cmd = await sandbox.commands.run('python -m http.server 8000', { + background: true, + }) - try { - await wait(20_000) + try { + await wait(20_000) - const host = sbx.getHost(8000) + const host = sandbox.getHost(8000) - const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) - assert.equal(res.status, 200) + assert.equal(res.status, 200) - await sbx.kill() + await sandbox.kill() - const res2 = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) - assert.equal(res2.status, 502) + const res2 = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + assert.equal(res2.status, 502) - const text = await res2.text() - assert.equal(text, 'Sandbox does not exist.') - } finally { - try { - await cmd.kill() - } catch (e) { - console.error(e) + const text = await res2.text() + assert.equal(text, 'Sandbox does not exist.') + } finally { + try { + await cmd.kill() + } catch (e) { + console.error(e) + } } } -}) +) From 06ef25766c93de70f99dbaf50b6a1135087ab1c5 Mon Sep 17 00:00:00 2001 From: 0div Date: Fri, 17 Jan 2025 18:54:13 -0800 Subject: [PATCH 7/9] address PR comments --- .../tests/sandbox/commands/sendStdin.test.ts | 6 +---- packages/js-sdk/tests/sandbox/host.test.ts | 24 ++++++++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts b/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts index 0d9cebd47..b6914fc67 100644 --- a/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts +++ b/packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts @@ -1,5 +1,5 @@ import { assert } from 'vitest' -import { sandboxTest, wait } from '../../setup.js' +import { sandboxTest } from '../../setup.js' sandboxTest('send stdin to process', async ({ sandbox }) => { const text = 'Hello, World!' @@ -7,7 +7,6 @@ sandboxTest('send stdin to process', async ({ sandbox }) => { await sandbox.commands.sendStdin(cmd.pid, text) - for (let i = 0; i < 5; i++) { if (cmd.stdout === text) { break @@ -37,8 +36,6 @@ sandboxTest('send special characters to stdin', async ({ sandbox }) => { await sandbox.commands.sendStdin(cmd.pid, text) - await wait(5_000) - for (let i = 0; i < 5; i++) { if (cmd.stdout === text) { break @@ -66,6 +63,5 @@ sandboxTest('send multiline string to stdin', async ({ sandbox }) => { await cmd.kill() - assert.equal(cmd.stdout, text) }) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index 9b68bfef4..2d3322e99 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -10,11 +10,20 @@ sandboxTest.skipIf(isDebug)( }) try { - await wait(10_000) + await wait(1000) const host = sandbox.getHost(8000) - const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + let res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + + for (let i = 0; i < 10; i++) { + if (res.status === 200) { + break + } + + res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + await wait(500) + } assert.equal(res.status, 200) } finally { @@ -35,17 +44,16 @@ sandboxTest.skipIf(isDebug)( }) try { - await wait(10_000) + const host = sandbox.getHost(49983) + const url = `${isDebug ? 'http' : 'https'}://${host}/health` - const host = sandbox.getHost(8000) - - const res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + const res = await fetch(url) - assert.equal(res.status, 200) + assert.equal(res.status, 204) await sandbox.kill() - const res2 = await fetch(`${isDebug ? 'http' : 'https'}://${host}`) + const res2 = await fetch(url) assert.equal(res2.status, 502) const text = await res2.text() From a7606f5d8024d7e20564efc28cca2e2aeda6f597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Nov=C3=A1k?= Date: Mon, 20 Jan 2025 14:58:11 -0800 Subject: [PATCH 8/9] Apply suggestions from code review --- packages/js-sdk/tests/sandbox/host.test.ts | 29 +++++++--------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index 2d3322e99..e517d6745 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -39,31 +39,20 @@ sandboxTest.skipIf(isDebug)( sandboxTest.skipIf(isDebug)( 'ping server in non-running sandbox', async ({ sandbox }) => { - const cmd = await sandbox.commands.run('python -m http.server 8000', { - background: true, - }) + const host = sandbox.getHost(49983) + const url = `${isDebug ? 'http' : 'https'}://${host}/health` - try { - const host = sandbox.getHost(49983) - const url = `${isDebug ? 'http' : 'https'}://${host}/health` - - const res = await fetch(url) + const res = await fetch(url) - assert.equal(res.status, 204) + assert.equal(res.status, 204) - await sandbox.kill() + await sandbox.kill() - const res2 = await fetch(url) - assert.equal(res2.status, 502) + const res2 = await fetch(url) + assert.equal(res2.status, 502) - const text = await res2.text() - assert.equal(text, 'Sandbox does not exist.') - } finally { - try { - await cmd.kill() - } catch (e) { - console.error(e) - } + const text = await res2.text() + assert.equal(text, 'Sandbox does not exist.') } } ) From f5cde50df429bbdafeb6cb1ac4db98d91e574a05 Mon Sep 17 00:00:00 2001 From: 0div Date: Mon, 27 Jan 2025 15:44:29 -0800 Subject: [PATCH 9/9] fix type in host test --- packages/js-sdk/tests/sandbox/host.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/js-sdk/tests/sandbox/host.test.ts b/packages/js-sdk/tests/sandbox/host.test.ts index e517d6745..e6f81ab03 100644 --- a/packages/js-sdk/tests/sandbox/host.test.ts +++ b/packages/js-sdk/tests/sandbox/host.test.ts @@ -53,6 +53,5 @@ sandboxTest.skipIf(isDebug)( const text = await res2.text() assert.equal(text, 'Sandbox does not exist.') - } } )