The best way to control Claude Code, Codex and more from any computer or phone

main
ts 483 lines 18.4 KB
Raw
1 import { Docker, DockerLive, dockerLive } from "@alchemy.run/docker";
2 import type { RegistryCredentials } from "@alchemy.run/docker/Docker";
3 import { classifyDockerRegistryError } from "@alchemy.run/docker/RegistryError";
4 import { assert, expect, layer } from "@alchemy.run/test";
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import * as ConfigProvider from "effect/ConfigProvider";
7 import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as FetchHttpClient from "effect/http/FetchHttpClient";
10 import * as Layer from "effect/Layer";
11 import * as Path from "effect/Path";
12 import { PlatformError, SystemError } from "effect/PlatformError";
13 import * as Redacted from "effect/Redacted";
14 import * as TestClock from "effect/testing/TestClock";
15 import { authenticatedRegistry, scopedBuildx } from "./Runtime.ts";
16
17 const describe = layer(
18 Layer.provideMerge(DockerLive, Layer.merge(NodeServices.layer, FetchHttpClient.layer)),
19 );
20
21 // Which CLI the client runs: declared in code, overridable by DOCKER_BIN.
22 describe("Docker CLI binary", (it) => {
23 const version = (layer: Layer.Layer<Docker, never, any>) =>
24 Effect.gen(function* () {
25 const docker = yield* Docker;
26 return (yield* docker.run(["--version"])).stdout;
27 }).pipe(Effect.provide(layer));
28
29 it.effect(
30 "runs the bin declared in code",
31 () =>
32 Effect.gen(function* () {
33 const error = yield* version(dockerLive({ bin: "alchemy-test-no-such-cli" })).pipe(
34 Effect.flip,
35 );
36 expect(String(error)).toContain("alchemy-test-no-such-cli");
37 expect(yield* version(dockerLive({ bin: "docker" }))).toMatch(/^Docker version/);
38 }).pipe(
39 // Ignore any DOCKER_BIN in the host environment for this case.
40 Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({}))),
41 ),
42 { tags: ["provider:docker", "local"] },
43 );
44
45 it.effect(
46 "lets DOCKER_BIN override the bin declared in code",
47 () =>
48 Effect.gen(function* () {
49 const output = yield* version(dockerLive({ bin: "alchemy-test-no-such-cli" }));
50 expect(output).toMatch(/^Docker version/);
51 }).pipe(
52 Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ DOCKER_BIN: "docker" }))),
53 ),
54 { tags: ["provider:docker", "local"] },
55 );
56 });
57
58 describe("Docker.materialize", (it) => {
59 it.effect(
60 "materializes a Dockerfile in the target directory",
61 () =>
62 Effect.gen(function* () {
63 const fs = yield* FileSystem.FileSystem;
64 const path = yield* Path.Path;
65 const docker = yield* Docker;
66 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-ctx-" });
67 const ctx = path.join(root, "ctx");
68 yield* docker.materialize({ context: ctx, dockerfile: "FROM scratch\n", files: [] });
69 const dockerfile = path.join(ctx, "Dockerfile");
70 expect(yield* fs.exists(dockerfile)).toBe(true);
71 expect(yield* fs.readFileString(dockerfile)).toBe("FROM scratch\n");
72 }),
73 { tags: ["unit", "provider:docker", "local"] },
74 );
75
76 it.effect(
77 "writes nested context files",
78 () =>
79 Effect.gen(function* () {
80 const fs = yield* FileSystem.FileSystem;
81 const path = yield* Path.Path;
82 const docker = yield* Docker;
83 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-path-" });
84 const ctx = path.join(root, "ctx");
85 yield* docker.materialize({
86 context: ctx,
87 dockerfile: "FROM scratch\n",
88 files: [{ path: "nested/hello.txt", content: "hi" }],
89 });
90 expect(yield* fs.readFileString(path.join(ctx, "nested", "hello.txt"))).toBe("hi");
91 }),
92 { tags: ["unit", "provider:docker", "local"] },
93 );
94 });
95
96 describe("Docker registry errors", (it) => {
97 for (const [description, tag] of [
98 [
99 "ERROR: failed to build: failed to solve: failed to push registry.example/image:latest: unknown: blob unknown to registry",
100 "DockerRegistryBlobUnknown",
101 ],
102 [
103 "#7 ERROR: failed to push: unknown: blob unknown to registry\n------\nERROR: failed to build: failed to solve: failed to push registry.example/image:latest: unknown: blob unknown to registry\n\nView build details: docker-desktop://dashboard/build/builder/node/build-id\n",
104 "DockerRegistryBlobUnknown",
105 ],
106 ["Command exited with code 1: blob unknown to registry", "DockerRegistryBlobUnknown"],
107 [
108 "ERROR: unexpected status from HEAD request to https://registry.example/v2/image/blobs/sha256:abc: 503 Service Unavailable",
109 "DockerRegistryUnavailable",
110 ],
111 ["ERROR: failed to push: 502 Bad Gateway", "DockerRegistryUnavailable"],
112 ["ERROR: unexpected status: 401 Unauthorized", "PlatformError"],
113 ["ERROR: unexpected status: 403 Forbidden", "PlatformError"],
114 ["ERROR: unexpected status: 400 Bad Request", "PlatformError"],
115 ["ERROR: failed to solve: process exited with code 1", "PlatformError"],
116 [
117 "ERROR: unexpected status from HEAD request to https://registry.example/v2/500/blobs/sha256:abc: 403 Forbidden",
118 "PlatformError",
119 ],
120 [
121 'ERROR: failed to solve: process "/bin/sh -c echo 503 Service Unavailable && exit 1" did not complete successfully: exit code: 1',
122 "PlatformError",
123 ],
124 ["#7 RUN echo 'blob unknown to registry'\nERROR: process exited with code 1", "PlatformError"],
125 ["#7 RUN echo '503 Service Unavailable'\nERROR: process exited with code 1", "PlatformError"],
126 ] as const) {
127 it.effect(
128 `classifies ${description}`,
129 () =>
130 Effect.sync(() => {
131 const error = new PlatformError(
132 new SystemError({
133 _tag: "Unknown",
134 module: "Docker",
135 method: "buildx.build",
136 description,
137 }),
138 );
139 const classified = classifyDockerRegistryError(error);
140 expect(classified._tag).toBe(tag);
141 if (classified._tag === "PlatformError") {
142 expect(classified).toBe(error);
143 } else {
144 expect(classified.cause).toBe(error);
145 expect(classified.message).toBe(error.message);
146 }
147 }),
148 { tags: ["unit", "provider:docker", "provider:docker:registry", "local"] },
149 );
150 }
151 });
152
153 const buildScratchImage = Effect.fn(function* (
154 label: string,
155 tag: string | [string, ...Array<string>],
156 credentials: RegistryCredentials,
157 platform?: string,
158 ) {
159 const fs = yield* FileSystem.FileSystem;
160 const path = yield* Path.Path;
161 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-publish-" });
162 yield* fs.writeFileString(
163 path.join(root, "Dockerfile"),
164 `FROM scratch\nLABEL alchemy.test=${label}\n`,
165 );
166 // A fresh service re-probes the Buildx plugin pinned by `scopedBuildx`.
167 return yield* Effect.gen(function* () {
168 const docker = yield* Docker;
169 return yield* docker.image.build({ context: root, tag, platform }, undefined, credentials);
170 }).pipe(Effect.provide(Layer.fresh(DockerLive)));
171 });
172
173 describe("Docker.image", (it) => {
174 it.effect(
175 "streams real pull output through deployment notes",
176 () =>
177 Effect.gen(function* () {
178 const docker = yield* Docker;
179 const notes: Array<{ message: string; kind?: string }> = [];
180 const session = {
181 note: (message: string, options?: { kind?: "status" | "output" }) =>
182 Effect.sync(() => {
183 notes.push({ message, kind: options?.kind });
184 }),
185 };
186 yield* docker.image.pull("alpine:3.21", "linux/amd64", undefined, session);
187 expect(notes.length).toBeGreaterThan(0);
188 expect(notes.every((note) => note.kind === "output")).toBe(true);
189 expect(notes.some((note) => note.message.includes("alpine:3.21"))).toBe(true);
190 }),
191 { tags: ["provider:docker", "local"], timeout: 120_000 },
192 );
193 });
194
195 describe("Docker.image publication", (it) => {
196 for (const [name, version, platform] of [
197 ["exports straight to the registry on Buildx >= 0.26", "v0.26.1", "linux/amd64"],
198 ["builds with --load then pushes on Buildx < 0.26", "v0.25.0", "linux/amd64"],
199 ["builds then pushes without a Buildx plugin", undefined, undefined],
200 ] as const) {
201 it.effect(
202 name,
203 () =>
204 Effect.gen(function* () {
205 const fs = yield* FileSystem.FileSystem;
206 const path = yield* Path.Path;
207 const docker = yield* Docker;
208 const registry = yield* authenticatedRegistry();
209 const tags: [string, string] = [
210 `${registry.host}/app:1`,
211 `${registry.host}/app:buildcache`,
212 ];
213 yield* Effect.addFinalizer(() => docker.image.remove(tags, true).pipe(Effect.ignore));
214 const { config } = yield* scopedBuildx(version);
215
216 yield* buildScratchImage(version ?? "legacy", tags, registry.credentials, platform);
217
218 expect(yield* registry.hasManifest("app", "1")).toBe(true);
219 expect(yield* registry.hasManifest("app", "buildcache")).toBe(true);
220 // Credentials ride per-invocation env/temp config, never the CLI's own.
221 const cliConfig = path.join(config, "config.json");
222 if (yield* fs.exists(cliConfig)) {
223 expect(yield* fs.readFileString(cliConfig)).not.toContain(registry.host);
224 }
225 }).pipe(
226 // Registry readiness and the Buildx download retry on real time.
227 TestClock.withLive,
228 ),
229 { tags: ["provider:docker", "local"], exclusive: true, timeout: 180_000 },
230 );
231 }
232
233 for (const [name, auth] of [
234 ["invalid JSON", '{"auths":{"source.invalid":{"auth":"AUTH_SECRET_SENTINEL"}},'],
235 ["invalid base64", '{"auths":{"source.invalid":{"auth":"AUTH_SECRET_SENTINEL!"}}}'],
236 [
237 "missing credential separator",
238 '{"auths":{"source.invalid":{"auth":"QVVUSF9TRUNSRVRfU0VOVElORUw="}}}',
239 ],
240 ]) {
241 it.effect(
242 `rejects ${name} DOCKER_AUTH_CONFIG without exposing registry credentials`,
243 () =>
244 Effect.gen(function* () {
245 const registry = yield* authenticatedRegistry();
246 yield* scopedBuildx("v0.26.1");
247
248 const result = yield* buildScratchImage(
249 "invalid-auth",
250 `${registry.host}/invalid-auth:1`,
251 {
252 ...registry.credentials,
253 password: Redacted.make("DESTINATION_SECRET_SENTINEL"),
254 },
255 ).pipe(
256 Effect.flip,
257 Effect.provide(
258 ConfigProvider.layer(ConfigProvider.fromUnknown({ DOCKER_AUTH_CONFIG: auth })),
259 ),
260 );
261 assert(result._tag === "PlatformError");
262 expect(result.reason._tag).toBe("InvalidData");
263 expect(result.reason.description).toContain("DOCKER_AUTH_CONFIG");
264 const serialized = yield* Effect.sync(() => JSON.stringify(result));
265 expect(serialized).not.toContain("AUTH_SECRET_SENTINEL");
266 expect(serialized).not.toContain("QVVUSF9TRUNSRVRfU0VOVElORUw=");
267 expect(serialized).not.toContain("DESTINATION_SECRET_SENTINEL");
268 // Rejected before anything was built or published.
269 expect(yield* registry.hasManifest("invalid-auth", "1")).toBe(false);
270 }).pipe(
271 // Registry readiness and the Buildx download retry on real time.
272 TestClock.withLive,
273 ),
274 { tags: ["provider:docker", "local"], exclusive: true, timeout: 180_000 },
275 );
276 }
277 });
278
279 // How a failing `docker` command is reported, against the real CLI.
280 describe("Docker.run failure output", (it) => {
281 it.effect(
282 "says so when a failing command wrote nothing",
283 () =>
284 Effect.gen(function* () {
285 const docker = yield* Docker;
286 const error = yield* docker
287 .run(["run", "--rm", "alpine:3.19", "sh", "-c", "exit 3"])
288 .pipe(Effect.flip);
289 expect(error.reason._tag).toBe("Unknown");
290 expect(error.reason.description).toContain("exited with code 3");
291 expect(error.reason.description).toContain("wrote no output");
292 }),
293 { tags: ["provider:docker", "local"], timeout: 60_000 },
294 );
295
296 it.effect(
297 "keeps stdout and stderr from a failing command",
298 () =>
299 Effect.gen(function* () {
300 const docker = yield* Docker;
301 const error = yield* docker
302 .run([
303 "run",
304 "--rm",
305 "alpine:3.19",
306 "sh",
307 "-c",
308 "echo step-log-on-stdout; echo reason-on-stderr >&2; exit 1",
309 ])
310 .pipe(Effect.flip);
311 expect(error.reason.description).toContain("step-log-on-stdout");
312 expect(error.reason.description).toContain("reason-on-stderr");
313 // The reason (stderr) comes first.
314 expect(error.reason.description!.indexOf("reason-on-stderr")).toBeLessThan(
315 error.reason.description!.indexOf("step-log-on-stdout"),
316 );
317 }),
318 { tags: ["provider:docker", "local"], timeout: 60_000 },
319 );
320
321 it.effect(
322 "still classifies a daemon NotFound from stderr",
323 () =>
324 Effect.gen(function* () {
325 const docker = yield* Docker;
326 const error = yield* docker
327 .run(["image", "inspect", "alchemy-test-no-such-image:missing"])
328 .pipe(Effect.flip);
329 expect(error.reason._tag).toBe("NotFound");
330 }),
331 { tags: ["provider:docker", "local"] },
332 );
333 });
334
335 describe("Docker.image", (it) => {
336 it.effect(
337 "builds a minimal image with content Dockerfile",
338 () =>
339 Effect.gen(function* () {
340 const fs = yield* FileSystem.FileSystem;
341 const path = yield* Path.Path;
342 const docker = yield* Docker;
343 const tag = "alchemy-docker-test:minimal";
344 yield* Effect.addFinalizer(() =>
345 docker.image.remove(tag, true).pipe(
346 Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
347 Effect.ignore,
348 ),
349 );
350 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-build-" });
351 const ctx = path.join(root, "ctx");
352 yield* docker.materialize({
353 context: ctx,
354 dockerfile: [
355 "FROM alpine:3.19",
356 "RUN echo ok > /tmp/ok.txt",
357 'CMD ["cat", "/tmp/ok.txt"]',
358 "",
359 ].join("\n"),
360 files: [],
361 });
362 yield* docker.image.build({ tag, context: ctx });
363 const inspect = yield* docker.image.inspect(tag);
364 expect(inspect.Id.length).toBeGreaterThan(0);
365 }),
366 { tags: ["unit", "provider:docker", "local"] },
367 );
368
369 it.effect(
370 "passes --platform and --build-arg",
371 () =>
372 Effect.gen(function* () {
373 const fs = yield* FileSystem.FileSystem;
374 const path = yield* Path.Path;
375 const docker = yield* Docker;
376 const tag = "alchemy-docker-test:args";
377 yield* Effect.addFinalizer(() =>
378 docker.image.remove(tag, true).pipe(
379 Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
380 Effect.ignore,
381 ),
382 );
383 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-build-" });
384 const ctx = path.join(root, "ctx");
385 yield* docker.materialize({
386 context: ctx,
387 dockerfile: [
388 "FROM alpine:3.19",
389 "ARG FOO=default",
390 'RUN echo "$FOO" > /out.txt',
391 "",
392 ].join("\n"),
393 files: [],
394 });
395 yield* docker.image.build({
396 tag,
397 context: ctx,
398 platform: "linux/amd64",
399 "build-arg": { FOO: "from-arg" },
400 });
401 const out = yield* docker.run(["run", "--rm", tag, "cat", "/out.txt"]);
402 expect(out.stdout.trim()).toBe("from-arg");
403 }),
404 { tags: ["unit", "provider:docker", "local"] },
405 );
406
407 it.effect(
408 "passes --secret through the environment and keeps it out of the image",
409 () =>
410 Effect.gen(function* () {
411 const fs = yield* FileSystem.FileSystem;
412 const path = yield* Path.Path;
413 const docker = yield* Docker;
414 const tag = "alchemy-docker-test:secret";
415 const value = "alchemy-build-secret-value";
416 yield* Effect.addFinalizer(() =>
417 docker.image.remove(tag, true).pipe(
418 Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
419 Effect.ignore,
420 ),
421 );
422 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-build-" });
423 const ctx = path.join(root, "ctx");
424 yield* docker.materialize({
425 context: ctx,
426 dockerfile: [
427 "FROM alpine:3.19",
428 // `PATH` as an id proves the value does not ride in a variable of
429 // that name: shadowing it would break `docker` itself.
430 "RUN --mount=type=secret,id=PATH,required=true " +
431 "wc -c < /run/secrets/PATH > /length.txt",
432 "",
433 ].join("\n"),
434 files: [],
435 });
436 yield* docker.image.build({
437 tag,
438 context: ctx,
439 secrets: { PATH: Redacted.make(value) },
440 });
441 const out = yield* docker.run(["run", "--rm", tag, "cat", "/length.txt"]);
442 expect(out.stdout.trim()).toBe(String(value.length));
443 const history = yield* docker.run(["image", "history", "--no-trunc", tag]);
444 expect(history.stdout).not.toContain(value);
445 }),
446 { tags: ["unit", "provider:docker", "local"] },
447 );
448
449 it.effect(
450 "respects multi-stage --target",
451 () =>
452 Effect.gen(function* () {
453 const fs = yield* FileSystem.FileSystem;
454 const path = yield* Path.Path;
455 const docker = yield* Docker;
456 const tag = "alchemy-docker-test:target";
457 yield* Effect.addFinalizer(() =>
458 docker.image.remove(tag, true).pipe(
459 Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
460 Effect.ignore,
461 ),
462 );
463 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-build-" });
464 const ctx = path.join(root, "ctx");
465 yield* docker.materialize({
466 context: ctx,
467 dockerfile: [
468 "FROM alpine:3.19 AS base",
469 "RUN echo base > /stage.txt",
470 "",
471 "FROM alpine:3.19 AS secondary",
472 "RUN echo secondary > /stage.txt",
473 "",
474 ].join("\n"),
475 files: [],
476 });
477 yield* docker.image.build({ tag, context: ctx, target: "secondary" });
478 const out = yield* docker.run(["run", "--rm", tag, "cat", "/stage.txt"]);
479 expect(out.stdout.trim()).toBe("secondary");
480 }),
481 { tags: ["unit", "provider:docker", "local"] },
482 );
483 });