Repository navigation
Add Daytona Cloud VM smoke parity #10162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a875b81
526853c
96c4266
ffce19d
873e1bb
5893485
a5b996c
3d736dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { | ||
| chmodSync, | ||
| existsSync, | ||
| mkdtempSync, | ||
| readFileSync, | ||
| rmSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { | ||
| projects, | ||
| requiredRuntimeEnvKeys, | ||
| withLinkedVercelProject, | ||
| } from "./projects.mjs"; | ||
|
|
||
| const scriptDir = path.dirname(fileURLToPath(import.meta.url)); | ||
| const webDir = path.resolve(scriptDir, "../.."); | ||
| const auditScript = path.join(scriptDir, "audit-vercel-env.mjs"); | ||
| const daytonaRequiredKeys = [ | ||
| "CMUX_VM_DAYTONA_ENABLED", | ||
| "DAYTONA_API_KEY", | ||
| "DAYTONA_SANDBOX_SNAPSHOT", | ||
| ]; | ||
|
|
||
| function renderEnv(values) { | ||
| return Object.entries(values) | ||
| .map(([key, value]) => `${key}=${JSON.stringify(value)}`) | ||
| .join("\n"); | ||
| } | ||
|
|
||
| function runStrictAudit(values) { | ||
| const fixtureDir = mkdtempSync(path.join(tmpdir(), "cmux-cloud-vm-audit-test-")); | ||
| const fakeVercel = path.join(fixtureDir, "vercel"); | ||
| const capturePath = path.join(fixtureDir, "pulled-env-path.txt"); | ||
| writeFileSync( | ||
| fakeVercel, | ||
| `#!/bin/sh | ||
| set -eu | ||
| if [ "$1" != "env" ] || [ "$2" != "pull" ]; then | ||
| echo "unexpected fake Vercel command" >&2 | ||
| exit 64 | ||
| fi | ||
| printf '%s' "$3" > "$FAKE_VERCEL_CAPTURE_PATH" | ||
| printf '%s\n' "$FAKE_VERCEL_ENV_CONTENT" > "$3" | ||
| `, | ||
| ); | ||
| chmodSync(fakeVercel, 0o755); | ||
|
|
||
| const result = spawnSync(process.execPath, [auditScript, "staging", "--strict"], { | ||
| cwd: webDir, | ||
| encoding: "utf8", | ||
| env: { | ||
| ...process.env, | ||
| VERCEL_CLI: fakeVercel, | ||
| FAKE_VERCEL_CAPTURE_PATH: capturePath, | ||
| FAKE_VERCEL_ENV_CONTENT: renderEnv(values), | ||
| }, | ||
| }); | ||
| const pulledEnvPath = readFileSync(capturePath, "utf8"); | ||
| rmSync(fixtureDir, { recursive: true, force: true }); | ||
| return { ...result, pulledEnvPath }; | ||
| } | ||
|
|
||
| describe("Cloud VM environment audit", () => { | ||
| test("strict audit requires Daytona key names without printing secret values", () => { | ||
| const configured = Object.fromEntries( | ||
| requiredRuntimeEnvKeys | ||
| .filter((key) => !daytonaRequiredKeys.includes(key)) | ||
| .map((key, index) => [key, `audit-secret-${index}-${key}`]), | ||
| ); | ||
| configured.STACK_SECRET_SERVER_KEY = "strict-audit-secret-sentinel"; | ||
|
|
||
| const result = runStrictAudit(configured); | ||
|
|
||
| expect(result.status).toBe(1); | ||
| expect(result.stderr).toBe(""); | ||
| const report = JSON.parse(result.stdout); | ||
| expect(report.ok).toBe(false); | ||
| expect([...report.missingRequired].sort()).toEqual([...daytonaRequiredKeys].sort()); | ||
| for (const value of Object.values(configured)) { | ||
| expect(result.stdout).not.toContain(value); | ||
| expect(result.stderr).not.toContain(value); | ||
| } | ||
| expect(existsSync(result.pulledEnvPath)).toBe(false); | ||
| expect(existsSync(path.dirname(result.pulledEnvPath))).toBe(false); | ||
| }); | ||
|
|
||
| test("linked Vercel scratch data is removed when an operation fails", () => { | ||
| let scratch; | ||
| expect(() => | ||
| withLinkedVercelProject(projects.staging, (candidate) => { | ||
| scratch = candidate; | ||
| expect(existsSync(path.join(candidate, ".vercel", "project.json"))).toBe(true); | ||
| throw new Error("fixture failure"); | ||
| }), | ||
| ).toThrow("fixture failure"); | ||
| expect(scratch).toBeDefined(); | ||
| expect(existsSync(scratch)).toBe(false); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ const skipAttach = rest.includes("--skip-attach"); | |
| const provider = optionValue(rest, "--provider") ?? "e2b"; | ||
| const targetUrl = optionValue(rest, "--url") ?? project.url; | ||
| const REQUEST_TIMEOUT_MS = 45_000; | ||
| const CLEANUP_DELETE_ATTEMPTS = 2; | ||
|
|
||
| if (shouldCreate && provider !== "e2b" && provider !== "freestyle" && provider !== "daytona") { | ||
| console.error("--provider must be e2b, freestyle, or daytona"); | ||
|
|
@@ -35,6 +36,19 @@ const { StackServerApp } = stackModule; | |
| let user; | ||
| let vmId; | ||
| let authHeaders; | ||
| let result; | ||
| let beforeCount = 0; | ||
| let operationError; | ||
| let vmCleanupError; | ||
| let userCleanupError; | ||
|
|
||
| class SmokeCleanupError extends Error { | ||
| constructor(kind, message, options) { | ||
| super(message, options); | ||
| this.name = "SmokeCleanupError"; | ||
| this.kind = kind; | ||
| } | ||
| } | ||
|
|
||
| async function fetchWithTimeout(url, init = {}, timeoutMs = REQUEST_TIMEOUT_MS) { | ||
| if (useVercelCurl) return vercelCurlFetch(url, init, timeoutMs); | ||
|
|
@@ -101,6 +115,88 @@ function vercelCurlFetch(url, init = {}, timeoutMs = REQUEST_TIMEOUT_MS) { | |
| } | ||
| } | ||
|
|
||
| async function destroyAndVerifyVm(cleanupVmId) { | ||
| const destroyStartedAt = performance.now(); | ||
| let lastDeleteError; | ||
| let deleted = false; | ||
| for (let attempt = 1; attempt <= CLEANUP_DELETE_ATTEMPTS; attempt += 1) { | ||
| try { | ||
| const destroy = await fetchWithTimeout(`${targetUrl}/api/vm/${encodeURIComponent(cleanupVmId)}`, { | ||
| method: "DELETE", | ||
| headers: authHeaders, | ||
| }); | ||
| const destroyText = await destroy.text(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '45,80p' web/scripts/cloud-vm/smoke-vm-api.mjs
sed -n '150,220p' web/scripts/cloud-vm/smoke-vm-api.mjs
rg -n 'fetchWithTimeout|destroy\.text|verify\.text|setTimeout|AbortController' web/scripts/cloud-vm/smoke-vm-api.mjs web/scripts/cloud-vm/smoke-vm-api.test.mjsRepository: manaflow-ai/cmux Length of output: 6025 🏁 Script executed: sed -n '88,115p' web/scripts/cloud-vm/smoke-vm-api.mjs
sed -n '205,270p' web/scripts/cloud-vm/smoke-vm-api.mjs
sed -n '430,560p' web/scripts/cloud-vm/smoke-vm-api.mjs
rg -n 'destroyAndVerifyVm|process\.|finally|timeout|AbortController|fetchWithTimeout' web/scripts/cloud-vm/smoke-vm-api.mjs web/scripts/cloud-vm/smoke-vm-api.test.mjs package.json web/package.jsonRepository: manaflow-ai/cmux Length of output: 9962 🏁 Script executed: sed -n '90,110p' web/scripts/cloud-vm/smoke-vm-api.mjs; sed -n '220,270p' web/scripts/cloud-vm/smoke-vm-api.mjs; tail -n 90 web/scripts/cloud-vm/smoke-vm-api.mjsRepository: manaflow-ai/cmux Length of output: 7148 Keep the timeout active while reading the response body. In the native Use a cleanup helper that buffers the body before clearing the abort timer. Preserve the existing 🤖 Prompt for AI Agents |
||
| if (destroy.status === 200) { | ||
| deleted = true; | ||
| break; | ||
| } | ||
| lastDeleteError = new SmokeCleanupError( | ||
| "delete", | ||
| `DELETE /api/vm/${cleanupVmId} expected 200, got ${destroy.status}: ${destroyText} (attempt ${attempt}/${CLEANUP_DELETE_ATTEMPTS})`, | ||
| ); | ||
| } catch (error) { | ||
| lastDeleteError = new SmokeCleanupError( | ||
| "delete", | ||
| `DELETE /api/vm/${cleanupVmId} failed (attempt ${attempt}/${CLEANUP_DELETE_ATTEMPTS}): ${error instanceof Error ? error.message : String(error)}`, | ||
| { cause: error }, | ||
| ); | ||
| } | ||
| } | ||
| if (!deleted) throw lastDeleteError ?? new SmokeCleanupError("delete", `DELETE /api/vm/${cleanupVmId} failed`); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| const destroyDurationMs = Math.round(performance.now() - destroyStartedAt); | ||
|
|
||
| let verify; | ||
| try { | ||
| verify = await fetchWithTimeout(`${targetUrl}/api/vm`, { headers: authHeaders }); | ||
| } catch (error) { | ||
| throw new SmokeCleanupError( | ||
| "verify", | ||
| `post-delete GET /api/vm failed: ${error instanceof Error ? error.message : String(error)}`, | ||
| { cause: error }, | ||
| ); | ||
| } | ||
| const verifyText = await verify.text(); | ||
| if (verify.status !== 200) { | ||
| throw new SmokeCleanupError( | ||
| "verify", | ||
| `post-delete GET /api/vm expected 200, got ${verify.status}: ${verifyText}`, | ||
| ); | ||
| } | ||
|
|
||
| let afterJson; | ||
| try { | ||
| afterJson = JSON.parse(verifyText); | ||
| } catch (error) { | ||
| throw new SmokeCleanupError("verify", "post-delete GET /api/vm returned invalid JSON", { cause: error }); | ||
| } | ||
| if (!Array.isArray(afterJson.vms)) { | ||
| throw new SmokeCleanupError("verify", "post-delete GET /api/vm response missing vms array"); | ||
| } | ||
| if (afterJson.vms.some((vm) => vm?.id === cleanupVmId)) { | ||
| throw new SmokeCleanupError("leak", `post-delete GET /api/vm still includes ${cleanupVmId}`); | ||
| } | ||
| if (afterJson.vms.length !== beforeCount) { | ||
| throw new SmokeCleanupError( | ||
| "leak", | ||
| `post-delete GET /api/vm returned ${afterJson.vms.length} VMs, expected ${beforeCount}`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| destroyed: true, | ||
| destroyDurationMs, | ||
| leakVerified: true, | ||
| afterCount: afterJson.vms.length, | ||
| }; | ||
| } | ||
|
|
||
| function cleanupFailurePrefix(error) { | ||
| if (!(error instanceof SmokeCleanupError)) return "cleanup_verify_failed_vm"; | ||
| if (error.kind === "delete") return "cleanup_delete_failed_vm"; | ||
| if (error.kind === "leak") return "cleanup_leaked_vm"; | ||
| return "cleanup_verify_failed_vm"; | ||
| } | ||
|
|
||
| try { | ||
| const env = loadTargetEnv(project); | ||
| requireEnvKeys(env, [ | ||
|
|
@@ -137,15 +233,17 @@ try { | |
| const authedText = await authed.text(); | ||
| if (authed.status !== 200) throw new Error(`authenticated GET /api/vm expected 200, got ${authed.status}: ${authedText}`); | ||
| const authedJson = JSON.parse(authedText); | ||
| if (!Array.isArray(authedJson.vms)) throw new Error("authenticated GET /api/vm response missing vms array"); | ||
| beforeCount = authedJson.vms.length; | ||
|
|
||
| const result = { | ||
| result = { | ||
| ok: true, | ||
| target, | ||
| projectId, | ||
| url: targetUrl, | ||
| unauthStatus: unauth.status, | ||
| authedListStatus: authed.status, | ||
| beforeCount: Array.isArray(authedJson.vms) ? authedJson.vms.length : null, | ||
| beforeCount, | ||
| }; | ||
|
|
||
| if (shouldCreate) { | ||
|
|
@@ -160,10 +258,10 @@ try { | |
| if (create.status !== 200) throw new Error(`POST /api/vm expected 200, got ${create.status}: ${createText}`); | ||
| const created = JSON.parse(createText); | ||
| if (!created.id) throw new Error("create response missing id"); | ||
| vmId = created.id; | ||
| if (created.provider !== provider) { | ||
| throw new Error(`POST /api/vm returned provider ${created.provider}, expected ${provider}`); | ||
| } | ||
| vmId = created.id; | ||
|
|
||
| let attachTransport; | ||
| let attachDurationMs; | ||
|
|
@@ -182,59 +280,48 @@ try { | |
| attachTransport = attached.transport; | ||
| } | ||
|
|
||
| const destroyStartedAt = performance.now(); | ||
| const destroy = await fetchWithTimeout(`${targetUrl}/api/vm/${encodeURIComponent(vmId)}`, { | ||
| method: "DELETE", | ||
| headers: authHeaders, | ||
| }); | ||
| const destroyDurationMs = Math.round(performance.now() - destroyStartedAt); | ||
| const destroyText = await destroy.text(); | ||
| if (destroy.status !== 200) throw new Error(`DELETE /api/vm/${vmId} expected 200, got ${destroy.status}: ${destroyText}`); | ||
| vmId = undefined; | ||
|
|
||
| Object.assign(result, { | ||
| createdProvider: created.provider, | ||
| imageVersion: created.imageVersion, | ||
| createDurationMs, | ||
| ...(skipAttach | ||
| ? { attachSkipped: true } | ||
| : { attachTransport, attachDurationMs }), | ||
| destroyed: true, | ||
| destroyDurationMs, | ||
| }); | ||
| } | ||
|
|
||
| console.log(JSON.stringify(result)); | ||
| } catch (error) { | ||
| operationError = error; | ||
| } finally { | ||
| if (vmId && authHeaders) { | ||
| const cleanupVmId = vmId; | ||
| try { | ||
| const destroy = await fetchWithTimeout(`${targetUrl}/api/vm/${encodeURIComponent(vmId)}`, { | ||
| method: "DELETE", | ||
| headers: authHeaders, | ||
| }); | ||
| if (destroy.status === 200) { | ||
| console.error(`cleanup_destroyed_vm=${vmId}`); | ||
| vmId = undefined; | ||
| } else { | ||
| const text = await destroy.text().catch(() => ""); | ||
| console.error(`cleanup_delete_failed_vm=${vmId} status=${destroy.status} body=${text}`); | ||
| } | ||
| } catch (cleanupError) { | ||
| console.error(`cleanup_delete_failed_vm=${vmId} error=${cleanupError instanceof Error ? cleanupError.message : String(cleanupError)}`); | ||
| const cleanupResult = await destroyAndVerifyVm(cleanupVmId); | ||
| Object.assign(result, cleanupResult); | ||
| vmId = undefined; | ||
| if (operationError) console.error(`cleanup_destroyed_vm=${cleanupVmId}`); | ||
| } catch (error) { | ||
| vmCleanupError = error; | ||
| console.error( | ||
| `${cleanupFailurePrefix(error)}=${cleanupVmId} error=${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| } | ||
| } | ||
| if (vmId) console.error(`cleanup_needed_vm=${vmId}`); | ||
| console.error(error instanceof Error ? error.message : String(error)); | ||
| process.exitCode = 1; | ||
| } finally { | ||
| if (user) { | ||
| try { | ||
| await user.delete(); | ||
| } catch (cleanupError) { | ||
| } catch (error) { | ||
| userCleanupError = error; | ||
| console.error( | ||
| `cleanup_delete_user_failed error=${cleanupError instanceof Error ? cleanupError.message : String(cleanupError)}`, | ||
| `cleanup_delete_user_failed error=${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (vmId) console.error(`cleanup_needed_vm=${vmId}`); | ||
| if (operationError) console.error(operationError instanceof Error ? operationError.message : String(operationError)); | ||
| if (operationError || vmCleanupError || userCleanupError) { | ||
| process.exitCode = 1; | ||
| } else { | ||
| console.log(JSON.stringify(result)); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.