Skip to content

Commit

Permalink
eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
0div committed Dec 16, 2024
1 parent db8d846 commit 1358b11
Showing 1 changed file with 114 additions and 95 deletions.
209 changes: 114 additions & 95 deletions packages/js-sdk/tests/sandbox/snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,138 @@

import { assert } from 'vitest'

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

sandboxTest.skipIf(isDebug)('pause and resume a sandbox', async ({ sandbox }) => {
assert.isTrue(await sandbox.isRunning())

await sandbox.pause()

assert.isFalse(await sandbox.isRunning())

await Sandbox.resume(sandbox.sandboxId)

assert.isTrue(await sandbox.isRunning())
})
sandboxTest.skipIf(isDebug)(
'pause and resume a sandbox',
async ({ sandbox }) => {
assert.isTrue(await sandbox.isRunning())

sandboxTest.skipIf(isDebug)('pause and resume a sandbox with env vars', async ({ template }) => {
// Environment variables of a process exist at runtime, and are not stored in some file or so.
// They are stored in the process's own memory
const sandbox = await Sandbox.create(template, { envs: { TEST_VAR: 'sfisback' } })
await sandbox.pause()

try {
const cmd = await sandbox.commands.run('echo "$TEST_VAR"')
assert.isFalse(await sandbox.isRunning())

assert.equal(cmd.exitCode, 0)
assert.equal(cmd.stdout.trim(), 'sfisback')
} catch {
sandbox.kill()
}
await Sandbox.resume(sandbox.sandboxId)

await sandbox.pause()
assert.isFalse(await sandbox.isRunning())

await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())

try {
const cmd = await sandbox.commands.run('echo "$TEST_VAR"')

assert.equal(cmd.exitCode, 0)
assert.equal(cmd.stdout.trim(), 'sfisback')
} finally {
await sandbox.kill()
assert.isTrue(await sandbox.isRunning())
}
})

sandboxTest.skipIf(isDebug)('pause and resume a sandbox with file', async ({ sandbox }) => {
const filename = 'test_snapshot.txt'
const content = 'This is a snapshot test file.'

const info = await sandbox.files.write(filename, content)
assert.equal(info.name, filename)
assert.equal(info.type, 'file')
assert.equal(info.path, `/home/user/${filename}`)

const exists = await sandbox.files.exists(filename)
assert.isTrue(exists)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)


await sandbox.pause()
assert.isFalse(await sandbox.isRunning())
)

sandboxTest.skipIf(isDebug)(
'pause and resume a sandbox with env vars',
async ({ template }) => {
// Environment variables of a process exist at runtime, and are not stored in some file or so.
// They are stored in the process's own memory
const sandbox = await Sandbox.create(template, {
envs: { TEST_VAR: 'sfisback' },
})

try {
const cmd = await sandbox.commands.run('echo "$TEST_VAR"')

assert.equal(cmd.exitCode, 0)
assert.equal(cmd.stdout.trim(), 'sfisback')
} catch {
sandbox.kill()
}

await sandbox.pause()
assert.isFalse(await sandbox.isRunning())

await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())

try {
const cmd = await sandbox.commands.run('echo "$TEST_VAR"')

assert.equal(cmd.exitCode, 0)
assert.equal(cmd.stdout.trim(), 'sfisback')
} finally {
await sandbox.kill()
}
}
)

sandboxTest.skipIf(isDebug)(
'pause and resume a sandbox with file',
async ({ sandbox }) => {
const filename = 'test_snapshot.txt'
const content = 'This is a snapshot test file.'

const info = await sandbox.files.write(filename, content)
assert.equal(info.name, filename)
assert.equal(info.type, 'file')
assert.equal(info.path, `/home/user/${filename}`)

const exists = await sandbox.files.exists(filename)
assert.isTrue(exists)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)

await sandbox.pause()
assert.isFalse(await sandbox.isRunning())

await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())

const exists2 = await sandbox.files.exists(filename)
assert.isTrue(exists2)
const readContent2 = await sandbox.files.read(filename)
assert.equal(readContent2, content)
}
)

await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())
sandboxTest.skipIf(isDebug)(
'pause and resume a sandbox with long running process',
async ({ sandbox }) => {
const filename = 'test_long_running.txt'

const exists2 = await sandbox.files.exists(filename)
assert.isTrue(exists2)
const readContent2 = await sandbox.files.read(filename)
assert.equal(readContent2, content)
})
sandbox.commands.run(`sleep 2 && echo "done" > /home/user/${filename}`, {
background: true,
})

sandboxTest.skipIf(isDebug)('pause and resume a sandbox with long running process', async ({ sandbox }) => {
const filename = 'test_long_running.txt'
// the file should not exist before 2 seconds have elapsed
const exists = await sandbox.files.exists(filename)
assert.isFalse(exists)

sandbox.commands.run(`sleep 2 && echo "done" > /home/user/${filename}`, { background: true })
await sandbox.pause()
assert.isFalse(await sandbox.isRunning())

// the file should not exist before 2 seconds have elapsed
const exists = await sandbox.files.exists(filename)
assert.isFalse(exists)
await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())

await sandbox.pause()
assert.isFalse(await sandbox.isRunning())
// the file should be created after more than 2 seconds have elapsed
await new Promise((resolve) => setTimeout(resolve, 2000))

await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())
const exists2 = await sandbox.files.exists(filename)
assert.isTrue(exists2)
const readContent2 = await sandbox.files.read(filename)
assert.equal(readContent2.trim(), 'done')
}
)

// the file should be created after more than 2 seconds have elapsed
await new Promise(resolve => setTimeout(resolve, 2000))
sandboxTest.skipIf(isDebug)(
'pause and resume a sandbox with http server',
async ({ sandbox }) => {
await sandbox.commands.run('python3 -m http.server 8000', {
background: true,
})

const exists2 = await sandbox.files.exists(filename)
assert.isTrue(exists2)
const readContent2 = await sandbox.files.read(filename)
assert.equal(readContent2.trim(), 'done')
})

sandboxTest.skipIf(isDebug)('pause and resume a sandbox with http server', async ({ sandbox }) => {
await sandbox.commands.run('python3 -m http.server 8000', { background: true })
let url = await sandbox.getHost(8000)

let url = await sandbox.getHost(8000)
await new Promise((resolve) => setTimeout(resolve, 3000))

await new Promise(resolve => setTimeout(resolve, 3000))

const response1 = await fetch(`https://${url}`)
assert.equal(response1.status, 200)
const response1 = await fetch(`https://${url}`)
assert.equal(response1.status, 200)

await sandbox.pause()
assert.isFalse(await sandbox.isRunning())
await sandbox.pause()
assert.isFalse(await sandbox.isRunning())

await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())
await Sandbox.resume(sandbox.sandboxId)
assert.isTrue(await sandbox.isRunning())

url = await sandbox.getHost(8000)
const response2 = await fetch(`https://${url}`)
assert.equal(response2.status, 200)
})
url = await sandbox.getHost(8000)
const response2 = await fetch(`https://${url}`)
assert.equal(response2.status, 200)
}
)

0 comments on commit 1358b11

Please sign in to comment.