The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { ContainerPlatform } from "@alchemy.run/cloudflare/Containers/ContainerPlatform"; |
| 4 | import { Docker } from "@alchemy.run/docker/Docker"; |
| 5 | import { assert, describe, expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as Containers from "@distilled.cloud/cloudflare/containers"; |
| 8 | import * as Deploy from "alchemy/Deploy"; |
| 9 | import * as Destroy from "alchemy/Destroy"; |
| 10 | import * as Drift from "alchemy/Drift"; |
| 11 | import * as Provider from "alchemy/Provider"; |
| 12 | import { Stack } from "alchemy/Stack"; |
| 13 | import { State, type ResourceState } from "alchemy/State"; |
| 14 | import * as Cause from "effect/Cause"; |
| 15 | import * as Config from "effect/Config"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Exit from "effect/Exit"; |
| 18 | import * as Fiber from "effect/Fiber"; |
| 19 | import * as FileSystem from "effect/FileSystem"; |
| 20 | import * as HttpClient from "effect/http/HttpClient"; |
| 21 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 22 | import * as Layer from "effect/Layer"; |
| 23 | import * as Path from "effect/Path"; |
| 24 | import { MinimumLogLevel } from "effect/References"; |
| 25 | import * as Schedule from "effect/Schedule"; |
| 26 | import * as Schema from "effect/Schema"; |
| 27 | import { |
| 28 | buildHistory, |
| 29 | supportsRegistryExport, |
| 30 | withBuilder, |
| 31 | withDockerEnv, |
| 32 | } from "./fixtures/buildx.ts"; |
| 33 | import MyContainerLive, { MyContainer } from "./fixtures/effectful/container.ts"; |
| 34 | import EffectfulContainerWorker from "./fixtures/effectful/worker.ts"; |
| 35 | import { ExternalContainer } from "./fixtures/external/object.ts"; |
| 36 | import ExternalContainerWorker from "./fixtures/external/worker.ts"; |
| 37 | import { applications, cyclicApplication } from "./fixtures/identity/applications.ts"; |
| 38 | import LinkedChanged from "./fixtures/linked/changed.ts"; |
| 39 | import { LinkedContainer } from "./fixtures/linked/container.ts"; |
| 40 | import LinkedFirst from "./fixtures/linked/first.ts"; |
| 41 | import { |
| 42 | publicationApplications, |
| 43 | sharedApplication, |
| 44 | recoveryApplications, |
| 45 | historyApplications, |
| 46 | } from "./fixtures/publication/applications.ts"; |
| 47 | import { EnvBucket, RemoteContainer } from "./fixtures/remote/object.ts"; |
| 48 | import RemoteContainerWorker from "./fixtures/remote/worker.ts"; |
| 49 | |
| 50 | /** Narrows a Containers application to the scheduler-placed shape. */ |
| 51 | const scheduledApplication = (app: Containers.ContainerApplicationItem) => { |
| 52 | assert("version" in app, `${app.name} is a Durable Object application`); |
| 53 | return app; |
| 54 | }; |
| 55 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 56 | const { test: registryCacheTest } = Test.make({ |
| 57 | // Each deployment must consult the registry, not a previous provider's map. |
| 58 | providers: Layer.fresh(Cloudflare.providers()), |
| 59 | }); |
| 60 | |
| 61 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 62 | |
| 63 | type Scratch = Parameters<Parameters<typeof test.provider>[1]>[0]; |
| 64 | |
| 65 | /** Deploy a standalone container application from a remote `image`. */ |
| 66 | const deployImage = (scratch: Scratch, image: string) => |
| 67 | scratch.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | return { app: yield* Cloudflare.Container("DigestReuse", { image }).Application }; |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | /** The live (active) image reference + version of an application. */ |
| 74 | const live = (accountId: string, applicationId: string) => |
| 75 | Containers.getContainerApplication({ accountId, applicationId }).pipe( |
| 76 | Effect.map(scheduledApplication), |
| 77 | Effect.map((app) => ({ |
| 78 | version: app.version, |
| 79 | image: app.configuration.image, |
| 80 | durableObjects: app.durableObjects ?? undefined, |
| 81 | })), |
| 82 | ); |
| 83 | |
| 84 | /** |
| 85 | * Poll until the application's active image is `image`. Cloudflare reports |
| 86 | * the ACTIVE configuration (and version) until a rollout completes, so any |
| 87 | * assertion on a changed image has to wait for the rollout. |
| 88 | */ |
| 89 | const waitForImage = (accountId: string, applicationId: string, image: string) => |
| 90 | live(accountId, applicationId).pipe( |
| 91 | Effect.repeat({ |
| 92 | schedule: Schedule.spaced("3 seconds"), |
| 93 | until: (app) => app.image === image, |
| 94 | times: 30, |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | /** `<repo>:<sourceHash>` — the mutable tag the provider pushed `app` as. */ |
| 99 | const taggedRefOf = (app: { configuration: { image?: string }; hash?: { image: string } }) => { |
| 100 | const digestRef = app.configuration.image!; |
| 101 | return `${digestRef.slice(0, digestRef.indexOf("@"))}:${app.hash!.image}`; |
| 102 | }; |
| 103 | |
| 104 | /** Rewrite the persisted attributes of the scratch row for `fqn`. */ |
| 105 | const patchRow = <A extends Record<string, any>>(fqn: string, patch: (attr: A) => A) => |
| 106 | Effect.gen(function* () { |
| 107 | const state = yield* yield* State; |
| 108 | const stk = yield* Stack; |
| 109 | const key = { stack: stk.name, stage: stk.stage, fqn }; |
| 110 | const row = (yield* state.get(key)) as ResourceState; |
| 111 | yield* state.set({ ...key, value: { ...row, attr: patch(row.attr as A) } }); |
| 112 | }); |
| 113 | |
| 114 | /** |
| 115 | * Deploys `CyclicOriginal`, then forgets it, leaving an application owned by |
| 116 | * another logical id. Returns a step that remembers it again, so destroy |
| 117 | * removes it through normal stack cleanup. |
| 118 | */ |
| 119 | const plantForeignApplication = Effect.fn(function* (stack: Scratch) { |
| 120 | yield* stack.destroy(); |
| 121 | const original = yield* stack.deploy( |
| 122 | Cloudflare.Container("CyclicOriginal", { |
| 123 | image: "mendhak/http-https-echo:41", |
| 124 | maxInstances: 1, |
| 125 | }).Application, |
| 126 | ); |
| 127 | const key = { stack: stack.name, stage: stack.stage, fqn: "CyclicOriginal" }; |
| 128 | const row = yield* Effect.gen(function* () { |
| 129 | const state = yield* yield* State; |
| 130 | const value = yield* state.get(key); |
| 131 | yield* state.delete(key); |
| 132 | return value!; |
| 133 | }).pipe(Effect.provide(stack.state)); |
| 134 | const restore = Effect.gen(function* () { |
| 135 | const state = yield* yield* State; |
| 136 | yield* state.set({ ...key, value: row }); |
| 137 | }).pipe(Effect.provide(stack.state)); |
| 138 | return { original, restore }; |
| 139 | }); |
| 140 | |
| 141 | const waitForApplicationDeleted = (accountId: string, applicationId: string) => |
| 142 | Containers.getContainerApplication({ accountId, applicationId }).pipe( |
| 143 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 144 | Effect.repeat({ |
| 145 | schedule: Schedule.spaced("1 second"), |
| 146 | until: (app) => app === undefined, |
| 147 | times: 8, |
| 148 | }), |
| 149 | ); |
| 150 | |
| 151 | // Every test owns a name-namespaced scratch stack and scopes its buildx |
| 152 | // builder through `DockerEnvironment`, so they all run concurrently. |
| 153 | describe.concurrent( |
| 154 | "ContainerApplication", |
| 155 | { tags: ["provider:cloudflare", "provider:cloudflare:container", "provider:docker", "live"] }, |
| 156 | () => { |
| 157 | test.provider( |
| 158 | "recovers an interrupted generated create and reconciles observed drift", |
| 159 | (stack) => |
| 160 | Effect.gen(function* () { |
| 161 | yield* stack.destroy(); |
| 162 | const first = yield* stack.deploy(applications()); |
| 163 | const state = yield* yield* State; |
| 164 | const key = { stack: stack.name, stage: stack.stage, fqn: "CachedIdentity" }; |
| 165 | const row = yield* state.get(key); |
| 166 | assert(row?.status === "created" || row?.status === "updated"); |
| 167 | yield* Containers.updateContainerApplication({ |
| 168 | accountId: first.owned.accountId, |
| 169 | applicationId: first.owned.applicationId, |
| 170 | maxInstances: 4, |
| 171 | }); |
| 172 | const observed = yield* Containers.getContainerApplication({ |
| 173 | accountId: first.owned.accountId, |
| 174 | applicationId: first.owned.applicationId, |
| 175 | }).pipe(Effect.map(scheduledApplication)); |
| 176 | expect(observed.maxInstances).toBe(4); |
| 177 | const interrupted = { ...row, status: "creating" as const, attr: undefined }; |
| 178 | yield* Effect.gen(function* () { |
| 179 | yield* state.set({ ...key, value: interrupted }); |
| 180 | const plan = yield* stack.plan(applications()); |
| 181 | expect(plan.resources.CachedIdentity).toMatchObject({ |
| 182 | action: "create", |
| 183 | state: { |
| 184 | instanceId: row.instanceId, |
| 185 | attr: { |
| 186 | applicationId: first.owned.applicationId, |
| 187 | applicationName: first.owned.applicationName, |
| 188 | accountId: first.owned.accountId, |
| 189 | maxInstances: 4, |
| 190 | }, |
| 191 | }, |
| 192 | }); |
| 193 | expect(plan.resources.OtherIdentity.action).toBe("noop"); |
| 194 | expect(yield* state.get(key)).toEqual(interrupted); |
| 195 | |
| 196 | const renamed = yield* stack.plan( |
| 197 | applications(2, `renamed-${first.owned.applicationName.slice(-24)}`), |
| 198 | ); |
| 199 | expect(renamed.resources.CachedIdentity.action).toBe("replace"); |
| 200 | expect(yield* state.get(key)).toEqual(interrupted); |
| 201 | expect( |
| 202 | yield* Containers.getContainerApplication({ |
| 203 | accountId: first.owned.accountId, |
| 204 | applicationId: first.owned.applicationId, |
| 205 | }), |
| 206 | ).toEqual(observed); |
| 207 | |
| 208 | yield* state.set({ ...key, value: { ...interrupted, providerMode: "local" } }); |
| 209 | const switched = yield* stack.plan(applications()); |
| 210 | expect(switched.resources.CachedIdentity.action).toBe("replace"); |
| 211 | expect(switched.resources.CachedIdentity.state?.attr).toBeUndefined(); |
| 212 | yield* state.set({ |
| 213 | ...key, |
| 214 | value: { ...interrupted, instanceId: "00000000000000000000000000000000" }, |
| 215 | }); |
| 216 | const unrelated = yield* stack.plan(applications()); |
| 217 | expect(unrelated.resources.CachedIdentity.action).toBe("create"); |
| 218 | expect(unrelated.resources.CachedIdentity.state?.attr).toBeUndefined(); |
| 219 | |
| 220 | yield* state.delete(key); |
| 221 | const fresh = yield* stack.plan(applications()); |
| 222 | expect(fresh.resources.CachedIdentity.action).toBe("create"); |
| 223 | expect(fresh.resources.CachedIdentity.state).toBeUndefined(); |
| 224 | expect(yield* state.get(key)).toBeUndefined(); |
| 225 | |
| 226 | yield* state.set({ ...key, value: interrupted }); |
| 227 | const recovered = yield* stack.deploy(applications(5)); |
| 228 | expect(recovered.owned.applicationId).toBe(first.owned.applicationId); |
| 229 | expect(recovered.other.applicationId).toBe(first.other.applicationId); |
| 230 | expect(yield* state.get(key)).toMatchObject({ |
| 231 | status: "created", |
| 232 | instanceId: row.instanceId, |
| 233 | attr: { applicationId: first.owned.applicationId, maxInstances: 5 }, |
| 234 | }); |
| 235 | const live = yield* Containers.getContainerApplication({ |
| 236 | accountId: first.owned.accountId, |
| 237 | applicationId: first.owned.applicationId, |
| 238 | }).pipe(Effect.map(scheduledApplication)); |
| 239 | expect(live.maxInstances).toBe(5); |
| 240 | }).pipe( |
| 241 | Effect.ensuring( |
| 242 | Effect.gen(function* () { |
| 243 | yield* stack.destroy(); |
| 244 | yield* Effect.gen(function* () { |
| 245 | const restored = yield* yield* State; |
| 246 | yield* restored.set({ ...key, value: row }); |
| 247 | expect(yield* restored.get(key)).toEqual(row); |
| 248 | }).pipe(Effect.provide(Layer.fresh(stack.state))); |
| 249 | yield* stack.destroy(); |
| 250 | }).pipe(Effect.orDie), |
| 251 | ), |
| 252 | ); |
| 253 | for (const app of [first.owned, first.other]) { |
| 254 | const deleted = yield* Containers.getContainerApplication({ |
| 255 | accountId: app.accountId, |
| 256 | applicationId: app.applicationId, |
| 257 | }).pipe( |
| 258 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 259 | Effect.repeat({ |
| 260 | schedule: Schedule.spaced("1 second"), |
| 261 | until: (app) => app === undefined, |
| 262 | times: 8, |
| 263 | }), |
| 264 | ); |
| 265 | expect(deleted).toBeUndefined(); |
| 266 | } |
| 267 | }).pipe(logLevel), |
| 268 | { timeout: 120_000 }, |
| 269 | ); |
| 270 | |
| 271 | // A cycle precreates on unresolved props, before `read` can check |
| 272 | // ownership; precreate would adopt and update an application by name. |
| 273 | // An explicit name never establishes ownership, so `observe` refuses it. |
| 274 | for (const scoped of [undefined, false]) { |
| 275 | test.provider( |
| 276 | `a cyclic precreate refuses an explicitly named application (scoped adoption ${scoped})`, |
| 277 | (stack) => |
| 278 | Effect.gen(function* () { |
| 279 | const { original, restore } = yield* plantForeignApplication(stack); |
| 280 | const request = { |
| 281 | accountId: original.accountId, |
| 282 | applicationId: original.applicationId, |
| 283 | }; |
| 284 | const before = yield* Containers.getContainerApplication(request); |
| 285 | const program = cyclicApplication(original.applicationName, scoped); |
| 286 | expect((yield* stack.plan(program)).resources.CyclicDifferent?.action).toBe("create"); |
| 287 | const error = yield* stack.deploy(program).pipe(Effect.flip); |
| 288 | expect(error).toMatchObject({ |
| 289 | _tag: "OwnedBySomeoneElse", |
| 290 | logicalId: "CyclicDifferent", |
| 291 | }); |
| 292 | expect(yield* Containers.getContainerApplication(request)).toEqual(before); |
| 293 | yield* stack.destroy(); |
| 294 | yield* restore; |
| 295 | yield* stack.destroy(); |
| 296 | expect( |
| 297 | yield* waitForApplicationDeleted(original.accountId, original.applicationId), |
| 298 | ).toBeUndefined(); |
| 299 | }).pipe(logLevel), |
| 300 | { timeout: 120_000 }, |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | test.provider( |
| 305 | "a cyclic precreate takes over an explicitly named application with adopt(true)", |
| 306 | (stack) => |
| 307 | Effect.gen(function* () { |
| 308 | const { original, restore } = yield* plantForeignApplication(stack); |
| 309 | const adopted = yield* stack.deploy(cyclicApplication(original.applicationName, true)); |
| 310 | expect(adopted.applicationId).toBe(original.applicationId); |
| 311 | yield* stack.destroy(); |
| 312 | expect( |
| 313 | yield* waitForApplicationDeleted(original.accountId, original.applicationId), |
| 314 | ).toBeUndefined(); |
| 315 | // The original row still names the taken-over application; deleting it again is a no-op. |
| 316 | yield* restore; |
| 317 | yield* stack.destroy(); |
| 318 | }).pipe(logLevel), |
| 319 | { timeout: 120_000 }, |
| 320 | ); |
| 321 | |
| 322 | test.provider( |
| 323 | "keeps explicit names unowned even when they match a generated identity", |
| 324 | (stack) => |
| 325 | Effect.gen(function* () { |
| 326 | yield* stack.destroy(); |
| 327 | const first = yield* stack.deploy(applications()); |
| 328 | const program = applications(2, first.owned.applicationName); |
| 329 | const state = yield* yield* State; |
| 330 | const key = { stack: stack.name, stage: stack.stage, fqn: "CachedIdentity" }; |
| 331 | const row = yield* state.get(key); |
| 332 | assert(row?.status === "created" || row?.status === "updated"); |
| 333 | const observed = yield* Containers.getContainerApplication({ |
| 334 | accountId: first.owned.accountId, |
| 335 | applicationId: first.owned.applicationId, |
| 336 | }); |
| 337 | yield* Effect.gen(function* () { |
| 338 | const interrupted = { |
| 339 | ...row, |
| 340 | props: { ...row.props, name: first.owned.applicationName }, |
| 341 | status: "creating" as const, |
| 342 | attr: undefined, |
| 343 | }; |
| 344 | yield* state.set({ ...key, value: interrupted }); |
| 345 | const recovery = yield* stack |
| 346 | .plan(program) |
| 347 | .pipe(Effect.catchTag("OwnedBySomeoneElse", () => Effect.succeed("unowned"))); |
| 348 | expect(recovery).toBe("unowned"); |
| 349 | expect(yield* state.get(key)).toEqual(interrupted); |
| 350 | yield* state.delete(key); |
| 351 | const discovery = yield* stack |
| 352 | .plan(program) |
| 353 | .pipe(Effect.catchTag("OwnedBySomeoneElse", () => Effect.succeed("unowned"))); |
| 354 | expect(discovery).toBe("unowned"); |
| 355 | expect(yield* state.get(key)).toBeUndefined(); |
| 356 | expect( |
| 357 | yield* Containers.getContainerApplication({ |
| 358 | accountId: first.owned.accountId, |
| 359 | applicationId: first.owned.applicationId, |
| 360 | }), |
| 361 | ).toEqual(observed); |
| 362 | }).pipe( |
| 363 | Effect.ensuring( |
| 364 | Effect.gen(function* () { |
| 365 | yield* stack.destroy(); |
| 366 | yield* Effect.gen(function* () { |
| 367 | const restored = yield* yield* State; |
| 368 | yield* restored.set({ ...key, value: row }); |
| 369 | expect(yield* restored.get(key)).toEqual(row); |
| 370 | }).pipe(Effect.provide(Layer.fresh(stack.state))); |
| 371 | yield* stack.destroy(); |
| 372 | }).pipe(Effect.orDie), |
| 373 | ), |
| 374 | ); |
| 375 | for (const app of [first.owned, first.other]) { |
| 376 | const deleted = yield* Containers.getContainerApplication({ |
| 377 | accountId: app.accountId, |
| 378 | applicationId: app.applicationId, |
| 379 | }).pipe( |
| 380 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 381 | Effect.repeat({ |
| 382 | schedule: Schedule.spaced("1 second"), |
| 383 | until: (app) => app === undefined, |
| 384 | times: 8, |
| 385 | }), |
| 386 | ); |
| 387 | expect(deleted).toBeUndefined(); |
| 388 | } |
| 389 | }).pipe(logLevel), |
| 390 | { timeout: 120_000 }, |
| 391 | ); |
| 392 | |
| 393 | test.provider( |
| 394 | "destroys an interrupted generated create without resuming it", |
| 395 | (stack) => |
| 396 | Effect.gen(function* () { |
| 397 | yield* stack.destroy(); |
| 398 | const first = yield* stack.deploy(applications()); |
| 399 | const state = yield* yield* State; |
| 400 | const key = { stack: stack.name, stage: stack.stage, fqn: "CachedIdentity" }; |
| 401 | const row = yield* state.get(key); |
| 402 | assert(row?.status === "created" || row?.status === "updated"); |
| 403 | yield* Effect.gen(function* () { |
| 404 | yield* state.set({ ...key, value: { ...row, status: "creating", attr: undefined } }); |
| 405 | yield* stack.destroy(); |
| 406 | for (const app of [first.owned, first.other]) { |
| 407 | const deleted = yield* Containers.getContainerApplication({ |
| 408 | accountId: app.accountId, |
| 409 | applicationId: app.applicationId, |
| 410 | }).pipe( |
| 411 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 412 | Effect.repeat({ |
| 413 | schedule: Schedule.spaced("1 second"), |
| 414 | until: (app) => app === undefined, |
| 415 | times: 8, |
| 416 | }), |
| 417 | ); |
| 418 | expect(deleted).toBeUndefined(); |
| 419 | } |
| 420 | expect(yield* state.get(key)).toBeUndefined(); |
| 421 | }).pipe( |
| 422 | Effect.ensuring( |
| 423 | Effect.gen(function* () { |
| 424 | const restored = yield* yield* State; |
| 425 | yield* restored.set({ ...key, value: row }); |
| 426 | expect(yield* restored.get(key)).toEqual(row); |
| 427 | }).pipe(Effect.provide(Layer.fresh(stack.state)), Effect.orDie), |
| 428 | ), |
| 429 | ); |
| 430 | yield* stack.destroy(); |
| 431 | }).pipe(logLevel), |
| 432 | { timeout: 120_000 }, |
| 433 | ); |
| 434 | |
| 435 | test.provider( |
| 436 | "compares each legacy application's previous digest independently", |
| 437 | (stack) => |
| 438 | Effect.gen(function* () { |
| 439 | yield* stack.destroy(); |
| 440 | const initial = yield* stack.deploy(historyApplications()); |
| 441 | expect(initial.first.hash?.digest).not.toBe(initial.second.hash?.digest); |
| 442 | const state = yield* yield* State; |
| 443 | const ids = ["HistoryFirst", "HistorySecond"]; |
| 444 | const updated = yield* Effect.acquireUseRelease( |
| 445 | Effect.forEach(ids, (id) => |
| 446 | state.get({ stack: stack.name, stage: stack.stage, fqn: id }), |
| 447 | ), |
| 448 | () => |
| 449 | Effect.gen(function* () { |
| 450 | for (const id of ids) { |
| 451 | yield* patchRow<typeof initial.first>(id, (attr) => ({ |
| 452 | ...attr, |
| 453 | hash: { image: attr.hash!.image }, |
| 454 | })); |
| 455 | } |
| 456 | const updated = yield* stack.deploy(historyApplications(true)); |
| 457 | assert(updated.target); |
| 458 | expect(updated.target.configuration.image).not.toBe( |
| 459 | initial.first.configuration.image, |
| 460 | ); |
| 461 | expect(updated.target.hash?.digest).toBe(initial.first.hash?.digest); |
| 462 | expect(updated.first.applicationId).toBe(initial.first.applicationId); |
| 463 | expect(updated.first.configuration.image).toBe(initial.first.configuration.image); |
| 464 | expect(updated.second.applicationId).toBe(initial.second.applicationId); |
| 465 | expect(updated.second.configuration.image).toBe(updated.target.configuration.image); |
| 466 | expect(updated.second.hash?.digest).toBe(initial.first.hash?.digest); |
| 467 | const desiredImage = updated.target.configuration.image; |
| 468 | const observed = yield* live( |
| 469 | updated.second.accountId, |
| 470 | updated.second.applicationId, |
| 471 | ).pipe( |
| 472 | Effect.repeat({ |
| 473 | schedule: Schedule.spaced("5 seconds"), |
| 474 | until: (app) => app.image === desiredImage, |
| 475 | times: 8, |
| 476 | }), |
| 477 | ); |
| 478 | expect(observed.image).toBe(updated.target.configuration.image); |
| 479 | return updated; |
| 480 | }), |
| 481 | (rows) => |
| 482 | Effect.forEach(rows, (row, i) => { |
| 483 | assert(row); |
| 484 | return state.set({ |
| 485 | stack: stack.name, |
| 486 | stage: stack.stage, |
| 487 | fqn: ids[i], |
| 488 | value: row, |
| 489 | }); |
| 490 | }).pipe(Effect.orDie), |
| 491 | ); |
| 492 | yield* stack.destroy(); |
| 493 | for (const app of [updated.first, updated.second, updated.target]) { |
| 494 | assert(app); |
| 495 | const deleted = yield* Containers.getContainerApplication({ |
| 496 | accountId: app.accountId, |
| 497 | applicationId: app.applicationId, |
| 498 | }).pipe( |
| 499 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 500 | Effect.repeat({ |
| 501 | schedule: Schedule.spaced("1 second"), |
| 502 | until: (app) => app === undefined, |
| 503 | times: 8, |
| 504 | }), |
| 505 | ); |
| 506 | expect(deleted).toBeUndefined(); |
| 507 | } |
| 508 | }).pipe(logLevel), |
| 509 | { timeout: 120_000 }, |
| 510 | ); |
| 511 | |
| 512 | for (const failure of ["failed", "interrupted"] as const) { |
| 513 | test.provider( |
| 514 | `retries ${failure === "failed" ? "a failed" : "an interrupted"} publication in the same provider`, |
| 515 | (stack) => |
| 516 | Effect.gen(function* () { |
| 517 | yield* stack.destroy(); |
| 518 | const provider = yield* Provider.findProvider(ContainerPlatform); |
| 519 | const program = (includeSecond = false) => |
| 520 | Effect.gen(function* () { |
| 521 | expect(yield* Provider.findProvider(ContainerPlatform)).toBe(provider); |
| 522 | return yield* recoveryApplications( |
| 523 | failure === "interrupted" ? 10 : 0, |
| 524 | includeSecond, |
| 525 | ); |
| 526 | }); |
| 527 | if (failure === "failed") { |
| 528 | const failed = yield* stack |
| 529 | .deploy(program()) |
| 530 | .pipe( |
| 531 | withDockerEnv({ BUILDX_BUILDER: "alchemy-publication-missing" }), |
| 532 | Effect.exit, |
| 533 | ); |
| 534 | assert(Exit.isFailure(failed)); |
| 535 | expect(Cause.pretty(failed.cause)).toContain("alchemy-publication-missing"); |
| 536 | } else { |
| 537 | yield* Effect.acquireUseRelease( |
| 538 | stack.deploy(program()).pipe(Effect.forkChild), |
| 539 | (fiber) => |
| 540 | Effect.gen(function* () { |
| 541 | const history = yield* buildHistory.pipe( |
| 542 | Effect.repeat({ |
| 543 | schedule: Schedule.spaced("1 second"), |
| 544 | until: (builds) => builds.some((build) => build.status === "Running"), |
| 545 | times: 10, |
| 546 | }), |
| 547 | ); |
| 548 | const running = history.find((build) => build.status === "Running"); |
| 549 | assert(running); |
| 550 | yield* Fiber.interrupt(fiber); |
| 551 | const interrupted = yield* Fiber.await(fiber); |
| 552 | assert(Exit.isFailure(interrupted)); |
| 553 | expect(Cause.hasInterrupts(interrupted.cause)).toBe(true); |
| 554 | const stopped = yield* buildHistory.pipe( |
| 555 | Effect.repeat({ |
| 556 | schedule: Schedule.spaced("1 second"), |
| 557 | until: (builds) => |
| 558 | builds.some( |
| 559 | (build) => build.ref === running.ref && build.status !== "Running", |
| 560 | ), |
| 561 | times: 8, |
| 562 | }), |
| 563 | ); |
| 564 | const stoppedBuild = stopped.find((build) => build.ref === running.ref); |
| 565 | assert(stoppedBuild); |
| 566 | expect(stoppedBuild.status).not.toBe("Running"); |
| 567 | }), |
| 568 | (fiber) => Fiber.interrupt(fiber), |
| 569 | ); |
| 570 | } |
| 571 | const failedBuilds = yield* buildHistory; |
| 572 | expect(failedBuilds.filter((build) => build.status === "Completed")).toHaveLength(0); |
| 573 | const recovered = yield* stack.deploy(program()); |
| 574 | const recoveredBuilds = yield* buildHistory; |
| 575 | expect(recoveredBuilds.length - failedBuilds.length).toBeGreaterThanOrEqual(1); |
| 576 | expect(recoveredBuilds.length - failedBuilds.length).toBeLessThanOrEqual(6); |
| 577 | expect(recoveredBuilds.filter((build) => build.status === "Completed")).toHaveLength(1); |
| 578 | const shared = yield* stack.deploy(program(true)); |
| 579 | assert(shared.second); |
| 580 | expect(shared.first.applicationId).toBe(recovered.first.applicationId); |
| 581 | expect(shared.second.applicationId).not.toBe(shared.first.applicationId); |
| 582 | expect(shared.second.configuration.image).toBe(shared.first.configuration.image); |
| 583 | expect(yield* buildHistory).toEqual(recoveredBuilds); |
| 584 | for (const app of [shared.first, shared.second]) { |
| 585 | const observed = yield* Containers.getContainerApplication({ |
| 586 | accountId: app.accountId, |
| 587 | applicationId: app.applicationId, |
| 588 | }).pipe(Effect.map(scheduledApplication)); |
| 589 | expect(observed.configuration.image).toBe(shared.first.configuration.image); |
| 590 | } |
| 591 | yield* stack.destroy(); |
| 592 | for (const app of [shared.first, shared.second]) { |
| 593 | const deleted = yield* Containers.getContainerApplication({ |
| 594 | accountId: app.accountId, |
| 595 | applicationId: app.applicationId, |
| 596 | }).pipe( |
| 597 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 598 | Effect.repeat({ |
| 599 | schedule: Schedule.spaced("1 second"), |
| 600 | until: (app) => app === undefined, |
| 601 | times: 8, |
| 602 | }), |
| 603 | ); |
| 604 | expect(deleted).toBeUndefined(); |
| 605 | } |
| 606 | }).pipe(withBuilder(`alchemy-publication-${failure}`), logLevel), |
| 607 | { timeout: 120_000 }, |
| 608 | ); |
| 609 | } |
| 610 | |
| 611 | test.provider( |
| 612 | "shares publication without mixing contexts or application settings", |
| 613 | (stack) => |
| 614 | Effect.gen(function* () { |
| 615 | yield* stack.destroy(); |
| 616 | const fs = yield* FileSystem.FileSystem; |
| 617 | const path = yield* Path.Path; |
| 618 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-publication-" }); |
| 619 | const contexts = { |
| 620 | shared: path.join(root, "shared"), |
| 621 | other: path.join(root, "other"), |
| 622 | changed: path.join(root, "changed"), |
| 623 | }; |
| 624 | for (const [kind, context] of Object.entries(contexts)) { |
| 625 | yield* fs.makeDirectory(context); |
| 626 | yield* fs.writeFileString(path.join(context, ".gitignore"), "payload.txt\n"); |
| 627 | yield* fs.writeFileString(path.join(context, "payload.txt"), kind); |
| 628 | yield* fs.writeFileString(path.join(context, "input.txt"), "first"); |
| 629 | yield* fs.writeFileString( |
| 630 | path.join(context, "Dockerfile"), |
| 631 | [ |
| 632 | "FROM alpine:3.19", |
| 633 | "COPY payload.txt input.txt /", |
| 634 | 'CMD ["sleep", "3600"]', |
| 635 | ...(kind === "changed" ? ["LABEL variant=changed"] : []), |
| 636 | ].join("\n"), |
| 637 | ); |
| 638 | } |
| 639 | const program = publicationApplications(contexts); |
| 640 | const first = yield* stack.deploy(program); |
| 641 | const history = yield* buildHistory; |
| 642 | expect(history.filter((build) => build.status === "Completed")).toHaveLength(3); |
| 643 | // Each distinct publication can make at most six export attempts. |
| 644 | expect(history.length).toBeLessThanOrEqual(18); |
| 645 | expect(first.first.hash?.image).not.toBe(first.other.hash?.image); |
| 646 | expect(first.first.configuration.image).toBe(first.second.configuration.image); |
| 647 | expect(first.first.configuration.image).not.toBe(first.other.configuration.image); |
| 648 | expect(first.first.hash?.image).not.toBe(first.changed.hash?.image); |
| 649 | expect(first.first.applicationId).not.toBe(first.second.applicationId); |
| 650 | for (const [slot, app] of [ |
| 651 | ["first", first.first], |
| 652 | ["second", first.second], |
| 653 | ] as const) { |
| 654 | const observed = yield* Containers.getContainerApplication({ |
| 655 | accountId: app.accountId, |
| 656 | applicationId: app.applicationId, |
| 657 | }).pipe(Effect.map(scheduledApplication)); |
| 658 | expect(observed.configuration.image).toBe(first.first.configuration.image); |
| 659 | expect(observed.configuration.environmentVariables).toContainEqual({ |
| 660 | name: "SLOT", |
| 661 | value: slot, |
| 662 | }); |
| 663 | expect(observed.maxInstances).toBe(slot === "first" ? 2 : 3); |
| 664 | } |
| 665 | const unchanged = yield* stack.deploy(program); |
| 666 | expect(unchanged.first.configuration.image).toBe(first.first.configuration.image); |
| 667 | expect(yield* buildHistory).toEqual(history); |
| 668 | |
| 669 | yield* fs.writeFileString(path.join(contexts.shared, "input.txt"), "second"); |
| 670 | const updated = yield* stack.deploy(program); |
| 671 | const updateHistory = (yield* buildHistory).filter( |
| 672 | (build) => !history.some((previous) => previous.ref === build.ref), |
| 673 | ); |
| 674 | expect(updateHistory.filter((build) => build.status === "Completed")).toHaveLength(1); |
| 675 | expect(updateHistory.length).toBeLessThanOrEqual(6); |
| 676 | expect(updated.first.applicationId).toBe(first.first.applicationId); |
| 677 | expect(updated.second.applicationId).toBe(first.second.applicationId); |
| 678 | expect(updated.first.configuration.image).toBe(updated.second.configuration.image); |
| 679 | expect(updated.first.hash?.digest).not.toBe(first.first.hash?.digest); |
| 680 | expect(updated.other.configuration.image).toBe(first.other.configuration.image); |
| 681 | expect(updated.changed.configuration.image).toBe(first.changed.configuration.image); |
| 682 | yield* stack.destroy(); |
| 683 | for (const app of Object.values(updated)) { |
| 684 | const deleted = yield* Containers.getContainerApplication({ |
| 685 | accountId: app.accountId, |
| 686 | applicationId: app.applicationId, |
| 687 | }).pipe( |
| 688 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 689 | Effect.repeat({ |
| 690 | schedule: Schedule.spaced("1 second"), |
| 691 | until: (app) => app === undefined, |
| 692 | times: 8, |
| 693 | }), |
| 694 | ); |
| 695 | expect(deleted).toBeUndefined(); |
| 696 | } |
| 697 | }).pipe(withBuilder("alchemy-publication-cache"), logLevel), |
| 698 | { timeout: 120_000 }, |
| 699 | ); |
| 700 | |
| 701 | registryCacheTest.provider( |
| 702 | "caches default publications and imports repository layers in a fresh builder", |
| 703 | (stack) => |
| 704 | Effect.gen(function* () { |
| 705 | yield* stack.destroy(); |
| 706 | const fs = yield* FileSystem.FileSystem; |
| 707 | const path = yield* Path.Path; |
| 708 | const docker = yield* Docker; |
| 709 | const context = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-default-cache-" }); |
| 710 | yield* fs.writeFileString( |
| 711 | path.join(context, "Dockerfile"), |
| 712 | 'FROM alpine:3.19\nRUN cat /proc/sys/kernel/random/uuid > /layer\nCOPY payload /payload\nCMD ["sleep", "3600"]\n', |
| 713 | ); |
| 714 | yield* fs.writeFileString(path.join(context, "payload"), "first"); |
| 715 | const program = sharedApplication(context); |
| 716 | const first = yield* stack.deploy(program); |
| 717 | expect(first.app.configuration.image).toContain(`/${first.app.applicationName}@sha256:`); |
| 718 | const firstHistory = yield* buildHistory; |
| 719 | expect(firstHistory.filter((build) => build.status === "Completed")).toHaveLength(1); |
| 720 | |
| 721 | const credentials = yield* Containers.createContainerRegistryCredentials({ |
| 722 | accountId: first.app.accountId, |
| 723 | registryId: "registry.cloudflare.com", |
| 724 | permissions: ["pull"], |
| 725 | expirationMinutes: 15, |
| 726 | }); |
| 727 | const username = credentials.username ?? credentials.user; |
| 728 | assert(username); |
| 729 | const client = yield* HttpClient.HttpClient; |
| 730 | const cacheDigest = Effect.gen(function* () { |
| 731 | const response = yield* client.execute( |
| 732 | HttpClientRequest.head( |
| 733 | `https://registry.cloudflare.com/v2/${first.app.accountId}/${first.app.applicationName}/manifests/buildcache`, |
| 734 | ).pipe( |
| 735 | HttpClientRequest.basicAuth(username, credentials.password), |
| 736 | HttpClientRequest.setHeader( |
| 737 | "Accept", |
| 738 | "application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json", |
| 739 | ), |
| 740 | ), |
| 741 | ); |
| 742 | expect(response.status).toBe(200); |
| 743 | return response.headers["docker-content-digest"]; |
| 744 | }); |
| 745 | expect(yield* cacheDigest).toBe(first.app.hash?.digest); |
| 746 | const layersOf = Effect.fn(function* (digest: string) { |
| 747 | const response = yield* client.execute( |
| 748 | HttpClientRequest.get( |
| 749 | `https://registry.cloudflare.com/v2/${first.app.accountId}/${first.app.applicationName}/manifests/${digest}`, |
| 750 | ).pipe( |
| 751 | HttpClientRequest.basicAuth(username, credentials.password), |
| 752 | HttpClientRequest.setHeader( |
| 753 | "Accept", |
| 754 | "application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json", |
| 755 | ), |
| 756 | ), |
| 757 | ); |
| 758 | expect(response.status).toBe(200); |
| 759 | const manifest = yield* response.json.pipe( |
| 760 | Effect.flatMap( |
| 761 | Schema.decodeUnknownEffect( |
| 762 | Schema.Struct({ layers: Schema.Array(Schema.Struct({ digest: Schema.String })) }), |
| 763 | ), |
| 764 | ), |
| 765 | ); |
| 766 | return manifest.layers.map((layer) => layer.digest); |
| 767 | }); |
| 768 | const firstLayers = yield* layersOf(first.app.hash!.digest!); |
| 769 | expect(firstLayers.length).toBeGreaterThanOrEqual(2); |
| 770 | |
| 771 | yield* fs.writeFileString(path.join(context, "payload"), "changed"); |
| 772 | const changed = yield* Effect.gen(function* () { |
| 773 | // A new BuildKit instance cannot reuse the first builder's local layers. |
| 774 | expect(yield* buildHistory).toHaveLength(0); |
| 775 | const changed = yield* stack.deploy(program); |
| 776 | const completed = (yield* buildHistory).filter((build) => build.status === "Completed"); |
| 777 | expect(completed).toHaveLength(1); |
| 778 | const log = yield* docker.run([ |
| 779 | "buildx", |
| 780 | "history", |
| 781 | "logs", |
| 782 | completed[0]!.ref.split("/").at(-1)!, |
| 783 | "--progress", |
| 784 | "plain", |
| 785 | ]); |
| 786 | const text = `${log.stdout}\n${log.stderr}`; |
| 787 | expect(text).toContain( |
| 788 | `importing cache manifest from registry.cloudflare.com/${first.app.accountId}/${first.app.applicationName}:buildcache`, |
| 789 | ); |
| 790 | const changedLayers = yield* layersOf(changed.app.hash!.digest!); |
| 791 | // Re-executing RUN would create a different UUID and therefore a different layer. |
| 792 | expect(changedLayers.slice(0, -1)).toEqual(firstLayers.slice(0, -1)); |
| 793 | expect(changedLayers.at(-1)).not.toBe(firstLayers.at(-1)); |
| 794 | return changed; |
| 795 | }).pipe(withBuilder("alchemy-default-cache-changed", { attestations: false })); |
| 796 | expect(changed.app.applicationId).toBe(first.app.applicationId); |
| 797 | expect(changed.app.applicationName).toBe(first.app.applicationName); |
| 798 | expect(changed.app.configuration.image).not.toBe(first.app.configuration.image); |
| 799 | // The registry keeps serving an overwritten tag's previous target |
| 800 | // for a while (observed ~25s), so wait for the new one. |
| 801 | expect( |
| 802 | yield* cacheDigest.pipe( |
| 803 | Effect.repeat({ |
| 804 | schedule: Schedule.spaced("4 seconds"), |
| 805 | until: (digest) => digest === changed.app.hash?.digest, |
| 806 | times: 10, |
| 807 | }), |
| 808 | ), |
| 809 | ).toBe(changed.app.hash?.digest); |
| 810 | |
| 811 | yield* fs.writeFileString(path.join(context, "payload"), "first"); |
| 812 | const restored = yield* stack |
| 813 | .deploy(program) |
| 814 | .pipe(withDockerEnv({ BUILDX_BUILDER: "alchemy-default-cache-must-not-build" })); |
| 815 | expect(restored.app.applicationId).toBe(first.app.applicationId); |
| 816 | expect(restored.app.configuration.image).toBe(first.app.configuration.image); |
| 817 | expect(yield* buildHistory).toEqual(firstHistory); |
| 818 | // Reusing a finished image leaves the latest exported layer cache in place. |
| 819 | expect(yield* cacheDigest).toBe(changed.app.hash?.digest); |
| 820 | expect( |
| 821 | (yield* waitForImage( |
| 822 | restored.app.accountId, |
| 823 | restored.app.applicationId, |
| 824 | restored.app.configuration.image!, |
| 825 | )).image, |
| 826 | ).toBe(first.app.configuration.image); |
| 827 | yield* stack.destroy(); |
| 828 | const gone = yield* Containers.getContainerApplication({ |
| 829 | accountId: first.app.accountId, |
| 830 | applicationId: first.app.applicationId, |
| 831 | }).pipe( |
| 832 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 833 | Effect.repeat({ |
| 834 | until: (app) => app === undefined, |
| 835 | schedule: Schedule.spaced("1 second"), |
| 836 | times: 8, |
| 837 | }), |
| 838 | ); |
| 839 | expect(gone).toBeUndefined(); |
| 840 | }).pipe(withBuilder("alchemy-default-cache", { attestations: false }), logLevel), |
| 841 | { timeout: 120_000 }, |
| 842 | ); |
| 843 | |
| 844 | test.provider( |
| 845 | "rebuilds a changed generated bundle in a fresh builder without pulling base layers", |
| 846 | (stack) => |
| 847 | Effect.gen(function* () { |
| 848 | yield* stack.destroy(); |
| 849 | const docker = yield* Docker; |
| 850 | const program = (live: typeof LinkedFirst) => |
| 851 | Effect.gen(function* () { |
| 852 | const app: Cloudflare.ContainerApplication = yield* LinkedContainer.Application; |
| 853 | return { app }; |
| 854 | }).pipe(Effect.provide(live)); |
| 855 | const first = yield* stack.deploy(program(LinkedFirst)); |
| 856 | |
| 857 | const changed = yield* Effect.gen(function* () { |
| 858 | const changed = yield* stack.deploy(program(LinkedChanged)); |
| 859 | const completed = (yield* buildHistory).filter((build) => build.status === "Completed"); |
| 860 | expect(completed).toHaveLength(1); |
| 861 | const log = yield* docker.run([ |
| 862 | "buildx", |
| 863 | "history", |
| 864 | "logs", |
| 865 | completed[0]!.ref.split("/").at(-1)!, |
| 866 | "--progress", |
| 867 | "plain", |
| 868 | ]); |
| 869 | const text = `${log.stdout}\n${log.stderr}`; |
| 870 | expect(text).toContain("importing cache manifest from registry.cloudflare.com/"); |
| 871 | // A plain COPY applies onto the base snapshot, so BuildKit |
| 872 | // downloads and extracts every cached base layer first. A linked |
| 873 | // copy is exported on its own and the base layers stay remote. |
| 874 | expect(text).not.toMatch(/extracting sha256:/); |
| 875 | return changed; |
| 876 | }).pipe(withBuilder("alchemy-linked-copy-changed", { attestations: false })); |
| 877 | expect(changed.app.applicationId).toBe(first.app.applicationId); |
| 878 | expect(changed.app.hash?.digest).not.toBe(first.app.hash?.digest); |
| 879 | |
| 880 | yield* stack.destroy(); |
| 881 | const gone = yield* Containers.getContainerApplication({ |
| 882 | accountId: first.app.accountId, |
| 883 | applicationId: first.app.applicationId, |
| 884 | }).pipe( |
| 885 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 886 | Effect.repeat({ |
| 887 | until: (app) => app === undefined, |
| 888 | schedule: Schedule.spaced("1 second"), |
| 889 | times: 8, |
| 890 | }), |
| 891 | ); |
| 892 | expect(gone).toBeUndefined(); |
| 893 | }).pipe(withBuilder("alchemy-linked-copy", { attestations: false }), logLevel), |
| 894 | { timeout: 180_000 }, |
| 895 | ); |
| 896 | |
| 897 | // Publication reuse was confined to a provider instance in: |
| 898 | // https://github.com/alchemy-run/alchemy/commit/275361dfe8358684439e42120298723305175d39 |
| 899 | registryCacheTest.provider( |
| 900 | "reuses registry images across fresh providers and invalidates changed build inputs", |
| 901 | (stack) => |
| 902 | Effect.gen(function* () { |
| 903 | yield* stack.destroy(); |
| 904 | const fs = yield* FileSystem.FileSystem; |
| 905 | const path = yield* Path.Path; |
| 906 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-registry-cache-" }); |
| 907 | const context = path.join(root, "context"); |
| 908 | const repositoryName = `alchemy-registry-cache-${stack.stage.toLowerCase()}`; |
| 909 | yield* fs.makeDirectory(context); |
| 910 | yield* fs.writeFileString(path.join(root, "bun.lock"), "outside-first"); |
| 911 | yield* fs.writeFileString(path.join(context, ".gitignore"), "payload\n"); |
| 912 | yield* fs.writeFileString(path.join(context, ".dockerignore"), "ignored\n"); |
| 913 | yield* fs.writeFileString(path.join(context, "ignored"), "ignored-first"); |
| 914 | // Fresh payload bytes force a cold source tag even after a prior run. |
| 915 | yield* fs.writeFileString(path.join(context, "payload"), root); |
| 916 | yield* fs.writeFileString( |
| 917 | path.join(context, "Dockerfile"), |
| 918 | 'FROM alpine:3.19\nCOPY payload /payload\nCMD ["sleep", "3600"]\n', |
| 919 | ); |
| 920 | const deploy = (id: string, repository = repositoryName) => |
| 921 | stack.deploy( |
| 922 | Effect.gen(function* () { |
| 923 | return { |
| 924 | app: yield* Cloudflare.Container(id, { context, publish: { repository } }) |
| 925 | .Application, |
| 926 | }; |
| 927 | }), |
| 928 | ); |
| 929 | |
| 930 | const first = yield* deploy("First"); |
| 931 | const history = yield* buildHistory; |
| 932 | expect(history.filter((build) => build.status === "Completed")).toHaveLength(1); |
| 933 | expect(first.app.configuration.image).toContain(`/${repositoryName}@sha256:`); |
| 934 | const credentials = yield* Containers.createContainerRegistryCredentials({ |
| 935 | accountId: first.app.accountId, |
| 936 | registryId: "registry.cloudflare.com", |
| 937 | permissions: ["pull"], |
| 938 | expirationMinutes: 15, |
| 939 | }); |
| 940 | const username = credentials.username ?? credentials.user; |
| 941 | assert(username); |
| 942 | const http = yield* HttpClient.HttpClient; |
| 943 | // The registry keeps serving an overwritten tag's previous target |
| 944 | // for a while (observed ~25s), so wait for the new one. |
| 945 | const cached = yield* http |
| 946 | .execute( |
| 947 | HttpClientRequest.head( |
| 948 | `https://registry.cloudflare.com/v2/${first.app.accountId}/${repositoryName}/manifests/buildcache`, |
| 949 | ).pipe( |
| 950 | HttpClientRequest.basicAuth(username, credentials.password), |
| 951 | HttpClientRequest.setHeader( |
| 952 | "Accept", |
| 953 | "application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json", |
| 954 | ), |
| 955 | ), |
| 956 | ) |
| 957 | .pipe( |
| 958 | Effect.repeat({ |
| 959 | schedule: Schedule.spaced("4 seconds"), |
| 960 | until: (response) => |
| 961 | response.headers["docker-content-digest"] === first.app.hash?.digest, |
| 962 | times: 10, |
| 963 | }), |
| 964 | ); |
| 965 | expect(cached.status).toBe(200); |
| 966 | expect(cached.headers["docker-content-digest"]).toBe(first.app.hash?.digest); |
| 967 | |
| 968 | yield* stack.destroy(); |
| 969 | yield* fs.writeFileString(path.join(root, "bun.lock"), "outside-second"); |
| 970 | yield* fs.writeFileString(path.join(context, "ignored"), "ignored-second"); |
| 971 | const second = yield* deploy("Second"); |
| 972 | expect(yield* buildHistory).toEqual(history); |
| 973 | expect(second.app.applicationId).not.toBe(first.app.applicationId); |
| 974 | expect(second.app.configuration.image).toBe(first.app.configuration.image); |
| 975 | |
| 976 | // Git ignores this file, but Docker copies it into the image. |
| 977 | yield* fs.writeFileString(path.join(context, "payload"), `${root}-changed`); |
| 978 | const changed = yield* deploy("Second"); |
| 979 | expect(changed.app.hash?.digest).not.toBe(second.app.hash?.digest); |
| 980 | expect( |
| 981 | (yield* buildHistory).filter((build) => build.status === "Completed"), |
| 982 | ).toHaveLength(2); |
| 983 | |
| 984 | const moved = yield* deploy("Second", `${repositoryName}-other`); |
| 985 | expect(moved.app.configuration.image).toContain(`/${repositoryName}-other@sha256:`); |
| 986 | expect( |
| 987 | (yield* buildHistory).filter((build) => build.status === "Completed"), |
| 988 | ).toHaveLength(3); |
| 989 | yield* stack.destroy(); |
| 990 | const deleted = yield* Containers.getContainerApplication({ |
| 991 | accountId: moved.app.accountId, |
| 992 | applicationId: moved.app.applicationId, |
| 993 | }).pipe( |
| 994 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 995 | Effect.repeat({ |
| 996 | schedule: Schedule.spaced("1 second"), |
| 997 | until: (app) => app === undefined, |
| 998 | times: 8, |
| 999 | }), |
| 1000 | ); |
| 1001 | expect(deleted).toBeUndefined(); |
| 1002 | }).pipe(withBuilder("alchemy-registry-cache"), logLevel), |
| 1003 | { timeout: 180_000 }, |
| 1004 | ); |
| 1005 | |
| 1006 | test.provider( |
| 1007 | "reuses shared registry images across stages without a builder and moves repositories with an unchanged digest", |
| 1008 | (stack) => |
| 1009 | Effect.gen(function* () { |
| 1010 | yield* stack.destroy(); |
| 1011 | const fs = yield* FileSystem.FileSystem; |
| 1012 | const path = yield* Path.Path; |
| 1013 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-shared-publication-" }); |
| 1014 | const context = path.join(root, "first"); |
| 1015 | const otherContext = path.join(root, "second"); |
| 1016 | for (const dir of [context, otherContext]) { |
| 1017 | yield* fs.makeDirectory(dir); |
| 1018 | yield* fs.writeFileString(path.join(dir, "payload.txt"), "first"); |
| 1019 | yield* fs.writeFileString( |
| 1020 | path.join(dir, "Dockerfile"), |
| 1021 | 'FROM alpine:3.19\nCOPY payload.txt /payload.txt\nCMD ["sleep", "3600"]\n', |
| 1022 | ); |
| 1023 | } |
| 1024 | const initial = yield* stack.deploy(sharedApplication(context)); |
| 1025 | const repositoryName = initial.app.applicationName; |
| 1026 | const shared = yield* stack.deploy(sharedApplication(context, repositoryName)); |
| 1027 | expect(shared.app.applicationId).toBe(initial.app.applicationId); |
| 1028 | const history = yield* buildHistory; |
| 1029 | expect(history.filter((build) => build.status === "Completed")).toHaveLength(2); |
| 1030 | |
| 1031 | const secondStage = { |
| 1032 | stage: `${stack.stage}-shared`, |
| 1033 | stack: Stack( |
| 1034 | stack.name, |
| 1035 | { providers: Layer.fresh(Cloudflare.providers()), state: stack.state }, |
| 1036 | sharedApplication(otherContext, repositoryName), |
| 1037 | ), |
| 1038 | }; |
| 1039 | yield* Destroy.destroy(secondStage); |
| 1040 | const reused = yield* Effect.gen(function* () { |
| 1041 | // A fresh provider and an unusable builder rule out process-local reuse. |
| 1042 | const reused = yield* Deploy.deploy(secondStage).pipe( |
| 1043 | withDockerEnv({ BUILDX_BUILDER: "alchemy-shared-cache-must-not-build" }), |
| 1044 | ); |
| 1045 | expect(reused.app.applicationId).not.toBe(shared.app.applicationId); |
| 1046 | expect(reused.app.hash?.image).toBe(shared.app.hash?.image); |
| 1047 | expect(reused.app.configuration.image).toBe(shared.app.configuration.image); |
| 1048 | expect(yield* buildHistory).toEqual(history); |
| 1049 | expect((yield* live(reused.app.accountId, reused.app.applicationId)).image).toBe( |
| 1050 | shared.app.configuration.image, |
| 1051 | ); |
| 1052 | |
| 1053 | const moved = yield* stack.deploy( |
| 1054 | sharedApplication(context, `${repositoryName}-moved`), |
| 1055 | ); |
| 1056 | expect(moved.app.applicationId).toBe(shared.app.applicationId); |
| 1057 | expect(moved.app.hash?.digest).toBe(shared.app.hash?.digest); |
| 1058 | expect(moved.app.configuration.image).toContain(`/${repositoryName}-moved@sha256:`); |
| 1059 | expect( |
| 1060 | (yield* waitForImage( |
| 1061 | moved.app.accountId, |
| 1062 | moved.app.applicationId, |
| 1063 | moved.app.configuration.image!, |
| 1064 | )).image, |
| 1065 | ).toBe(moved.app.configuration.image); |
| 1066 | expect((yield* live(reused.app.accountId, reused.app.applicationId)).image).toBe( |
| 1067 | shared.app.configuration.image, |
| 1068 | ); |
| 1069 | return reused; |
| 1070 | }).pipe(Effect.ensuring(Destroy.destroy(secondStage).pipe(Effect.orDie))); |
| 1071 | yield* stack.destroy(); |
| 1072 | for (const app of [shared.app, reused.app]) { |
| 1073 | const deleted = yield* Containers.getContainerApplication({ |
| 1074 | accountId: app.accountId, |
| 1075 | applicationId: app.applicationId, |
| 1076 | }).pipe( |
| 1077 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 1078 | Effect.repeat({ |
| 1079 | schedule: Schedule.spaced("1 second"), |
| 1080 | until: (app) => app === undefined, |
| 1081 | times: 8, |
| 1082 | }), |
| 1083 | ); |
| 1084 | expect(deleted).toBeUndefined(); |
| 1085 | } |
| 1086 | }).pipe( |
| 1087 | // Timestamped provenance would give identical image builds different digests. |
| 1088 | withBuilder("alchemy-shared-registry-cache", { attestations: false }), |
| 1089 | logLevel, |
| 1090 | ), |
| 1091 | { timeout: 120_000 }, |
| 1092 | ); |
| 1093 | |
| 1094 | for (const fixture of [ |
| 1095 | { |
| 1096 | name: "Dockerfile", |
| 1097 | program: Effect.gen(function* () { |
| 1098 | const worker = yield* ExternalContainerWorker; |
| 1099 | const app: Cloudflare.ContainerApplication = yield* ExternalContainer.Application; |
| 1100 | return { app, url: worker.url.as<string>() }; |
| 1101 | }), |
| 1102 | route: "/hello", |
| 1103 | response: "hello from external container", |
| 1104 | }, |
| 1105 | { |
| 1106 | name: "generated", |
| 1107 | program: Effect.gen(function* () { |
| 1108 | const worker = yield* EffectfulContainerWorker; |
| 1109 | const app: Cloudflare.ContainerApplication = yield* MyContainer.Application; |
| 1110 | return { app, url: worker.url.as<string>() }; |
| 1111 | }).pipe(Effect.provide(MyContainerLive)), |
| 1112 | route: "/ping", |
| 1113 | response: "pong", |
| 1114 | }, |
| 1115 | ]) { |
| 1116 | test.provider( |
| 1117 | `exports ${fixture.name} builds from a non-loading builder to the registry`, |
| 1118 | (stack) => |
| 1119 | Effect.gen(function* () { |
| 1120 | yield* stack.destroy(); |
| 1121 | const docker = yield* Docker; |
| 1122 | const fs = yield* FileSystem.FileSystem; |
| 1123 | const path = yield* Path.Path; |
| 1124 | const configDir = yield* Config.String("DOCKER_CONFIG").pipe( |
| 1125 | Config.orElse(() => |
| 1126 | Config.String("HOME").pipe(Config.map((home) => path.join(home, ".docker"))), |
| 1127 | ), |
| 1128 | ); |
| 1129 | const configFile = path.join(configDir, "config.json"); |
| 1130 | const readConfig = fs |
| 1131 | .readFileString(configFile) |
| 1132 | .pipe( |
| 1133 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)), |
| 1134 | ); |
| 1135 | const configBefore = yield* readConfig; |
| 1136 | const builder = `alchemy-registry-export-${fixture.name.toLowerCase()}`; |
| 1137 | const selected = yield* docker.run(["buildx", "inspect"]); |
| 1138 | expect(selected.stdout).toMatch(/Driver:\s+docker-container/); |
| 1139 | expect(selected.stdout).toContain(builder); |
| 1140 | |
| 1141 | const deployed = yield* stack.deploy(fixture.program); |
| 1142 | const tag = taggedRefOf(deployed.app); |
| 1143 | const local = yield* docker.image |
| 1144 | .inspect(tag) |
| 1145 | .pipe( |
| 1146 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)), |
| 1147 | ); |
| 1148 | // Buildx 0.26+ exports from BuildKit straight to the registry, so |
| 1149 | // the image never enters the local store. Older plugins `--load` |
| 1150 | // it and `docker push` from there. |
| 1151 | const exported = yield* supportsRegistryExport; |
| 1152 | if (exported) { |
| 1153 | expect(local).toBeUndefined(); |
| 1154 | } else { |
| 1155 | expect(local).toBeDefined(); |
| 1156 | } |
| 1157 | expect(deployed.app.hash?.digest).toMatch(/^sha256:[a-f0-9]{64}$/); |
| 1158 | |
| 1159 | const { accountId, applicationId } = deployed.app; |
| 1160 | const credentials = yield* Containers.createContainerRegistryCredentials({ |
| 1161 | accountId, |
| 1162 | registryId: "registry.cloudflare.com", |
| 1163 | permissions: ["pull"], |
| 1164 | expirationMinutes: 15, |
| 1165 | }); |
| 1166 | const username = credentials.username ?? credentials.user; |
| 1167 | assert(username); |
| 1168 | const client = yield* HttpClient.HttpClient; |
| 1169 | const repository = tag.slice("registry.cloudflare.com/".length, tag.lastIndexOf(":")); |
| 1170 | const manifest = yield* client.execute( |
| 1171 | HttpClientRequest.get( |
| 1172 | `https://registry.cloudflare.com/v2/${repository}/manifests/${deployed.app.hash!.digest}`, |
| 1173 | ).pipe( |
| 1174 | HttpClientRequest.basicAuth(username, credentials.password), |
| 1175 | HttpClientRequest.setHeader( |
| 1176 | "Accept", |
| 1177 | "application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json", |
| 1178 | ), |
| 1179 | ), |
| 1180 | ); |
| 1181 | expect(manifest.status).toBe(200); |
| 1182 | expect(manifest.headers["docker-content-digest"]).toBe(deployed.app.hash!.digest); |
| 1183 | const cache = yield* client.execute( |
| 1184 | HttpClientRequest.head( |
| 1185 | `https://registry.cloudflare.com/v2/${repository}/manifests/buildcache`, |
| 1186 | ).pipe( |
| 1187 | HttpClientRequest.basicAuth(username, credentials.password), |
| 1188 | HttpClientRequest.setHeader( |
| 1189 | "Accept", |
| 1190 | "application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json", |
| 1191 | ), |
| 1192 | ), |
| 1193 | ); |
| 1194 | expect(cache.status).toBe(200); |
| 1195 | expect(cache.headers["docker-content-digest"]).toBe(deployed.app.hash!.digest); |
| 1196 | if (exported) { |
| 1197 | // BuildKit's registry exporter publishes an OCI index (with |
| 1198 | // attestations) carrying the requested platform. |
| 1199 | const index = yield* manifest.json.pipe( |
| 1200 | Effect.flatMap( |
| 1201 | Schema.decodeUnknownEffect( |
| 1202 | Schema.Struct({ |
| 1203 | mediaType: Schema.String, |
| 1204 | manifests: Schema.Array( |
| 1205 | Schema.Struct({ |
| 1206 | platform: Schema.Struct({ |
| 1207 | os: Schema.String, |
| 1208 | architecture: Schema.String, |
| 1209 | }), |
| 1210 | }), |
| 1211 | ), |
| 1212 | }), |
| 1213 | ), |
| 1214 | ), |
| 1215 | ); |
| 1216 | expect(index.mediaType).toBe("application/vnd.oci.image.index.v1+json"); |
| 1217 | expect( |
| 1218 | index.manifests.some( |
| 1219 | ({ platform }) => platform.os === "linux" && platform.architecture === "amd64", |
| 1220 | ), |
| 1221 | ).toBe(true); |
| 1222 | } else { |
| 1223 | // `docker push --platform` ships the single platform variant. |
| 1224 | const single = yield* manifest.json.pipe( |
| 1225 | Effect.flatMap( |
| 1226 | Schema.decodeUnknownEffect(Schema.Struct({ mediaType: Schema.String })), |
| 1227 | ), |
| 1228 | ); |
| 1229 | expect([ |
| 1230 | "application/vnd.oci.image.manifest.v1+json", |
| 1231 | "application/vnd.docker.distribution.manifest.v2+json", |
| 1232 | ]).toContain(single.mediaType); |
| 1233 | } |
| 1234 | const observed = yield* Containers.getContainerApplication({ |
| 1235 | accountId, |
| 1236 | applicationId, |
| 1237 | }).pipe(Effect.map(scheduledApplication)); |
| 1238 | expect(observed.configuration.image).toBe(deployed.app.configuration.image); |
| 1239 | |
| 1240 | const response = yield* Effect.gen(function* () { |
| 1241 | const response = yield* client.get(`${deployed.url}${fixture.route}`); |
| 1242 | return { status: response.status, body: yield* response.text }; |
| 1243 | }).pipe( |
| 1244 | Effect.timeout("45 seconds"), |
| 1245 | Effect.repeat({ |
| 1246 | schedule: Schedule.spaced("3 seconds"), |
| 1247 | until: (response) => |
| 1248 | response.status === 200 && response.body.includes(fixture.response), |
| 1249 | times: 8, |
| 1250 | }), |
| 1251 | ); |
| 1252 | expect(response.status).toBe(200); |
| 1253 | expect(response.body).toContain(fixture.response); |
| 1254 | expect(yield* readConfig).toBe(configBefore); |
| 1255 | const unchanged = yield* stack.deploy(fixture.program); |
| 1256 | expect(unchanged.app.applicationId).toBe(applicationId); |
| 1257 | expect(unchanged.app.hash).toEqual(deployed.app.hash); |
| 1258 | |
| 1259 | yield* stack.destroy(); |
| 1260 | const deleted = yield* Containers.getContainerApplication({ |
| 1261 | accountId, |
| 1262 | applicationId, |
| 1263 | }).pipe( |
| 1264 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 1265 | Effect.repeat({ |
| 1266 | schedule: Schedule.spaced("1 second"), |
| 1267 | until: (app) => app === undefined, |
| 1268 | times: 8, |
| 1269 | }), |
| 1270 | ); |
| 1271 | expect(deleted).toBeUndefined(); |
| 1272 | }).pipe(withBuilder(`alchemy-registry-export-${fixture.name.toLowerCase()}`), logLevel), |
| 1273 | { |
| 1274 | tags: ["provider:cloudflare:r2", "provider:cloudflare:worker"], |
| 1275 | timeout: 120_000, |
| 1276 | }, |
| 1277 | ); |
| 1278 | } |
| 1279 | |
| 1280 | for (const maxInstances of [2, 4]) { |
| 1281 | for (const field of ["applicationId", "applicationName", "accountId"] as const) { |
| 1282 | test.provider( |
| 1283 | `plans replacement for cached ${field} mismatches with ${maxInstances === 2 ? "unchanged" : "changed"} props`, |
| 1284 | (stack) => |
| 1285 | Effect.gen(function* () { |
| 1286 | yield* stack.destroy(); |
| 1287 | |
| 1288 | const first = yield* stack.deploy(applications()); |
| 1289 | const { accountId } = first.owned; |
| 1290 | const observed = yield* Effect.forEach([first.owned, first.other], (app) => |
| 1291 | Containers.getContainerApplication({ |
| 1292 | accountId, |
| 1293 | applicationId: app.applicationId, |
| 1294 | }).pipe(Effect.map(scheduledApplication)), |
| 1295 | ); |
| 1296 | const state = yield* yield* State; |
| 1297 | const key = { stack: stack.name, stage: stack.stage, fqn: "CachedIdentity" }; |
| 1298 | const row = yield* state.get(key); |
| 1299 | assert(row?.status === "created" || row?.status === "updated"); |
| 1300 | |
| 1301 | yield* Effect.gen(function* () { |
| 1302 | const inconsistent = { |
| 1303 | ...row, |
| 1304 | attr: { |
| 1305 | ...row.attr, |
| 1306 | [field]: |
| 1307 | field === "accountId" |
| 1308 | ? "00000000000000000000000000000000" |
| 1309 | : first.other[field], |
| 1310 | }, |
| 1311 | }; |
| 1312 | yield* state.set({ ...key, value: inconsistent }); |
| 1313 | |
| 1314 | const plan = yield* stack.plan(applications(maxInstances)); |
| 1315 | expect(plan.resources.CachedIdentity.action).toBe("replace"); |
| 1316 | expect(plan.resources.CachedIdentity.state).toEqual(inconsistent); |
| 1317 | expect(plan.resources.OtherIdentity.action).toBe("noop"); |
| 1318 | expect(yield* state.get(key)).toEqual(inconsistent); |
| 1319 | |
| 1320 | for (const before of observed) { |
| 1321 | const after = yield* Containers.getContainerApplication({ |
| 1322 | accountId, |
| 1323 | applicationId: before.id, |
| 1324 | }); |
| 1325 | expect(after).toMatchObject({ |
| 1326 | id: before.id, |
| 1327 | name: before.name, |
| 1328 | version: before.version, |
| 1329 | maxInstances: before.maxInstances, |
| 1330 | configuration: before.configuration, |
| 1331 | }); |
| 1332 | } |
| 1333 | }).pipe( |
| 1334 | // Restore the real identity before teardown, even on failure. |
| 1335 | Effect.ensuring(state.set({ ...key, value: row }).pipe(Effect.orDie)), |
| 1336 | ); |
| 1337 | |
| 1338 | const checked = yield* Drift.detect(stack); |
| 1339 | expect(checked.resources.CachedIdentity.attr).toMatchObject({ |
| 1340 | applicationId: first.owned.applicationId, |
| 1341 | applicationName: first.owned.applicationName, |
| 1342 | accountId, |
| 1343 | }); |
| 1344 | const recovered = yield* stack.deploy(applications()); |
| 1345 | expect(recovered.owned.applicationId).toBe(first.owned.applicationId); |
| 1346 | expect(recovered.other.applicationId).toBe(first.other.applicationId); |
| 1347 | |
| 1348 | yield* stack.destroy(); |
| 1349 | for (const app of [first.owned, first.other]) { |
| 1350 | const deleted = yield* Containers.getContainerApplication({ |
| 1351 | accountId, |
| 1352 | applicationId: app.applicationId, |
| 1353 | }).pipe( |
| 1354 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 1355 | Effect.repeat({ |
| 1356 | schedule: Schedule.spaced("1 second"), |
| 1357 | until: (app) => app === undefined, |
| 1358 | times: 8, |
| 1359 | }), |
| 1360 | ); |
| 1361 | expect(deleted).toBeUndefined(); |
| 1362 | } |
| 1363 | }).pipe(logLevel), |
| 1364 | { timeout: 120_000 }, |
| 1365 | ); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | test.provider( |
| 1370 | "replaces the fixture when its configured name changes", |
| 1371 | (stack) => |
| 1372 | Effect.gen(function* () { |
| 1373 | yield* stack.destroy(); |
| 1374 | const first = yield* stack.deploy(applications()); |
| 1375 | const name = `renamed-${first.owned.applicationName.slice(-24)}`; |
| 1376 | const renamed = applications(2, name); |
| 1377 | |
| 1378 | const scaling = yield* stack.plan(applications(4)); |
| 1379 | expect(scaling.resources.CachedIdentity.action).toBe("update"); |
| 1380 | const plan = yield* stack.plan(renamed); |
| 1381 | expect(plan.resources.CachedIdentity.action).toBe("replace"); |
| 1382 | expect(plan.resources.OtherIdentity.action).toBe("noop"); |
| 1383 | const before = yield* Containers.getContainerApplication({ |
| 1384 | accountId: first.owned.accountId, |
| 1385 | applicationId: first.owned.applicationId, |
| 1386 | }).pipe(Effect.map(scheduledApplication)); |
| 1387 | expect(before.name).toBe(first.owned.applicationName); |
| 1388 | expect(before.maxInstances).toBe(2); |
| 1389 | |
| 1390 | const second = yield* stack.deploy(renamed); |
| 1391 | expect(second.owned.applicationName).toBe(name); |
| 1392 | expect(second.owned.applicationId).not.toBe(first.owned.applicationId); |
| 1393 | expect(second.other.applicationId).toBe(first.other.applicationId); |
| 1394 | const observed = yield* Containers.getContainerApplication({ |
| 1395 | accountId: second.owned.accountId, |
| 1396 | applicationId: second.owned.applicationId, |
| 1397 | }); |
| 1398 | expect(observed.name).toBe(name); |
| 1399 | |
| 1400 | yield* stack.destroy(); |
| 1401 | for (const app of [first.owned, second.owned, second.other]) { |
| 1402 | const deleted = yield* Containers.getContainerApplication({ |
| 1403 | accountId: app.accountId, |
| 1404 | applicationId: app.applicationId, |
| 1405 | }).pipe( |
| 1406 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 1407 | Effect.repeat({ |
| 1408 | schedule: Schedule.spaced("1 second"), |
| 1409 | until: (app) => app === undefined, |
| 1410 | times: 8, |
| 1411 | }), |
| 1412 | ); |
| 1413 | expect(deleted).toBeUndefined(); |
| 1414 | } |
| 1415 | }).pipe(logLevel), |
| 1416 | { timeout: 120_000 }, |
| 1417 | ); |
| 1418 | |
| 1419 | test.provider( |
| 1420 | "forwards the fixture's memoryMib to Cloudflare", |
| 1421 | (stack) => |
| 1422 | Effect.gen(function* () { |
| 1423 | yield* stack.destroy(); |
| 1424 | |
| 1425 | const app = yield* stack.deploy(RemoteContainer.Application); |
| 1426 | expect(app.configuration.memoryMib).toBe(4096); |
| 1427 | const live = yield* Containers.getContainerApplication({ |
| 1428 | accountId: app.accountId, |
| 1429 | applicationId: app.applicationId, |
| 1430 | }).pipe(Effect.map(scheduledApplication)); |
| 1431 | expect(live.configuration.memoryMib).toBe(4096); |
| 1432 | expect(live.configuration.instanceType).not.toBe("lite"); |
| 1433 | |
| 1434 | yield* stack.destroy(); |
| 1435 | const deleted = yield* Containers.getContainerApplication({ |
| 1436 | accountId: app.accountId, |
| 1437 | applicationId: app.applicationId, |
| 1438 | }).pipe( |
| 1439 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 1440 | Effect.repeat({ |
| 1441 | schedule: Schedule.spaced("1 second"), |
| 1442 | until: (app) => app === undefined, |
| 1443 | times: 8, |
| 1444 | }), |
| 1445 | ); |
| 1446 | expect(deleted).toBeUndefined(); |
| 1447 | }).pipe(logLevel), |
| 1448 | { tags: ["provider:cloudflare:r2"], timeout: 120_000 }, |
| 1449 | ); |
| 1450 | |
| 1451 | // Canonical `list()` test (Cloudflare account collection, pattern (b)). |
| 1452 | // `listContainerApplications` returns the full application objects in one |
| 1453 | // (non-paginated) response, so `list()` maps each into the exact `read` |
| 1454 | // Attributes shape. Deploying a real container application requires a Docker |
| 1455 | // build + push to the Cloudflare registry (not feasible in this harness), so |
| 1456 | // this is a read-only enumeration assertion: the result is a well-typed array |
| 1457 | // (possibly empty on an account with no container applications) and every |
| 1458 | // element carries the full Attributes shape. |
| 1459 | test.provider("list enumerates container applications", (stack) => |
| 1460 | Effect.gen(function* () { |
| 1461 | yield* stack.destroy(); |
| 1462 | |
| 1463 | const provider = yield* Provider.findProvider(Cloudflare.Container); |
| 1464 | const all = yield* provider.list(); |
| 1465 | |
| 1466 | expect(Array.isArray(all)).toBe(true); |
| 1467 | for (const app of all) { |
| 1468 | expect(typeof app.applicationId).toBe("string"); |
| 1469 | expect(typeof app.applicationName).toBe("string"); |
| 1470 | expect(typeof app.accountId).toBe("string"); |
| 1471 | expect(app.configuration).toBeDefined(); |
| 1472 | } |
| 1473 | |
| 1474 | yield* stack.destroy(); |
| 1475 | }).pipe(logLevel), |
| 1476 | ); |
| 1477 | |
| 1478 | // Placement constraints pass through to Cloudflare unchanged and read back |
| 1479 | // stable, so an unchanged redeploy plans no update. Whether an instance |
| 1480 | // lands in the jurisdiction depends on account capacity; this pins the API |
| 1481 | // boundary. Adapted from #1340 (@yovanoc). |
| 1482 | test.provider( |
| 1483 | "placement constraints round-trip and stay stable", |
| 1484 | (stack) => |
| 1485 | Effect.gen(function* () { |
| 1486 | yield* stack.destroy(); |
| 1487 | |
| 1488 | const program = Effect.gen(function* () { |
| 1489 | return yield* Cloudflare.Container("ConstrainedPlacement", { |
| 1490 | image: "mendhak/http-https-echo:41", |
| 1491 | constraints: { jurisdiction: "eu", regions: ["WEUR", "EEUR"] }, |
| 1492 | }).Application; |
| 1493 | }); |
| 1494 | const app = yield* stack.deploy(program); |
| 1495 | expect(app.constraints).toEqual({ jurisdiction: "eu", regions: ["WEUR", "EEUR"] }); |
| 1496 | |
| 1497 | const observed = yield* Containers.getContainerApplication({ |
| 1498 | accountId: app.accountId, |
| 1499 | applicationId: app.applicationId, |
| 1500 | }); |
| 1501 | expect(observed.constraints).toEqual({ jurisdiction: "eu", regions: ["WEUR", "EEUR"] }); |
| 1502 | |
| 1503 | const plan = yield* stack.plan(program); |
| 1504 | expect(plan.resources.ConstrainedPlacement?.action).toBe("noop"); |
| 1505 | |
| 1506 | yield* stack.destroy(); |
| 1507 | }).pipe(logLevel), |
| 1508 | { timeout: 180_000 }, |
| 1509 | ); |
| 1510 | |
| 1511 | // Right after a Worker upload creates a Durable Object namespace, the |
| 1512 | // containers API can briefly report it missing; the provider retries this |
| 1513 | // typed error. A namespace that never existed returns the same response, |
| 1514 | // which pins the error to its tag. |
| 1515 | test.provider( |
| 1516 | "attaching an application to a missing Durable Object namespace fails with DurableObjectNamespaceNotFound", |
| 1517 | (stack) => |
| 1518 | Effect.gen(function* () { |
| 1519 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 1520 | const error = yield* Containers.createDurableObjectContainerApplication({ |
| 1521 | accountId, |
| 1522 | name: `alchemy-missing-namespace-${stack.stage}`, |
| 1523 | schedulingPolicy: "durable_object", |
| 1524 | durableObjects: { namespaceId: "0".repeat(32) }, |
| 1525 | configuration: {}, |
| 1526 | }).pipe(Effect.flip); |
| 1527 | expect(error._tag).toBe("DurableObjectNamespaceNotFound"); |
| 1528 | }).pipe(logLevel), |
| 1529 | ); |
| 1530 | |
| 1531 | // Issue #953 (2): an `image` that already references the target registry |
| 1532 | // (e.g. pushed by CI) is deployed as-is — no docker pull/tag/push |
| 1533 | // round-trip. The first deploy pushes a public image into the account |
| 1534 | // registry the normal way; the second deploy references it directly, both |
| 1535 | // by digest and by tag. The old (remote) path would have re-tagged it into |
| 1536 | // a repository named after the consumer app, so `configuration.image` |
| 1537 | // landing on the source's digest reference proves the as-is path ran. |
| 1538 | // |
| 1539 | // The tag consumer is the only place `resolveRegistryDigest` (the |
| 1540 | // `HEAD /v2/<repo>/manifests/<tag>` probe against Cloudflare's registry) |
| 1541 | // runs on the happy path — a digest reference short-circuits it and a |
| 1542 | // local push reads the digest from `docker push` output. |
| 1543 | test.provider( |
| 1544 | "pre-pushed registry image is deployed as-is", |
| 1545 | (scratch) => |
| 1546 | Effect.gen(function* () { |
| 1547 | yield* scratch.destroy(); |
| 1548 | |
| 1549 | const source = yield* scratch.deploy( |
| 1550 | Effect.gen(function* () { |
| 1551 | return { |
| 1552 | app: yield* Cloudflare.Container("PrepushSource", { |
| 1553 | image: "mendhak/http-https-echo:latest", |
| 1554 | }).Application, |
| 1555 | }; |
| 1556 | }), |
| 1557 | ); |
| 1558 | const pushedRef = source.app.configuration.image!; |
| 1559 | expect(pushedRef).toMatch(/^registry\.cloudflare\.com\/.*@sha256:[a-f0-9]{64}$/); |
| 1560 | // The mutable tag the provider pushed: `<repo>:<sourceHash>`. |
| 1561 | const taggedRef = `${pushedRef.slice(0, pushedRef.indexOf("@"))}:${source.app.hash!.image}`; |
| 1562 | |
| 1563 | const all = yield* scratch.deploy( |
| 1564 | Effect.gen(function* () { |
| 1565 | return { |
| 1566 | app: yield* Cloudflare.Container("PrepushSource", { |
| 1567 | image: "mendhak/http-https-echo:latest", |
| 1568 | }).Application, |
| 1569 | byDigest: yield* Cloudflare.Container("PrepushByDigest", { image: pushedRef }) |
| 1570 | .Application, |
| 1571 | byTag: yield* Cloudflare.Container("PrepushByTag", { image: taggedRef }) |
| 1572 | .Application, |
| 1573 | }; |
| 1574 | }), |
| 1575 | ); |
| 1576 | expect(all.byDigest.configuration.image).toBe(pushedRef); |
| 1577 | expect(all.byTag.configuration.image).toBe(pushedRef); |
| 1578 | |
| 1579 | yield* scratch.destroy(); |
| 1580 | }).pipe(logLevel), |
| 1581 | { timeout: 600_000 }, |
| 1582 | ); |
| 1583 | |
| 1584 | // #2919: an image only in the local Docker (built on the machine or |
| 1585 | // `docker load`ed) has no registry to pull from. Deploy publishes the |
| 1586 | // local copy when the pull fails, and an image that is neither pullable |
| 1587 | // nor local fails with the commands that fix it. |
| 1588 | test.provider( |
| 1589 | "image only in the local Docker is published from the local copy", |
| 1590 | (scratch) => |
| 1591 | Effect.gen(function* () { |
| 1592 | yield* scratch.destroy(); |
| 1593 | const docker = yield* Docker; |
| 1594 | const localOnly = `alchemy-test.invalid/local-only-echo:${scratch.stage}`; |
| 1595 | yield* docker.image.pull("mendhak/http-https-echo:41", "linux/amd64"); |
| 1596 | yield* docker.image.tag("mendhak/http-https-echo:41", localOnly); |
| 1597 | |
| 1598 | const app = yield* scratch |
| 1599 | .deploy( |
| 1600 | Effect.gen(function* () { |
| 1601 | return yield* Cloudflare.Container("LocalOnlyImage", { image: localOnly }) |
| 1602 | .Application; |
| 1603 | }), |
| 1604 | ) |
| 1605 | .pipe(Effect.ensuring(Effect.ignore(docker.image.remove(localOnly)))); |
| 1606 | expect(app.configuration.image).toMatch( |
| 1607 | /^registry\.cloudflare\.com\/.*@sha256:[a-f0-9]{64}$/, |
| 1608 | ); |
| 1609 | |
| 1610 | const failure = yield* scratch |
| 1611 | .deploy( |
| 1612 | Effect.gen(function* () { |
| 1613 | return yield* Cloudflare.Container("MissingImage", { |
| 1614 | image: `alchemy-test.invalid/missing:${scratch.stage}`, |
| 1615 | }).Application; |
| 1616 | }), |
| 1617 | ) |
| 1618 | .pipe(Effect.flip); |
| 1619 | const message = Cause.pretty(Cause.fail(failure)); |
| 1620 | expect(message).toContain("could not be pulled and is not in the local Docker"); |
| 1621 | expect(message).toContain("docker login"); |
| 1622 | expect(message).toContain("docker build --platform linux/amd64"); |
| 1623 | |
| 1624 | yield* scratch.destroy(); |
| 1625 | }).pipe(logLevel), |
| 1626 | { timeout: 300_000 }, |
| 1627 | ); |
| 1628 | |
| 1629 | // #1282: the image tag is `<repo>:<sourceHash>`, so any change to the |
| 1630 | // build inputs pushes a new tag — even when the resulting image is |
| 1631 | // byte-for-byte identical (a context file the Dockerfile never COPYs, a |
| 1632 | // lockfile, or here: the same remote image spelled with an explicit |
| 1633 | // registry host). The provider used to feed that new tag straight into |
| 1634 | // `updateContainerApplication`, minting a new application version and |
| 1635 | // rolling every instance for nothing. It now resolves the pushed manifest |
| 1636 | // digest and, when it matches the live image, skips the update entirely. |
| 1637 | test.provider( |
| 1638 | "re-pushing an identical image does not create a new application version", |
| 1639 | (scratch) => |
| 1640 | Effect.gen(function* () { |
| 1641 | yield* scratch.destroy(); |
| 1642 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 1643 | |
| 1644 | const first = yield* deployImage(scratch, "mendhak/http-https-echo:41"); |
| 1645 | |
| 1646 | // Same image, different reference: the source hash changes and the |
| 1647 | // image is pulled + pushed again, but the registry digest does not. |
| 1648 | const second = yield* deployImage(scratch, "docker.io/mendhak/http-https-echo:41"); |
| 1649 | expect(second.app.applicationId).toBe(first.app.applicationId); |
| 1650 | expect(second.app.configuration.image).toBe(first.app.configuration.image); |
| 1651 | expect(second.app.version).toBe(first.app.version); |
| 1652 | expect(yield* live(accountId, first.app.applicationId)).toMatchObject({ |
| 1653 | version: first.app.version, |
| 1654 | image: first.app.configuration.image, |
| 1655 | }); |
| 1656 | // The live reference is the immutable digest, not the mutable tag. |
| 1657 | expect(first.app.configuration.image).toMatch( |
| 1658 | /^registry\.cloudflare\.com\/.*@sha256:[a-f0-9]{64}$/, |
| 1659 | ); |
| 1660 | // #2602: the "no effective changes" path saves the configuration as |
| 1661 | // `read` reports it (with Cloudflare's defaults filled in), not as |
| 1662 | // requested, so drift reports nothing. |
| 1663 | const afterNoop = yield* Drift.detect(scratch); |
| 1664 | expect(afterNoop.resources.DigestReuse?.action).toBe("unchanged"); |
| 1665 | |
| 1666 | // A genuinely different image still updates and rolls out. The API |
| 1667 | // reports the ACTIVE configuration (and version) until the rollout |
| 1668 | // completes, and the deploy waits for it, so the new digest is |
| 1669 | // already live when it returns. |
| 1670 | const third = yield* deployImage(scratch, "mendhak/http-https-echo:40"); |
| 1671 | expect(third.app.applicationId).toBe(first.app.applicationId); |
| 1672 | expect(third.app.configuration.image).toMatch( |
| 1673 | /^registry\.cloudflare\.com\/.*@sha256:[a-f0-9]{64}$/, |
| 1674 | ); |
| 1675 | expect(third.app.configuration.image).not.toBe(first.app.configuration.image); |
| 1676 | const rolledOut = yield* live(accountId, first.app.applicationId); |
| 1677 | expect(rolledOut.image).toBe(third.app.configuration.image); |
| 1678 | expect(first.app.version).toEqual(expect.any(Number)); |
| 1679 | expect(rolledOut.version).toBeGreaterThan(first.app.version!); |
| 1680 | // #2602: reconcile saves the application after the rollout, so the |
| 1681 | // saved version and configuration match what `read` returns. |
| 1682 | expect(third.app.version).toBe(rolledOut.version); |
| 1683 | const afterRollout = yield* Drift.detect(scratch); |
| 1684 | expect(afterRollout.resources.DigestReuse?.action).toBe("unchanged"); |
| 1685 | |
| 1686 | yield* scratch.destroy(); |
| 1687 | }).pipe(logLevel), |
| 1688 | { timeout: 900_000 }, |
| 1689 | ); |
| 1690 | |
| 1691 | test.provider( |
| 1692 | "Container.ref reads a container application deployed by another stack", |
| 1693 | (scratch) => |
| 1694 | Effect.gen(function* () { |
| 1695 | yield* scratch.destroy(); |
| 1696 | const owner = yield* scratch.deploy( |
| 1697 | Effect.gen(function* () { |
| 1698 | return { |
| 1699 | app: yield* Cloudflare.Container("RefTarget", { |
| 1700 | image: "mendhak/http-https-echo:41", |
| 1701 | }).Application, |
| 1702 | }; |
| 1703 | }), |
| 1704 | ); |
| 1705 | |
| 1706 | // A second stack reaches the application only through `ref`. |
| 1707 | const consumer = { |
| 1708 | stage: scratch.stage, |
| 1709 | stack: Stack( |
| 1710 | `${scratch.name}-consumer`, |
| 1711 | { providers: Layer.fresh(Cloudflare.providers()), state: scratch.state }, |
| 1712 | Effect.gen(function* () { |
| 1713 | const app = yield* Cloudflare.Container.ref("RefTarget", { stack: scratch.name }); |
| 1714 | return { |
| 1715 | applicationId: app.applicationId, |
| 1716 | applicationName: app.applicationName, |
| 1717 | image: app.configuration.image, |
| 1718 | }; |
| 1719 | }), |
| 1720 | ), |
| 1721 | }; |
| 1722 | const referenced = yield* Deploy.deploy(consumer).pipe( |
| 1723 | Effect.ensuring(Destroy.destroy(consumer).pipe(Effect.orDie)), |
| 1724 | ); |
| 1725 | expect(referenced).toEqual({ |
| 1726 | applicationId: owner.app.applicationId, |
| 1727 | applicationName: owner.app.applicationName, |
| 1728 | image: owner.app.configuration.image, |
| 1729 | }); |
| 1730 | |
| 1731 | // Destroying the referencing stack leaves the application running. |
| 1732 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 1733 | expect((yield* live(accountId, owner.app.applicationId)).image).toBe( |
| 1734 | owner.app.configuration.image, |
| 1735 | ); |
| 1736 | |
| 1737 | yield* scratch.destroy(); |
| 1738 | }).pipe(logLevel), |
| 1739 | { timeout: 900_000 }, |
| 1740 | ); |
| 1741 | |
| 1742 | // State written before #1282 carries only `hash.image`, and its live |
| 1743 | // application runs the mutable `<repo>:<sourceHash>` tag. On the first |
| 1744 | // reconcile after upgrading, the provider resolves that tag's digest |
| 1745 | // through the registry, keeps the live tag reference when the rebuilt |
| 1746 | // image matches, and persists the digest + configuration fingerprint |
| 1747 | // through one normal update. Every later source-hash drift is then a noop. |
| 1748 | test.provider( |
| 1749 | "legacy state without a digest migrates through one update", |
| 1750 | (scratch) => |
| 1751 | Effect.gen(function* () { |
| 1752 | yield* scratch.destroy(); |
| 1753 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 1754 | |
| 1755 | const first = yield* deployImage(scratch, "mendhak/http-https-echo:41"); |
| 1756 | const { applicationId } = first.app; |
| 1757 | const taggedRef = taggedRefOf(first.app); |
| 1758 | |
| 1759 | // Put the cloud where a pre-digest engine left it: the mutable tag |
| 1760 | // is the ACTIVE image (update + rollout, then wait for it to land). |
| 1761 | const legacyConfiguration = { ...first.app.configuration, image: taggedRef }; |
| 1762 | yield* Containers.updateContainerApplication({ |
| 1763 | accountId, |
| 1764 | applicationId, |
| 1765 | configuration: legacyConfiguration, |
| 1766 | }); |
| 1767 | yield* Containers.createContainerApplicationRollout({ |
| 1768 | accountId, |
| 1769 | applicationId, |
| 1770 | description: "legacy tag reference", |
| 1771 | strategy: "rolling", |
| 1772 | kind: "full_auto", |
| 1773 | stepPercentage: 100, |
| 1774 | targetConfiguration: legacyConfiguration, |
| 1775 | }); |
| 1776 | const legacy = yield* waitForImage(accountId, applicationId, taggedRef); |
| 1777 | |
| 1778 | // And the state row: tag reference, source hash only. |
| 1779 | yield* patchRow<typeof first.app>("DigestReuse", (attr) => ({ |
| 1780 | ...attr, |
| 1781 | configuration: { ...attr.configuration, image: taggedRef }, |
| 1782 | hash: { image: attr.hash!.image }, |
| 1783 | })); |
| 1784 | |
| 1785 | // Source-hash drift on the migrated row: the rebuilt digest is |
| 1786 | // compared against the live tag's digest (resolved via the |
| 1787 | // registry), the live reference is kept, and the digest + |
| 1788 | // fingerprint are persisted through one update. That update still |
| 1789 | // carries one rollout — the live configuration is Cloudflare- |
| 1790 | // enriched, so the pre-fingerprint `deepEqual` misses — which is |
| 1791 | // exactly what the fingerprint prevents from here on. |
| 1792 | const migrated = yield* deployImage(scratch, "docker.io/mendhak/http-https-echo:41"); |
| 1793 | expect(migrated.app.applicationId).toBe(applicationId); |
| 1794 | expect(migrated.app.configuration.image).toBe(taggedRef); |
| 1795 | expect(migrated.app.hash?.digest).toBe(first.app.hash?.digest); |
| 1796 | expect(migrated.app.hash?.configuration).toBeDefined(); |
| 1797 | const after = yield* live(accountId, applicationId).pipe( |
| 1798 | Effect.repeat({ |
| 1799 | schedule: Schedule.spaced("3 seconds"), |
| 1800 | until: (app) => |
| 1801 | app.version != null && legacy.version != null && app.version > legacy.version, |
| 1802 | times: 30, |
| 1803 | }), |
| 1804 | ); |
| 1805 | expect(after.image).toBe(taggedRef); |
| 1806 | |
| 1807 | // From here on, drift is free: no update, no rollout. |
| 1808 | const settled = yield* deployImage(scratch, "mendhak/http-https-echo:41"); |
| 1809 | expect(settled.app.configuration.image).toBe(taggedRef); |
| 1810 | expect(settled.app.hash?.image).not.toBe(migrated.app.hash?.image); |
| 1811 | yield* Effect.sleep("10 seconds"); |
| 1812 | expect(yield* live(accountId, applicationId)).toMatchObject({ |
| 1813 | image: taggedRef, |
| 1814 | version: after.version, |
| 1815 | }); |
| 1816 | |
| 1817 | yield* scratch.destroy(); |
| 1818 | }).pipe(logLevel), |
| 1819 | { timeout: 900_000 }, |
| 1820 | ); |
| 1821 | |
| 1822 | // The Durable Object attachment is immutable, so a reconcile that finds an |
| 1823 | // application without it (precreate's stub, or a deploy that died between |
| 1824 | // precreate and reconcile) deletes and re-creates the application. When |
| 1825 | // the source hash has drifted in between, that path rebuilds the image and |
| 1826 | // must apply the same digest comparison as a plain update. |
| 1827 | test.provider( |
| 1828 | "re-creating an application to attach its Durable Object reuses an unchanged digest", |
| 1829 | (scratch) => |
| 1830 | Effect.gen(function* () { |
| 1831 | yield* scratch.destroy(); |
| 1832 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 1833 | |
| 1834 | const program = Effect.gen(function* () { |
| 1835 | yield* EnvBucket; |
| 1836 | const worker = yield* RemoteContainerWorker; |
| 1837 | const app = yield* RemoteContainer.Application; |
| 1838 | return { url: worker.url.as<string>(), app }; |
| 1839 | }); |
| 1840 | |
| 1841 | const first = yield* scratch.deploy(program); |
| 1842 | const namespaceId = first.app.durableObjects?.namespaceId; |
| 1843 | expect(namespaceId).toBeDefined(); |
| 1844 | const digestRef = first.app.configuration.image!; |
| 1845 | |
| 1846 | // Out-of-band, leave the cloud the way precreate does: same name and |
| 1847 | // configuration, no Durable Object attachment. |
| 1848 | yield* Containers.deleteContainerApplication({ |
| 1849 | accountId, |
| 1850 | applicationId: first.app.applicationId, |
| 1851 | }); |
| 1852 | const deleted = yield* Effect.gen(function* () { |
| 1853 | const app = yield* Containers.getContainerApplication({ |
| 1854 | accountId, |
| 1855 | applicationId: first.app.applicationId, |
| 1856 | }).pipe( |
| 1857 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 1858 | ); |
| 1859 | const apps = yield* Containers.listContainerApplications({ accountId }); |
| 1860 | const absentFromList = apps.every((app) => app.id !== first.app.applicationId); |
| 1861 | if (app !== undefined && absentFromList) { |
| 1862 | yield* Effect.logInfo( |
| 1863 | "Deleted container is absent from list but still readable by ID", |
| 1864 | { applicationId: first.app.applicationId }, |
| 1865 | ); |
| 1866 | } |
| 1867 | return app === undefined && absentFromList; |
| 1868 | }).pipe( |
| 1869 | Effect.repeat({ |
| 1870 | schedule: Schedule.spaced("2 seconds"), |
| 1871 | until: (deleted) => deleted, |
| 1872 | times: 8, |
| 1873 | }), |
| 1874 | ); |
| 1875 | expect(deleted).toBe(true); |
| 1876 | assert(first.app.maxInstances !== undefined); |
| 1877 | assert(first.app.configuration.image !== undefined); |
| 1878 | const detached = yield* Containers.createContainerApplication({ |
| 1879 | accountId, |
| 1880 | name: first.app.applicationName, |
| 1881 | maxInstances: first.app.maxInstances, |
| 1882 | instances: first.app.instances, |
| 1883 | schedulingPolicy: first.app.schedulingPolicy, |
| 1884 | constraints: first.app.constraints, |
| 1885 | affinities: first.app.affinities, |
| 1886 | configuration: { |
| 1887 | ...first.app.configuration, |
| 1888 | image: first.app.configuration.image, |
| 1889 | }, |
| 1890 | }); |
| 1891 | expect(detached.id).not.toBe(first.app.applicationId); |
| 1892 | expect(detached.durableObjects ?? undefined).toBeUndefined(); |
| 1893 | const observed = yield* Containers.getContainerApplication({ |
| 1894 | accountId, |
| 1895 | applicationId: detached.id, |
| 1896 | }).pipe( |
| 1897 | Effect.retry({ |
| 1898 | while: (error) => error._tag === "ContainerApplicationNotFound", |
| 1899 | schedule: Schedule.spaced("1 second"), |
| 1900 | times: 8, |
| 1901 | }), |
| 1902 | ); |
| 1903 | expect(observed.name).toBe(first.app.applicationName); |
| 1904 | expect(observed.durableObjects ?? undefined).toBeUndefined(); |
| 1905 | |
| 1906 | // Stale the persisted source hash so the re-create must rebuild. |
| 1907 | yield* patchRow<typeof first.app>("RemoteContainer", (attr) => ({ |
| 1908 | ...attr, |
| 1909 | hash: { ...attr.hash!, image: "0000000000000000" }, |
| 1910 | })); |
| 1911 | |
| 1912 | const second = yield* scratch.deploy(program); |
| 1913 | yield* Effect.logInfo("Container reattachment identities", { |
| 1914 | original: first.app.applicationId, |
| 1915 | detached: detached.id, |
| 1916 | recovered: second.app.applicationId, |
| 1917 | }); |
| 1918 | expect(second.app.applicationId).not.toBe(first.app.applicationId); |
| 1919 | expect(second.app.applicationId).not.toBe(detached.id); |
| 1920 | expect(second.app.durableObjects?.namespaceId).toBe(namespaceId); |
| 1921 | expect(second.app.configuration.image).toBe(digestRef); |
| 1922 | expect(second.app.hash?.digest).toBe(first.app.hash?.digest); |
| 1923 | expect(second.app.hash?.image).not.toBe("0000000000000000"); |
| 1924 | expect( |
| 1925 | yield* live(accountId, second.app.applicationId).pipe( |
| 1926 | Effect.retry({ |
| 1927 | while: (error) => error._tag === "ContainerApplicationNotFound", |
| 1928 | schedule: Schedule.spaced("1 second"), |
| 1929 | times: 8, |
| 1930 | }), |
| 1931 | ), |
| 1932 | ).toMatchObject({ image: digestRef, durableObjects: { namespaceId } }); |
| 1933 | |
| 1934 | yield* scratch.destroy(); |
| 1935 | }).pipe(logLevel), |
| 1936 | { tags: ["provider:cloudflare:r2", "provider:cloudflare:worker"], timeout: 900_000 }, |
| 1937 | ); |
| 1938 | |
| 1939 | // The API rejects network keys it does not know (`unrecognized key: |
| 1940 | // "assignipv4"`), so camelCase props must reach it as wire names. |
| 1941 | test.provider( |
| 1942 | "deploys and updates a network configuration", |
| 1943 | (stack) => |
| 1944 | Effect.gen(function* () { |
| 1945 | yield* stack.destroy(); |
| 1946 | const network = { assignIpv4: "none", assignIpv6: "none", mode: "private" } as const; |
| 1947 | const program = (maxInstances: number) => |
| 1948 | Cloudflare.Container("Networked", { |
| 1949 | image: "mendhak/http-https-echo:41", |
| 1950 | maxInstances, |
| 1951 | network, |
| 1952 | }).Application; |
| 1953 | |
| 1954 | const first = yield* stack.deploy(program(1)); |
| 1955 | expect(first.configuration.network).toMatchObject(network); |
| 1956 | const observed = yield* Containers.getContainerApplication({ |
| 1957 | accountId: first.accountId, |
| 1958 | applicationId: first.applicationId, |
| 1959 | }).pipe(Effect.map(scheduledApplication)); |
| 1960 | expect(observed.configuration.network).toMatchObject(network); |
| 1961 | |
| 1962 | const updated = yield* stack.deploy(program(2)); |
| 1963 | expect(updated.applicationId).toBe(first.applicationId); |
| 1964 | expect(updated.maxInstances).toBe(2); |
| 1965 | expect((yield* stack.plan(program(2))).resources.Networked.action).toBe("noop"); |
| 1966 | |
| 1967 | yield* stack.destroy(); |
| 1968 | expect( |
| 1969 | yield* waitForApplicationDeleted(first.accountId, first.applicationId), |
| 1970 | ).toBeUndefined(); |
| 1971 | }).pipe(logLevel), |
| 1972 | { timeout: 300_000 }, |
| 1973 | ); |
| 1974 | }, |
| 1975 | ); |