| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { describe, expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import EksTestFunctionLive, { EksTestFunction } from "./handler.ts"; |
| 9 | |
| 10 | const testOptions = { providers: AWS.providers() }; |
| 11 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 12 | const sharedStack = Core.scratchStack(testOptions, "EksBindings"); |
| 13 | |
| 14 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 15 | // over 60s on a fresh deploy. |
| 16 | const readinessPolicy = Schedule.max([ |
| 17 | Schedule.fixed("2 seconds"), |
| 18 | Schedule.recurs(75), |
| 19 | ]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | |
| 23 | const getJson = (path: string) => |
| 24 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 25 | Effect.flatMap((response) => |
| 26 | response.status >= 500 |
| 27 | ? Effect.fail(new Error(`transient ${response.status}`)) |
| 28 | : Effect.succeed(response), |
| 29 | ), |
| 30 | // The fixture occasionally answers a transient 5xx under load (cold |
| 31 | // re-init, IAM propagation on the freshly attached policy). Bounded retry. |
| 32 | Effect.retry({ |
| 33 | schedule: Schedule.max([ |
| 34 | Schedule.exponential("500 millis"), |
| 35 | Schedule.recurs(6), |
| 36 | ]), |
| 37 | }), |
| 38 | Effect.flatMap((response) => response.json), |
| 39 | ); |
| 40 | |
| 41 | // The account-scoped EKS bindings need no cluster: ListClusters, |
| 42 | // ListAccessPolicies, DescribeClusterVersions, DescribeAddonVersions, and |
| 43 | // DescribeAddonConfiguration all answer against the account/region and the |
| 44 | // AWS-managed catalogs. The cluster-scoped bindings are exercised by the |
| 45 | // env-gated ClusterBindings.test.ts (an EKS control plane takes ~10+ minutes |
| 46 | // to provision — far beyond the routine test budget). |
| 47 | describe.sequential( |
| 48 | "EKS Bindings", |
| 49 | { tags: ["provider:aws", "provider:aws:eks", "provider:aws:lambda", "live"] }, |
| 50 | () => { |
| 51 | beforeAll( |
| 52 | Effect.gen(function* () { |
| 53 | yield* Effect.logInfo("EKS test setup: destroying previous resources"); |
| 54 | yield* sharedStack.destroy(); |
| 55 | |
| 56 | yield* Effect.logInfo("EKS test setup: deploying fixture"); |
| 57 | const attrs = yield* sharedStack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* EksTestFunction; |
| 60 | }).pipe(Effect.provide(EksTestFunctionLive)), |
| 61 | ); |
| 62 | |
| 63 | expect(attrs.functionUrl).toBeTruthy(); |
| 64 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 65 | |
| 66 | const readinessUrl = `${baseUrl}/bindings`; |
| 67 | yield* Effect.logInfo( |
| 68 | `EKS test setup: probing readiness at ${readinessUrl}`, |
| 69 | ); |
| 70 | yield* HttpClient.get(readinessUrl).pipe( |
| 71 | Effect.flatMap((response) => |
| 72 | response.status === 200 |
| 73 | ? Effect.succeed(response) |
| 74 | : Effect.fail( |
| 75 | new Error(`Function not ready: ${response.status}`), |
| 76 | ), |
| 77 | ), |
| 78 | Effect.tapError((error) => |
| 79 | Effect.logWarning( |
| 80 | `EKS test setup: fixture not ready yet (${String(error)})`, |
| 81 | ), |
| 82 | ), |
| 83 | Effect.retry({ schedule: readinessPolicy }), |
| 84 | ); |
| 85 | }), |
| 86 | { timeout: 240_000 }, |
| 87 | ); |
| 88 | |
| 89 | afterAll( |
| 90 | Effect.gen(function* () { |
| 91 | yield* sharedStack.destroy(); |
| 92 | }), |
| 93 | { timeout: 120_000 }, |
| 94 | ); |
| 95 | |
| 96 | describe("binding registration", () => { |
| 97 | test.provider("all 5 capabilities initialize in the runtime", (_stack) => |
| 98 | Effect.gen(function* () { |
| 99 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 100 | expect(response.bound).toHaveLength(5); |
| 101 | expect(response.bound).toContain("listClusters"); |
| 102 | expect(response.bound).toContain("describeAddonConfiguration"); |
| 103 | }), |
| 104 | ); |
| 105 | }); |
| 106 | |
| 107 | describe("ListClusters", () => { |
| 108 | test.provider("enumerates the account's clusters", (_stack) => |
| 109 | Effect.gen(function* () { |
| 110 | const response = (yield* getJson("/clusters")) as { count: number }; |
| 111 | expect(typeof response.count).toBe("number"); |
| 112 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 113 | }), |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | describe("ListAccessPolicies", () => { |
| 118 | test.provider("returns the AWS-managed access policy catalog", (_stack) => |
| 119 | Effect.gen(function* () { |
| 120 | const response = (yield* getJson("/access-policies")) as { |
| 121 | count: number; |
| 122 | names: string[]; |
| 123 | }; |
| 124 | // AWS ships a standing set of managed policies |
| 125 | // (AmazonEKSAdminPolicy, AmazonEKSViewPolicy, …). |
| 126 | expect(response.count).toBeGreaterThan(0); |
| 127 | expect(response.names).toContain("AmazonEKSViewPolicy"); |
| 128 | }), |
| 129 | ); |
| 130 | }); |
| 131 | |
| 132 | describe("DescribeClusterVersions", () => { |
| 133 | test.provider("reports a default Kubernetes version", (_stack) => |
| 134 | Effect.gen(function* () { |
| 135 | const response = (yield* getJson("/cluster-versions")) as { |
| 136 | count: number; |
| 137 | defaultVersion?: string; |
| 138 | }; |
| 139 | expect(response.count).toBeGreaterThan(0); |
| 140 | expect(response.defaultVersion).toMatch(/^\d+\.\d+$/); |
| 141 | }), |
| 142 | ); |
| 143 | }); |
| 144 | |
| 145 | describe("DescribeAddonVersions", () => { |
| 146 | test.provider("finds the vpc-cni add-on in the catalog", (_stack) => |
| 147 | Effect.gen(function* () { |
| 148 | const response = (yield* getJson("/addon-versions")) as { |
| 149 | count: number; |
| 150 | addonName?: string; |
| 151 | }; |
| 152 | expect(response.count).toBe(1); |
| 153 | expect(response.addonName).toBe("vpc-cni"); |
| 154 | }), |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | describe("DescribeAddonConfiguration", () => { |
| 159 | test.provider( |
| 160 | "reads the configuration schema for a live vpc-cni version", |
| 161 | (_stack) => |
| 162 | Effect.gen(function* () { |
| 163 | const response = (yield* getJson("/addon-schema")) as { |
| 164 | addonVersion?: string; |
| 165 | hasSchema?: boolean; |
| 166 | }; |
| 167 | expect(response.addonVersion).toBeTruthy(); |
| 168 | expect(response.hasSchema).toBe(true); |
| 169 | }), |
| 170 | ); |
| 171 | }); |
| 172 | }, |
| 173 | ); |