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

main
ts 289 lines 12.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { InstanceProfile } from "@alchemy.run/aws/IAM/InstanceProfile";
4 import { Role } from "@alchemy.run/aws/IAM/Role";
5 import {
6 Component,
7 DistributionConfiguration,
8 ImagePipeline,
9 ImageRecipe,
10 InfrastructureConfiguration,
11 } from "@alchemy.run/aws/ImageBuilder";
12 import { expect } from "@alchemy.run/test";
13 import * as Test from "@alchemy.run/test/Alchemy";
14 import * as imagebuilder from "@distilled.cloud/aws/imagebuilder";
15 import * as Effect from "effect/Effect";
16 import * as Result from "effect/Result";
17 import * as Schedule from "effect/Schedule";
18
19 const { test } = Test.make({ providers: AWS.providers() });
20
21 // Ungated typed-error probe: prove the distilled error union carries the
22 // not-found tag every read/delete path in this service depends on. Runs in
23 // every CI pass at near-zero cost.
24 test.provider(
25 "getComponent on a nonexistent ARN fails with ResourceNotFoundException",
26 () =>
27 Effect.gen(function* () {
28 const { accountId, region } = yield* AWSEnvironment.current;
29 const error = yield* Effect.flip(
30 imagebuilder.getComponent({
31 componentBuildVersionArn: `arn:aws:imagebuilder:${region}:${accountId}:component/alchemy-nonexistent-probe/1.0.0/1`,
32 }),
33 );
34 expect(error._tag).toBe("ResourceNotFoundException");
35 }),
36 { tags: ["provider:aws", "provider:aws:imagebuilder", "live"] },
37 );
38
39 // Ungated typed-error probe: the Smithy model omits ResourceNotFoundException
40 // from GetImage / DeleteImage / CancelImageCreation even though the wire
41 // returns it — patched in distilled (patches/imagebuilder.json). This pins
42 // the patch so the image data-plane bindings can rely on the typed tag.
43 test.provider(
44 "getImage/deleteImage on a nonexistent ARN fail with ResourceNotFoundException",
45 () =>
46 Effect.gen(function* () {
47 const { accountId, region } = yield* AWSEnvironment.current;
48 const arn = `arn:aws:imagebuilder:${region}:${accountId}:image/alchemy-nonexistent-probe/1.0.0/1`;
49 const getError = yield* Effect.flip(imagebuilder.getImage({ imageBuildVersionArn: arn }));
50 expect(getError._tag).toBe("ResourceNotFoundException");
51 const deleteError = yield* Effect.flip(
52 imagebuilder.deleteImage({ imageBuildVersionArn: arn }),
53 );
54 expect(deleteError._tag).toBe("ResourceNotFoundException");
55 }),
56 { tags: ["provider:aws", "provider:aws:imagebuilder", "live"] },
57 );
58
59 const componentData = (marker: string) =>
60 [
61 "name: alchemy-test-component",
62 "description: no-op component used by alchemy live tests",
63 "schemaVersion: 1.0",
64 "phases:",
65 " - name: build",
66 " steps:",
67 " - name: hello",
68 " action: ExecuteBash",
69 " inputs:",
70 " commands:",
71 ` - echo ${marker}`,
72 ].join("\n");
73
74 /** Poll until an Image Builder resource is gone (typed NotFound). */
75 const untilGone = <A, E extends { readonly _tag: string }, R>(get: Effect.Effect<A, E, R>) =>
76 Effect.gen(function* () {
77 const result = yield* Effect.result(get);
78 if (Result.isSuccess(result)) {
79 return yield* Effect.fail(new Error("resource still exists"));
80 }
81 if (result.failure._tag !== "ResourceNotFoundException") {
82 return yield* Effect.fail(new Error(`unexpected error: ${result.failure._tag}`));
83 }
84 }).pipe(
85 Effect.retry({ schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]) }),
86 );
87
88 test.provider(
89 "image builder config lifecycle: create, update, replace, destroy",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 const { region } = yield* AWSEnvironment.current;
95 const parentImage = `arn:aws:imagebuilder:${region}:aws:image/amazon-linux-2023-x86/x.x.x`;
96
97 const program = (options: {
98 componentVersion: string;
99 distributionDescription: string;
100 pipelineDescription: string;
101 }) =>
102 Effect.gen(function* () {
103 const role = yield* Role("BuilderRole", {
104 assumeRolePolicyDocument: {
105 Version: "2012-10-17",
106 Statement: [
107 {
108 Effect: "Allow",
109 Principal: { Service: "ec2.amazonaws.com" },
110 Action: ["sts:AssumeRole"],
111 },
112 ],
113 },
114 managedPolicyArns: [
115 "arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder",
116 "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
117 ],
118 });
119 const profile = yield* InstanceProfile("BuilderProfile", { roleName: role.roleName });
120 const component = yield* Component("Setup", {
121 platform: "Linux",
122 semanticVersion: options.componentVersion,
123 data: componentData("hello-from-alchemy"),
124 tags: { fixture: "imagebuilder" },
125 });
126 const recipe = yield* ImageRecipe("Recipe", {
127 parentImage,
128 semanticVersion: options.componentVersion,
129 components: [{ componentArn: component.componentBuildVersionArn }],
130 tags: { fixture: "imagebuilder" },
131 });
132 const infra = yield* InfrastructureConfiguration("Infra", {
133 instanceProfileName: profile.instanceProfileName,
134 instanceTypes: ["t3.micro"],
135 terminateInstanceOnFailure: true,
136 tags: { fixture: "imagebuilder" },
137 });
138 const distribution = yield* DistributionConfiguration("Dist", {
139 description: options.distributionDescription,
140 distributions: [
141 {
142 region,
143 amiDistributionConfiguration: {
144 name: "alchemy-imagebuilder-test-{{ imagebuilder:buildDate }}",
145 amiTags: { fixture: "imagebuilder" },
146 },
147 },
148 ],
149 tags: { fixture: "imagebuilder" },
150 });
151 const pipeline = yield* ImagePipeline("Pipeline", {
152 imageRecipeArn: recipe.imageRecipeArn,
153 infrastructureConfigurationArn: infra.infrastructureConfigurationArn,
154 distributionConfigurationArn: distribution.distributionConfigurationArn,
155 description: options.pipelineDescription,
156 status: "DISABLED",
157 // Duration-typed test timeout — converted to wire minutes.
158 imageTestsConfiguration: { imageTestsEnabled: false, timeout: "2 hours" },
159 tags: { fixture: "imagebuilder" },
160 });
161 return { component, recipe, infra, distribution, pipeline };
162 });
163
164 // 1. Create.
165 const created = yield* stack.deploy(
166 program({
167 componentVersion: "1.0.0",
168 distributionDescription: "alchemy imagebuilder test",
169 pipelineDescription: "alchemy imagebuilder pipeline",
170 }),
171 );
172
173 expect(created.component.componentBuildVersionArn).toContain(":component/");
174 expect(created.component.componentBuildVersionArn).toContain("/1.0.0/1");
175 expect(created.component.platform).toBe("Linux");
176 expect(created.recipe.imageRecipeArn).toContain(":image-recipe/");
177 expect(created.infra.infrastructureConfigurationArn).toContain(
178 ":infrastructure-configuration/",
179 );
180 expect(created.distribution.distributionConfigurationArn).toContain(
181 ":distribution-configuration/",
182 );
183 expect(created.pipeline.imagePipelineArn).toContain(":image-pipeline/");
184 expect(created.pipeline.status).toBe("DISABLED");
185
186 // Out-of-band verification via distilled.
187 const observedPipeline = yield* imagebuilder.getImagePipeline({
188 imagePipelineArn: created.pipeline.imagePipelineArn,
189 });
190 expect(observedPipeline.imagePipeline?.imageRecipeArn).toBe(created.recipe.imageRecipeArn);
191 expect(observedPipeline.imagePipeline?.infrastructureConfigurationArn).toBe(
192 created.infra.infrastructureConfigurationArn,
193 );
194 expect(observedPipeline.imagePipeline?.status).toBe("DISABLED");
195 // The Duration-typed test timeout landed as wire minutes.
196 expect(observedPipeline.imagePipeline?.imageTestsConfiguration?.timeoutMinutes).toBe(120);
197 expect(observedPipeline.imagePipeline?.tags?.["alchemy::id"]).toBe("Pipeline");
198
199 const observedRecipe = yield* imagebuilder.getImageRecipe({
200 imageRecipeArn: created.recipe.imageRecipeArn,
201 });
202 expect(observedRecipe.imageRecipe?.components?.[0]?.componentArn).toBe(
203 created.component.componentBuildVersionArn,
204 );
205 expect(observedRecipe.imageRecipe?.parentImage).toBe(parentImage);
206
207 const observedInfra = yield* imagebuilder.getInfrastructureConfiguration({
208 infrastructureConfigurationArn: created.infra.infrastructureConfigurationArn,
209 });
210 expect(observedInfra.infrastructureConfiguration?.instanceProfileName).toBe(
211 created.infra.instanceProfileName,
212 );
213 expect(observedInfra.infrastructureConfiguration?.instanceTypes).toEqual(["t3.micro"]);
214
215 // 2. Update mutable configs in place (same ARNs).
216 const updated = yield* stack.deploy(
217 program({
218 componentVersion: "1.0.0",
219 distributionDescription: "alchemy imagebuilder test (updated)",
220 pipelineDescription: "alchemy imagebuilder pipeline (updated)",
221 }),
222 );
223 expect(updated.distribution.distributionConfigurationArn).toBe(
224 created.distribution.distributionConfigurationArn,
225 );
226 expect(updated.pipeline.imagePipelineArn).toBe(created.pipeline.imagePipelineArn);
227 const updatedDistribution = yield* imagebuilder.getDistributionConfiguration({
228 distributionConfigurationArn: updated.distribution.distributionConfigurationArn,
229 });
230 expect(updatedDistribution.distributionConfiguration?.description).toBe(
231 "alchemy imagebuilder test (updated)",
232 );
233 const updatedPipeline = yield* imagebuilder.getImagePipeline({
234 imagePipelineArn: updated.pipeline.imagePipelineArn,
235 });
236 expect(updatedPipeline.imagePipeline?.description).toBe(
237 "alchemy imagebuilder pipeline (updated)",
238 );
239
240 // 3. Replace immutable versions: bumping the component version
241 // replaces component + recipe; the pipeline updates in place to
242 // the new recipe ARN.
243 const replaced = yield* stack.deploy(
244 program({
245 componentVersion: "1.0.1",
246 distributionDescription: "alchemy imagebuilder test (updated)",
247 pipelineDescription: "alchemy imagebuilder pipeline (updated)",
248 }),
249 );
250 expect(replaced.component.componentBuildVersionArn).not.toBe(
251 created.component.componentBuildVersionArn,
252 );
253 expect(replaced.component.componentBuildVersionArn).toContain("/1.0.1/1");
254 expect(replaced.recipe.imageRecipeArn).not.toBe(created.recipe.imageRecipeArn);
255 expect(replaced.pipeline.imagePipelineArn).toBe(created.pipeline.imagePipelineArn);
256 const replacedPipeline = yield* imagebuilder.getImagePipeline({
257 imagePipelineArn: replaced.pipeline.imagePipelineArn,
258 });
259 expect(replacedPipeline.imagePipeline?.imageRecipeArn).toBe(replaced.recipe.imageRecipeArn);
260
261 // 4. Destroy and verify everything is gone out-of-band.
262 yield* stack.destroy();
263 yield* untilGone(
264 imagebuilder.getImagePipeline({ imagePipelineArn: replaced.pipeline.imagePipelineArn }),
265 );
266 yield* untilGone(
267 imagebuilder.getImageRecipe({ imageRecipeArn: replaced.recipe.imageRecipeArn }),
268 );
269 yield* untilGone(
270 imagebuilder.getComponent({
271 componentBuildVersionArn: replaced.component.componentBuildVersionArn,
272 }),
273 );
274 yield* untilGone(
275 imagebuilder.getInfrastructureConfiguration({
276 infrastructureConfigurationArn: replaced.infra.infrastructureConfigurationArn,
277 }),
278 );
279 yield* untilGone(
280 imagebuilder.getDistributionConfiguration({
281 distributionConfigurationArn: replaced.distribution.distributionConfigurationArn,
282 }),
283 );
284 }),
285 {
286 tags: ["provider:aws", "provider:aws:iam", "provider:aws:imagebuilder", "live"],
287 timeout: 180_000,
288 },
289 );