| 1 | // Exercise the real Docker authentication flow against a disposable local |
| 2 | // Worker/R2 instance. No Cloudflare account or production password is used. |
| 3 | import { spawn } from "node:child_process"; |
| 4 | import { randomBytes } from "node:crypto"; |
| 5 | import { mkdtemp, rm } from "node:fs/promises"; |
| 6 | import { tmpdir } from "node:os"; |
| 7 | import { join } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | import { Miniflare, Log, LogLevel } from "miniflare"; |
| 10 | |
| 11 | const image = process.argv[2]; |
| 12 | if (!image) throw new Error("Usage: node registry/docker-smoke.mjs SOURCE_IMAGE"); |
| 13 | const work = await mkdtemp(join(tmpdir(), "raz-docker-registry-")); |
| 14 | const password = randomBytes(32).toString("hex"); |
| 15 | const mf = new Miniflare({ |
| 16 | modules: true, |
| 17 | scriptPath: fileURLToPath(new URL("dist/worker.mjs", import.meta.url)), |
| 18 | compatibilityDate: "2026-07-30", |
| 19 | host: "127.0.0.1", |
| 20 | port: 0, |
| 21 | r2Buckets: ["REGISTRY"], |
| 22 | bindings: { REGISTRY_USERNAME: "publisher", REGISTRY_PASSWORD: password }, |
| 23 | log: new Log(LogLevel.ERROR), |
| 24 | }); |
| 25 | |
| 26 | function docker(args, input) { |
| 27 | return new Promise((resolve, reject) => { |
| 28 | const child = spawn("docker", args, { |
| 29 | stdio: [input ? "pipe" : "ignore", "inherit", "inherit"], |
| 30 | timeout: 300_000, |
| 31 | }); |
| 32 | if (input) child.stdin.end(input); |
| 33 | child.on("error", reject); |
| 34 | child.on("exit", (code) => code === 0 ? resolve() : reject(new Error(`docker ${args[0]} exited ${code}`))); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | let target; |
| 39 | try { |
| 40 | const address = await mf.ready; |
| 41 | const registry = `localhost:${address.port}`; |
| 42 | target = `${registry}/raz:local-smoke-${Date.now()}`; |
| 43 | await docker(["--config", work, "login", registry, "--username", "publisher", "--password-stdin"], password); |
| 44 | await docker(["tag", image, target]); |
| 45 | await docker(["--config", work, "push", target]); |
| 46 | // Remove just this test's alias. The source image and its tags stay intact. |
| 47 | await docker(["image", "rm", target]); |
| 48 | const anonymous = await mkdtemp(join(work, "anonymous-")); |
| 49 | await docker(["--config", anonymous, "pull", target]); |
| 50 | await docker(["--config", anonymous, "run", "--rm", target, "sh", "/usr/local/share/raz-ci/smoke.sh"]); |
| 51 | console.log("Authenticated Docker push and anonymous pull/run passed."); |
| 52 | } finally { |
| 53 | if (target) await docker(["image", "rm", target]).catch(() => {}); |
| 54 | await mf.dispose(); |
| 55 | await rm(work, { recursive: true, force: true }); |
| 56 | } |