1import * as AWS from "@/AWS";
2import * as Core from "@/Test/Core";
3import * as Test from "@/Test/Alchemy";
4import { describe, expect } from "alchemy-test";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import * as HttpClientRequest from "effect/http/HttpClientRequest";
10import EfsBindingsFunctionLive, {
11 EfsBindingsFunction,
12} from "./bindings-handler";
13
14const testOptions = { providers: AWS.providers() };
15const { test, beforeAll, afterAll } = Test.make(testOptions);
16const sharedStack = Core.scratchStack(testOptions, "EFSBindings");
17
18// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
19// over 60s on a fresh deploy under parallel-suite load.
20const readinessPolicy = Schedule.max([
21 Schedule.fixed("2 seconds"),
22 Schedule.recurs(75),
23]);
24
25let baseUrl: string;
26
27class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
28 readonly status: number;
29 readonly body: string;
30}> {}
31
32// The shared Lambda fixture occasionally answers a transient 5xx under load
33// (cold re-init, IAM propagation on the freshly attached policy that the
34// handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
35// 4xx/assertion failure surfaces immediately.
36const send = (request: HttpClientRequest.HttpClientRequest) =>
37 HttpClient.execute(request).pipe(
38 Effect.flatMap((response) =>
39 response.status >= 500
40 ? response.text.pipe(
41 Effect.flatMap((body) =>
42 Effect.fail(
43 new TransientUpstream({ status: response.status, body }),
44 ),
45 ),
46 )
47 : Effect.succeed(response),
48 ),
49 Effect.retry({
50 while: (e) => e._tag === "TransientUpstream",
51 schedule: Schedule.max([
52 Schedule.exponential("500 millis"),
53 Schedule.recurs(6),
54 ]),
55 }),
56 );
57
58describe.sequential(
59 "EFS Bindings",
60 { tags: ["provider:aws", "provider:aws:efs", "provider:aws:lambda", "live"] },
61 () => {
62 beforeAll(
63 Effect.gen(function* () {
64 yield* Effect.logInfo("EFS test setup: destroying previous resources");
65 yield* sharedStack.destroy();
66
67 yield* Effect.logInfo("EFS test setup: deploying fixture");
68 const { functionUrl } = yield* sharedStack.deploy(
69 Effect.gen(function* () {
70 return yield* EfsBindingsFunction;
71 }).pipe(Effect.provide(EfsBindingsFunctionLive)),
72 );
73
74 expect(functionUrl).toBeTruthy();
75 baseUrl = functionUrl!.replace(/\/+$/, "");
76
77 const readinessUrl = `${baseUrl}/bindings`;
78 yield* Effect.logInfo(
79 `EFS test setup: probing readiness at ${readinessUrl}`,
80 );
81 yield* HttpClient.get(readinessUrl).pipe(
82 Effect.flatMap((response) =>
83 response.status === 200
84 ? Effect.succeed(response)
85 : Effect.fail(
86 new Error(`Function not ready: ${response.status}`),
87 ),
88 ),
89 Effect.tapError((error) =>
90 Effect.logWarning(
91 `EFS test setup: fixture not ready yet (${String(error)})`,
92 ),
93 ),
94 Effect.retry({ schedule: readinessPolicy }),
95 );
96 }),
97 { timeout: 240_000 },
98 );
99
100 afterAll(sharedStack.destroy(), { timeout: 240_000 });
101
102 describe("binding registration", () => {
103 test.provider("all 10 capabilities initialize in the runtime", (_stack) =>
104 Effect.gen(function* () {
105 const response = yield* send(
106 HttpClientRequest.get(`${baseUrl}/bindings`),
107 ).pipe(Effect.flatMap((r) => r.json));
108 expect((response as any).bound).toHaveLength(10);
109 }),
110 );
111 });
112
113 describe("DescribeFileSystem", () => {
114 test.provider("reads the bound file system's description", (_stack) =>
115 Effect.gen(function* () {
116 const response = (yield* send(
117 HttpClientRequest.get(`${baseUrl}/file-system`),
118 ).pipe(Effect.flatMap((r) => r.json))) as any;
119 expect(response.fileSystemId).toMatch(/^fs-/);
120 expect(response.state).toBe("available");
121 expect(response.encrypted).toBe(true);
122 }),
123 );
124 });
125
126 describe("DescribeMountTargets", () => {
127 test.provider("lists the file system's mount targets", (_stack) =>
128 Effect.gen(function* () {
129 const response = (yield* send(
130 HttpClientRequest.get(`${baseUrl}/mount-targets`),
131 ).pipe(Effect.flatMap((r) => r.json))) as any;
132 // The fixture file system has no mount targets — assert the call
133 // round-trips (FileSystemId injection + fs-scoped IAM).
134 expect(response.count).toBe(0);
135 }),
136 );
137 });
138
139 describe("DescribeAccessPoints", () => {
140 test.provider("lists the file system's access points", (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* send(
143 HttpClientRequest.get(`${baseUrl}/access-points`),
144 ).pipe(Effect.flatMap((r) => r.json))) as any;
145 expect(typeof response.count).toBe("number");
146 }),
147 );
148 });
149
150 describe("PutBackupPolicy + DescribeBackupPolicy", () => {
151 test.provider("enables then disables automatic backups", (_stack) =>
152 Effect.gen(function* () {
153 const initial = (yield* send(
154 HttpClientRequest.get(`${baseUrl}/backup-policy`),
155 ).pipe(Effect.flatMap((r) => r.json))) as any;
156 expect(["NONE", "DISABLED", "DISABLING"]).toContain(initial.status);
157
158 const enabled = (yield* send(
159 HttpClientRequest.post(`${baseUrl}/backup-policy/enable`),
160 ).pipe(Effect.flatMap((r) => r.json))) as any;
161 expect(["ENABLED", "ENABLING"]).toContain(enabled.status);
162
163 const disabled = (yield* send(
164 HttpClientRequest.post(`${baseUrl}/backup-policy/disable`),
165 ).pipe(Effect.flatMap((r) => r.json))) as any;
166 expect(["DISABLED", "DISABLING"]).toContain(disabled.status);
167 }),
168 );
169 });
170
171 describe("PutLifecycleConfiguration + DescribeLifecycleConfiguration", () => {
172 test.provider("sets, reads, and clears lifecycle policies", (_stack) =>
173 Effect.gen(function* () {
174 const set = (yield* send(
175 HttpClientRequest.post(`${baseUrl}/lifecycle/set`),
176 ).pipe(Effect.flatMap((r) => r.json))) as any;
177 expect(set.count).toBe(1);
178
179 const read = (yield* send(
180 HttpClientRequest.get(`${baseUrl}/lifecycle`),
181 ).pipe(Effect.flatMap((r) => r.json))) as any;
182 expect(read.count).toBe(1);
183
184 const cleared = (yield* send(
185 HttpClientRequest.post(`${baseUrl}/lifecycle/clear`),
186 ).pipe(Effect.flatMap((r) => r.json))) as any;
187 expect(cleared.count).toBe(0);
188 }),
189 );
190 });
191
192 describe("CreateAccessPoint + DeleteAccessPoint", () => {
193 test.provider(
194 "creates and deletes an access point at runtime",
195 (_stack) =>
196 Effect.gen(function* () {
197 const response = (yield* send(
198 HttpClientRequest.post(`${baseUrl}/access-point`),
199 ).pipe(Effect.flatMap((r) => r.json))) as any;
200 expect(response.accessPointId).toMatch(/^fsap-/);
201 expect(response.deleted).toBe(true);
202 }),
203 );
204 });
205
206 describe("DescribeReplicationConfigurations", () => {
207 test.provider(
208 "returns the typed ReplicationNotFound for an unreplicated file system",
209 (_stack) =>
210 Effect.gen(function* () {
211 const response = (yield* send(
212 HttpClientRequest.get(`${baseUrl}/replication`),
213 ).pipe(Effect.flatMap((r) => r.json))) as any;
214 expect(response.hasReplication).toBe(false);
215 }),
216 );
217 });
218 },
219);