The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Image, type ImageProps } from "@alchemy.run/aws/ECR/Image"; |
| 3 | import { Repository } from "@alchemy.run/aws/ECR/Repository"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as ecr from "@distilled.cloud/aws/ecr"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // Checked-in build fixture: `FROM scratch` + one COPY'd file, so the build is |
| 16 | // fully deterministic and never fetches a base image over the network. |
| 17 | const fixtureDir = `${import.meta.dirname}/fixtures/image`; |
| 18 | |
| 19 | test.provider( |
| 20 | "PR 1591: build, push, relocate without update, rebuild on content change, destroy", |
| 21 | (stack) => |
| 22 | Effect.gen(function* () { |
| 23 | const fs = yield* FileSystem.FileSystem; |
| 24 | const path = yield* Path.Path; |
| 25 | yield* stack.destroy(); |
| 26 | |
| 27 | // Keep both relocation and content edits outside the checked-in fixture. |
| 28 | const root = yield* fs.makeTempDirectoryScoped({ |
| 29 | prefix: "alchemy-ecr-image-1591-", |
| 30 | }); |
| 31 | const context = path.join(root, "original"); |
| 32 | const relocated = path.join(root, "relocated"); |
| 33 | yield* fs.copy(fixtureDir, context); |
| 34 | |
| 35 | const program = (context: string) => |
| 36 | Effect.gen(function* () { |
| 37 | const repository = yield* Repository("ImageRepository", {}); |
| 38 | return yield* Image("Image", { |
| 39 | repositoryUri: repository.repositoryUri, |
| 40 | context, |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | const first = yield* stack.deploy(program(context)); |
| 45 | expect(first.ownsRepository).toBe(false); |
| 46 | expect(first.digest).toMatch(/^sha256:/); |
| 47 | expect(first.imageUri).toBe(`${first.repositoryUri}:${first.imageTag}`); |
| 48 | |
| 49 | // Out-of-band: the manifest exists in ECR with the reported digest. |
| 50 | const described = yield* ecr.describeImages({ |
| 51 | repositoryName: first.repositoryName, |
| 52 | imageIds: [{ imageTag: first.imageTag }], |
| 53 | }); |
| 54 | expect(described.imageDetails?.[0]?.imageDigest).toBe(first.digest); |
| 55 | expect(described.imageDetails?.[0]?.imagePushedAt).toBeDefined(); |
| 56 | |
| 57 | const unchangedPlan = yield* stack.plan(program(context)); |
| 58 | expect(unchangedPlan.resources["Image"]).toMatchObject({ |
| 59 | action: "noop", |
| 60 | }); |
| 61 | const second = yield* stack.deploy(program(context)); |
| 62 | expect(second.imageTag).toBe(first.imageTag); |
| 63 | expect(second.digest).toBe(first.digest); |
| 64 | |
| 65 | // Renaming preserves the bytes and permissions used by the content hash. |
| 66 | yield* fs.rename(context, relocated); |
| 67 | const relocatedPlan = yield* stack.plan(program(relocated)); |
| 68 | expect(relocatedPlan.resources["Image"]).toMatchObject({ |
| 69 | action: "noop", |
| 70 | }); |
| 71 | const moved = yield* stack.deploy(program(relocated)); |
| 72 | expect(moved.repositoryUri).toBe(first.repositoryUri); |
| 73 | expect(moved.imageUri).toBe(first.imageUri); |
| 74 | expect(moved.imageTag).toBe(first.imageTag); |
| 75 | expect(moved.digest).toBe(first.digest); |
| 76 | const movedDescription = yield* ecr.describeImages({ |
| 77 | repositoryName: moved.repositoryName, |
| 78 | imageIds: [{ imageTag: moved.imageTag }], |
| 79 | }); |
| 80 | expect(movedDescription.imageDetails?.[0]?.imageDigest).toBe(first.digest); |
| 81 | expect(movedDescription.imageDetails?.[0]?.imagePushedAt).toEqual( |
| 82 | described.imageDetails?.[0]?.imagePushedAt, |
| 83 | ); |
| 84 | |
| 85 | const deleted = yield* ecr.batchDeleteImage({ |
| 86 | repositoryName: moved.repositoryName, |
| 87 | imageIds: [{ imageTag: moved.imageTag }], |
| 88 | }); |
| 89 | expect(deleted.failures ?? []).toEqual([]); |
| 90 | yield* assertImageDeleted(moved.repositoryName, moved.imageTag); |
| 91 | expect((yield* stack.plan(program(relocated))).resources.Image?.action).toBe("update"); |
| 92 | const repaired = yield* stack.deploy(program(relocated)); |
| 93 | expect(repaired.imageTag).toBe(first.imageTag); |
| 94 | expect(repaired.digest).toBe(first.digest); |
| 95 | expect((yield* stack.plan(program(relocated))).resources.Image?.action).toBe("noop"); |
| 96 | |
| 97 | // Changed bytes at the relocated path must still schedule a rebuild. |
| 98 | yield* fs.writeFileString(path.join(relocated, "hello.txt"), "hello again from alchemy\n"); |
| 99 | const changedPlan = yield* stack.plan(program(relocated)); |
| 100 | expect(changedPlan.resources["Image"]).toMatchObject({ |
| 101 | action: "update", |
| 102 | }); |
| 103 | const third = yield* stack.deploy(program(relocated)); |
| 104 | expect(third.repositoryUri).toBe(first.repositoryUri); |
| 105 | expect(third.imageTag).not.toBe(first.imageTag); |
| 106 | expect(third.digest).not.toBe(first.digest); |
| 107 | const redescribed = yield* ecr.describeImages({ |
| 108 | repositoryName: third.repositoryName, |
| 109 | imageIds: [{ imageTag: third.imageTag }], |
| 110 | }); |
| 111 | expect(redescribed.imageDetails?.[0]?.imageDigest).toBe(third.digest); |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | // The Repository owns the repo and force-deletes it (taking every |
| 116 | // image tag with it). |
| 117 | yield* assertRepositoryDeleted(first.repositoryName); |
| 118 | }).pipe(Effect.scoped), |
| 119 | { tags: ["provider:aws", "provider:aws:ecr", "live"], timeout: 120_000 }, |
| 120 | ); |
| 121 | |
| 122 | test.provider( |
| 123 | "auto-creates and owns a repository when none is given", |
| 124 | (stack) => |
| 125 | Effect.gen(function* () { |
| 126 | yield* stack.destroy(); |
| 127 | |
| 128 | const image = yield* stack.deploy( |
| 129 | Effect.gen(function* () { |
| 130 | return yield* Image("OwnedImage", { |
| 131 | context: fixtureDir, |
| 132 | }); |
| 133 | }), |
| 134 | ); |
| 135 | |
| 136 | expect(image.ownsRepository).toBe(true); |
| 137 | expect(image.digest).toMatch(/^sha256:/); |
| 138 | |
| 139 | // Out-of-band: the auto-created repository holds the pushed manifest. |
| 140 | const described = yield* ecr.describeImages({ |
| 141 | repositoryName: image.repositoryName, |
| 142 | imageIds: [{ imageTag: image.imageTag }], |
| 143 | }); |
| 144 | expect(described.imageDetails?.[0]?.imageDigest).toBe(image.digest); |
| 145 | |
| 146 | const ownershipPlan = yield* stack.plan( |
| 147 | Image("OwnedImage", { |
| 148 | context: fixtureDir, |
| 149 | repositoryUri: image.repositoryUri, |
| 150 | }), |
| 151 | ); |
| 152 | expect(ownershipPlan.resources.OwnedImage?.action).toBe("update"); |
| 153 | |
| 154 | yield* ecr.deleteRepository({ |
| 155 | repositoryName: image.repositoryName, |
| 156 | force: true, |
| 157 | }); |
| 158 | yield* assertRepositoryDeleted(image.repositoryName); |
| 159 | const original = Image("OwnedImage", { context: fixtureDir }); |
| 160 | expect((yield* stack.plan(original)).resources.OwnedImage?.action).toBe("update"); |
| 161 | const restored = yield* stack.deploy(original); |
| 162 | expect(restored.repositoryUri).toBe(image.repositoryUri); |
| 163 | expect(restored.imageTag).toBe(image.imageTag); |
| 164 | expect(restored.ownsRepository).toBe(true); |
| 165 | expect( |
| 166 | (yield* ecr.describeImages({ |
| 167 | repositoryName: restored.repositoryName, |
| 168 | imageIds: [{ imageTag: restored.imageTag }], |
| 169 | })).imageDetails?.[0]?.imageDigest, |
| 170 | ).toBe(restored.digest); |
| 171 | |
| 172 | yield* stack.destroy(); |
| 173 | |
| 174 | // Destroying an owning Image force-deletes its repository. |
| 175 | yield* assertRepositoryDeleted(image.repositoryName); |
| 176 | }), |
| 177 | { tags: ["provider:aws", "provider:aws:ecr", "live"], timeout: 120_000 }, |
| 178 | ); |
| 179 | |
| 180 | test.provider( |
| 181 | "build arguments, platform, and repository changes still update the image", |
| 182 | (stack) => |
| 183 | Effect.gen(function* () { |
| 184 | yield* stack.destroy(); |
| 185 | const path = yield* Path.Path; |
| 186 | const program = ( |
| 187 | options: Pick<ImageProps, "buildArgs" | "platform" | "dockerfile">, |
| 188 | alternate = false, |
| 189 | ) => |
| 190 | Effect.gen(function* () { |
| 191 | const primary = yield* Repository("Primary", {}); |
| 192 | const secondary = yield* Repository("Secondary", {}); |
| 193 | return yield* Image("ConfiguredImage", { |
| 194 | context: fixtureDir, |
| 195 | repositoryUri: alternate ? secondary.repositoryUri : primary.repositoryUri, |
| 196 | ...options, |
| 197 | }); |
| 198 | }); |
| 199 | const firstOptions = { buildArgs: { BUILD_LABEL: "first" } }; |
| 200 | const first = yield* stack.deploy(program(firstOptions)); |
| 201 | expect( |
| 202 | (yield* stack.plan( |
| 203 | program({ |
| 204 | ...firstOptions, |
| 205 | dockerfile: path.join(fixtureDir, "Dockerfile"), |
| 206 | platform: "linux/amd64", |
| 207 | }), |
| 208 | )).resources.ConfiguredImage?.action, |
| 209 | ).toBe("noop"); |
| 210 | |
| 211 | const secondOptions = { buildArgs: { BUILD_LABEL: "second" } }; |
| 212 | expect((yield* stack.plan(program(secondOptions))).resources.ConfiguredImage?.action).toBe( |
| 213 | "update", |
| 214 | ); |
| 215 | const second = yield* stack.deploy(program(secondOptions)); |
| 216 | expect(second.imageTag).not.toBe(first.imageTag); |
| 217 | expect(second.digest).not.toBe(first.digest); |
| 218 | |
| 219 | const armOptions = { ...secondOptions, platform: "linux/arm64" }; |
| 220 | expect((yield* stack.plan(program(armOptions))).resources.ConfiguredImage?.action).toBe( |
| 221 | "update", |
| 222 | ); |
| 223 | const arm = yield* stack.deploy(program(armOptions)); |
| 224 | expect(arm.imageTag).not.toBe(second.imageTag); |
| 225 | expect(arm.digest).not.toBe(second.digest); |
| 226 | |
| 227 | expect((yield* stack.plan(program(armOptions, true))).resources.ConfiguredImage?.action).toBe( |
| 228 | "update", |
| 229 | ); |
| 230 | const moved = yield* stack.deploy(program(armOptions, true)); |
| 231 | expect(moved.repositoryUri).not.toBe(arm.repositoryUri); |
| 232 | expect(moved.imageTag).toBe(arm.imageTag); |
| 233 | expect(moved.ownsRepository).toBe(false); |
| 234 | expect( |
| 235 | (yield* ecr.describeImages({ |
| 236 | repositoryName: moved.repositoryName, |
| 237 | imageIds: [{ imageTag: moved.imageTag }], |
| 238 | })).imageDetails?.[0]?.imageDigest, |
| 239 | ).toBe(moved.digest); |
| 240 | expect((yield* stack.plan(program(armOptions, true))).resources.ConfiguredImage?.action).toBe( |
| 241 | "noop", |
| 242 | ); |
| 243 | |
| 244 | yield* stack.destroy(); |
| 245 | yield* assertRepositoryDeleted(first.repositoryName); |
| 246 | yield* assertRepositoryDeleted(moved.repositoryName); |
| 247 | }), |
| 248 | { tags: ["provider:aws", "provider:aws:ecr", "live"], timeout: 120_000 }, |
| 249 | ); |
| 250 | |
| 251 | test.provider( |
| 252 | "files excluded by .dockerignore do not change the image tag", |
| 253 | (stack) => |
| 254 | Effect.gen(function* () { |
| 255 | const fs = yield* FileSystem.FileSystem; |
| 256 | const path = yield* Path.Path; |
| 257 | yield* stack.destroy(); |
| 258 | |
| 259 | const context = yield* fs.makeTempDirectoryScoped({ |
| 260 | prefix: "alchemy-ecr-image-dockerignore-", |
| 261 | }); |
| 262 | yield* fs.copy(fixtureDir, context); |
| 263 | yield* fs.writeFileString(path.join(context, ".dockerignore"), "node_modules\n"); |
| 264 | const ignored = path.join(context, "node_modules", "pkg"); |
| 265 | yield* fs.makeDirectory(ignored, { recursive: true }); |
| 266 | yield* fs.writeFileString(path.join(ignored, "index.js"), "first\n"); |
| 267 | |
| 268 | const program = Effect.gen(function* () { |
| 269 | const repository = yield* Repository("IgnoreRepository", {}); |
| 270 | return yield* Image("IgnoreImage", { |
| 271 | repositoryUri: repository.repositoryUri, |
| 272 | context, |
| 273 | }); |
| 274 | }); |
| 275 | |
| 276 | const first = yield* stack.deploy(program); |
| 277 | |
| 278 | // A local install rewrites ignored files Docker never receives. |
| 279 | yield* fs.writeFileString(path.join(ignored, "index.js"), "second\n"); |
| 280 | yield* fs.writeFileString(path.join(ignored, "extra.js"), "extra\n"); |
| 281 | expect((yield* stack.plan(program)).resources.IgnoreImage?.action).toBe("noop"); |
| 282 | const second = yield* stack.deploy(program); |
| 283 | expect(second.imageTag).toBe(first.imageTag); |
| 284 | expect(second.digest).toBe(first.digest); |
| 285 | |
| 286 | // Files Docker does receive still rebuild the image. |
| 287 | yield* fs.writeFileString(path.join(context, "hello.txt"), "kept and changed\n"); |
| 288 | expect((yield* stack.plan(program)).resources.IgnoreImage?.action).toBe("update"); |
| 289 | const third = yield* stack.deploy(program); |
| 290 | expect(third.imageTag).not.toBe(first.imageTag); |
| 291 | expect(third.digest).not.toBe(first.digest); |
| 292 | expect( |
| 293 | (yield* ecr.describeImages({ |
| 294 | repositoryName: third.repositoryName, |
| 295 | imageIds: [{ imageTag: third.imageTag }], |
| 296 | })).imageDetails?.[0]?.imageDigest, |
| 297 | ).toBe(third.digest); |
| 298 | |
| 299 | yield* stack.destroy(); |
| 300 | yield* assertRepositoryDeleted(first.repositoryName); |
| 301 | }).pipe(Effect.scoped), |
| 302 | { tags: ["provider:aws", "provider:aws:ecr", "live"], timeout: 120_000 }, |
| 303 | ); |
| 304 | |
| 305 | class ImageStillExists extends Data.TaggedError("ImageStillExists") {} |
| 306 | |
| 307 | const assertImageDeleted = Effect.fn(function* (repositoryName: string, imageTag: string) { |
| 308 | yield* ecr.describeImages({ repositoryName, imageIds: [{ imageTag }] }).pipe( |
| 309 | Effect.flatMap(() => Effect.fail(new ImageStillExists())), |
| 310 | Effect.retry({ |
| 311 | while: (error) => error._tag === "ImageStillExists", |
| 312 | schedule: Schedule.spaced("1 second"), |
| 313 | times: 8, |
| 314 | }), |
| 315 | Effect.catchTag("ImageNotFoundException", () => Effect.void), |
| 316 | ); |
| 317 | }); |
| 318 | |
| 319 | class RepositoryStillExists extends Data.TaggedError("RepositoryStillExists") {} |
| 320 | |
| 321 | const assertRepositoryDeleted = Effect.fn(function* (repositoryName: string) { |
| 322 | yield* ecr.describeRepositories({ repositoryNames: [repositoryName] }).pipe( |
| 323 | Effect.flatMap(() => Effect.fail(new RepositoryStillExists())), |
| 324 | Effect.retry({ |
| 325 | while: (e) => e._tag === "RepositoryStillExists", |
| 326 | schedule: Schedule.spaced("1 second"), |
| 327 | times: 8, |
| 328 | }), |
| 329 | Effect.catchTag("RepositoryNotFoundException", () => Effect.void), |
| 330 | ); |
| 331 | }); |