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

main
ts 241 lines 9.3 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import { Workflow } from "@alchemy.run/aws/MWAAServerless";
4 import { Bucket } from "@alchemy.run/aws/S3/Bucket";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
8 import * as mwaa from "@distilled.cloud/aws/mwaa-serverless";
9 import * as s3 from "@distilled.cloud/aws/s3";
10 import * as sts from "@distilled.cloud/aws/sts";
11 import * as Output from "alchemy/Output";
12 import * as Data from "effect/Data";
13 import * as Effect from "effect/Effect";
14 import * as Schedule from "effect/Schedule";
15
16 const { test } = Test.make({ providers: AWS.providers() });
17
18 // Typed-error probe: prove the distilled error union carries the not-found
19 // tag this provider's read/delete paths depend on, at near-zero cost.
20 test.provider(
21 "getWorkflow on a nonexistent workflow fails with ResourceNotFoundException",
22 () =>
23 Effect.gen(function* () {
24 const identity = yield* sts.getCallerIdentity({});
25 const region = process.env.AWS_REGION ?? "us-west-2";
26 // Workflow ARNs end in `-{10 alnum chars}` (a service-assigned id
27 // suffix) — the API rejects anything else with a ValidationException
28 // before the lookup even runs.
29 const bogusArn = `arn:aws:airflow-serverless:${region}:${identity.Account}:workflow/alchemy-nonexistent-probe-0123456789`;
30 const error = yield* Effect.flip(mwaa.getWorkflow({ WorkflowArn: bogusArn }));
31 expect(error._tag).toBe("ResourceNotFoundException");
32 }),
33 { tags: ["provider:aws", "provider:aws:mwaaserverless", "live"] },
34 );
35
36 // A deterministic, checked-in workflow definition (YAML DAG using a
37 // supported AWS operator). The bucket name is interpolated because the
38 // definition must reference a real bucket the execution role can list.
39 const workflowDefinition = (bucket: string) =>
40 [
41 "alchemy-mwaa-serverless-test:",
42 " default_args:",
43 " owner: alchemy",
44 " schedule: null",
45 " tasks:",
46 " list_definitions:",
47 " operator: airflow.providers.amazon.aws.operators.s3.S3ListOperator",
48 ` bucket: ${bucket}`,
49 " prefix: workflows/",
50 "",
51 ].join("\n");
52
53 const DEFINITION_KEY = "workflows/test.yaml";
54
55 // The definition bucket + execution role, shared by every deploy phase of
56 // the gated lifecycle (same logical IDs across incremental deploys).
57 const infrastructure = Effect.gen(function* () {
58 const bucket = yield* Bucket("Definitions", { forceDestroy: true });
59 const role = yield* Role("WorkflowRole", {
60 assumeRolePolicyDocument: {
61 Version: "2012-10-17",
62 Statement: [
63 {
64 Effect: "Allow",
65 Principal: { Service: "airflow-serverless.amazonaws.com" },
66 Action: ["sts:AssumeRole"],
67 },
68 ],
69 },
70 inlinePolicies: {
71 definitions: {
72 Version: "2012-10-17",
73 Statement: [
74 {
75 Effect: "Allow",
76 Action: ["s3:GetObject*", "s3:GetBucket*", "s3:List*"],
77 Resource: [bucket.bucketArn, Output.interpolate`${bucket.bucketArn}/*`],
78 },
79 ],
80 },
81 },
82 });
83 return { bucket, role };
84 });
85
86 const workflowPhase = (description: string, name?: string) =>
87 Effect.gen(function* () {
88 const { bucket, role } = yield* infrastructure;
89 const workflow = yield* Workflow("Etl", {
90 name,
91 definitionS3Location: { bucket: bucket.bucketName, objectKey: DEFINITION_KEY },
92 roleArn: role.roleArn,
93 description,
94 tags: { fixture: "mwaa-serverless-workflow" },
95 });
96 return { bucket, role, workflow };
97 });
98
99 const findWorkflow = (workflowArn: string) =>
100 mwaa
101 .getWorkflow({ WorkflowArn: workflowArn })
102 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)));
103
104 class WorkflowStillExists extends Data.TaggedError("WorkflowStillExists")<{
105 readonly workflowArn: string;
106 }> {}
107
108 class LogGroupStillExists extends Data.TaggedError("LogGroupStillExists")<{
109 readonly logGroupName: string;
110 }> {}
111
112 // MWAA Serverless auto-creates `/aws/mwaa-serverless/{arn resource id}/`
113 // per workflow; the provider's delete reaps it. Derive the exact name from
114 // the workflow ARN (the resource id embeds the service-assigned suffix).
115 const logGroupNameFor = (workflowArn: string) =>
116 `/aws/mwaa-serverless/${workflowArn.split(":workflow/")[1]}/`;
117
118 const assertLogGroupDeleted = (workflowArn: string) =>
119 Effect.gen(function* () {
120 const logGroupName = logGroupNameFor(workflowArn);
121 const found = yield* logs.describeLogGroups({ logGroupNamePrefix: logGroupName });
122 const exists = (found.logGroups ?? []).some((group) => group.logGroupName === logGroupName);
123 if (exists) {
124 return yield* Effect.fail(new LogGroupStillExists({ logGroupName }));
125 }
126 }).pipe(
127 Effect.retry({
128 while: (e): boolean => e._tag === "LogGroupStillExists",
129 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(5)]),
130 }),
131 );
132
133 // Crash-resilience sweep: if a run dies between createWorkflow and the
134 // provider delete, the auto-created log group (whose name embeds a
135 // service-assigned random suffix we cannot predict up front) would orphan.
136 // Reap every log group carrying this test's deterministic workflow-name
137 // prefixes so an interrupted run is cleaned up by the next one.
138 const TEST_LOG_GROUP_PREFIXES = [
139 // engine-generated physical name for the `Etl` logical ID
140 "/aws/mwaa-serverless/create-update-and-delete-a-workflow-Etl-",
141 // explicit rename used by the replacement phase
142 "/aws/mwaa-serverless/alchemy-mwaa-serverless-renamed-",
143 ];
144
145 const reapTestLogGroups = Effect.gen(function* () {
146 for (const prefix of TEST_LOG_GROUP_PREFIXES) {
147 const found = yield* logs.describeLogGroups({ logGroupNamePrefix: prefix });
148 for (const group of found.logGroups ?? []) {
149 if (group.logGroupName !== undefined) {
150 yield* logs
151 .deleteLogGroup({ logGroupName: group.logGroupName })
152 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
153 }
154 }
155 }
156 });
157
158 const assertWorkflowDeleted = (workflowArn: string) =>
159 findWorkflow(workflowArn).pipe(
160 Effect.flatMap((workflow) =>
161 workflow === undefined || workflow.WorkflowStatus === "DELETING"
162 ? Effect.void
163 : Effect.fail(new WorkflowStillExists({ workflowArn })),
164 ),
165 Effect.retry({
166 while: (e) => e._tag === "WorkflowStillExists",
167 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]),
168 }),
169 );
170
171 test.provider(
172 "create, update, and delete a workflow",
173 (stack) =>
174 Effect.gen(function* () {
175 yield* stack.destroy();
176
177 // Phase 1 — the definition object must exist in S3 before the
178 // workflow can be created, so deploy the bucket + role first and
179 // upload the definition out-of-band.
180 const infra = yield* stack.deploy(infrastructure);
181 yield* s3.putObject({
182 Bucket: infra.bucket.bucketName,
183 Key: DEFINITION_KEY,
184 Body: new TextEncoder().encode(workflowDefinition(infra.bucket.bucketName)),
185 ContentType: "application/yaml",
186 });
187
188 // Phase 2 — create the workflow.
189 const { workflow } = yield* stack.deploy(
190 workflowPhase("alchemy mwaa-serverless test workflow"),
191 );
192
193 expect(workflow.workflowArn).toContain(":workflow/");
194 expect(workflow.name).toBeDefined();
195
196 // Out-of-band verification via distilled.
197 const observed = yield* mwaa.getWorkflow({ WorkflowArn: workflow.workflowArn });
198 expect(observed.Name).toBe(workflow.name);
199 expect(observed.RoleArn).toBeDefined();
200 const tags = yield* mwaa.listTagsForResource({ ResourceArn: workflow.workflowArn });
201 expect(tags.Tags?.fixture).toBe("mwaa-serverless-workflow");
202
203 // Phase 3 — update mutable config (description) in place; the
204 // workflow ARN must be stable across the update.
205 const updated = yield* stack.deploy(
206 workflowPhase("alchemy mwaa-serverless test workflow (updated)"),
207 );
208 expect(updated.workflow.workflowArn).toBe(workflow.workflowArn);
209 const reobserved = yield* mwaa.getWorkflow({ WorkflowArn: workflow.workflowArn });
210 expect(reobserved.Description).toBe("alchemy mwaa-serverless test workflow (updated)");
211
212 // Phase 4 — changing the name is a replacement: a new workflow (new
213 // ARN) is created and the old one deleted.
214 const replaced = yield* stack.deploy(
215 workflowPhase(
216 "alchemy mwaa-serverless test workflow (replaced)",
217 "alchemy-mwaa-serverless-renamed",
218 ),
219 );
220 expect(replaced.workflow.workflowArn).not.toBe(workflow.workflowArn);
221 expect(replaced.workflow.name).toBe("alchemy-mwaa-serverless-renamed");
222 yield* assertWorkflowDeleted(workflow.workflowArn);
223 // The replaced-away workflow's auto-created log group must be reaped
224 // along with it, or every rename leaks a log group.
225 yield* assertLogGroupDeleted(workflow.workflowArn);
226
227 yield* stack.destroy();
228 yield* assertWorkflowDeleted(replaced.workflow.workflowArn);
229 yield* assertLogGroupDeleted(replaced.workflow.workflowArn);
230 }).pipe(Effect.ensuring(Effect.orDie(reapTestLogGroups))),
231 {
232 tags: [
233 "provider:aws",
234 "provider:aws:iam",
235 "provider:aws:mwaaserverless",
236 "provider:aws:s3",
237 "live",
238 ],
239 timeout: 600_000,
240 },
241 );