@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 236 lines 9.08 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 Domain,
4 Environment,
5 EnvironmentBlueprintConfiguration,
6 Project,
7 } from "@alchemy.run/aws/DataZone";
8 import * as IAM from "@alchemy.run/aws/IAM";
9 import * as S3 from "@alchemy.run/aws/S3";
10 import { expect } from "@alchemy.run/test";
11 import * as Test from "@alchemy.run/test/Alchemy";
12 import * as athena from "@distilled.cloud/aws/athena";
13 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
14 import * as datazone from "@distilled.cloud/aws/datazone";
15 import * as glue from "@distilled.cloud/aws/glue";
16 import * as lf from "@distilled.cloud/aws/lakeformation";
17 import * as Effect from "effect/Effect";
18 import * as Result from "effect/Result";
19
20 const { test } = Test.make({ providers: AWS.providers() });
21
22 // Environment deployment drives a CloudFormation stack (several minutes each
23 // way) — the full lifecycle only runs when explicitly requested.
24 const RUN_SLOW = !process.env.FAST;
25
26 // The DefaultDataLake blueprint requires an S3 location whose bucket name
27 // contains "datazone" (the provisioning policy scopes S3 access to
28 // *datazone* buckets). Deterministic constant — never Date.now().
29 const DATALAKE_BUCKET = "amazon-datazone-391965393224-usw2-alchemy-test";
30
31 // Ungated: the typed error union covers the not-found probe — proves the
32 // distilled error mapping without provisioning anything.
33 test.provider(
34 "getEnvironment on a nonexistent domain is typed",
35 () =>
36 Effect.gen(function* () {
37 const result = yield* datazone
38 .getEnvironment({
39 domainIdentifier: "dzd_000000000000",
40 identifier: "0000000000",
41 })
42 .pipe(Effect.result);
43 expect(Result.isFailure(result)).toBe(true);
44 if (Result.isFailure(result)) {
45 expect([
46 "ResourceNotFoundException",
47 "AccessDeniedException",
48 "ValidationException",
49 ]).toContain(result.failure._tag);
50 }
51 }),
52 { tags: ["provider:aws", "provider:aws:datazone", "live"] },
53 );
54
55 /** Domain + roles + bucket + blueprint config + project shared by both deploys. */
56 const baseInfra = Effect.gen(function* () {
57 const domain = yield* Domain("EnvTestDomain", {
58 description: "alchemy datazone environment test",
59 });
60 // DataZone itself assumes the provisioning role to run the environment's
61 // CloudFormation deployment.
62 const provisioningRole = yield* IAM.Role("EnvProvisioningRole", {
63 assumeRolePolicyDocument: {
64 Version: "2012-10-17",
65 Statement: [
66 {
67 Effect: "Allow",
68 Principal: { Service: "datazone.amazonaws.com" },
69 Action: ["sts:AssumeRole", "sts:TagSession"],
70 },
71 ],
72 },
73 managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonDataZoneRedshiftGlueProvisioningPolicy"],
74 });
75 const manageAccessRole = yield* IAM.Role("EnvManageAccessRole", {
76 assumeRolePolicyDocument: {
77 Version: "2012-10-17",
78 Statement: [
79 {
80 Effect: "Allow",
81 Principal: { Service: "datazone.amazonaws.com" },
82 Action: ["sts:AssumeRole", "sts:TagSession"],
83 },
84 ],
85 },
86 managedPolicyArns: [
87 "arn:aws:iam::aws:policy/service-role/AmazonDataZoneGlueManageAccessRolePolicy",
88 ],
89 });
90 const bucket = yield* S3.Bucket("EnvDataLakeBucket", {
91 bucketName: DATALAKE_BUCKET,
92 forceDestroy: true,
93 });
94 const config = yield* EnvironmentBlueprintConfiguration("EnvDataLakeBlueprint", {
95 domainId: domain.domainId,
96 environmentBlueprint: "DefaultDataLake",
97 enabledRegions: ["us-west-2"],
98 provisioningRoleArn: provisioningRole.roleArn,
99 manageAccessRoleArn: manageAccessRole.roleArn,
100 regionalParameters: {
101 "us-west-2": { S3Location: `s3://${DATALAKE_BUCKET}` },
102 },
103 });
104 const project = yield* Project("EnvTestProject", {
105 domainId: domain.domainId,
106 description: "environment test project",
107 });
108 return { domain, config, project, provisioningRole, manageAccessRole };
109 });
110
111 const lakeFormationAdmins = lf
112 .getDataLakeSettings({})
113 .pipe(
114 Effect.map((r) =>
115 (r.DataLakeSettings?.DataLakeAdmins ?? []).map((p) => p.DataLakePrincipalIdentifier),
116 ),
117 );
118
119 test.provider.skipIf(!RUN_SLOW)(
120 "create environment from the DefaultDataLake blueprint, delete",
121 (stack) =>
122 Effect.gen(function* () {
123 yield* stack.destroy();
124
125 // 1. Deploy the domain, project, roles, bucket, and blueprint config.
126 const base = yield* stack.deploy(
127 Effect.gen(function* () {
128 const { domain, config, project, provisioningRole, manageAccessRole } = yield* baseInfra;
129 return {
130 domainId: domain.domainId,
131 projectId: project.projectId,
132 environmentBlueprintId: config.environmentBlueprintId,
133 provisioningRoleArn: provisioningRole.roleArn,
134 manageAccessRoleArn: manageAccessRole.roleArn,
135 };
136 }),
137 );
138
139 // 2. Create an environment profile out-of-band (no alchemy resource
140 // for it yet) and deploy the Environment from it.
141 const profile = yield* datazone.createEnvironmentProfile({
142 domainIdentifier: base.domainId,
143 name: "alchemy-env-test-profile",
144 environmentBlueprintIdentifier: base.environmentBlueprintId,
145 projectIdentifier: base.projectId,
146 awsAccountId: "391965393224",
147 awsAccountRegion: "us-west-2",
148 });
149
150 const result = yield* stack
151 .deploy(
152 Effect.gen(function* () {
153 const { domain, project } = yield* baseInfra;
154 const env = yield* Environment("TestEnvironment", {
155 domainId: domain.domainId,
156 projectId: project.projectId,
157 environmentProfileId: profile.id,
158 description: "alchemy environment",
159 });
160 return {
161 environmentId: env.environmentId,
162 status: env.status,
163 };
164 }),
165 )
166 .pipe(
167 Effect.ensuring(
168 // the environment profile blocks domain deletion — always remove
169 // it before the final destroy, even when the deploy fails.
170 datazone
171 .deleteEnvironmentProfile({
172 domainIdentifier: base.domainId,
173 identifier: profile.id,
174 })
175 .pipe(Effect.ignore),
176 ),
177 );
178
179 expect(result.environmentId).toBeDefined();
180 expect(result.status).toBe("ACTIVE");
181
182 // out-of-band: the environment is ACTIVE.
183 const created = yield* datazone.getEnvironment({
184 domainIdentifier: base.domainId,
185 identifier: result.environmentId,
186 });
187 expect(created.status).toBe("ACTIVE");
188 expect(created.projectId).toBe(base.projectId);
189 // DataZone made the blueprint's manage-access role a Lake Formation
190 // administrator when it deployed the environment.
191 expect(yield* lakeFormationAdmins).toContain(base.manageAccessRoleArn);
192 // The blueprint stack retains these on delete; the provider removes them.
193 const provisioned = (name: string) =>
194 created.provisionedResources?.find((r) => r.name === name)?.value;
195 const databases = [provisioned("glueProducerDBName"), provisioned("glueConsumerDBName")];
196 const workGroup = provisioned("athenaWorkGroupName");
197 const logGroup = provisioned("logGroupArn")?.split(":")[6];
198 expect(databases.every(Boolean)).toBe(true);
199 expect(workGroup).toBeDefined();
200 expect(logGroup).toBeDefined();
201
202 // 3. Destroy — environment, project, blueprint config, and domain.
203 yield* stack.destroy();
204 const gone = yield* datazone
205 .getEnvironment({
206 domainIdentifier: base.domainId,
207 identifier: result.environmentId,
208 })
209 .pipe(
210 Effect.map((env) => env.status === "DELETED"),
211 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
212 // once the domain is deleted, DataZone reports AccessDenied (auth
213 // is checked before existence) — also gone.
214 Effect.catchTag("AccessDeniedException", () => Effect.succeed(true)),
215 );
216 expect(gone).toBe(true);
217 // ...and the blueprint configuration's delete removed both roles'
218 // entries again, so none dangles once the roles are deleted.
219 const adminsAfterDestroy = yield* lakeFormationAdmins;
220 expect(adminsAfterDestroy).not.toContain(base.manageAccessRoleArn);
221 expect(adminsAfterDestroy).not.toContain(base.provisioningRoleArn);
222 // ...and nothing the environment's stack retained was left behind.
223 for (const database of databases) {
224 const lookup = yield* glue.getDatabase({ Name: database! }).pipe(Effect.result);
225 expect(Result.isFailure(lookup) && lookup.failure._tag).toBe("EntityNotFoundException");
226 }
227 const workGroups = yield* athena.listWorkGroups({});
228 expect((workGroups.WorkGroups ?? []).map((w) => w.Name)).not.toContain(workGroup);
229 const logGroups = yield* logs.describeLogGroups({ logGroupNamePrefix: logGroup });
230 expect(logGroups.logGroups ?? []).toHaveLength(0);
231 }),
232 {
233 tags: ["provider:aws", "provider:aws:datazone", "provider:aws:iam", "provider:aws:s3", "live"],
234 timeout: 1_100_000,
235 },
236 );