| 1 | import * as AWS from "@/AWS"; |
| 2 | import { Cluster } from "@/AWS/DocDBElastic"; |
| 3 | import { AWSEnvironment } from "@/AWS/Environment"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as docdbelastic from "@distilled.cloud/aws/docdb-elastic"; |
| 6 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 7 | import { expect } from "alchemy-test"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // Ungated typed-error probe: prove the distilled error union carries the |
| 16 | // not-found tag this provider's read/delete paths depend on. Runs in every |
| 17 | // CI pass at near-zero cost, unlike the gated lifecycle below. |
| 18 | test.provider( |
| 19 | "getCluster on a nonexistent cluster ARN fails with ResourceNotFoundException", |
| 20 | () => |
| 21 | Effect.gen(function* () { |
| 22 | const { accountId, region } = yield* AWSEnvironment.current; |
| 23 | const error = yield* Effect.flip( |
| 24 | docdbelastic.getCluster({ |
| 25 | clusterArn: `arn:aws:docdb-elastic:${region}:${accountId}:cluster/00000000-0000-0000-0000-000000000000`, |
| 26 | }), |
| 27 | ); |
| 28 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 29 | }), |
| 30 | { tags: ["provider:aws", "provider:aws:docdbelastic", "live"] }, |
| 31 | ); |
| 32 | |
| 33 | // Resolve two default-for-AZ subnets and the default security group. |
| 34 | const defaultNetwork = Effect.gen(function* () { |
| 35 | const vpc = yield* getDefaultVpc; |
| 36 | const subnets = yield* EC2.describeSubnets({ |
| 37 | Filters: [ |
| 38 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 39 | { Name: "default-for-az", Values: ["true"] }, |
| 40 | ], |
| 41 | }); |
| 42 | // DocumentDB elastic clusters are not offered in every AZ (e.g. |
| 43 | // us-west-2d) — pick subnets from the region's first three AZs (suffix |
| 44 | // a/b/c) only. |
| 45 | const subnetIds = (subnets.Subnets ?? []) |
| 46 | .filter((s) => /[abc]$/.test(s.AvailabilityZone ?? "")) |
| 47 | .sort((l, r) => |
| 48 | (l.AvailabilityZone ?? "").localeCompare(r.AvailabilityZone ?? ""), |
| 49 | ) |
| 50 | .map((s) => s.SubnetId) |
| 51 | .filter((id): id is `subnet-${string}` => id !== undefined) |
| 52 | .slice(0, 2); |
| 53 | const groups = yield* EC2.describeSecurityGroups({ |
| 54 | Filters: [ |
| 55 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 56 | { Name: "group-name", Values: ["default"] }, |
| 57 | ], |
| 58 | }); |
| 59 | const securityGroupId = groups.SecurityGroups?.[0]?.GroupId; |
| 60 | if (subnetIds.length < 2 || securityGroupId === undefined) { |
| 61 | return yield* Effect.die( |
| 62 | new Error("default VPC is missing subnets or its default security group"), |
| 63 | ); |
| 64 | } |
| 65 | return { subnetIds, securityGroupIds: [securityGroupId] }; |
| 66 | }); |
| 67 | |
| 68 | // Deletion is verified as INITIATED (status DELETING, irreversible) or fully |
| 69 | // gone. Full disappearance takes several more minutes server-side. |
| 70 | const assertClusterDeleting = (arn: string) => |
| 71 | Effect.gen(function* () { |
| 72 | const status = yield* docdbelastic.getCluster({ clusterArn: arn }).pipe( |
| 73 | Effect.map((r) => r.cluster.status), |
| 74 | Effect.catchTag("ResourceNotFoundException", () => |
| 75 | Effect.succeed("GONE" as const), |
| 76 | ), |
| 77 | ); |
| 78 | if (status !== "GONE" && status !== "DELETING") { |
| 79 | return yield* Effect.fail( |
| 80 | new Error(`cluster '${arn}' still exists (status: ${status})`), |
| 81 | ); |
| 82 | } |
| 83 | }).pipe( |
| 84 | Effect.retry({ |
| 85 | schedule: Schedule.max([ |
| 86 | Schedule.fixed("10 seconds"), |
| 87 | Schedule.recurs(18), |
| 88 | ]), |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | // A DocumentDB elastic cluster takes ~8-10 minutes to provision and bills |
| 93 | // per shard-vCPU-hour while it exists. The full lifecycle is gated behind |
| 94 | // AWS_TEST_SLOW=1 and always destroys what it created. |
| 95 | test.provider.skipIf(!process.env.AWS_TEST_SLOW)( |
| 96 | "create elastic cluster (1 shard, capacity 2), verify, destroy", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const network = yield* defaultNetwork; |
| 102 | |
| 103 | const { cluster } = yield* stack.deploy( |
| 104 | Effect.gen(function* () { |
| 105 | const cluster = yield* Cluster("Documents", { |
| 106 | adminUserName: "alchemyadmin", |
| 107 | adminUserPassword: Redacted.make("AlchemyTestPassw0rd"), |
| 108 | shardCapacity: 2, |
| 109 | shardCount: 1, |
| 110 | subnetIds: network.subnetIds, |
| 111 | vpcSecurityGroupIds: network.securityGroupIds, |
| 112 | backupRetentionPeriod: "1 day", |
| 113 | tags: { fixture: "docdb-elastic-cluster" }, |
| 114 | }); |
| 115 | return { cluster }; |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | expect(cluster.clusterName).toBeDefined(); |
| 120 | expect(cluster.clusterArn).toContain(":cluster/"); |
| 121 | expect(cluster.status).toBe("ACTIVE"); |
| 122 | expect(cluster.clusterEndpoint).toContain("docdb-elastic"); |
| 123 | expect(cluster.adminUserName).toBe("alchemyadmin"); |
| 124 | expect(cluster.authType).toBe("PLAIN_TEXT"); |
| 125 | expect(cluster.shardCapacity).toBe(2); |
| 126 | expect(cluster.shardCount).toBe(1); |
| 127 | expect(new Set(cluster.subnetIds)).toEqual(new Set(network.subnetIds)); |
| 128 | |
| 129 | // Out-of-band verification via distilled. |
| 130 | const described = yield* docdbelastic.getCluster({ |
| 131 | clusterArn: cluster.clusterArn, |
| 132 | }); |
| 133 | expect(described.cluster.status).toBe("ACTIVE"); |
| 134 | expect(described.cluster.shardCount).toBe(1); |
| 135 | |
| 136 | // Destroy immediately — clusters bill while they exist — and verify |
| 137 | // deletion was initiated out-of-band. |
| 138 | yield* stack.destroy(); |
| 139 | yield* assertClusterDeleting(cluster.clusterArn); |
| 140 | }), |
| 141 | // cluster create (~10 min) + delete initiation, one test. |
| 142 | { |
| 143 | tags: [ |
| 144 | "provider:aws", |
| 145 | "provider:aws:docdbelastic", |
| 146 | "provider:aws:ec2", |
| 147 | "live", |
| 148 | ], |
| 149 | timeout: 1_500_000, |
| 150 | }, |
| 151 | ); |