| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 5 | import { describe, expect } from "alchemy-test"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import DlmTestFunctionLive, { DlmTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "DLMBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([ |
| 20 | Schedule.fixed("2 seconds"), |
| 21 | Schedule.recurs(75), |
| 22 | ]); |
| 23 | |
| 24 | let baseUrl: string; |
| 25 | let functionArn: string; |
| 26 | |
| 27 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 28 | readonly status: number; |
| 29 | readonly body: string; |
| 30 | }> {} |
| 31 | |
| 32 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 33 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 34 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 35 | // 4xx/assertion failure surfaces immediately. |
| 36 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 37 | HttpClient.execute(request).pipe( |
| 38 | Effect.flatMap((response) => |
| 39 | response.status >= 500 |
| 40 | ? response.text.pipe( |
| 41 | Effect.flatMap((body) => |
| 42 | Effect.fail( |
| 43 | new TransientUpstream({ status: response.status, body }), |
| 44 | ), |
| 45 | ), |
| 46 | ) |
| 47 | : Effect.succeed(response), |
| 48 | ), |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "TransientUpstream", |
| 51 | schedule: Schedule.max([ |
| 52 | Schedule.exponential("500 millis"), |
| 53 | Schedule.recurs(6), |
| 54 | ]), |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | const getJson = (path: string) => |
| 59 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe( |
| 60 | Effect.flatMap((r) => r.json), |
| 61 | ); |
| 62 | |
| 63 | describe.sequential( |
| 64 | "DLM Bindings", |
| 65 | { tags: ["provider:aws", "provider:aws:dlm", "provider:aws:lambda", "live"] }, |
| 66 | () => { |
| 67 | beforeAll( |
| 68 | Effect.gen(function* () { |
| 69 | yield* Effect.logInfo("DLM test setup: destroying previous resources"); |
| 70 | yield* sharedStack.destroy(); |
| 71 | |
| 72 | yield* Effect.logInfo("DLM test setup: deploying fixture"); |
| 73 | const attrs = yield* sharedStack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | return yield* DlmTestFunction; |
| 76 | }).pipe(Effect.provide(DlmTestFunctionLive)), |
| 77 | ); |
| 78 | |
| 79 | expect(attrs.functionUrl).toBeTruthy(); |
| 80 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 81 | functionArn = attrs.functionArn; |
| 82 | |
| 83 | const readinessUrl = `${baseUrl}/bindings`; |
| 84 | yield* Effect.logInfo( |
| 85 | `DLM test setup: probing readiness at ${readinessUrl}`, |
| 86 | ); |
| 87 | yield* HttpClient.get(readinessUrl).pipe( |
| 88 | Effect.flatMap((response) => |
| 89 | response.status === 200 |
| 90 | ? Effect.succeed(response) |
| 91 | : Effect.fail( |
| 92 | new Error(`Function not ready: ${response.status}`), |
| 93 | ), |
| 94 | ), |
| 95 | Effect.tapError((error) => |
| 96 | Effect.logWarning( |
| 97 | `DLM test setup: fixture not ready yet (${String(error)})`, |
| 98 | ), |
| 99 | ), |
| 100 | Effect.retry({ schedule: readinessPolicy }), |
| 101 | ); |
| 102 | }), |
| 103 | { timeout: 240_000 }, |
| 104 | ); |
| 105 | |
| 106 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 107 | |
| 108 | describe("binding registration", () => { |
| 109 | test.provider("both capabilities initialize in the runtime", (_stack) => |
| 110 | Effect.gen(function* () { |
| 111 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 112 | expect(response.bound).toHaveLength(2); |
| 113 | }), |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | describe("GetLifecyclePolicy", () => { |
| 118 | test.provider( |
| 119 | "reads the bound policy's detail (injected policy id)", |
| 120 | (_stack) => |
| 121 | Effect.gen(function* () { |
| 122 | const response = (yield* getJson("/policy")) as { |
| 123 | policyId: string; |
| 124 | state: string; |
| 125 | policyType: string; |
| 126 | }; |
| 127 | expect(response.policyId).toMatch(/^policy-/); |
| 128 | expect(response.state).toBe("DISABLED"); |
| 129 | expect(response.policyType).toBe("EBS_SNAPSHOT_MANAGEMENT"); |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | describe("GetLifecyclePolicies", () => { |
| 135 | test.provider( |
| 136 | "enumerates the account's policies including the fixture's", |
| 137 | (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | const policy = (yield* getJson("/policy")) as { policyId: string }; |
| 140 | const response = (yield* getJson("/policies")) as { |
| 141 | ids: string[]; |
| 142 | }; |
| 143 | expect(response.ids).toContain(policy.policyId); |
| 144 | }), |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | describe( |
| 149 | "consumePolicyEvents", |
| 150 | { tags: ["provider:aws:eventbridge"] }, |
| 151 | () => { |
| 152 | test.provider( |
| 153 | "the deploy created an EventBridge rule targeting the function", |
| 154 | (_stack) => |
| 155 | Effect.gen(function* () { |
| 156 | // Out-of-band via distilled: the fixture's consumePolicyEvents |
| 157 | // must have materialized as a rule on the default bus with the |
| 158 | // Lambda as target. |
| 159 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 160 | TargetArn: functionArn, |
| 161 | }); |
| 162 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 163 | }), |
| 164 | ); |
| 165 | }, |
| 166 | ); |
| 167 | }, |
| 168 | ); |