The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect } from "@alchemy.run/test"; |
| 2 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Deferred from "effect/Deferred"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Exit from "effect/Exit"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import { Unowned } from "@/AdoptPolicy"; |
| 10 | import * as Drift from "@/Drift"; |
| 11 | import { onEvent, type ApplyEvent, type Event } from "@/Events.ts"; |
| 12 | import * as Namespace from "@/Namespace.ts"; |
| 13 | import * as RemovalPolicy from "@/RemovalPolicy.ts"; |
| 14 | import { Stack } from "@/Stack"; |
| 15 | import { State, type ResourceState } from "@/State"; |
| 16 | import { |
| 17 | Bucket, |
| 18 | DriftResource, |
| 19 | makeTestCloud, |
| 20 | ResourceFailure, |
| 21 | TestCloud, |
| 22 | TestLayers, |
| 23 | TestResource, |
| 24 | TestResourceHooks, |
| 25 | failOn, |
| 26 | failOnMultiple, |
| 27 | inDev, |
| 28 | ModalResource, |
| 29 | modalCalls, |
| 30 | type TestCloudService, |
| 31 | } from "./test.resources.ts"; |
| 32 | |
| 33 | const { test } = Test.make({ providers: TestLayers() }); |
| 34 | |
| 35 | const getState = Effect.fn(function* <S = ResourceState>(resourceId: string) { |
| 36 | const state = yield* yield* State; |
| 37 | const stk = yield* Stack; |
| 38 | return (yield* state.get({ stack: stk.name, stage: stk.stage, fqn: resourceId })) as S; |
| 39 | }); |
| 40 | |
| 41 | const seed = Effect.fn(function* (rows: Record<string, any>) { |
| 42 | const state = yield* yield* State; |
| 43 | const stk = yield* Stack; |
| 44 | for (const [fqn, value] of Object.entries(rows)) { |
| 45 | yield* state.set({ stack: stk.name, stage: stk.stage, fqn, value }); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | /** Run drift; `events` collects the apply events a repair emits. */ |
| 50 | const runDrift = (options: { readonly dryRun?: boolean; readonly events?: ApplyEvent[] } = {}) => |
| 51 | Effect.gen(function* () { |
| 52 | const stk = yield* Stack; |
| 53 | const identity = { name: stk.name, stage: stk.stage }; |
| 54 | return yield* options.dryRun |
| 55 | ? Drift.detect(identity) |
| 56 | : Drift.repair(identity).pipe( |
| 57 | onEvent((event) => |
| 58 | Effect.sync(() => { |
| 59 | if (event.type === "apply.resource.status" || event.type === "apply.resource.note") { |
| 60 | options.events?.push(event); |
| 61 | } |
| 62 | }), |
| 63 | ), |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | const withCloud = |
| 68 | (cloud: TestCloudService) => |
| 69 | <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 70 | effect.pipe(Effect.provide(Layer.succeed(TestCloud, cloud))); |
| 71 | |
| 72 | const reconcileCalls = (cloud: TestCloudService) => |
| 73 | cloud.calls.filter((c) => c.op === "reconcile").map((c) => c.id); |
| 74 | |
| 75 | // `id[sid,…]` per reconcile, in call order — shows both the order and which |
| 76 | // bindings each reconcile carried. |
| 77 | const reconcileTrace = (cloud: TestCloudService) => |
| 78 | cloud.calls |
| 79 | .filter((c) => c.op === "reconcile") |
| 80 | .map((c) => `${c.id}[${(c.bindings ?? []).join(",")}]`); |
| 81 | |
| 82 | const statusesOf = (events: ApplyEvent[], fqn: string) => |
| 83 | events.flatMap((e) => (e.type === "apply.resource.status" && e.fqn === fqn ? [e.status] : [])); |
| 84 | |
| 85 | const readCalls = (cloud: TestCloudService) => |
| 86 | cloud.calls.filter((c) => c.op === "read").map((c) => c.id); |
| 87 | |
| 88 | const instanceId = "852f6ec2e19b66589825efe14dca2971"; |
| 89 | |
| 90 | describe("drift detection and repair", { tags: ["unit", "local"] }, () => { |
| 91 | test.provider( |
| 92 | "clean deployment: every resource is unchanged and reconcile is not invoked", |
| 93 | (stack) => { |
| 94 | const cloud = makeTestCloud(); |
| 95 | return Effect.gen(function* () { |
| 96 | yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | yield* DriftResource("A", { value: "a" }); |
| 99 | yield* DriftResource("B", { value: "b" }); |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | cloud.calls.length = 0; |
| 104 | const result = yield* runDrift(); |
| 105 | |
| 106 | expect(result.resources).toMatchObject({ |
| 107 | A: { action: "unchanged", resourceType: "Test.DriftResource" }, |
| 108 | B: { action: "unchanged", resourceType: "Test.DriftResource" }, |
| 109 | }); |
| 110 | // drift observed both resources but touched neither |
| 111 | expect(readCalls(cloud).sort()).toEqual(["A", "B"]); |
| 112 | expect(reconcileCalls(cloud)).toEqual([]); |
| 113 | // cloud and state are untouched |
| 114 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 115 | expect((yield* getState("A"))?.status).toEqual("created"); |
| 116 | }).pipe(withCloud(cloud)); |
| 117 | }, |
| 118 | ); |
| 119 | |
| 120 | test.provider("out-of-band value drift is repaired back to the desired state", (stack) => { |
| 121 | const cloud = makeTestCloud(); |
| 122 | return Effect.gen(function* () { |
| 123 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 124 | |
| 125 | // someone edits the resource in the console |
| 126 | cloud.resources.get("A")!.value = "hijacked"; |
| 127 | |
| 128 | cloud.calls.length = 0; |
| 129 | const result = yield* runDrift(); |
| 130 | |
| 131 | expect(result.resources.A).toMatchObject({ action: "repaired", attr: { value: "a" } }); |
| 132 | // cloud converged back to the last-deployed desired state |
| 133 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 134 | expect(reconcileCalls(cloud)).toEqual(["A"]); |
| 135 | // state refreshed with the reconciled attributes |
| 136 | expect(yield* getState("A")).toMatchObject({ |
| 137 | status: "updated", |
| 138 | attr: { value: "a" }, |
| 139 | props: { value: "a" }, |
| 140 | }); |
| 141 | }).pipe(withCloud(cloud)); |
| 142 | }); |
| 143 | |
| 144 | test.provider("a scoped repair leaves resources outside its scope alone", (stack) => { |
| 145 | const cloud = makeTestCloud(); |
| 146 | return Effect.gen(function* () { |
| 147 | yield* stack.deploy( |
| 148 | Effect.gen(function* () { |
| 149 | yield* DriftResource("A", { value: "a" }); |
| 150 | yield* DriftResource("B", { value: "b" }); |
| 151 | }), |
| 152 | ); |
| 153 | |
| 154 | // A drifted before the scan was approved; B drifts afterwards. |
| 155 | cloud.resources.get("A")!.value = "hijacked"; |
| 156 | cloud.resources.get("B")!.value = "hijacked"; |
| 157 | |
| 158 | cloud.calls.length = 0; |
| 159 | const stk = yield* Stack; |
| 160 | const result = yield* Drift.repair({ name: stk.name, stage: stk.stage }, { only: ["A"] }); |
| 161 | |
| 162 | expect(Object.keys(result.resources)).toEqual(["A"]); |
| 163 | expect(reconcileCalls(cloud)).toEqual(["A"]); |
| 164 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 165 | expect(cloud.resources.get("B")).toMatchObject({ value: "hijacked" }); |
| 166 | }).pipe(withCloud(cloud)); |
| 167 | }); |
| 168 | |
| 169 | test.provider("nested tag drift is detected and repaired", (stack) => { |
| 170 | const cloud = makeTestCloud(); |
| 171 | return Effect.gen(function* () { |
| 172 | yield* stack.deploy(DriftResource("A", { value: "a", tags: { team: "alchemy" } })); |
| 173 | |
| 174 | // foreign tags appear and an owned tag is clobbered |
| 175 | cloud.resources.get("A")!.tags = { team: "intruder", extra: "tag" }; |
| 176 | |
| 177 | const result = yield* runDrift(); |
| 178 | |
| 179 | expect(result.resources.A?.action).toEqual("repaired"); |
| 180 | expect(cloud.resources.get("A")!.tags).toEqual({ team: "alchemy" }); |
| 181 | expect((yield* getState("A"))?.attr?.tags).toEqual({ team: "alchemy" }); |
| 182 | }).pipe(withCloud(cloud)); |
| 183 | }); |
| 184 | |
| 185 | test.provider( |
| 186 | "a resource deleted out-of-band is recreated under the same instance id", |
| 187 | (stack) => { |
| 188 | const cloud = makeTestCloud(); |
| 189 | return Effect.gen(function* () { |
| 190 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 191 | const before = yield* getState("A"); |
| 192 | |
| 193 | // someone deletes the resource in the console |
| 194 | cloud.resources.delete("A"); |
| 195 | |
| 196 | const result = yield* runDrift(); |
| 197 | |
| 198 | expect(result.resources.A).toMatchObject({ |
| 199 | action: "recreated", |
| 200 | attr: { id: "A", value: "a" }, |
| 201 | }); |
| 202 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 203 | expect(yield* getState("A")).toMatchObject({ |
| 204 | status: "created", |
| 205 | // same instance id so deterministic physical names converge |
| 206 | instanceId: before!.instanceId, |
| 207 | attr: { value: "a" }, |
| 208 | }); |
| 209 | }).pipe(withCloud(cloud)); |
| 210 | }, |
| 211 | ); |
| 212 | |
| 213 | test.provider("drift is idempotent", (stack) => { |
| 214 | const cloud = makeTestCloud(); |
| 215 | return Effect.gen(function* () { |
| 216 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 217 | cloud.resources.get("A")!.value = "hijacked"; |
| 218 | |
| 219 | const first = yield* runDrift(); |
| 220 | expect(first.resources.A?.action).toEqual("repaired"); |
| 221 | |
| 222 | cloud.calls.length = 0; |
| 223 | const second = yield* runDrift(); |
| 224 | expect(second.resources.A?.action).toEqual("unchanged"); |
| 225 | expect(reconcileCalls(cloud)).toEqual([]); |
| 226 | }).pipe(withCloud(cloud)); |
| 227 | }); |
| 228 | |
| 229 | test.provider("mixed fleet: only drifted and missing resources are reconciled", (stack) => { |
| 230 | const cloud = makeTestCloud(); |
| 231 | return Effect.gen(function* () { |
| 232 | yield* stack.deploy( |
| 233 | Effect.gen(function* () { |
| 234 | yield* DriftResource("Clean", { value: "clean" }); |
| 235 | yield* DriftResource("Drifted", { value: "drifted" }); |
| 236 | yield* DriftResource("Missing", { value: "missing" }); |
| 237 | }), |
| 238 | ); |
| 239 | |
| 240 | cloud.resources.get("Drifted")!.value = "hijacked"; |
| 241 | cloud.resources.delete("Missing"); |
| 242 | |
| 243 | cloud.calls.length = 0; |
| 244 | const result = yield* runDrift(); |
| 245 | |
| 246 | expect(result.resources).toMatchObject({ |
| 247 | Clean: { action: "unchanged" }, |
| 248 | Drifted: { action: "repaired" }, |
| 249 | Missing: { action: "recreated" }, |
| 250 | }); |
| 251 | expect(reconcileCalls(cloud).sort()).toEqual(["Drifted", "Missing"]); |
| 252 | expect(cloud.resources.get("Drifted")).toMatchObject({ value: "drifted" }); |
| 253 | expect(cloud.resources.get("Missing")).toMatchObject({ value: "missing" }); |
| 254 | }).pipe(withCloud(cloud)); |
| 255 | }); |
| 256 | |
| 257 | test.provider("empty state: drift succeeds with no resources", () => |
| 258 | Effect.gen(function* () { |
| 259 | const result = yield* runDrift(); |
| 260 | expect(result.resources).toEqual({}); |
| 261 | }), |
| 262 | ); |
| 263 | |
| 264 | test.provider("a provider whose read reflects state back never drifts", (stack) => |
| 265 | Effect.gen(function* () { |
| 266 | // TestResource.read returns the persisted output when no hook is |
| 267 | // installed — the degenerate "cannot observe drift" case. |
| 268 | yield* stack.deploy(TestResource("A", { string: "test-string" })); |
| 269 | const result = yield* runDrift(); |
| 270 | expect(result.resources.A?.action).toEqual("unchanged"); |
| 271 | }), |
| 272 | ); |
| 273 | }); |
| 274 | |
| 275 | describe("dry run", { tags: ["unit", "local"] }, () => { |
| 276 | test.provider("reports drift without repairing the cloud or touching state", (stack) => { |
| 277 | const cloud = makeTestCloud(); |
| 278 | return Effect.gen(function* () { |
| 279 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 280 | cloud.resources.get("A")!.value = "hijacked"; |
| 281 | |
| 282 | cloud.calls.length = 0; |
| 283 | const result = yield* runDrift({ dryRun: true }); |
| 284 | |
| 285 | expect(result.resources.A).toMatchObject({ |
| 286 | action: "drifted", |
| 287 | // dry-run reports the OBSERVED (drifted) attributes |
| 288 | attr: { value: "hijacked" }, |
| 289 | }); |
| 290 | expect(reconcileCalls(cloud)).toEqual([]); |
| 291 | // neither the cloud nor the state store were touched |
| 292 | expect(cloud.resources.get("A")).toMatchObject({ value: "hijacked" }); |
| 293 | expect(yield* getState("A")).toMatchObject({ status: "created", attr: { value: "a" } }); |
| 294 | }).pipe(withCloud(cloud)); |
| 295 | }); |
| 296 | |
| 297 | test.provider("reports missing resources without recreating them", (stack) => { |
| 298 | const cloud = makeTestCloud(); |
| 299 | return Effect.gen(function* () { |
| 300 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 301 | cloud.resources.delete("A"); |
| 302 | |
| 303 | cloud.calls.length = 0; |
| 304 | const result = yield* runDrift({ dryRun: true }); |
| 305 | |
| 306 | expect(result.resources.A?.action).toEqual("missing"); |
| 307 | expect(reconcileCalls(cloud)).toEqual([]); |
| 308 | expect(cloud.resources.has("A")).toBe(false); |
| 309 | // state still remembers the last-deployed attributes |
| 310 | expect((yield* getState("A"))?.attr).toMatchObject({ value: "a" }); |
| 311 | }).pipe(withCloud(cloud)); |
| 312 | }); |
| 313 | |
| 314 | test.provider("reports unchanged resources as unchanged", (stack) => { |
| 315 | const cloud = makeTestCloud(); |
| 316 | return Effect.gen(function* () { |
| 317 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 318 | const result = yield* runDrift({ dryRun: true }); |
| 319 | expect(result.resources.A?.action).toEqual("unchanged"); |
| 320 | }).pipe(withCloud(cloud)); |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | describe("skipped resources", { tags: ["unit", "local"] }, () => { |
| 325 | test.provider("a provider without read is skipped with a reason", (stack) => |
| 326 | Effect.gen(function* () { |
| 327 | yield* stack.deploy(Bucket("MyBucket", { name: "test-bucket" })); |
| 328 | |
| 329 | const result = yield* runDrift(); |
| 330 | |
| 331 | expect(result.resources.MyBucket).toMatchObject({ |
| 332 | action: "skipped", |
| 333 | resourceType: "Test.Bucket", |
| 334 | }); |
| 335 | expect(result.resources.MyBucket?.reason).toContain("read"); |
| 336 | // skipping is not destructive |
| 337 | expect((yield* getState("MyBucket"))?.status).toEqual("created"); |
| 338 | }), |
| 339 | ); |
| 340 | |
| 341 | test.provider("resources stuck in a non-terminal status are skipped", () => { |
| 342 | const cloud = makeTestCloud(); |
| 343 | return Effect.gen(function* () { |
| 344 | yield* seed({ |
| 345 | Stuck: { |
| 346 | status: "creating", |
| 347 | fqn: "Stuck", |
| 348 | logicalId: "Stuck", |
| 349 | instanceId, |
| 350 | resourceType: "Test.DriftResource", |
| 351 | namespace: undefined, |
| 352 | providerVersion: 0, |
| 353 | props: { value: "v" }, |
| 354 | bindings: [], |
| 355 | downstream: [], |
| 356 | }, |
| 357 | }); |
| 358 | |
| 359 | const result = yield* runDrift(); |
| 360 | |
| 361 | expect(result.resources.Stuck?.action).toEqual("skipped"); |
| 362 | expect(result.resources.Stuck?.reason).toContain("creating"); |
| 363 | // no lifecycle operation ran against the unstable resource |
| 364 | expect(cloud.calls).toEqual([]); |
| 365 | }).pipe(withCloud(cloud)); |
| 366 | }); |
| 367 | |
| 368 | test.provider("resources with a pending replacement chain are skipped", () => |
| 369 | Effect.gen(function* () { |
| 370 | const oldGeneration = { |
| 371 | status: "created", |
| 372 | fqn: "R", |
| 373 | logicalId: "R", |
| 374 | instanceId: "00000000000000000000000000000000", |
| 375 | resourceType: "Test.DriftResource", |
| 376 | namespace: undefined, |
| 377 | providerVersion: 0, |
| 378 | props: { value: "v1" }, |
| 379 | attr: { id: "R", value: "v1", tags: {}, env: {} }, |
| 380 | bindings: [], |
| 381 | downstream: [], |
| 382 | }; |
| 383 | yield* seed({ |
| 384 | R: { |
| 385 | status: "replaced", |
| 386 | fqn: "R", |
| 387 | logicalId: "R", |
| 388 | instanceId, |
| 389 | resourceType: "Test.DriftResource", |
| 390 | namespace: undefined, |
| 391 | providerVersion: 0, |
| 392 | props: { value: "v2" }, |
| 393 | attr: { id: "R", value: "v2", tags: {}, env: {} }, |
| 394 | bindings: [], |
| 395 | downstream: [], |
| 396 | deleteFirst: false, |
| 397 | old: oldGeneration, |
| 398 | }, |
| 399 | }); |
| 400 | |
| 401 | const result = yield* runDrift(); |
| 402 | |
| 403 | expect(result.resources.R?.action).toEqual("skipped"); |
| 404 | expect(result.resources.R?.reason).toContain("replacement"); |
| 405 | }), |
| 406 | ); |
| 407 | |
| 408 | test.provider("action state rows are ignored", (stack) => { |
| 409 | const cloud = makeTestCloud(); |
| 410 | return Effect.gen(function* () { |
| 411 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 412 | yield* seed({ |
| 413 | MyTask: { |
| 414 | kind: "action", |
| 415 | status: "ran", |
| 416 | fqn: "MyTask", |
| 417 | logicalId: "MyTask", |
| 418 | actionType: "Test.Task", |
| 419 | namespace: undefined, |
| 420 | inputHash: "abc", |
| 421 | input: {}, |
| 422 | output: { ok: true }, |
| 423 | downstream: [], |
| 424 | }, |
| 425 | }); |
| 426 | |
| 427 | const result = yield* runDrift(); |
| 428 | |
| 429 | expect(result.resources.MyTask).toBeUndefined(); |
| 430 | expect(result.resources.A?.action).toEqual("unchanged"); |
| 431 | // the action row survives untouched |
| 432 | expect(yield* getState("MyTask")).toMatchObject({ kind: "action", status: "ran" }); |
| 433 | }).pipe(withCloud(cloud)); |
| 434 | }); |
| 435 | }); |
| 436 | |
| 437 | describe("failures", { tags: ["unit", "local"] }, () => { |
| 438 | test.provider( |
| 439 | "a failing repair does not prevent sibling repairs, and fails the drift", |
| 440 | (stack) => { |
| 441 | const cloud = makeTestCloud(); |
| 442 | return Effect.gen(function* () { |
| 443 | yield* stack.deploy( |
| 444 | Effect.gen(function* () { |
| 445 | yield* DriftResource("A", { value: "a" }); |
| 446 | yield* DriftResource("B", { value: "b" }); |
| 447 | }), |
| 448 | ); |
| 449 | |
| 450 | cloud.resources.get("A")!.value = "hijacked-a"; |
| 451 | cloud.resources.get("B")!.value = "hijacked-b"; |
| 452 | |
| 453 | // drift repair goes through the provider's update intent |
| 454 | const exit = yield* runDrift().pipe( |
| 455 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("B", "update"))), |
| 456 | Effect.exit, |
| 457 | ); |
| 458 | |
| 459 | expect(Exit.isFailure(exit)).toBe(true); |
| 460 | if (Exit.isFailure(exit)) { |
| 461 | const rendered = Cause.pretty(exit.cause); |
| 462 | expect(rendered).toContain("Resource 'B' (Test.DriftResource) failed during update"); |
| 463 | expect(rendered).toContain("Failed to create"); |
| 464 | } |
| 465 | // A was still repaired even though B failed |
| 466 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 467 | expect(yield* getState("A")).toMatchObject({ status: "updated", attr: { value: "a" } }); |
| 468 | // B's cloud state is still drifted and its state row untouched |
| 469 | expect(cloud.resources.get("B")).toMatchObject({ value: "hijacked-b" }); |
| 470 | expect(yield* getState("B")).toMatchObject({ status: "created", attr: { value: "b" } }); |
| 471 | |
| 472 | // a follow-up drift (without the failure) converges B too |
| 473 | const result = yield* runDrift(); |
| 474 | expect(result.resources).toMatchObject({ |
| 475 | A: { action: "unchanged" }, |
| 476 | B: { action: "repaired" }, |
| 477 | }); |
| 478 | expect(cloud.resources.get("B")).toMatchObject({ value: "b" }); |
| 479 | }).pipe(withCloud(cloud)); |
| 480 | }, |
| 481 | ); |
| 482 | |
| 483 | test.provider("a failing recreate goes through the provider's create intent", (stack) => { |
| 484 | const cloud = makeTestCloud(); |
| 485 | return Effect.gen(function* () { |
| 486 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 487 | cloud.resources.delete("A"); |
| 488 | |
| 489 | const exit = yield* runDrift().pipe( |
| 490 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("A", "create"))), |
| 491 | Effect.exit, |
| 492 | ); |
| 493 | |
| 494 | expect(Exit.isFailure(exit)).toBe(true); |
| 495 | expect(cloud.resources.has("A")).toBe(false); |
| 496 | |
| 497 | // recovery: the next drift recreates it |
| 498 | const result = yield* runDrift(); |
| 499 | expect(result.resources.A?.action).toEqual("recreated"); |
| 500 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 501 | }).pipe(withCloud(cloud)); |
| 502 | }); |
| 503 | |
| 504 | test.provider("a failing read fails the drift", (stack) => |
| 505 | Effect.gen(function* () { |
| 506 | yield* stack.deploy(TestResource("A", { string: "test-string" })); |
| 507 | |
| 508 | const exit = yield* runDrift().pipe( |
| 509 | Effect.provide( |
| 510 | Layer.succeed(TestResourceHooks, { |
| 511 | read: () => Effect.fail(new ResourceFailure("read exploded")), |
| 512 | }), |
| 513 | ), |
| 514 | Effect.exit, |
| 515 | ); |
| 516 | |
| 517 | expect(Exit.isFailure(exit)).toBe(true); |
| 518 | if (Exit.isFailure(exit)) { |
| 519 | const rendered = Cause.pretty(exit.cause); |
| 520 | expect(rendered).toContain("Resource 'A' (Test.TestResource) failed during read"); |
| 521 | expect(rendered).toContain("read exploded"); |
| 522 | } |
| 523 | // state untouched by the failed observation |
| 524 | expect(yield* getState("A")).toMatchObject({ |
| 525 | status: "created", |
| 526 | attr: { string: "test-string" }, |
| 527 | }); |
| 528 | }), |
| 529 | ); |
| 530 | }); |
| 531 | |
| 532 | describe("ownership", { tags: ["unit", "local"] }, () => { |
| 533 | test.provider("an unowned read result with matching attributes is unchanged", (stack) => { |
| 534 | const cloud = makeTestCloud(); |
| 535 | return Effect.gen(function* () { |
| 536 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 537 | |
| 538 | // ownership markers drifted but the attributes still match — the |
| 539 | // Unowned brand is a plan-time routing hint, not drift by itself |
| 540 | cloud.unowned.add("A"); |
| 541 | |
| 542 | cloud.calls.length = 0; |
| 543 | const result = yield* runDrift(); |
| 544 | |
| 545 | expect(result.resources.A?.action).toEqual("unchanged"); |
| 546 | expect(reconcileCalls(cloud)).toEqual([]); |
| 547 | }).pipe(withCloud(cloud)); |
| 548 | }); |
| 549 | |
| 550 | test.provider( |
| 551 | "an unowned, drifted resource is repaired and the brand never persists", |
| 552 | (stack) => { |
| 553 | const cloud = makeTestCloud(); |
| 554 | return Effect.gen(function* () { |
| 555 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 556 | |
| 557 | cloud.unowned.add("A"); |
| 558 | cloud.resources.get("A")!.value = "hijacked"; |
| 559 | |
| 560 | const result = yield* runDrift(); |
| 561 | |
| 562 | expect(result.resources.A?.action).toEqual("repaired"); |
| 563 | expect(cloud.resources.get("A")).toMatchObject({ value: "a" }); |
| 564 | const state = yield* getState("A"); |
| 565 | expect(state).toMatchObject({ status: "updated", attr: { value: "a" } }); |
| 566 | expect(Unowned.is(state!.attr)).toBe(false); |
| 567 | }).pipe(withCloud(cloud)); |
| 568 | }, |
| 569 | ); |
| 570 | }); |
| 571 | |
| 572 | describe("bindings", { tags: ["unit", "local"] }, () => { |
| 573 | test.provider("binding-derived attributes are repaired from the persisted bindings", (stack) => { |
| 574 | const cloud = makeTestCloud(); |
| 575 | return Effect.gen(function* () { |
| 576 | const created = yield* stack.deploy( |
| 577 | Effect.gen(function* () { |
| 578 | const a = yield* DriftResource("A", { value: "a" }); |
| 579 | yield* a.bind("Env", { env: { FEATURE_FLAG: "on" } }); |
| 580 | return a; |
| 581 | }), |
| 582 | ); |
| 583 | expect(created.env).toEqual({ FEATURE_FLAG: "on" }); |
| 584 | |
| 585 | // the binding-derived env is wiped out-of-band |
| 586 | cloud.resources.get("A")!.env = {}; |
| 587 | |
| 588 | const result = yield* runDrift(); |
| 589 | |
| 590 | expect(result.resources.A?.action).toEqual("repaired"); |
| 591 | // reconcile received the persisted bindings and restored the env |
| 592 | expect(cloud.resources.get("A")!.env).toEqual({ FEATURE_FLAG: "on" }); |
| 593 | expect((yield* getState("A"))?.attr?.env).toEqual({ FEATURE_FLAG: "on" }); |
| 594 | }).pipe(withCloud(cloud)); |
| 595 | }); |
| 596 | }); |
| 597 | |
| 598 | describe("plan", { tags: ["unit", "local"] }, () => { |
| 599 | test.provider("projects detection results onto plan node actions", (stack) => { |
| 600 | const cloud = makeTestCloud(); |
| 601 | return Effect.gen(function* () { |
| 602 | yield* stack.deploy( |
| 603 | Effect.gen(function* () { |
| 604 | yield* DriftResource("Clean", { value: "clean" }); |
| 605 | yield* DriftResource("Drifted", { value: "drifted" }); |
| 606 | yield* DriftResource("Missing", { value: "missing" }); |
| 607 | }), |
| 608 | ); |
| 609 | |
| 610 | cloud.resources.get("Drifted")!.value = "hijacked"; |
| 611 | cloud.resources.delete("Missing"); |
| 612 | |
| 613 | cloud.calls.length = 0; |
| 614 | const stk = yield* Stack; |
| 615 | const { result, plan } = yield* Drift.plan({ name: stk.name, stage: stk.stage }); |
| 616 | |
| 617 | // drifted -> update, missing -> create, unchanged -> noop |
| 618 | expect(plan.resources.Clean?.action).toEqual("noop"); |
| 619 | expect(plan.resources.Clean?.drift).toBeUndefined(); |
| 620 | expect(plan.resources.Drifted?.action).toEqual("update"); |
| 621 | expect(plan.resources.Missing?.action).toEqual("create"); |
| 622 | expect(plan.resources.Drifted).toMatchObject({ |
| 623 | drift: { expected: { value: "drifted" }, actual: { value: "hijacked" } }, |
| 624 | }); |
| 625 | expect(plan.resources.Missing).toMatchObject({ |
| 626 | drift: { expected: { value: "missing" }, missing: true }, |
| 627 | }); |
| 628 | // detection result rides along |
| 629 | expect(result.resources).toMatchObject({ |
| 630 | Clean: { action: "unchanged" }, |
| 631 | Drifted: { action: "drifted" }, |
| 632 | Missing: { action: "missing" }, |
| 633 | }); |
| 634 | // the synthetic resource carries what the renderers need |
| 635 | expect(plan.resources.Drifted?.resource).toMatchObject({ |
| 636 | FQN: "Drifted", |
| 637 | LogicalId: "Drifted", |
| 638 | Type: "Test.DriftResource", |
| 639 | }); |
| 640 | // resource-only view: no deletions/actions in a drift plan |
| 641 | expect(plan.deletions).toEqual({}); |
| 642 | expect(plan.actions).toEqual({}); |
| 643 | expect(plan.actionDeletions).toEqual({}); |
| 644 | // planning is detection-only: nothing was repaired |
| 645 | expect(cloud.resources.get("Drifted")).toMatchObject({ value: "hijacked" }); |
| 646 | expect(reconcileCalls(cloud)).toEqual([]); |
| 647 | }).pipe(withCloud(cloud)); |
| 648 | }); |
| 649 | |
| 650 | test.provider("a deleted chain plans every member as a create, carrying its edges", (stack) => { |
| 651 | const cloud = makeTestCloud(); |
| 652 | return Effect.gen(function* () { |
| 653 | yield* stack.deploy( |
| 654 | Effect.gen(function* () { |
| 655 | const topic = yield* DriftResource("Topic", {}); |
| 656 | yield* DriftResource("Subscription", { dependsOn: [topic.id] }); |
| 657 | }), |
| 658 | ); |
| 659 | cloud.resources.delete("Topic"); |
| 660 | cloud.resources.delete("Subscription"); |
| 661 | |
| 662 | cloud.calls.length = 0; |
| 663 | const stk = yield* Stack; |
| 664 | const { plan } = yield* Drift.plan({ name: stk.name, stage: stk.stage }); |
| 665 | |
| 666 | expect(plan.resources.Topic).toMatchObject({ |
| 667 | action: "create", |
| 668 | drift: { missing: true }, |
| 669 | downstream: ["Subscription"], |
| 670 | }); |
| 671 | expect(plan.resources.Subscription).toMatchObject({ action: "create", downstream: [] }); |
| 672 | expect(reconcileCalls(cloud)).toEqual([]); |
| 673 | }).pipe(withCloud(cloud)); |
| 674 | }); |
| 675 | |
| 676 | test.provider("reports each observed resource as events", (stack) => { |
| 677 | const cloud = makeTestCloud(); |
| 678 | return Effect.gen(function* () { |
| 679 | yield* stack.deploy( |
| 680 | Effect.gen(function* () { |
| 681 | yield* DriftResource("Clean", { value: "clean" }); |
| 682 | yield* DriftResource("Drifted", { value: "drifted" }); |
| 683 | }), |
| 684 | ); |
| 685 | cloud.resources.get("Drifted")!.value = "hijacked"; |
| 686 | |
| 687 | const events: Array<Event> = []; |
| 688 | const stk = yield* Stack; |
| 689 | yield* Drift.plan({ name: stk.name, stage: stk.stage }).pipe( |
| 690 | onEvent((event) => |
| 691 | Effect.sync(() => { |
| 692 | events.push(event); |
| 693 | }), |
| 694 | ), |
| 695 | ); |
| 696 | |
| 697 | // Phase markers bracket the observation fan-out. |
| 698 | expect( |
| 699 | events.filter((event) => event.type === "plan.phase").map(({ phase }) => phase), |
| 700 | ).toEqual(["loading-state", "computing-plan"]); |
| 701 | // Every read emits a start (so renderers can show in-flight rows) |
| 702 | // paired with a completion. |
| 703 | expect( |
| 704 | events |
| 705 | .filter((event) => event.type === "plan.resource.started") |
| 706 | .map(({ logicalId }) => logicalId) |
| 707 | .sort(), |
| 708 | ).toEqual(["Clean", "Drifted"]); |
| 709 | const nodes = events.filter((event) => event.type === "plan.resource.completed"); |
| 710 | expect(nodes).toHaveLength(2); |
| 711 | expect(nodes.map(({ completed }) => completed).sort()).toEqual([1, 2]); |
| 712 | expect(Object.fromEntries(nodes.map((event) => [event.logicalId, event.action]))).toEqual({ |
| 713 | Clean: "noop", |
| 714 | Drifted: "update", |
| 715 | }); |
| 716 | for (const event of nodes) { |
| 717 | expect(event).toMatchObject({ type: "plan.resource.completed", total: 2 }); |
| 718 | } |
| 719 | }).pipe(withCloud(cloud)); |
| 720 | }); |
| 721 | |
| 722 | test.provider("skipped resources render as noop nodes carrying persisted state", (stack) => |
| 723 | Effect.gen(function* () { |
| 724 | yield* stack.deploy(Bucket("MyBucket", { name: "test-bucket" })); |
| 725 | |
| 726 | const stk = yield* Stack; |
| 727 | const { result, plan } = yield* Drift.plan({ name: stk.name, stage: stk.stage }); |
| 728 | |
| 729 | expect(result.resources.MyBucket?.action).toEqual("skipped"); |
| 730 | expect(plan.resources.MyBucket).toMatchObject({ |
| 731 | action: "noop", |
| 732 | state: { status: "created" }, |
| 733 | resource: { LogicalId: "MyBucket", Type: "Test.Bucket" }, |
| 734 | }); |
| 735 | }), |
| 736 | ); |
| 737 | |
| 738 | test.provider("namespaced resources keep their namespace, FQN and bindings", (stack) => { |
| 739 | const cloud = makeTestCloud(); |
| 740 | return Effect.gen(function* () { |
| 741 | const Site = (id: string) => |
| 742 | Effect.gen(function* () { |
| 743 | const a = yield* DriftResource("A", { value: "a" }); |
| 744 | yield* a.bind("Env", { env: { KEY: "on" } }); |
| 745 | return a; |
| 746 | }).pipe(Namespace.push(id)); |
| 747 | |
| 748 | yield* stack.deploy(Site("Site")); |
| 749 | |
| 750 | cloud.resources.get("A")!.value = "hijacked"; |
| 751 | |
| 752 | const stk = yield* Stack; |
| 753 | const { plan } = yield* Drift.plan({ name: stk.name, stage: stk.stage }); |
| 754 | |
| 755 | const node = plan.resources["Site/A"]; |
| 756 | expect(node?.action).toEqual("update"); |
| 757 | expect(node?.resource).toMatchObject({ FQN: "Site/A", LogicalId: "A" }); |
| 758 | expect(node?.resource.Namespace).toBeDefined(); |
| 759 | // persisted bindings surface as noop rows (topology, not changes) |
| 760 | expect(node?.bindings).toMatchObject([{ sid: "Env", action: "noop" }]); |
| 761 | }).pipe(withCloud(cloud)); |
| 762 | }); |
| 763 | }); |
| 764 | |
| 765 | describe("repair events", { tags: ["unit", "local"] }, () => { |
| 766 | test.provider("repair reports progress as events", (stack) => { |
| 767 | const cloud = makeTestCloud(); |
| 768 | const events: ApplyEvent[] = []; |
| 769 | return Effect.gen(function* () { |
| 770 | yield* stack.deploy( |
| 771 | Effect.gen(function* () { |
| 772 | yield* DriftResource("Drifted", { value: "drifted" }); |
| 773 | yield* DriftResource("Missing", { value: "missing" }); |
| 774 | yield* Bucket("MyBucket", { name: "test-bucket" }); |
| 775 | }), |
| 776 | ); |
| 777 | |
| 778 | cloud.resources.get("Drifted")!.value = "hijacked"; |
| 779 | cloud.resources.delete("Missing"); |
| 780 | |
| 781 | yield* runDrift({ events }); |
| 782 | |
| 783 | // drift repair reports the update lifecycle |
| 784 | expect(events).toContainEqual({ |
| 785 | type: "apply.resource.status", |
| 786 | fqn: "Drifted", |
| 787 | id: "Drifted", |
| 788 | resourceType: "Test.DriftResource", |
| 789 | status: "updating", |
| 790 | }); |
| 791 | expect(events).toContainEqual({ |
| 792 | type: "apply.resource.status", |
| 793 | fqn: "Drifted", |
| 794 | id: "Drifted", |
| 795 | resourceType: "Test.DriftResource", |
| 796 | status: "updated", |
| 797 | }); |
| 798 | // recreation reports the create lifecycle |
| 799 | expect(events).toContainEqual({ |
| 800 | type: "apply.resource.status", |
| 801 | fqn: "Missing", |
| 802 | id: "Missing", |
| 803 | resourceType: "Test.DriftResource", |
| 804 | status: "creating", |
| 805 | }); |
| 806 | expect(events).toContainEqual({ |
| 807 | type: "apply.resource.status", |
| 808 | fqn: "Missing", |
| 809 | id: "Missing", |
| 810 | resourceType: "Test.DriftResource", |
| 811 | status: "created", |
| 812 | }); |
| 813 | // skipped resources settle with a terminal status and a reason note |
| 814 | expect(events).toContainEqual({ |
| 815 | type: "apply.resource.status", |
| 816 | fqn: "MyBucket", |
| 817 | id: "MyBucket", |
| 818 | resourceType: "Test.Bucket", |
| 819 | status: "skipped", |
| 820 | }); |
| 821 | expect( |
| 822 | events.some( |
| 823 | (e) => |
| 824 | e.type === "apply.resource.note" && e.id === "MyBucket" && e.message.includes("read"), |
| 825 | ), |
| 826 | ).toBe(true); |
| 827 | }).pipe(withCloud(cloud)); |
| 828 | }); |
| 829 | }); |
| 830 | |
| 831 | // Repairs follow the persisted `downstream` edges, like Apply: an upstream |
| 832 | // is recreated before anything that points at it. The TestCloud enforces |
| 833 | // this — a DriftResource reconcile fails when a `dependsOn` id is missing. |
| 834 | describe("dependency order", { tags: ["unit", "local"] }, () => { |
| 835 | test.provider("a deleted chain is recreated upstream first", (stack) => { |
| 836 | const cloud = makeTestCloud(); |
| 837 | return Effect.gen(function* () { |
| 838 | yield* stack.deploy( |
| 839 | Effect.gen(function* () { |
| 840 | const topic = yield* DriftResource("Topic", { value: "topic" }); |
| 841 | const queue = yield* DriftResource("Queue", { value: "queue", dependsOn: [topic.id] }); |
| 842 | yield* DriftResource("Subscription", { value: "sub", dependsOn: [queue.id] }); |
| 843 | }), |
| 844 | ); |
| 845 | expect((yield* getState("Topic"))?.downstream).toEqual(["Queue"]); |
| 846 | |
| 847 | for (const id of ["Topic", "Queue", "Subscription"]) cloud.resources.delete(id); |
| 848 | cloud.calls.length = 0; |
| 849 | const result = yield* runDrift(); |
| 850 | |
| 851 | expect(result.resources).toMatchObject({ |
| 852 | Topic: { action: "recreated" }, |
| 853 | Queue: { action: "recreated" }, |
| 854 | Subscription: { action: "recreated" }, |
| 855 | }); |
| 856 | expect(reconcileCalls(cloud)).toEqual(["Topic", "Queue", "Subscription"]); |
| 857 | }).pipe(withCloud(cloud)); |
| 858 | }); |
| 859 | |
| 860 | test.provider("a diamond waits for every upstream", (stack) => { |
| 861 | const cloud = makeTestCloud(); |
| 862 | return Effect.gen(function* () { |
| 863 | yield* stack.deploy( |
| 864 | Effect.gen(function* () { |
| 865 | const root = yield* DriftResource("Root", {}); |
| 866 | const left = yield* DriftResource("Left", { dependsOn: [root.id] }); |
| 867 | const right = yield* DriftResource("Right", { dependsOn: [root.id] }); |
| 868 | yield* DriftResource("Leaf", { dependsOn: [left.id, right.id] }); |
| 869 | }), |
| 870 | ); |
| 871 | |
| 872 | for (const id of ["Root", "Left", "Right", "Leaf"]) cloud.resources.delete(id); |
| 873 | cloud.calls.length = 0; |
| 874 | yield* runDrift(); |
| 875 | |
| 876 | const order = reconcileCalls(cloud); |
| 877 | expect(order[0]).toEqual("Root"); |
| 878 | expect(order.slice(1, 3).sort()).toEqual(["Left", "Right"]); |
| 879 | expect(order[3]).toEqual("Leaf"); |
| 880 | }).pipe(withCloud(cloud)); |
| 881 | }); |
| 882 | |
| 883 | test.provider("independent resources are repaired concurrently", (stack) => { |
| 884 | const cloud = makeTestCloud(); |
| 885 | return Effect.gen(function* () { |
| 886 | yield* stack.deploy( |
| 887 | Effect.gen(function* () { |
| 888 | yield* DriftResource("X", {}); |
| 889 | yield* DriftResource("Y", {}); |
| 890 | }), |
| 891 | ); |
| 892 | cloud.resources.delete("X"); |
| 893 | cloud.resources.delete("Y"); |
| 894 | |
| 895 | // Each create waits for the other to start: only a concurrent |
| 896 | // repair can finish. |
| 897 | const started = { |
| 898 | X: yield* Deferred.make<void>(), |
| 899 | Y: yield* Deferred.make<void>(), |
| 900 | }; |
| 901 | const hooks = { |
| 902 | create: (id: string) => |
| 903 | id === "X" || id === "Y" |
| 904 | ? Deferred.succeed(started[id], undefined).pipe( |
| 905 | Effect.andThen(Deferred.await(started[id === "X" ? "Y" : "X"])), |
| 906 | Effect.timeout("2 seconds"), |
| 907 | ) |
| 908 | : Effect.void, |
| 909 | }; |
| 910 | const result = yield* runDrift().pipe( |
| 911 | Effect.provide(Layer.succeed(TestResourceHooks, hooks)), |
| 912 | ); |
| 913 | |
| 914 | expect(result.resources).toMatchObject({ |
| 915 | X: { action: "recreated" }, |
| 916 | Y: { action: "recreated" }, |
| 917 | }); |
| 918 | }).pipe(withCloud(cloud)); |
| 919 | }); |
| 920 | |
| 921 | test.provider("a missing resource under an intact upstream recreates directly", (stack) => { |
| 922 | const cloud = makeTestCloud(); |
| 923 | return Effect.gen(function* () { |
| 924 | yield* stack.deploy( |
| 925 | Effect.gen(function* () { |
| 926 | const topic = yield* DriftResource("Topic", { value: "topic" }); |
| 927 | yield* DriftResource("Subscription", { value: "sub", dependsOn: [topic.id] }); |
| 928 | }), |
| 929 | ); |
| 930 | cloud.resources.delete("Subscription"); |
| 931 | |
| 932 | cloud.calls.length = 0; |
| 933 | const result = yield* runDrift(); |
| 934 | |
| 935 | expect(result.resources).toMatchObject({ |
| 936 | Topic: { action: "unchanged" }, |
| 937 | Subscription: { action: "recreated" }, |
| 938 | }); |
| 939 | expect(reconcileCalls(cloud)).toEqual(["Subscription"]); |
| 940 | }).pipe(withCloud(cloud)); |
| 941 | }); |
| 942 | |
| 943 | test.provider( |
| 944 | "a drifted resource is repaired after its missing upstream is recreated", |
| 945 | (stack) => { |
| 946 | const cloud = makeTestCloud(); |
| 947 | return Effect.gen(function* () { |
| 948 | yield* stack.deploy( |
| 949 | Effect.gen(function* () { |
| 950 | const topic = yield* DriftResource("Topic", { value: "topic" }); |
| 951 | yield* DriftResource("Subscription", { value: "sub", dependsOn: [topic.id] }); |
| 952 | }), |
| 953 | ); |
| 954 | cloud.resources.delete("Topic"); |
| 955 | cloud.resources.get("Subscription")!.value = "hijacked"; |
| 956 | |
| 957 | cloud.calls.length = 0; |
| 958 | const result = yield* runDrift(); |
| 959 | |
| 960 | expect(result.resources).toMatchObject({ |
| 961 | Topic: { action: "recreated" }, |
| 962 | Subscription: { action: "repaired", attr: { value: "sub" } }, |
| 963 | }); |
| 964 | expect(reconcileCalls(cloud)).toEqual(["Topic", "Subscription"]); |
| 965 | }).pipe(withCloud(cloud)); |
| 966 | }, |
| 967 | ); |
| 968 | |
| 969 | test.provider("namespaced resources order by FQN", (stack) => { |
| 970 | const cloud = makeTestCloud(); |
| 971 | return Effect.gen(function* () { |
| 972 | yield* stack.deploy( |
| 973 | Effect.gen(function* () { |
| 974 | const topic = yield* DriftResource("Topic", {}); |
| 975 | yield* DriftResource("Subscription", { dependsOn: [topic.id] }); |
| 976 | }).pipe(Namespace.push("Uploads")), |
| 977 | ); |
| 978 | expect((yield* getState("Uploads/Topic"))?.downstream).toEqual(["Uploads/Subscription"]); |
| 979 | cloud.resources.delete("Topic"); |
| 980 | cloud.resources.delete("Subscription"); |
| 981 | |
| 982 | cloud.calls.length = 0; |
| 983 | const result = yield* runDrift(); |
| 984 | |
| 985 | expect(result.resources["Uploads/Subscription"]?.action).toEqual("recreated"); |
| 986 | expect(reconcileCalls(cloud)).toEqual(["Topic", "Subscription"]); |
| 987 | }).pipe(withCloud(cloud)); |
| 988 | }); |
| 989 | |
| 990 | test.provider("cyclic or dangling persisted edges do not deadlock the repair", () => { |
| 991 | const cloud = makeTestCloud(); |
| 992 | const row = (fqn: string, downstream: string[]) => ({ |
| 993 | status: "created", |
| 994 | fqn, |
| 995 | logicalId: fqn, |
| 996 | instanceId, |
| 997 | resourceType: "Test.DriftResource", |
| 998 | namespace: undefined, |
| 999 | providerVersion: 0, |
| 1000 | props: { value: fqn }, |
| 1001 | attr: { id: fqn, value: fqn, tags: {}, env: {} }, |
| 1002 | bindings: [], |
| 1003 | downstream, |
| 1004 | }); |
| 1005 | return Effect.gen(function* () { |
| 1006 | // Merged historical edges can disagree with each other; a cycle among |
| 1007 | // them, or an edge to a row that no longer exists, must not block. |
| 1008 | yield* seed({ A: row("A", ["B", "Ghost"]), B: row("B", ["A"]), C: row("C", ["A"]) }); |
| 1009 | |
| 1010 | const result = yield* runDrift().pipe(Effect.timeout("2 seconds")); |
| 1011 | |
| 1012 | expect(result.resources).toMatchObject({ |
| 1013 | A: { action: "recreated" }, |
| 1014 | B: { action: "recreated" }, |
| 1015 | C: { action: "recreated" }, |
| 1016 | }); |
| 1017 | // the acyclic edge still orders C before A |
| 1018 | const order = reconcileCalls(cloud); |
| 1019 | expect(order.indexOf("C")).toBeLessThan(order.indexOf("A")); |
| 1020 | }).pipe(withCloud(cloud)); |
| 1021 | }); |
| 1022 | |
| 1023 | test.provider("dry run reports a deleted chain without ordering any repair", (stack) => { |
| 1024 | const cloud = makeTestCloud(); |
| 1025 | return Effect.gen(function* () { |
| 1026 | yield* stack.deploy( |
| 1027 | Effect.gen(function* () { |
| 1028 | const topic = yield* DriftResource("Topic", {}); |
| 1029 | yield* DriftResource("Subscription", { dependsOn: [topic.id] }); |
| 1030 | }), |
| 1031 | ); |
| 1032 | cloud.resources.delete("Topic"); |
| 1033 | cloud.resources.delete("Subscription"); |
| 1034 | |
| 1035 | cloud.calls.length = 0; |
| 1036 | const result = yield* runDrift({ dryRun: true }); |
| 1037 | |
| 1038 | expect(result.resources).toMatchObject({ |
| 1039 | Topic: { action: "missing" }, |
| 1040 | Subscription: { action: "missing" }, |
| 1041 | }); |
| 1042 | expect(reconcileCalls(cloud)).toEqual([]); |
| 1043 | }).pipe(withCloud(cloud)); |
| 1044 | }); |
| 1045 | }); |
| 1046 | |
| 1047 | // Missing resources are recreated bare, then bound once every missing |
| 1048 | // resource exists again: binding data can point at a resource recreated |
| 1049 | // later, and a binding edge inside a cycle is not a persisted `downstream` |
| 1050 | // edge. The TestCloud fails a reconcile whose binding `targets` are missing. |
| 1051 | describe("bindings of recreated resources", { tags: ["unit", "local"] }, () => { |
| 1052 | test.provider("a missing resource is recreated bare, then bound", (stack) => { |
| 1053 | const cloud = makeTestCloud(); |
| 1054 | return Effect.gen(function* () { |
| 1055 | yield* stack.deploy( |
| 1056 | Effect.gen(function* () { |
| 1057 | const a = yield* DriftResource("A", { value: "a" }); |
| 1058 | yield* a.bind("Env", { env: { FEATURE_FLAG: "on" } }); |
| 1059 | }), |
| 1060 | ); |
| 1061 | cloud.resources.delete("A"); |
| 1062 | |
| 1063 | cloud.calls.length = 0; |
| 1064 | const events: ApplyEvent[] = []; |
| 1065 | const result = yield* runDrift({ events }); |
| 1066 | |
| 1067 | expect(result.resources.A).toMatchObject({ |
| 1068 | action: "recreated", |
| 1069 | attr: { env: { FEATURE_FLAG: "on" } }, |
| 1070 | }); |
| 1071 | expect(reconcileTrace(cloud)).toEqual(["A[]", "A[Env]"]); |
| 1072 | expect(cloud.resources.get("A")!.env).toEqual({ FEATURE_FLAG: "on" }); |
| 1073 | expect(yield* getState("A")).toMatchObject({ |
| 1074 | status: "created", |
| 1075 | bindings: [{ sid: "Env" }], |
| 1076 | attr: { env: { FEATURE_FLAG: "on" } }, |
| 1077 | }); |
| 1078 | // one create lifecycle, even across both reconciles |
| 1079 | expect(statusesOf(events, "A")).toEqual(["creating", "created"]); |
| 1080 | }).pipe(withCloud(cloud)); |
| 1081 | }); |
| 1082 | |
| 1083 | test.provider("a missing resource without bindings is reconciled once", (stack) => { |
| 1084 | const cloud = makeTestCloud(); |
| 1085 | return Effect.gen(function* () { |
| 1086 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 1087 | cloud.resources.delete("A"); |
| 1088 | |
| 1089 | cloud.calls.length = 0; |
| 1090 | yield* runDrift(); |
| 1091 | |
| 1092 | expect(reconcileTrace(cloud)).toEqual(["A[]"]); |
| 1093 | }).pipe(withCloud(cloud)); |
| 1094 | }); |
| 1095 | |
| 1096 | test.provider("a drifted resource keeps its bindings in a single reconcile", (stack) => { |
| 1097 | const cloud = makeTestCloud(); |
| 1098 | return Effect.gen(function* () { |
| 1099 | yield* stack.deploy( |
| 1100 | Effect.gen(function* () { |
| 1101 | const a = yield* DriftResource("A", { value: "a" }); |
| 1102 | yield* a.bind("Env", { env: { FEATURE_FLAG: "on" } }); |
| 1103 | }), |
| 1104 | ); |
| 1105 | cloud.resources.get("A")!.value = "hijacked"; |
| 1106 | |
| 1107 | cloud.calls.length = 0; |
| 1108 | yield* runDrift(); |
| 1109 | |
| 1110 | expect(reconcileTrace(cloud)).toEqual(["A[Env]"]); |
| 1111 | }).pipe(withCloud(cloud)); |
| 1112 | }); |
| 1113 | |
| 1114 | // The #1536 shape: the topic's props point at the bucket, and the bucket's |
| 1115 | // notification binding points back at the topic. |
| 1116 | test.provider("a binding cycle is recreated bare, then bound", (stack) => { |
| 1117 | const cloud = makeTestCloud(); |
| 1118 | return Effect.gen(function* () { |
| 1119 | yield* stack.deploy( |
| 1120 | Effect.gen(function* () { |
| 1121 | const bucket = yield* DriftResource("Bucket", { value: "bucket" }); |
| 1122 | const topic = yield* DriftResource("Topic", { value: "topic", dependsOn: [bucket.id] }); |
| 1123 | yield* bucket.bind("Notifications", { targets: [topic.id] }); |
| 1124 | }), |
| 1125 | ); |
| 1126 | // the cyclic binding edge is not persisted as a downstream edge |
| 1127 | expect((yield* getState("Topic"))?.downstream).toEqual([]); |
| 1128 | cloud.resources.delete("Bucket"); |
| 1129 | cloud.resources.delete("Topic"); |
| 1130 | |
| 1131 | cloud.calls.length = 0; |
| 1132 | const result = yield* runDrift(); |
| 1133 | |
| 1134 | expect(result.resources).toMatchObject({ |
| 1135 | Bucket: { action: "recreated" }, |
| 1136 | Topic: { action: "recreated" }, |
| 1137 | }); |
| 1138 | expect(reconcileTrace(cloud)).toEqual(["Bucket[]", "Topic[]", "Bucket[Notifications]"]); |
| 1139 | expect((yield* getState("Bucket"))?.bindings).toMatchObject([{ sid: "Notifications" }]); |
| 1140 | }).pipe(withCloud(cloud)); |
| 1141 | }); |
| 1142 | |
| 1143 | test.provider("a binding pointing at a later-recreated resource is attached last", (stack) => { |
| 1144 | const cloud = makeTestCloud(); |
| 1145 | return Effect.gen(function* () { |
| 1146 | yield* stack.deploy( |
| 1147 | Effect.gen(function* () { |
| 1148 | const a = yield* DriftResource("A", {}); |
| 1149 | const b = yield* DriftResource("B", { dependsOn: [a.id] }); |
| 1150 | yield* a.bind("Points", { targets: [b.id] }); |
| 1151 | }), |
| 1152 | ); |
| 1153 | cloud.resources.delete("A"); |
| 1154 | cloud.resources.delete("B"); |
| 1155 | |
| 1156 | cloud.calls.length = 0; |
| 1157 | yield* runDrift(); |
| 1158 | |
| 1159 | expect(reconcileTrace(cloud)).toEqual(["A[]", "B[]", "A[Points]"]); |
| 1160 | }).pipe(withCloud(cloud)); |
| 1161 | }); |
| 1162 | |
| 1163 | test.provider("bindings are attached in dependency order", (stack) => { |
| 1164 | const cloud = makeTestCloud(); |
| 1165 | return Effect.gen(function* () { |
| 1166 | yield* stack.deploy( |
| 1167 | Effect.gen(function* () { |
| 1168 | const a = yield* DriftResource("A", {}); |
| 1169 | yield* a.bind("Env", { env: { KEY: "a" } }); |
| 1170 | const b = yield* DriftResource("B", { dependsOn: [a.id] }); |
| 1171 | yield* b.bind("Env", { env: { KEY: "b" } }); |
| 1172 | }), |
| 1173 | ); |
| 1174 | cloud.resources.delete("A"); |
| 1175 | cloud.resources.delete("B"); |
| 1176 | |
| 1177 | cloud.calls.length = 0; |
| 1178 | yield* runDrift(); |
| 1179 | |
| 1180 | expect(reconcileTrace(cloud)).toEqual(["A[]", "B[]", "A[Env]", "B[Env]"]); |
| 1181 | }).pipe(withCloud(cloud)); |
| 1182 | }); |
| 1183 | |
| 1184 | test.provider( |
| 1185 | "a recreated dependent is bound after its drifted upstream is repaired", |
| 1186 | (stack) => { |
| 1187 | const cloud = makeTestCloud(); |
| 1188 | return Effect.gen(function* () { |
| 1189 | yield* stack.deploy( |
| 1190 | Effect.gen(function* () { |
| 1191 | const a = yield* DriftResource("A", { value: "a" }); |
| 1192 | const b = yield* DriftResource("B", { dependsOn: [a.id] }); |
| 1193 | yield* b.bind("Env", { env: { KEY: "b" } }); |
| 1194 | }), |
| 1195 | ); |
| 1196 | cloud.resources.get("A")!.value = "hijacked"; |
| 1197 | cloud.resources.delete("B"); |
| 1198 | |
| 1199 | cloud.calls.length = 0; |
| 1200 | const result = yield* runDrift(); |
| 1201 | |
| 1202 | expect(result.resources).toMatchObject({ |
| 1203 | A: { action: "repaired" }, |
| 1204 | B: { action: "recreated", attr: { env: { KEY: "b" } } }, |
| 1205 | }); |
| 1206 | expect(reconcileTrace(cloud)).toEqual(["B[]", "A[]", "B[Env]"]); |
| 1207 | }).pipe(withCloud(cloud)); |
| 1208 | }, |
| 1209 | ); |
| 1210 | |
| 1211 | test.provider("a failed bind leaves an unbound row that the next deploy binds", (stack) => { |
| 1212 | const cloud = makeTestCloud(); |
| 1213 | return Effect.gen(function* () { |
| 1214 | const program = Effect.gen(function* () { |
| 1215 | const a = yield* DriftResource("A", { value: "a" }); |
| 1216 | yield* a.bind("Env", { env: { FEATURE_FLAG: "on" } }); |
| 1217 | return a; |
| 1218 | }); |
| 1219 | yield* stack.deploy(program); |
| 1220 | cloud.resources.delete("A"); |
| 1221 | |
| 1222 | // the bare recreate succeeds; attaching the binding (an update of the |
| 1223 | // recreated resource) fails |
| 1224 | const exit = yield* runDrift().pipe( |
| 1225 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("A", "update"))), |
| 1226 | Effect.exit, |
| 1227 | ); |
| 1228 | expect(Exit.isFailure(exit)).toBe(true); |
| 1229 | expect(cloud.resources.get("A")).toMatchObject({ value: "a", env: {} }); |
| 1230 | // state records the resource without the binding it does not have |
| 1231 | expect(yield* getState("A")).toMatchObject({ status: "created", bindings: [] }); |
| 1232 | |
| 1233 | // so the next deploy plans the binding and attaches it |
| 1234 | const deployed = yield* stack.deploy(program); |
| 1235 | expect(deployed.env).toEqual({ FEATURE_FLAG: "on" }); |
| 1236 | expect(cloud.resources.get("A")!.env).toEqual({ FEATURE_FLAG: "on" }); |
| 1237 | }).pipe(withCloud(cloud)); |
| 1238 | }); |
| 1239 | }); |
| 1240 | |
| 1241 | // Like Apply, a dependent never runs against an upstream whose repair |
| 1242 | // failed: it settles as skipped, and the drift fails with the upstream's |
| 1243 | // error alone. |
| 1244 | describe("dependency failures", { tags: ["unit", "local"] }, () => { |
| 1245 | const chain = (...ids: string[]) => |
| 1246 | Effect.gen(function* () { |
| 1247 | let previous: DriftResource | undefined; |
| 1248 | for (const id of ids) { |
| 1249 | previous = yield* DriftResource(id, { |
| 1250 | value: id, |
| 1251 | dependsOn: previous ? [previous.id] : undefined, |
| 1252 | }); |
| 1253 | } |
| 1254 | }); |
| 1255 | |
| 1256 | const failureOf = <A, E>(exit: Exit.Exit<A, E>) => { |
| 1257 | expect(Exit.isFailure(exit)).toBe(true); |
| 1258 | return Exit.isFailure(exit) ? Cause.pretty(exit.cause) : ""; |
| 1259 | }; |
| 1260 | |
| 1261 | test.provider("a failed upstream recreate skips its dependents", (stack) => { |
| 1262 | const cloud = makeTestCloud(); |
| 1263 | return Effect.gen(function* () { |
| 1264 | yield* stack.deploy( |
| 1265 | Effect.gen(function* () { |
| 1266 | yield* chain("Topic", "Subscription"); |
| 1267 | yield* DriftResource("Other", {}); |
| 1268 | }), |
| 1269 | ); |
| 1270 | for (const id of ["Topic", "Subscription", "Other"]) cloud.resources.delete(id); |
| 1271 | |
| 1272 | cloud.calls.length = 0; |
| 1273 | const events: ApplyEvent[] = []; |
| 1274 | const exit = yield* runDrift({ events }).pipe( |
| 1275 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("Topic", "create"))), |
| 1276 | Effect.exit, |
| 1277 | ); |
| 1278 | |
| 1279 | const rendered = failureOf(exit); |
| 1280 | expect(rendered).toContain("Resource 'Topic' (Test.DriftResource) failed during create"); |
| 1281 | expect(rendered).not.toContain("'Subscription'"); |
| 1282 | // the dependent never reached its provider |
| 1283 | expect(reconcileCalls(cloud).sort()).toEqual(["Other", "Topic"]); |
| 1284 | expect(cloud.resources.has("Other")).toBe(true); |
| 1285 | expect(statusesOf(events, "Subscription")).toEqual(["skipped"]); |
| 1286 | expect(events).toContainEqual( |
| 1287 | expect.objectContaining({ |
| 1288 | type: "apply.resource.note", |
| 1289 | id: "Subscription", |
| 1290 | message: "not repaired: upstream 'Topic' failed", |
| 1291 | }), |
| 1292 | ); |
| 1293 | // the dependent's row is untouched |
| 1294 | expect(yield* getState("Subscription")).toMatchObject({ status: "created" }); |
| 1295 | |
| 1296 | // the next drift converges the rest of the chain |
| 1297 | const result = yield* runDrift(); |
| 1298 | expect(result.resources).toMatchObject({ |
| 1299 | Topic: { action: "recreated" }, |
| 1300 | Subscription: { action: "recreated" }, |
| 1301 | Other: { action: "unchanged" }, |
| 1302 | }); |
| 1303 | }).pipe(withCloud(cloud)); |
| 1304 | }); |
| 1305 | |
| 1306 | test.provider("a failed upstream repair skips its drifted dependents", (stack) => { |
| 1307 | const cloud = makeTestCloud(); |
| 1308 | return Effect.gen(function* () { |
| 1309 | yield* stack.deploy(chain("Topic", "Subscription")); |
| 1310 | cloud.resources.get("Topic")!.value = "hijacked"; |
| 1311 | cloud.resources.get("Subscription")!.value = "hijacked"; |
| 1312 | |
| 1313 | cloud.calls.length = 0; |
| 1314 | const exit = yield* runDrift().pipe( |
| 1315 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("Topic", "update"))), |
| 1316 | Effect.exit, |
| 1317 | ); |
| 1318 | |
| 1319 | expect(failureOf(exit)).toContain( |
| 1320 | "Resource 'Topic' (Test.DriftResource) failed during update", |
| 1321 | ); |
| 1322 | expect(reconcileCalls(cloud)).toEqual(["Topic"]); |
| 1323 | expect(cloud.resources.get("Subscription")).toMatchObject({ value: "hijacked" }); |
| 1324 | }).pipe(withCloud(cloud)); |
| 1325 | }); |
| 1326 | |
| 1327 | test.provider("a failed upstream bind skips dependents still to be repaired", (stack) => { |
| 1328 | const cloud = makeTestCloud(); |
| 1329 | return Effect.gen(function* () { |
| 1330 | yield* stack.deploy( |
| 1331 | Effect.gen(function* () { |
| 1332 | const topic = yield* DriftResource("Topic", {}); |
| 1333 | yield* topic.bind("Env", { env: { KEY: "on" } }); |
| 1334 | yield* DriftResource("Missing", { dependsOn: [topic.id] }); |
| 1335 | yield* DriftResource("Drifted", { value: "drifted", dependsOn: [topic.id] }); |
| 1336 | }), |
| 1337 | ); |
| 1338 | cloud.resources.delete("Topic"); |
| 1339 | cloud.resources.delete("Missing"); |
| 1340 | cloud.resources.get("Drifted")!.value = "hijacked"; |
| 1341 | |
| 1342 | cloud.calls.length = 0; |
| 1343 | // the bare recreate succeeds, attaching the binding fails |
| 1344 | const exit = yield* runDrift().pipe( |
| 1345 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("Topic", "update"))), |
| 1346 | Effect.exit, |
| 1347 | ); |
| 1348 | |
| 1349 | expect(failureOf(exit)).toContain( |
| 1350 | "Resource 'Topic' (Test.DriftResource) failed during create", |
| 1351 | ); |
| 1352 | // the missing dependent only needed Topic to exist, so it was |
| 1353 | // recreated before the bind; the drifted dependent waits on the bind |
| 1354 | expect(reconcileTrace(cloud)).toEqual(["Topic[]", "Missing[]", "Topic[Env]"]); |
| 1355 | expect(cloud.resources.has("Missing")).toBe(true); |
| 1356 | expect(cloud.resources.get("Drifted")).toMatchObject({ value: "hijacked" }); |
| 1357 | }).pipe(withCloud(cloud)); |
| 1358 | }); |
| 1359 | |
| 1360 | test.provider("skips propagate down a chain", (stack) => { |
| 1361 | const cloud = makeTestCloud(); |
| 1362 | return Effect.gen(function* () { |
| 1363 | yield* stack.deploy(chain("A", "B", "C")); |
| 1364 | for (const id of ["A", "B", "C"]) cloud.resources.delete(id); |
| 1365 | |
| 1366 | cloud.calls.length = 0; |
| 1367 | const events: ApplyEvent[] = []; |
| 1368 | const exit = yield* runDrift({ events }).pipe( |
| 1369 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("A", "create"))), |
| 1370 | Effect.exit, |
| 1371 | ); |
| 1372 | |
| 1373 | failureOf(exit); |
| 1374 | expect(reconcileCalls(cloud)).toEqual(["A"]); |
| 1375 | expect(statusesOf(events, "B")).toEqual(["skipped"]); |
| 1376 | expect(statusesOf(events, "C")).toEqual(["skipped"]); |
| 1377 | // each skip names its direct upstream |
| 1378 | expect(events).toContainEqual( |
| 1379 | expect.objectContaining({ id: "C", message: "not repaired: upstream 'B' failed" }), |
| 1380 | ); |
| 1381 | }).pipe(withCloud(cloud)); |
| 1382 | }); |
| 1383 | |
| 1384 | test.provider("a failed diamond branch skips the leaf but not the other branch", (stack) => { |
| 1385 | const cloud = makeTestCloud(); |
| 1386 | return Effect.gen(function* () { |
| 1387 | yield* stack.deploy( |
| 1388 | Effect.gen(function* () { |
| 1389 | const root = yield* DriftResource("Root", {}); |
| 1390 | const left = yield* DriftResource("Left", { dependsOn: [root.id] }); |
| 1391 | const right = yield* DriftResource("Right", { dependsOn: [root.id] }); |
| 1392 | yield* DriftResource("Leaf", { dependsOn: [left.id, right.id] }); |
| 1393 | }), |
| 1394 | ); |
| 1395 | for (const id of ["Root", "Left", "Right", "Leaf"]) cloud.resources.delete(id); |
| 1396 | |
| 1397 | cloud.calls.length = 0; |
| 1398 | const exit = yield* runDrift().pipe( |
| 1399 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("Left", "create"))), |
| 1400 | Effect.exit, |
| 1401 | ); |
| 1402 | |
| 1403 | failureOf(exit); |
| 1404 | expect(reconcileCalls(cloud).sort()).toEqual(["Left", "Right", "Root"]); |
| 1405 | expect(cloud.resources.has("Right")).toBe(true); |
| 1406 | expect(cloud.resources.has("Leaf")).toBe(false); |
| 1407 | }).pipe(withCloud(cloud)); |
| 1408 | }); |
| 1409 | |
| 1410 | test.provider("a failed dependent leaves its upstream repaired", (stack) => { |
| 1411 | const cloud = makeTestCloud(); |
| 1412 | return Effect.gen(function* () { |
| 1413 | yield* stack.deploy(chain("Topic", "Subscription")); |
| 1414 | cloud.resources.delete("Topic"); |
| 1415 | cloud.resources.delete("Subscription"); |
| 1416 | |
| 1417 | const exit = yield* runDrift().pipe( |
| 1418 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("Subscription", "create"))), |
| 1419 | Effect.exit, |
| 1420 | ); |
| 1421 | |
| 1422 | failureOf(exit); |
| 1423 | expect(cloud.resources.has("Topic")).toBe(true); |
| 1424 | expect(yield* getState("Topic")).toMatchObject({ status: "created" }); |
| 1425 | const detected = yield* runDrift({ dryRun: true }); |
| 1426 | expect(detected.resources).toMatchObject({ |
| 1427 | Topic: { action: "unchanged" }, |
| 1428 | Subscription: { action: "missing" }, |
| 1429 | }); |
| 1430 | }).pipe(withCloud(cloud)); |
| 1431 | }); |
| 1432 | |
| 1433 | test.provider("independent failures are all reported", (stack) => { |
| 1434 | const cloud = makeTestCloud(); |
| 1435 | return Effect.gen(function* () { |
| 1436 | yield* stack.deploy( |
| 1437 | Effect.gen(function* () { |
| 1438 | yield* DriftResource("X", {}); |
| 1439 | yield* DriftResource("Y", {}); |
| 1440 | }), |
| 1441 | ); |
| 1442 | cloud.resources.delete("X"); |
| 1443 | cloud.resources.get("Y")!.value = "hijacked"; |
| 1444 | |
| 1445 | const exit = yield* runDrift().pipe( |
| 1446 | Effect.provide( |
| 1447 | Layer.succeed( |
| 1448 | TestResourceHooks, |
| 1449 | failOnMultiple([ |
| 1450 | { id: "X", hook: "create" }, |
| 1451 | { id: "Y", hook: "update" }, |
| 1452 | ]), |
| 1453 | ), |
| 1454 | ), |
| 1455 | Effect.exit, |
| 1456 | ); |
| 1457 | |
| 1458 | const rendered = failureOf(exit); |
| 1459 | expect(rendered).toContain("Resource 'X' (Test.DriftResource) failed during create"); |
| 1460 | expect(rendered).toContain("Resource 'Y' (Test.DriftResource) failed during update"); |
| 1461 | }).pipe(withCloud(cloud)); |
| 1462 | }); |
| 1463 | |
| 1464 | test.provider("a failed bare recreate skips attaching its bindings", (stack) => { |
| 1465 | const cloud = makeTestCloud(); |
| 1466 | return Effect.gen(function* () { |
| 1467 | yield* stack.deploy( |
| 1468 | Effect.gen(function* () { |
| 1469 | const a = yield* DriftResource("A", {}); |
| 1470 | yield* a.bind("Env", { env: { KEY: "on" } }); |
| 1471 | }), |
| 1472 | ); |
| 1473 | cloud.resources.delete("A"); |
| 1474 | |
| 1475 | cloud.calls.length = 0; |
| 1476 | const exit = yield* runDrift().pipe( |
| 1477 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("A", "create"))), |
| 1478 | Effect.exit, |
| 1479 | ); |
| 1480 | |
| 1481 | failureOf(exit); |
| 1482 | expect(reconcileTrace(cloud)).toEqual(["A[]"]); |
| 1483 | // the row is untouched, so the resource is still reported missing |
| 1484 | expect(yield* getState("A")).toMatchObject({ bindings: [{ sid: "Env" }] }); |
| 1485 | const detected = yield* runDrift({ dryRun: true }); |
| 1486 | expect(detected.resources.A?.action).toEqual("missing"); |
| 1487 | }).pipe(withCloud(cloud)); |
| 1488 | }); |
| 1489 | |
| 1490 | test.provider("an interrupted repair keeps finished repairs and resumes", (stack) => { |
| 1491 | const cloud = makeTestCloud(); |
| 1492 | return Effect.gen(function* () { |
| 1493 | yield* stack.deploy(chain("A", "B")); |
| 1494 | cloud.resources.delete("A"); |
| 1495 | cloud.resources.delete("B"); |
| 1496 | |
| 1497 | const exit = yield* runDrift().pipe( |
| 1498 | Effect.provide( |
| 1499 | Layer.succeed(TestResourceHooks, { |
| 1500 | create: (id: string) => (id === "B" ? Effect.never : Effect.void), |
| 1501 | }), |
| 1502 | ), |
| 1503 | Effect.timeout("200 millis"), |
| 1504 | Effect.exit, |
| 1505 | ); |
| 1506 | |
| 1507 | expect(Exit.isFailure(exit)).toBe(true); |
| 1508 | expect(cloud.resources.has("A")).toBe(true); |
| 1509 | expect(cloud.resources.has("B")).toBe(false); |
| 1510 | expect(yield* getState("B")).toMatchObject({ status: "created", attr: { value: "B" } }); |
| 1511 | |
| 1512 | const result = yield* runDrift(); |
| 1513 | expect(result.resources).toMatchObject({ |
| 1514 | A: { action: "unchanged" }, |
| 1515 | B: { action: "recreated" }, |
| 1516 | }); |
| 1517 | }).pipe(withCloud(cloud)); |
| 1518 | }); |
| 1519 | }); |
| 1520 | |
| 1521 | describe("dependency order of drifted resources", { tags: ["unit", "local"] }, () => { |
| 1522 | test.provider("a drifted chain is repaired upstream first", (stack) => { |
| 1523 | const cloud = makeTestCloud(); |
| 1524 | return Effect.gen(function* () { |
| 1525 | yield* stack.deploy( |
| 1526 | Effect.gen(function* () { |
| 1527 | const topic = yield* DriftResource("Topic", { value: "topic" }); |
| 1528 | yield* DriftResource("Subscription", { value: "sub", dependsOn: [topic.id] }); |
| 1529 | }), |
| 1530 | ); |
| 1531 | cloud.resources.get("Topic")!.value = "hijacked"; |
| 1532 | cloud.resources.get("Subscription")!.value = "hijacked"; |
| 1533 | |
| 1534 | cloud.calls.length = 0; |
| 1535 | const result = yield* runDrift(); |
| 1536 | |
| 1537 | expect(result.resources).toMatchObject({ |
| 1538 | Topic: { action: "repaired" }, |
| 1539 | Subscription: { action: "repaired" }, |
| 1540 | }); |
| 1541 | expect(reconcileCalls(cloud)).toEqual(["Topic", "Subscription"]); |
| 1542 | }).pipe(withCloud(cloud)); |
| 1543 | }); |
| 1544 | |
| 1545 | test.provider("a missing dependent of a drifted upstream is recreated first", (stack) => { |
| 1546 | const cloud = makeTestCloud(); |
| 1547 | return Effect.gen(function* () { |
| 1548 | yield* stack.deploy( |
| 1549 | Effect.gen(function* () { |
| 1550 | const topic = yield* DriftResource("Topic", { value: "topic" }); |
| 1551 | yield* DriftResource("Subscription", { value: "sub", dependsOn: [topic.id] }); |
| 1552 | }), |
| 1553 | ); |
| 1554 | cloud.resources.get("Topic")!.value = "hijacked"; |
| 1555 | cloud.resources.delete("Subscription"); |
| 1556 | |
| 1557 | cloud.calls.length = 0; |
| 1558 | const result = yield* runDrift(); |
| 1559 | |
| 1560 | expect(result.resources).toMatchObject({ |
| 1561 | Topic: { action: "repaired", attr: { value: "topic" } }, |
| 1562 | Subscription: { action: "recreated" }, |
| 1563 | }); |
| 1564 | // recreations only need their upstream to exist, so they run before |
| 1565 | // any in-place repair |
| 1566 | expect(reconcileCalls(cloud)).toEqual(["Subscription", "Topic"]); |
| 1567 | }).pipe(withCloud(cloud)); |
| 1568 | }); |
| 1569 | |
| 1570 | test.provider("independent drifted resources are repaired concurrently", (stack) => { |
| 1571 | const cloud = makeTestCloud(); |
| 1572 | return Effect.gen(function* () { |
| 1573 | yield* stack.deploy( |
| 1574 | Effect.gen(function* () { |
| 1575 | yield* DriftResource("X", { value: "x" }); |
| 1576 | yield* DriftResource("Y", { value: "y" }); |
| 1577 | }), |
| 1578 | ); |
| 1579 | cloud.resources.get("X")!.value = "hijacked"; |
| 1580 | cloud.resources.get("Y")!.value = "hijacked"; |
| 1581 | |
| 1582 | const started = { |
| 1583 | X: yield* Deferred.make<void>(), |
| 1584 | Y: yield* Deferred.make<void>(), |
| 1585 | }; |
| 1586 | const hooks = { |
| 1587 | update: (id: string) => |
| 1588 | id === "X" || id === "Y" |
| 1589 | ? Deferred.succeed(started[id], undefined).pipe( |
| 1590 | Effect.andThen(Deferred.await(started[id === "X" ? "Y" : "X"])), |
| 1591 | Effect.timeout("2 seconds"), |
| 1592 | ) |
| 1593 | : Effect.void, |
| 1594 | }; |
| 1595 | const result = yield* runDrift().pipe( |
| 1596 | Effect.provide(Layer.succeed(TestResourceHooks, hooks)), |
| 1597 | ); |
| 1598 | |
| 1599 | expect(result.resources).toMatchObject({ |
| 1600 | X: { action: "repaired" }, |
| 1601 | Y: { action: "repaired" }, |
| 1602 | }); |
| 1603 | }).pipe(withCloud(cloud)); |
| 1604 | }); |
| 1605 | |
| 1606 | test.provider("a stuck upstream is skipped and does not block its dependents", (stack) => { |
| 1607 | const cloud = makeTestCloud(); |
| 1608 | return Effect.gen(function* () { |
| 1609 | yield* stack.deploy( |
| 1610 | Effect.gen(function* () { |
| 1611 | const topic = yield* DriftResource("Topic", {}); |
| 1612 | yield* DriftResource("Subscription", { dependsOn: [topic.id] }); |
| 1613 | }), |
| 1614 | ); |
| 1615 | const topic = yield* getState("Topic"); |
| 1616 | yield* seed({ Topic: { ...topic, status: "creating" } }); |
| 1617 | cloud.resources.delete("Subscription"); |
| 1618 | |
| 1619 | const result = yield* runDrift(); |
| 1620 | |
| 1621 | expect(result.resources).toMatchObject({ |
| 1622 | Topic: { action: "skipped" }, |
| 1623 | Subscription: { action: "recreated" }, |
| 1624 | }); |
| 1625 | }).pipe(withCloud(cloud)); |
| 1626 | }); |
| 1627 | }); |
| 1628 | |
| 1629 | describe("persisted rows", { tags: ["unit", "local"] }, () => { |
| 1630 | const deployed = Effect.gen(function* () { |
| 1631 | const a = yield* DriftResource("A", { value: "a" }).pipe(RemovalPolicy.retain(true)); |
| 1632 | yield* DriftResource("B", { value: "b", dependsOn: [a.id] }); |
| 1633 | return a; |
| 1634 | }).pipe(Namespace.push("Site")); |
| 1635 | |
| 1636 | const carried = (row: ResourceState | undefined) => ({ |
| 1637 | namespace: row?.namespace, |
| 1638 | instanceId: row?.instanceId, |
| 1639 | downstream: row?.downstream, |
| 1640 | removalPolicy: row?.removalPolicy, |
| 1641 | providerVersion: row?.providerVersion, |
| 1642 | props: row?.props, |
| 1643 | bindings: row?.bindings, |
| 1644 | }); |
| 1645 | |
| 1646 | test.provider("a recreated row keeps everything but its attributes", (stack) => { |
| 1647 | const cloud = makeTestCloud(); |
| 1648 | return Effect.gen(function* () { |
| 1649 | yield* stack.deploy(deployed); |
| 1650 | const before = yield* getState("Site/A"); |
| 1651 | expect(before?.removalPolicy).toEqual("retain"); |
| 1652 | cloud.resources.delete("A"); |
| 1653 | |
| 1654 | yield* runDrift(); |
| 1655 | |
| 1656 | const after = yield* getState("Site/A"); |
| 1657 | expect(after?.status).toEqual("created"); |
| 1658 | expect(carried(after)).toEqual(carried(before)); |
| 1659 | }).pipe(withCloud(cloud)); |
| 1660 | }); |
| 1661 | |
| 1662 | test.provider("a recreated and rebound row keeps everything but its attributes", (stack) => { |
| 1663 | const cloud = makeTestCloud(); |
| 1664 | return Effect.gen(function* () { |
| 1665 | yield* stack.deploy( |
| 1666 | Effect.gen(function* () { |
| 1667 | const a = yield* deployed; |
| 1668 | yield* a.bind("Env", { env: { KEY: "on" } }); |
| 1669 | }), |
| 1670 | ); |
| 1671 | const before = yield* getState("Site/A"); |
| 1672 | cloud.resources.delete("A"); |
| 1673 | |
| 1674 | yield* runDrift(); |
| 1675 | |
| 1676 | const after = yield* getState("Site/A"); |
| 1677 | expect(after?.status).toEqual("created"); |
| 1678 | expect(carried(after)).toEqual(carried(before)); |
| 1679 | }).pipe(withCloud(cloud)); |
| 1680 | }); |
| 1681 | |
| 1682 | test.provider("a repaired row keeps everything but its attributes", (stack) => { |
| 1683 | const cloud = makeTestCloud(); |
| 1684 | return Effect.gen(function* () { |
| 1685 | yield* stack.deploy(deployed); |
| 1686 | const before = yield* getState("Site/A"); |
| 1687 | cloud.resources.get("A")!.value = "hijacked"; |
| 1688 | |
| 1689 | yield* runDrift(); |
| 1690 | |
| 1691 | const after = yield* getState("Site/A"); |
| 1692 | expect(after?.status).toEqual("updated"); |
| 1693 | expect(carried(after)).toEqual(carried(before)); |
| 1694 | }).pipe(withCloud(cloud)); |
| 1695 | }); |
| 1696 | |
| 1697 | test.provider("a recreated, rebound graph is unchanged on the next drift", (stack) => { |
| 1698 | const cloud = makeTestCloud(); |
| 1699 | return Effect.gen(function* () { |
| 1700 | yield* stack.deploy( |
| 1701 | Effect.gen(function* () { |
| 1702 | const bucket = yield* DriftResource("Bucket", {}); |
| 1703 | const topic = yield* DriftResource("Topic", { dependsOn: [bucket.id] }); |
| 1704 | yield* bucket.bind("Notifications", { targets: [topic.id], env: { KEY: "on" } }); |
| 1705 | }), |
| 1706 | ); |
| 1707 | cloud.resources.delete("Bucket"); |
| 1708 | cloud.resources.delete("Topic"); |
| 1709 | yield* runDrift(); |
| 1710 | |
| 1711 | cloud.calls.length = 0; |
| 1712 | const result = yield* runDrift(); |
| 1713 | |
| 1714 | expect(result.resources).toMatchObject({ |
| 1715 | Bucket: { action: "unchanged" }, |
| 1716 | Topic: { action: "unchanged" }, |
| 1717 | }); |
| 1718 | expect(reconcileCalls(cloud)).toEqual([]); |
| 1719 | }).pipe(withCloud(cloud)); |
| 1720 | }); |
| 1721 | |
| 1722 | test.provider("the next deploy after a recreate is a noop", (stack) => { |
| 1723 | const cloud = makeTestCloud(); |
| 1724 | return Effect.gen(function* () { |
| 1725 | const program = Effect.gen(function* () { |
| 1726 | const a = yield* DriftResource("A", { value: "a" }); |
| 1727 | yield* a.bind("Env", { env: { KEY: "on" } }); |
| 1728 | yield* DriftResource("B", { dependsOn: [a.id] }); |
| 1729 | }); |
| 1730 | yield* stack.deploy(program); |
| 1731 | cloud.resources.delete("A"); |
| 1732 | cloud.resources.delete("B"); |
| 1733 | yield* runDrift(); |
| 1734 | |
| 1735 | cloud.calls.length = 0; |
| 1736 | yield* stack.deploy(program); |
| 1737 | expect(reconcileCalls(cloud)).toEqual([]); |
| 1738 | }).pipe(withCloud(cloud)); |
| 1739 | }); |
| 1740 | }); |
| 1741 | |
| 1742 | // Drift repairs a row with the provider variant of the mode that created it, |
| 1743 | // and the repaired row keeps its stamp. |
| 1744 | describe("provider modes", { tags: ["unit", "local"] }, () => { |
| 1745 | const callsFor = (stackName: string, op: "reconcile" | "delete") => |
| 1746 | modalCalls.filter((c) => c.stack === stackName && c.op === op).map((c) => c.mode); |
| 1747 | |
| 1748 | test.provider("a local row is recreated by the local provider and stays local", (stack) => { |
| 1749 | const cloud = makeTestCloud(); |
| 1750 | return Effect.gen(function* () { |
| 1751 | yield* inDev(stack.deploy(ModalResource("A", { value: "v" }))); |
| 1752 | expect((yield* getState("A"))?.providerMode).toEqual("local"); |
| 1753 | cloud.resources.delete("A"); |
| 1754 | |
| 1755 | const before = callsFor(stack.name, "reconcile").length; |
| 1756 | const result = yield* runDrift(); |
| 1757 | |
| 1758 | expect(result.resources.A).toMatchObject({ |
| 1759 | action: "recreated", |
| 1760 | attr: { runtime: "local" }, |
| 1761 | }); |
| 1762 | expect(callsFor(stack.name, "reconcile").slice(before)).toEqual(["local"]); |
| 1763 | expect(yield* getState("A")).toMatchObject({ status: "created", providerMode: "local" }); |
| 1764 | }).pipe(withCloud(cloud)); |
| 1765 | }); |
| 1766 | |
| 1767 | test.provider("a live row is repaired by the live provider and stays live", (stack) => { |
| 1768 | const cloud = makeTestCloud(); |
| 1769 | return Effect.gen(function* () { |
| 1770 | yield* stack.deploy(ModalResource("A", { value: "v" })); |
| 1771 | expect((yield* getState("A"))?.providerMode).toEqual("live"); |
| 1772 | cloud.resources.get("A")!.value = "hijacked"; |
| 1773 | |
| 1774 | const before = callsFor(stack.name, "reconcile").length; |
| 1775 | const result = yield* runDrift(); |
| 1776 | |
| 1777 | expect(result.resources.A).toMatchObject({ action: "repaired", attr: { value: "v" } }); |
| 1778 | expect(callsFor(stack.name, "reconcile").slice(before)).toEqual(["live"]); |
| 1779 | expect(yield* getState("A")).toMatchObject({ status: "updated", providerMode: "live" }); |
| 1780 | }).pipe(withCloud(cloud)); |
| 1781 | }); |
| 1782 | |
| 1783 | test.provider("a repaired local row is not replaced by the next dev deploy", (stack) => { |
| 1784 | const cloud = makeTestCloud(); |
| 1785 | return Effect.gen(function* () { |
| 1786 | yield* inDev(stack.deploy(ModalResource("A", { value: "v" }))); |
| 1787 | const instanceId = (yield* getState("A"))?.instanceId; |
| 1788 | cloud.resources.delete("A"); |
| 1789 | yield* runDrift(); |
| 1790 | |
| 1791 | const before = callsFor(stack.name, "delete").length; |
| 1792 | yield* inDev(stack.deploy(ModalResource("A", { value: "v" }))); |
| 1793 | |
| 1794 | expect(callsFor(stack.name, "delete").slice(before)).toEqual([]); |
| 1795 | expect(yield* getState("A")).toMatchObject({ instanceId, providerMode: "local" }); |
| 1796 | }).pipe(withCloud(cloud)); |
| 1797 | }); |
| 1798 | }); |
| 1799 | |
| 1800 | describe("repair progress", { tags: ["unit", "local"] }, () => { |
| 1801 | test.provider("repair reports each observation as it lands", (stack) => { |
| 1802 | const cloud = makeTestCloud(); |
| 1803 | return Effect.gen(function* () { |
| 1804 | yield* stack.deploy( |
| 1805 | Effect.gen(function* () { |
| 1806 | yield* DriftResource("Clean", {}); |
| 1807 | yield* DriftResource("Drifted", { value: "drifted" }); |
| 1808 | yield* DriftResource("Missing", {}); |
| 1809 | }), |
| 1810 | ); |
| 1811 | cloud.resources.get("Drifted")!.value = "hijacked"; |
| 1812 | cloud.resources.delete("Missing"); |
| 1813 | |
| 1814 | const events: Array<Event> = []; |
| 1815 | yield* runDrift().pipe( |
| 1816 | onEvent((event) => |
| 1817 | Effect.sync(() => { |
| 1818 | events.push(event); |
| 1819 | }), |
| 1820 | ), |
| 1821 | ); |
| 1822 | |
| 1823 | const nodes = events.filter((event) => event.type === "plan.resource.completed"); |
| 1824 | expect(Object.fromEntries(nodes.map((event) => [event.logicalId, event.action]))).toEqual({ |
| 1825 | Clean: "noop", |
| 1826 | Drifted: "update", |
| 1827 | Missing: "create", |
| 1828 | }); |
| 1829 | expect(nodes.map(({ completed }) => completed).sort()).toEqual([1, 2, 3]); |
| 1830 | }).pipe(withCloud(cloud)); |
| 1831 | }); |
| 1832 | |
| 1833 | test.provider("a failed bind settles the row as failed, never created", (stack) => { |
| 1834 | const cloud = makeTestCloud(); |
| 1835 | return Effect.gen(function* () { |
| 1836 | yield* stack.deploy( |
| 1837 | Effect.gen(function* () { |
| 1838 | const a = yield* DriftResource("A", {}); |
| 1839 | yield* a.bind("Env", { env: { KEY: "on" } }); |
| 1840 | }), |
| 1841 | ); |
| 1842 | cloud.resources.delete("A"); |
| 1843 | |
| 1844 | const events: ApplyEvent[] = []; |
| 1845 | yield* runDrift({ events }).pipe( |
| 1846 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("A", "update"))), |
| 1847 | Effect.exit, |
| 1848 | ); |
| 1849 | |
| 1850 | expect(statusesOf(events, "A")).toEqual(["creating", "fail"]); |
| 1851 | }).pipe(withCloud(cloud)); |
| 1852 | }); |
| 1853 | |
| 1854 | test.provider("a failed repair settles the row as failed", (stack) => { |
| 1855 | const cloud = makeTestCloud(); |
| 1856 | return Effect.gen(function* () { |
| 1857 | yield* stack.deploy(DriftResource("A", { value: "a" })); |
| 1858 | cloud.resources.get("A")!.value = "hijacked"; |
| 1859 | |
| 1860 | const events: ApplyEvent[] = []; |
| 1861 | yield* runDrift({ events }).pipe( |
| 1862 | Effect.provide(Layer.succeed(TestResourceHooks, failOn("A", "update"))), |
| 1863 | Effect.exit, |
| 1864 | ); |
| 1865 | |
| 1866 | expect(statusesOf(events, "A")).toEqual(["updating", "fail"]); |
| 1867 | }).pipe(withCloud(cloud)); |
| 1868 | }); |
| 1869 | }); |
| 1870 | |
| 1871 | describe("observation", { tags: ["unit", "local"] }, () => { |
| 1872 | test.provider("every resource is read concurrently, regardless of dependencies", (stack) => { |
| 1873 | const cloud = makeTestCloud(); |
| 1874 | return Effect.gen(function* () { |
| 1875 | yield* stack.deploy( |
| 1876 | Effect.gen(function* () { |
| 1877 | const topic = yield* DriftResource("Topic", {}); |
| 1878 | yield* DriftResource("Subscription", { dependsOn: [topic.id] }); |
| 1879 | }), |
| 1880 | ); |
| 1881 | |
| 1882 | // Each read waits for the other to start: only concurrent reads finish. |
| 1883 | const started = { |
| 1884 | Topic: yield* Deferred.make<void>(), |
| 1885 | Subscription: yield* Deferred.make<void>(), |
| 1886 | }; |
| 1887 | cloud.onRead = (id) => |
| 1888 | id === "Topic" || id === "Subscription" |
| 1889 | ? Deferred.succeed(started[id], undefined).pipe( |
| 1890 | Effect.andThen(Deferred.await(started[id === "Topic" ? "Subscription" : "Topic"])), |
| 1891 | Effect.timeout("2 seconds"), |
| 1892 | Effect.orDie, |
| 1893 | ) |
| 1894 | : Effect.void; |
| 1895 | |
| 1896 | const result = yield* runDrift({ dryRun: true }); |
| 1897 | expect(result.resources).toMatchObject({ |
| 1898 | Topic: { action: "unchanged" }, |
| 1899 | Subscription: { action: "unchanged" }, |
| 1900 | }); |
| 1901 | }).pipe(withCloud(cloud)); |
| 1902 | }); |
| 1903 | }); |
| 1904 | |
| 1905 | // Drift repairs from persisted, fully-resolved props: it has no stack |
| 1906 | // program to re-evaluate. When a recreated upstream comes back with a new |
| 1907 | // identity (an AWS-assigned ARN), dependents keep the old value until the |
| 1908 | // next deploy re-resolves their props. |
| 1909 | describe("recreated identities", { tags: ["unit", "local"] }, () => { |
| 1910 | const program = Effect.gen(function* () { |
| 1911 | const topic = yield* DriftResource("Topic", {}); |
| 1912 | const subscription = yield* DriftResource("Subscription", { ref: topic.arn }); |
| 1913 | return { topic: topic.arn, subscription: subscription.ref }; |
| 1914 | }); |
| 1915 | |
| 1916 | test.provider("a dependent picks up its upstream's new identity on the next deploy", (stack) => { |
| 1917 | const cloud = makeTestCloud(); |
| 1918 | return Effect.gen(function* () { |
| 1919 | const before = yield* stack.deploy(program); |
| 1920 | expect(before).toEqual({ topic: "Topic#1", subscription: "Topic#1" }); |
| 1921 | cloud.resources.delete("Topic"); |
| 1922 | |
| 1923 | const result = yield* runDrift(); |
| 1924 | expect(result.resources).toMatchObject({ |
| 1925 | Topic: { action: "recreated", attr: { arn: "Topic#2" } }, |
| 1926 | // still consistent with its persisted props, so drift leaves it |
| 1927 | Subscription: { action: "unchanged", attr: { ref: "Topic#1" } }, |
| 1928 | }); |
| 1929 | |
| 1930 | cloud.calls.length = 0; |
| 1931 | const after = yield* stack.deploy(program); |
| 1932 | expect(after).toEqual({ topic: "Topic#2", subscription: "Topic#2" }); |
| 1933 | expect(reconcileCalls(cloud)).toEqual(["Subscription"]); |
| 1934 | expect(cloud.resources.get("Subscription")).toMatchObject({ ref: "Topic#2" }); |
| 1935 | }).pipe(withCloud(cloud)); |
| 1936 | }); |
| 1937 | |
| 1938 | test.provider( |
| 1939 | "a dependent recreated alongside its upstream is corrected by the next deploy", |
| 1940 | (stack) => { |
| 1941 | const cloud = makeTestCloud(); |
| 1942 | return Effect.gen(function* () { |
| 1943 | yield* stack.deploy(program); |
| 1944 | cloud.resources.delete("Topic"); |
| 1945 | cloud.resources.delete("Subscription"); |
| 1946 | |
| 1947 | yield* runDrift(); |
| 1948 | // recreated from its persisted props |
| 1949 | expect(cloud.resources.get("Subscription")).toMatchObject({ ref: "Topic#1" }); |
| 1950 | |
| 1951 | const after = yield* stack.deploy(program); |
| 1952 | expect(after).toEqual({ topic: "Topic#2", subscription: "Topic#2" }); |
| 1953 | }).pipe(withCloud(cloud)); |
| 1954 | }, |
| 1955 | ); |
| 1956 | }); |
| 1957 | |
| 1958 | describe("unstable statuses", { tags: ["unit", "local"] }, () => { |
| 1959 | const base = { |
| 1960 | fqn: "R", |
| 1961 | logicalId: "R", |
| 1962 | instanceId, |
| 1963 | resourceType: "Test.DriftResource", |
| 1964 | namespace: undefined, |
| 1965 | providerVersion: 0, |
| 1966 | props: { value: "v" }, |
| 1967 | attr: { id: "R", value: "v", tags: {}, env: {} }, |
| 1968 | bindings: [], |
| 1969 | downstream: [], |
| 1970 | }; |
| 1971 | const old = { ...base, status: "created" }; |
| 1972 | const rows = { |
| 1973 | creating: { ...base, status: "creating" }, |
| 1974 | updating: { |
| 1975 | ...base, |
| 1976 | status: "updating", |
| 1977 | old: { props: base.props, attr: base.attr, bindings: [] }, |
| 1978 | }, |
| 1979 | replacing: { ...base, status: "replacing", deleteFirst: false, old }, |
| 1980 | replaced: { ...base, status: "replaced", deleteFirst: false, old }, |
| 1981 | deleting: { ...base, status: "deleting" }, |
| 1982 | }; |
| 1983 | |
| 1984 | for (const [status, row] of Object.entries(rows)) { |
| 1985 | test.provider(`a '${status}' row is skipped without touching the cloud`, () => { |
| 1986 | const cloud = makeTestCloud(); |
| 1987 | return Effect.gen(function* () { |
| 1988 | yield* seed({ R: row }); |
| 1989 | |
| 1990 | const result = yield* runDrift(); |
| 1991 | |
| 1992 | expect(result.resources.R?.action).toEqual("skipped"); |
| 1993 | expect(result.resources.R?.reason).toContain( |
| 1994 | status === "replacing" || status === "replaced" ? "replacement" : status, |
| 1995 | ); |
| 1996 | expect(cloud.calls).toEqual([]); |
| 1997 | expect((yield* getState("R"))?.status).toEqual(status); |
| 1998 | }).pipe(withCloud(cloud)); |
| 1999 | }); |
| 2000 | } |
| 2001 | }); |
| 2002 | |
| 2003 | describe("Redacted props and attributes", { tags: ["unit", "local"] }, () => { |
| 2004 | const secretAttrs = (string: string) => ({ string, redacted: Redacted.make("hunter2") }) as never; |
| 2005 | |
| 2006 | test.provider("an equal Redacted attribute is not drift", (stack) => |
| 2007 | Effect.gen(function* () { |
| 2008 | yield* stack.deploy(TestResource("A", { string: "x", redacted: Redacted.make("hunter2") })); |
| 2009 | const persisted = yield* getState<{ attr: TestResource["Attributes"] }>("A"); |
| 2010 | |
| 2011 | const result = yield* runDrift().pipe( |
| 2012 | Effect.provide( |
| 2013 | Layer.succeed(TestResourceHooks, { |
| 2014 | read: () => Effect.succeed({ ...persisted!.attr, redacted: Redacted.make("hunter2") }), |
| 2015 | }), |
| 2016 | ), |
| 2017 | ); |
| 2018 | |
| 2019 | expect(result.resources.A?.action).toEqual("unchanged"); |
| 2020 | }), |
| 2021 | ); |
| 2022 | |
| 2023 | test.provider("a recreate keeps Redacted props and attributes redacted", (stack) => |
| 2024 | Effect.gen(function* () { |
| 2025 | yield* stack.deploy(TestResource("A", { string: "x", redacted: Redacted.make("hunter2") })); |
| 2026 | |
| 2027 | const result = yield* runDrift().pipe( |
| 2028 | Effect.provide(Layer.succeed(TestResourceHooks, { read: () => Effect.succeed(undefined) })), |
| 2029 | ); |
| 2030 | |
| 2031 | expect(result.resources.A?.action).toEqual("recreated"); |
| 2032 | const state = yield* getState("A"); |
| 2033 | expect(Redacted.isRedacted((state!.props as any).redacted)).toBe(true); |
| 2034 | expect(Redacted.value((state!.props as any).redacted)).toBe("hunter2"); |
| 2035 | expect(Redacted.isRedacted((state!.attr as any).redacted)).toBe(true); |
| 2036 | expect(Redacted.value((state!.attr as any).redacted)).toBe("hunter2"); |
| 2037 | }), |
| 2038 | ); |
| 2039 | |
| 2040 | test.provider("a repair keeps Redacted props redacted", (stack) => |
| 2041 | Effect.gen(function* () { |
| 2042 | yield* stack.deploy(TestResource("A", { string: "x", redacted: Redacted.make("hunter2") })); |
| 2043 | |
| 2044 | const result = yield* runDrift().pipe( |
| 2045 | Effect.provide( |
| 2046 | Layer.succeed(TestResourceHooks, { read: () => Effect.succeed(secretAttrs("hijacked")) }), |
| 2047 | ), |
| 2048 | ); |
| 2049 | |
| 2050 | expect(result.resources.A?.action).toEqual("repaired"); |
| 2051 | const state = yield* getState("A"); |
| 2052 | expect((state!.attr as any).string).toEqual("x"); |
| 2053 | expect(Redacted.value((state!.props as any).redacted)).toBe("hunter2"); |
| 2054 | expect(Redacted.value((state!.attr as any).redacted)).toBe("hunter2"); |
| 2055 | }), |
| 2056 | ); |
| 2057 | }); |