| 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 OpenSearchBindingsTestFunctionLive, { |
| 9 | OpenSearchBindingsTestFunction, |
| 10 | } from "./fixtures/handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "OpenSearchBindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy. |
| 18 | const readinessPolicy = Schedule.max([ |
| 19 | Schedule.fixed("2 seconds"), |
| 20 | Schedule.recurs(75), |
| 21 | ]); |
| 22 | |
| 23 | let baseUrl: string; |
| 24 | |
| 25 | const getJson = (path: string) => |
| 26 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 27 | Effect.flatMap((response) => |
| 28 | response.status >= 500 |
| 29 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 30 | : Effect.succeed(response), |
| 31 | ), |
| 32 | Effect.retry({ |
| 33 | schedule: Schedule.max([ |
| 34 | Schedule.exponential("500 millis"), |
| 35 | Schedule.recurs(6), |
| 36 | ]), |
| 37 | }), |
| 38 | Effect.flatMap((r) => r.json), |
| 39 | ); |
| 40 | |
| 41 | describe.sequential( |
| 42 | "OpenSearch Bindings", |
| 43 | { |
| 44 | tags: [ |
| 45 | "provider:aws", |
| 46 | "provider:aws:lambda", |
| 47 | "provider:aws:opensearch", |
| 48 | "live", |
| 49 | ], |
| 50 | }, |
| 51 | () => { |
| 52 | beforeAll( |
| 53 | Effect.gen(function* () { |
| 54 | yield* Effect.logInfo( |
| 55 | "OpenSearch test setup: destroying previous resources", |
| 56 | ); |
| 57 | yield* sharedStack.destroy(); |
| 58 | |
| 59 | yield* Effect.logInfo("OpenSearch test setup: deploying fixture"); |
| 60 | const { functionUrl } = yield* sharedStack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | return yield* OpenSearchBindingsTestFunction; |
| 63 | }).pipe(Effect.provide(OpenSearchBindingsTestFunctionLive)), |
| 64 | ); |
| 65 | |
| 66 | expect(functionUrl).toBeTruthy(); |
| 67 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 68 | |
| 69 | const readinessUrl = `${baseUrl}/bindings`; |
| 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.retry({ schedule: readinessPolicy }), |
| 79 | ); |
| 80 | }), |
| 81 | { timeout: 240_000 }, |
| 82 | ); |
| 83 | |
| 84 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 85 | |
| 86 | describe("binding registration", () => { |
| 87 | test.provider("all capabilities initialize in the runtime", (_stack) => |
| 88 | Effect.gen(function* () { |
| 89 | const response = yield* getJson("/bindings"); |
| 90 | expect((response as any).bound).toHaveLength(19); |
| 91 | }), |
| 92 | ); |
| 93 | }); |
| 94 | |
| 95 | describe("DescribeDomain", () => { |
| 96 | test.provider( |
| 97 | "surfaces the typed not-found tag for a nonexistent domain", |
| 98 | (_stack) => |
| 99 | Effect.gen(function* () { |
| 100 | const response = yield* getJson("/domain"); |
| 101 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 102 | }), |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | describe("DescribeDomains", () => { |
| 107 | test.provider( |
| 108 | "returns an empty status list for unknown domain names", |
| 109 | (_stack) => |
| 110 | Effect.gen(function* () { |
| 111 | const response = yield* getJson("/domains-batch"); |
| 112 | expect((response as any).count).toBe(0); |
| 113 | }), |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | describe("DescribeDomainConfig", () => { |
| 118 | test.provider( |
| 119 | "surfaces the typed not-found tag for a nonexistent domain", |
| 120 | (_stack) => |
| 121 | Effect.gen(function* () { |
| 122 | const response = yield* getJson("/domain-config"); |
| 123 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 124 | }), |
| 125 | ); |
| 126 | }); |
| 127 | |
| 128 | describe("DescribeDomainHealth", () => { |
| 129 | test.provider( |
| 130 | // OpenSearch reports a missing domain on this operation as the typed |
| 131 | // `BaseException` ("Domain not found"), not ResourceNotFoundException. |
| 132 | "surfaces the typed BaseException tag for a nonexistent domain", |
| 133 | (_stack) => |
| 134 | Effect.gen(function* () { |
| 135 | const response = yield* getJson("/domain-health"); |
| 136 | expect((response as any).tag).toBe("BaseException"); |
| 137 | }), |
| 138 | ); |
| 139 | }); |
| 140 | |
| 141 | describe("DescribeDomainNodes", () => { |
| 142 | test.provider( |
| 143 | "surfaces the typed BaseException tag for a nonexistent domain", |
| 144 | (_stack) => |
| 145 | Effect.gen(function* () { |
| 146 | const response = yield* getJson("/domain-nodes"); |
| 147 | expect((response as any).tag).toBe("BaseException"); |
| 148 | }), |
| 149 | ); |
| 150 | }); |
| 151 | |
| 152 | describe("DescribeDomainChangeProgress", () => { |
| 153 | test.provider( |
| 154 | // "No progress information found" surfaces as the typed BaseException. |
| 155 | "surfaces the typed BaseException tag when no change is in flight", |
| 156 | (_stack) => |
| 157 | Effect.gen(function* () { |
| 158 | const response = yield* getJson("/change-progress"); |
| 159 | expect((response as any).tag).toBe("BaseException"); |
| 160 | }), |
| 161 | ); |
| 162 | }); |
| 163 | |
| 164 | describe("ListDomainNames", () => { |
| 165 | test.provider("lists the account's domains", (_stack) => |
| 166 | Effect.gen(function* () { |
| 167 | const response = yield* getJson("/domain-names"); |
| 168 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 169 | }), |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | describe("DescribeDomainAutoTunes", () => { |
| 174 | test.provider( |
| 175 | "surfaces the typed not-found tag for a nonexistent domain", |
| 176 | (_stack) => |
| 177 | Effect.gen(function* () { |
| 178 | const response = yield* getJson("/auto-tunes"); |
| 179 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 180 | }), |
| 181 | ); |
| 182 | }); |
| 183 | |
| 184 | describe("ListScheduledActions", () => { |
| 185 | test.provider( |
| 186 | "surfaces the typed not-found tag for a nonexistent domain", |
| 187 | (_stack) => |
| 188 | Effect.gen(function* () { |
| 189 | const response = yield* getJson("/scheduled-actions"); |
| 190 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 191 | }), |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | describe("StartDomainMaintenance", () => { |
| 196 | test.provider( |
| 197 | "surfaces the typed not-found tag for a nonexistent domain", |
| 198 | (_stack) => |
| 199 | Effect.gen(function* () { |
| 200 | const response = yield* getJson("/start-maintenance-probe"); |
| 201 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 202 | }), |
| 203 | ); |
| 204 | }); |
| 205 | |
| 206 | describe("GetDomainMaintenanceStatus", () => { |
| 207 | test.provider( |
| 208 | "surfaces the typed BaseException tag for a nonexistent domain", |
| 209 | (_stack) => |
| 210 | Effect.gen(function* () { |
| 211 | const response = yield* getJson("/maintenance-status-probe"); |
| 212 | expect((response as any).tag).toBe("BaseException"); |
| 213 | }), |
| 214 | ); |
| 215 | }); |
| 216 | |
| 217 | describe("ListDomainMaintenances", () => { |
| 218 | test.provider( |
| 219 | "surfaces the typed BaseException tag for a nonexistent domain", |
| 220 | (_stack) => |
| 221 | Effect.gen(function* () { |
| 222 | const response = yield* getJson("/maintenances"); |
| 223 | expect((response as any).tag).toBe("BaseException"); |
| 224 | }), |
| 225 | ); |
| 226 | }); |
| 227 | |
| 228 | describe("StartServiceSoftwareUpdate", () => { |
| 229 | test.provider( |
| 230 | "surfaces the typed not-found tag for a nonexistent domain", |
| 231 | (_stack) => |
| 232 | Effect.gen(function* () { |
| 233 | const response = yield* getJson("/software-update-probe"); |
| 234 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 235 | }), |
| 236 | ); |
| 237 | }); |
| 238 | |
| 239 | describe("CancelServiceSoftwareUpdate", () => { |
| 240 | test.provider( |
| 241 | "surfaces the typed not-found tag for a nonexistent domain", |
| 242 | (_stack) => |
| 243 | Effect.gen(function* () { |
| 244 | const response = yield* getJson("/cancel-software-update-probe"); |
| 245 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 246 | }), |
| 247 | ); |
| 248 | }); |
| 249 | |
| 250 | describe("GetUpgradeStatus", () => { |
| 251 | test.provider( |
| 252 | "surfaces the typed not-found tag for a nonexistent domain", |
| 253 | (_stack) => |
| 254 | Effect.gen(function* () { |
| 255 | const response = yield* getJson("/upgrade-status"); |
| 256 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 257 | }), |
| 258 | ); |
| 259 | }); |
| 260 | |
| 261 | describe("GetUpgradeHistory", () => { |
| 262 | test.provider( |
| 263 | "surfaces the typed not-found tag for a nonexistent domain", |
| 264 | (_stack) => |
| 265 | Effect.gen(function* () { |
| 266 | const response = yield* getJson("/upgrade-history"); |
| 267 | expect((response as any).tag).toBe("ResourceNotFoundException"); |
| 268 | }), |
| 269 | ); |
| 270 | }); |
| 271 | |
| 272 | describe("GetCompatibleVersions", () => { |
| 273 | test.provider("maps engine versions to upgrade targets", (_stack) => |
| 274 | Effect.gen(function* () { |
| 275 | const response = yield* getJson("/compatible-versions"); |
| 276 | expect((response as any).count).toBeGreaterThan(0); |
| 277 | }), |
| 278 | ); |
| 279 | }); |
| 280 | |
| 281 | describe("ListVersions", () => { |
| 282 | test.provider("lists the supported engine versions", (_stack) => |
| 283 | Effect.gen(function* () { |
| 284 | const response = yield* getJson("/versions"); |
| 285 | expect((response as any).count).toBeGreaterThan(0); |
| 286 | }), |
| 287 | ); |
| 288 | }); |
| 289 | |
| 290 | describe("ListInstanceTypeDetails", () => { |
| 291 | test.provider("lists instance types for an engine version", (_stack) => |
| 292 | Effect.gen(function* () { |
| 293 | const response = yield* getJson("/instance-types"); |
| 294 | expect((response as any).count).toBeGreaterThan(0); |
| 295 | }), |
| 296 | ); |
| 297 | }); |
| 298 | }, |
| 299 | ); |