Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

3.8 KB
Raw Blame
import { assert, test, vi } from 'vitest'

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

async function waitForState(
  sandbox: Sandbox,
  state: 'paused' | 'running'
): Promise<void> {
  await vi.waitFor(
    async () => {
      assert.equal(
        (await sandbox.getInfo({ requestTimeoutMs: 5_000 })).state,
        state
      )
    },
    { timeout: 30_000, interval: 500 }
  )
}

test.skipIf(isDebug)(
  'auto-pause without auto-resume requires connect to wake',
  async () => {
    const sandbox = await Sandbox.create(template, {
      timeoutMs: 3_000,
      lifecycle: {
        onTimeout: 'pause',
        autoResume: false,
      },
    })

    try {
      await waitForState(sandbox, 'paused')
      assert.isFalse(await sandbox.isRunning())

      await sandbox.connect({ requestTimeoutMs: 120_000 })

      await waitForState(sandbox, 'running')
      assert.isTrue(await sandbox.isRunning())
    } finally {
      await sandbox.kill().catch(() => {})
    }
  },
  210_000
)

test.skipIf(isDebug)(
  'filesystem-only auto-pause reboots on connect',
  async () => {
    // mode: 'filesystem' makes the timeout auto-pause filesystem-only, so resuming
    // cold-boots the sandbox from disk.
    const sandbox = await Sandbox.create(template, {
      timeoutMs: 3_000,
      lifecycle: { onTimeout: { action: 'pause', mode: 'filesystem' } },
    })

    try {
      const marker = 'auto-pause-fs-only'
      await sandbox.files.write('/home/user/auto-pause-marker.txt', marker)
      // Read via a command, not files.read: envd's non-gzip download path
      // serves procfs files as an empty 200 (it sizes them by stat, which is
      // 0), so clients that don't negotiate gzip — like workerd's fetch —
      // silently get '' (infra#3363).
      const bootBefore = (
        await sandbox.commands.run('cat /proc/sys/kernel/random/boot_id')
      ).stdout.trim()

      await waitForState(sandbox, 'paused')

      // A filesystem-only snapshot cannot auto-resume on traffic; connect
      // resumes it by cold-booting.
      await sandbox.connect({ requestTimeoutMs: 120_000 })

      const persisted = (
        await sandbox.files.read('/home/user/auto-pause-marker.txt')
      ).trim()
      assert.equal(persisted, marker)

      const bootAfter = (
        await sandbox.commands.run('cat /proc/sys/kernel/random/boot_id')
      ).stdout.trim()
      assert.notEqual(bootAfter, bootBefore)
    } finally {
      await sandbox.kill().catch(() => {})
    }
  },
  210_000
)

sandboxTest.skipIf(isDebug)(
  'auto-resume wakes paused sandbox on http request',
  async ({ sandboxTestId }) => {
    const sandbox = await Sandbox.create(template, {
      metadata: { sandboxTestId },
      timeoutMs: 3_000,
      lifecycle: {
        onTimeout: 'pause',
        autoResume: true,
      },
    })

    try {
      await sandbox.commands.run(corsHttpServerCmd(8000), {
        background: true,
      })

      await waitForState(sandbox, 'paused')

      // Each request is a wake signal; keep sending them until the guest
      // server answers, since a request that races the pause finalizing or
      // times out at the gateway while the sandbox resumes may not wake it.
      const url = `https://${sandbox.getHost(8000)}`
      await waitForHttpStatus(url, 200, { timeoutMs: 60_000 })
      await waitForState(sandbox, 'running')
      assert.isTrue(await sandbox.isRunning())
    } catch (error) {
      let state = 'unknown'
      try {
        state = (await sandbox.getInfo({ requestTimeoutMs: 5_000 })).state
      } catch {}
      console.error(
        `\n[AUTO-RESUME FAILED] Sandbox ID: ${sandbox.sandboxId}, state=${state}`
      )
      throw error
    } finally {
      await sandbox.kill().catch(() => {})
    }
  },
  150_000
)