|
| 1 | +import crypto from "node:crypto"; |
| 2 | +import path from "node:path"; |
| 3 | +import { InputContainerSchema } from "@cloudflare/config"; |
| 4 | +import { beforeEach, describe, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + buildAndMaybePush, |
| 7 | + buildOutputContainerConfigs, |
| 8 | + cleanupBuiltImages, |
| 9 | + verifyDockerInstalled, |
| 10 | +} from "../index"; |
| 11 | + |
| 12 | +vi.mock("../src/build", async (importOriginal) => ({ |
| 13 | + ...(await importOriginal()), |
| 14 | + buildAndMaybePush: vi.fn(), |
| 15 | + cleanupBuiltImages: vi.fn(), |
| 16 | +})); |
| 17 | +vi.mock("../src/utils", async (importOriginal) => ({ |
| 18 | + ...(await importOriginal()), |
| 19 | + verifyDockerInstalled: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +const UUIDS: `${string}-${string}-${string}-${string}-${string}`[] = [ |
| 23 | + "11111111-1111-4111-8111-111111111111", |
| 24 | + "22222222-2222-4222-8222-222222222222", |
| 25 | +]; |
| 26 | + |
| 27 | +describe("buildOutputContainerConfigs", () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.restoreAllMocks(); |
| 30 | + vi.clearAllMocks(); |
| 31 | + vi.spyOn(crypto, "randomUUID") |
| 32 | + .mockReturnValueOnce(UUIDS[0]) |
| 33 | + .mockReturnValueOnce(UUIDS[1]); |
| 34 | + vi.mocked(buildAndMaybePush).mockImplementation(async (args) => ({ |
| 35 | + newTag: args.tag, |
| 36 | + })); |
| 37 | + }); |
| 38 | + |
| 39 | + it("preserves remote references without invoking Docker", async ({ |
| 40 | + expect, |
| 41 | + }) => { |
| 42 | + const config = InputContainerSchema.parse({ |
| 43 | + type: "container", |
| 44 | + name: "remote-container", |
| 45 | + image: { reference: "registry.example.com/app:latest" }, |
| 46 | + }); |
| 47 | + |
| 48 | + await expect( |
| 49 | + buildOutputContainerConfigs({ |
| 50 | + containers: [{ directoryName: "remote", config }], |
| 51 | + root: "/project", |
| 52 | + pathToDocker: "docker", |
| 53 | + }) |
| 54 | + ).resolves.toEqual({ |
| 55 | + containers: [ |
| 56 | + { |
| 57 | + directoryName: "remote", |
| 58 | + config: { |
| 59 | + ...config, |
| 60 | + image: { reference: "registry.example.com/app:latest" }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | + builtImages: [], |
| 65 | + }); |
| 66 | + expect(verifyDockerInstalled).not.toHaveBeenCalled(); |
| 67 | + expect(buildAndMaybePush).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("builds a standard Container with resolved paths and build variables", async ({ |
| 71 | + expect, |
| 72 | + }) => { |
| 73 | + const config = InputContainerSchema.parse({ |
| 74 | + type: "container", |
| 75 | + name: "My-Container", |
| 76 | + image: { |
| 77 | + dockerfile: "./container/Dockerfile", |
| 78 | + buildContext: "./container", |
| 79 | + buildVars: { VERSION: "1" }, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const result = await buildOutputContainerConfigs({ |
| 84 | + containers: [{ directoryName: "app", config }], |
| 85 | + root: "/project", |
| 86 | + pathToDocker: "/usr/bin/docker", |
| 87 | + }); |
| 88 | + |
| 89 | + expect(verifyDockerInstalled).toHaveBeenCalledOnce(); |
| 90 | + expect(buildAndMaybePush).toHaveBeenCalledWith( |
| 91 | + { |
| 92 | + tag: `my-container:wrangler-${UUIDS[0]}`, |
| 93 | + pathToDockerfile: path.resolve("/project", "container/Dockerfile"), |
| 94 | + buildContext: path.resolve("/project", "container"), |
| 95 | + args: { VERSION: "1" }, |
| 96 | + platform: "linux/amd64", |
| 97 | + }, |
| 98 | + "/usr/bin/docker", |
| 99 | + false, |
| 100 | + undefined, |
| 101 | + false |
| 102 | + ); |
| 103 | + expect(result.containers[0]?.config).toEqual({ |
| 104 | + ...config, |
| 105 | + image: { localReference: `my-container:wrangler-${UUIDS[0]}` }, |
| 106 | + }); |
| 107 | + expect(result.builtImages).toEqual([ |
| 108 | + { localTag: `my-container:wrangler-${UUIDS[0]}` }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("builds each Durable Object named image with a sanitized repository", async ({ |
| 113 | + expect, |
| 114 | + }) => { |
| 115 | + const config = InputContainerSchema.parse({ |
| 116 | + type: "container", |
| 117 | + name: "Session Container", |
| 118 | + schedulingPolicy: "durable-object", |
| 119 | + images: { |
| 120 | + "Primary Image": { dockerfile: "./primary/Dockerfile" }, |
| 121 | + fallback: { reference: "registry.example.com/fallback:latest" }, |
| 122 | + worker: { dockerfile: "./worker/Dockerfile" }, |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + const result = await buildOutputContainerConfigs({ |
| 127 | + containers: [{ directoryName: "sessions", config }], |
| 128 | + root: "/project", |
| 129 | + pathToDocker: "docker", |
| 130 | + }); |
| 131 | + |
| 132 | + expect(verifyDockerInstalled).toHaveBeenCalledOnce(); |
| 133 | + expect(buildAndMaybePush).toHaveBeenCalledTimes(2); |
| 134 | + expect(result.containers[0]?.config).toEqual({ |
| 135 | + ...config, |
| 136 | + images: { |
| 137 | + "Primary Image": { |
| 138 | + localReference: `session-container-primary-image:wrangler-${UUIDS[0]}`, |
| 139 | + }, |
| 140 | + fallback: { reference: "registry.example.com/fallback:latest" }, |
| 141 | + worker: { |
| 142 | + localReference: `session-container-worker:wrangler-${UUIDS[1]}`, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it("cleans images built before a later build fails", async ({ expect }) => { |
| 149 | + const first = InputContainerSchema.parse({ |
| 150 | + type: "container", |
| 151 | + name: "first", |
| 152 | + image: { dockerfile: "./first/Dockerfile" }, |
| 153 | + }); |
| 154 | + const second = InputContainerSchema.parse({ |
| 155 | + type: "container", |
| 156 | + name: "second", |
| 157 | + image: { dockerfile: "./second/Dockerfile" }, |
| 158 | + }); |
| 159 | + vi.mocked(buildAndMaybePush) |
| 160 | + .mockResolvedValueOnce({ newTag: `first:wrangler-${UUIDS[0]}` }) |
| 161 | + .mockRejectedValueOnce(new Error("build failed")); |
| 162 | + |
| 163 | + await expect( |
| 164 | + buildOutputContainerConfigs({ |
| 165 | + containers: [ |
| 166 | + { directoryName: "first", config: first }, |
| 167 | + { directoryName: "second", config: second }, |
| 168 | + ], |
| 169 | + root: "/project", |
| 170 | + pathToDocker: "docker", |
| 171 | + }) |
| 172 | + ).rejects.toThrow("build failed"); |
| 173 | + expect(cleanupBuiltImages).toHaveBeenCalledWith( |
| 174 | + [{ localTag: `first:wrangler-${UUIDS[0]}` }], |
| 175 | + "docker" |
| 176 | + ); |
| 177 | + }); |
| 178 | +}); |
0 commit comments