t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/AWS/ECS/InlineDockerfileTask.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

117 lines
4.3 KiB
TypeScript

import * as AWS from "@/AWS";
import { Cluster } from "@/AWS/ECS/Cluster.ts";
import * as Test from "@/Test/Alchemy";
import * as ecs from "@distilled.cloud/aws/ecs";
import { expect } from "alchemy-test";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import { getDefaultVpcNetwork } from "../DefaultVpc.ts";
import InlineDockerfileTaskLive, {
InlineDockerfileTask,
} from "./fixtures/inline-dockerfile-task.ts";
const { test } = Test.make({ providers: AWS.providers() });
// Live proof of the `main` + `Dockerfile.inline` environment composition: the
// fixture's inline content carries a `RUN` that bakes a marker file into an
// image layer, and its bundled `{ run }` program reads the marker back at
// container runtime, exiting non-zero if absent. Exit code 0 proves the
// inline preamble replaced the generated `FROM` (the RUN executed at build
// time) AND the bundle was layered on top of that environment.
//
// Docker + ECR + Fargate placement is minutes of wall clock, so like the
// other Task e2e tests this is gated out of the default sweep: run it
// explicitly with `AWS_TEST_SLOW=1`.
test.provider.skipIf(!process.env.AWS_TEST_SLOW || !!process.env.FAST)(
"inline-dockerfile environment bakes RUN artifact into the bundled image",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
// Reuse the standing default VPC: a public subnet + public IP is
// required for the task ENI to pull the image from ECR.
const { subnetIds } = yield* getDefaultVpcNetwork;
const subnetId = subnetIds[0];
expect(subnetId).toBeTruthy();
const { clusterArn, taskDefinitionArn } = yield* stack.deploy(
Effect.gen(function* () {
const cluster = yield* Cluster("InlineDockerfileCluster", {
clusterName: "alchemy-test-inline-dockerfile",
});
const task = yield* InlineDockerfileTask;
return {
clusterArn: cluster.clusterArn,
taskDefinitionArn: task.taskDefinitionArn,
};
}).pipe(Effect.provide(InlineDockerfileTaskLive)),
);
expect(taskDefinitionArn).toBeTruthy();
// Launch the one-shot task once, out-of-band. The task/execution
// roles were created seconds ago: ECS rejects `runTask` with
// "unable to assume the role" until IAM propagates, so retry that
// one typed error (bounded).
const started = yield* ecs
.runTask({
cluster: clusterArn,
taskDefinition: taskDefinitionArn!,
launchType: "FARGATE",
count: 1,
startedBy: "alchemy-inline-dockerfile-test",
networkConfiguration: {
awsvpcConfiguration: {
subnets: [subnetId!],
assignPublicIp: "ENABLED",
},
},
})
.pipe(
Effect.retry({
while: (e) =>
e._tag === "ClientException" &&
(e.message ?? "").includes("unable to assume the role"),
schedule: Schedule.spaced("5 seconds"),
times: 12,
}),
);
expect(started.failures ?? []).toEqual([]);
const taskArn = started.tasks?.[0]?.taskArn;
expect(taskArn).toBeTruthy();
// Wait for the task to stop: image pull + container boot + the
// one-shot program running to completion (~1-3 minutes cold).
const stopped = yield* ecs
.describeTasks({ cluster: clusterArn, tasks: [taskArn!] })
.pipe(
Effect.flatMap((result) => {
const task = result.tasks?.[0];
return task?.lastStatus === "STOPPED"
? Effect.succeed(task)
: Effect.fail(
new Error(`task not stopped yet: ${task?.lastStatus}`),
);
}),
Effect.tapError((error) => Effect.logInfo(String(error))),
Effect.retry({ schedule: Schedule.spaced("6 seconds"), times: 50 }),
);
// Exit 0 requires the artifact to have been baked by the inline RUN.
expect(stopped.stoppedReason ?? "").not.toContain("CannotPullContainer");
expect(stopped.containers?.[0]?.exitCode).toBe(0);
yield* stack.destroy();
}),
{
tags: [
"provider:aws",
"provider:aws:ec2",
"provider:aws:ecs",
"provider:docker",
"provider:docker:dockerfile",
"live",
],
timeout: 900_000,
},
);