|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { execFile } from "node:child_process" |
| 3 | +import { createHash } from "node:crypto" |
| 4 | +import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises" |
| 5 | +import { tmpdir } from "node:os" |
| 6 | +import { join } from "node:path" |
| 7 | +import { promisify } from "node:util" |
| 8 | + |
| 9 | +const execFileAsync = promisify(execFile) |
| 10 | +const root = await mkdtemp(join(tmpdir(), "wp-codebox-readonly-mounts-integration-")) |
| 11 | +const readonlySource = join(root, "readonly.bin") |
| 12 | +const readwriteSource = join(root, "readwrite.bin") |
| 13 | +const recipePath = join(root, "recipe.json") |
| 14 | +const artifactsPath = join(root, "artifacts") |
| 15 | +const readonlyBytes = Buffer.from([0, 255, 1, 2, 3, 127, 128]) |
| 16 | +const overwrittenBytes = Buffer.from([128, 127, 3, 2, 1, 255, 0]) |
| 17 | +const stagingDirectoriesBefore = await readonlyStagingDirectories() |
| 18 | + |
| 19 | +try { |
| 20 | + await writeFile(readonlySource, readonlyBytes) |
| 21 | + await writeFile(readwriteSource, readonlyBytes) |
| 22 | + await writeFile(recipePath, `${JSON.stringify({ |
| 23 | + schema: "wp-codebox/workspace-recipe/v1", |
| 24 | + runtime: { backend: "wordpress-playground", wp: "6.5", blueprint: { steps: [] } }, |
| 25 | + inputs: { |
| 26 | + mounts: [ |
| 27 | + { source: readonlySource, target: "/wordpress/readonly.bin", mode: "readonly" }, |
| 28 | + { source: readwriteSource, target: "/wordpress/readwrite.bin", mode: "readwrite" }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + workflow: { |
| 32 | + steps: [{ |
| 33 | + command: "wordpress.run-php", |
| 34 | + args: [`code=$contents = base64_decode('${overwrittenBytes.toString("base64")}'); file_put_contents('/wordpress/readonly.bin', $contents); file_put_contents('/wordpress/readwrite.bin', $contents);`], |
| 35 | + }], |
| 36 | + }, |
| 37 | + })}\n`) |
| 38 | + |
| 39 | + const output = await runRecipe() |
| 40 | + if (output) { |
| 41 | + assert.equal(output.success, true, JSON.stringify(output)) |
| 42 | + assert.equal(sha256(await readFile(readonlySource)), sha256(readonlyBytes), "readonly host bytes must survive an actual Playground PHP overwrite") |
| 43 | + assert.deepEqual(await readFile(readwriteSource), overwrittenBytes, "readwrite host bytes must reflect an actual Playground PHP overwrite") |
| 44 | + assert.deepEqual(await readonlyStagingDirectories(), stagingDirectoriesBefore, "recipe-run cleanup must remove readonly mount staging") |
| 45 | + } |
| 46 | +} finally { |
| 47 | + await rm(root, { recursive: true, force: true }) |
| 48 | +} |
| 49 | + |
| 50 | +assert.equal(isUnavailableWordPressRuntimeSource({ |
| 51 | + stdout: JSON.stringify({ |
| 52 | + schema: "wp-codebox/recipe-run/v1", |
| 53 | + success: false, |
| 54 | + phaseEvidence: [{ name: "runtime_startup", status: "failed", error: { message: "Unable to resolve Playground startup asset wordpress-archive-cache for WordPress 6.5: fetch failed" } }], |
| 55 | + }), |
| 56 | +}), true, "only a structured pre-runtime WordPress archive acquisition failure may skip") |
| 57 | + |
| 58 | +assert.equal(isUnavailableWordPressRuntimeSource({ |
| 59 | + stdout: JSON.stringify({ |
| 60 | + schema: "wp-codebox/recipe-run/v1", |
| 61 | + success: false, |
| 62 | + phaseEvidence: [{ name: "runtime_startup", status: "completed" }, { name: "run_workloads", status: "failed", error: { message: "fetch failed" } }], |
| 63 | + }), |
| 64 | +}), false, "a post-start failure containing fetch failed must fail the integration test") |
| 65 | + |
| 66 | +function sha256(bytes: Buffer): string { |
| 67 | + return createHash("sha256").update(bytes).digest("hex") |
| 68 | +} |
| 69 | + |
| 70 | +async function readonlyStagingDirectories(): Promise<string[]> { |
| 71 | + return (await readdir(tmpdir())).filter((entry) => entry.startsWith("wp-codebox-readonly-mounts-")).sort() |
| 72 | +} |
| 73 | + |
| 74 | +async function runRecipe(): Promise<RecipeRunOutput | undefined> { |
| 75 | + try { |
| 76 | + const result = await execFileAsync(process.execPath, ["packages/cli/dist/index.js", "recipe-run", "--recipe", recipePath, "--artifacts", artifactsPath, "--json"], { |
| 77 | + cwd: process.cwd(), |
| 78 | + timeout: 300_000, |
| 79 | + maxBuffer: 2 * 1024 * 1024, |
| 80 | + }) |
| 81 | + return recipeRunOutput(result.stdout) |
| 82 | + } catch (error) { |
| 83 | + if (isUnavailableWordPressRuntimeSource(error)) { |
| 84 | + console.log("playground readonly mount integration skipped: WordPress runtime source was unavailable before runtime startup") |
| 85 | + return undefined |
| 86 | + } |
| 87 | + throw error |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +interface RecipeRunOutput { |
| 92 | + schema?: string |
| 93 | + success?: boolean |
| 94 | + phaseEvidence?: Array<{ |
| 95 | + name?: string |
| 96 | + status?: string |
| 97 | + error?: { message?: string } |
| 98 | + }> |
| 99 | +} |
| 100 | + |
| 101 | +function isUnavailableWordPressRuntimeSource(error: unknown): boolean { |
| 102 | + const output = recipeRunOutput(error && typeof error === "object" && "stdout" in error ? error.stdout : undefined) |
| 103 | + const startup = output?.phaseEvidence?.find((phase) => phase.name === "runtime_startup") |
| 104 | + const message = startup?.error?.message ?? "" |
| 105 | + return output?.schema === "wp-codebox/recipe-run/v1" |
| 106 | + && startup?.status === "failed" |
| 107 | + && /Unable to resolve Playground startup asset (wordpress-archive-cache|wordpress-release-metadata)/.test(message) |
| 108 | + && /fetch failed|Could not resolve host|Connection timed out|network is unreachable/i.test(message) |
| 109 | +} |
| 110 | + |
| 111 | +function recipeRunOutput(value: unknown): RecipeRunOutput | undefined { |
| 112 | + if (typeof value !== "string") { |
| 113 | + return undefined |
| 114 | + } |
| 115 | + try { |
| 116 | + const output = JSON.parse(value) as RecipeRunOutput |
| 117 | + return output && typeof output === "object" ? output : undefined |
| 118 | + } catch { |
| 119 | + return undefined |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +console.log("playground readonly mount integration ok") |
0 commit comments