@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 199 lines 7.53 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Subnet } from "@alchemy.run/aws/EC2";
3 import { Listener, LoadBalancer, TargetGroup } from "@alchemy.run/aws/ELBv2";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as EC2 from "@distilled.cloud/aws/ec2";
7 import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2";
8 import * as Drift from "alchemy/Drift";
9 import * as Provider from "alchemy/Provider";
10 import * as Effect from "effect/Effect";
11 import { MinimumLogLevel } from "effect/References";
12 import { getDefaultVpc } from "../DefaultVpc.ts";
13
14 const { test } = Test.make({ providers: AWS.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 // Reuse the account/region default VPC and carve stack-owned subnets (subnets
19 // don't count against the VPC limit). An ALB needs subnets in at least two AZs.
20 test.provider(
21 "list enumerates the deployed load balancer",
22 (stack) =>
23 Effect.gen(function* () {
24 yield* stack.destroy();
25
26 const azResult = yield* EC2.describeAvailabilityZones({});
27 const azs =
28 azResult.AvailabilityZones?.filter((az) => az.State === "available").flatMap((az) =>
29 az.ZoneName ? [az.ZoneName] : [],
30 ) ?? [];
31 const [az1, az2] = azs;
32 expect(az1).toBeTruthy();
33 expect(az2).toBeTruthy();
34
35 const defaultVpc = yield* getDefaultVpc;
36
37 const deployed = yield* stack.deploy(
38 Effect.gen(function* () {
39 const subnet1 = yield* Subnet("ListLbSubnet1", {
40 vpcId: defaultVpc.vpcId,
41 cidrBlock: defaultVpc.subnetCidrBlock(222),
42 availabilityZone: az1,
43 });
44
45 const subnet2 = yield* Subnet("ListLbSubnet2", {
46 vpcId: defaultVpc.vpcId,
47 cidrBlock: defaultVpc.subnetCidrBlock(223),
48 availabilityZone: az2,
49 });
50
51 const loadBalancer = yield* LoadBalancer("ListLb", {
52 subnets: [subnet1.subnetId, subnet2.subnetId],
53 scheme: "internal",
54 type: "application",
55 });
56
57 return { loadBalancer };
58 }),
59 );
60
61 const provider = yield* Provider.findProvider(LoadBalancer);
62 const all = yield* provider.list();
63
64 expect(all.some((x) => x.loadBalancerArn === deployed.loadBalancer.loadBalancerArn)).toBe(
65 true,
66 );
67
68 yield* stack.destroy();
69
70 // Out-of-band: the load balancer is gone after destroy.
71 const after = yield* elbv2
72 .describeLoadBalancers({
73 LoadBalancerArns: [deployed.loadBalancer.loadBalancerArn],
74 })
75 .pipe(
76 Effect.map((r) => r.LoadBalancers?.length ?? 0),
77 Effect.catchTag("LoadBalancerNotFoundException", () => Effect.succeed(0)),
78 );
79 expect(after).toBe(0);
80 }).pipe(logLevel),
81 {
82 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elbv2", "live"],
83 timeout: 600_000,
84 },
85 );
86
87 const toRecord = (attributes: readonly { Key?: string; Value?: string }[] | undefined) =>
88 Object.fromEntries((attributes ?? []).map((a) => [a.Key ?? "", a.Value ?? ""]));
89
90 // #1969: load balancer and listener attribute changes made outside alchemy are
91 // detected as drift and repaired back to the declared values.
92 test.provider(
93 "detects and repairs out-of-band attribute drift",
94 (stack) =>
95 Effect.gen(function* () {
96 yield* stack.destroy();
97
98 const azResult = yield* EC2.describeAvailabilityZones({});
99 const [az1, az2] =
100 azResult.AvailabilityZones?.filter((az) => az.State === "available").flatMap((az) =>
101 az.ZoneName ? [az.ZoneName] : [],
102 ) ?? [];
103 const defaultVpc = yield* getDefaultVpc;
104
105 const lbAttributes = { "idle_timeout.timeout_seconds": "3600" };
106 const listenerAttributes = { "routing.http.response.server.enabled": "false" };
107 const deployed = yield* stack.deploy(
108 Effect.gen(function* () {
109 const subnet1 = yield* Subnet("DriftLbSubnet1", {
110 vpcId: defaultVpc.vpcId,
111 cidrBlock: defaultVpc.subnetCidrBlock(224),
112 availabilityZone: az1,
113 });
114 const subnet2 = yield* Subnet("DriftLbSubnet2", {
115 vpcId: defaultVpc.vpcId,
116 cidrBlock: defaultVpc.subnetCidrBlock(225),
117 availabilityZone: az2,
118 });
119 const loadBalancer = yield* LoadBalancer("DriftLb", {
120 subnets: [subnet1.subnetId, subnet2.subnetId],
121 scheme: "internal",
122 type: "application",
123 attributes: lbAttributes,
124 });
125 const targetGroup = yield* TargetGroup("DriftLbTargetGroup", {
126 vpcId: defaultVpc.vpcId,
127 port: 80,
128 protocol: "HTTP",
129 targetType: "ip",
130 });
131 const listener = yield* Listener("DriftLbListener", {
132 loadBalancerArn: loadBalancer.loadBalancerArn,
133 targetGroupArn: targetGroup.targetGroupArn,
134 port: 80,
135 protocol: "HTTP",
136 attributes: listenerAttributes,
137 });
138 return { loadBalancer, listener };
139 }),
140 );
141 const lbArn = deployed.loadBalancer.loadBalancerArn;
142 const listenerArn = deployed.listener.listenerArn;
143 expect(deployed.loadBalancer.attributes).toEqual(lbAttributes);
144 expect(deployed.listener.attributes).toEqual(listenerAttributes);
145
146 const target = { name: stack.name, stage: stack.stage };
147 const clean = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
148 // #1968: ELB returns subnets in its own order; that is not drift.
149 expect(clean.resources.DriftLb?.action).toBe("unchanged");
150 expect(clean.resources.DriftLb?.attr.attributes).toEqual(lbAttributes);
151 expect(clean.resources.DriftLbListener?.action).toBe("unchanged");
152
153 yield* elbv2.modifyLoadBalancerAttributes({
154 LoadBalancerArn: lbArn,
155 Attributes: [{ Key: "idle_timeout.timeout_seconds", Value: "60" }],
156 });
157 yield* elbv2.modifyListenerAttributes({
158 ListenerArn: listenerArn,
159 Attributes: [{ Key: "routing.http.response.server.enabled", Value: "true" }],
160 });
161
162 const drifted = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
163 expect(drifted.resources.DriftLb).toMatchObject({
164 action: "drifted",
165 attr: { attributes: { "idle_timeout.timeout_seconds": "60" } },
166 });
167 expect(drifted.resources.DriftLbListener).toMatchObject({
168 action: "drifted",
169 attr: { attributes: { "routing.http.response.server.enabled": "true" } },
170 });
171
172 const repaired = yield* Drift.repair(target).pipe(Effect.provide(stack.state));
173 expect(repaired.resources.DriftLb).toMatchObject({
174 action: "repaired",
175 attr: { attributes: lbAttributes },
176 });
177 expect(repaired.resources.DriftLbListener?.action).toBe("repaired");
178 expect(
179 toRecord(
180 (yield* elbv2.describeLoadBalancerAttributes({ LoadBalancerArn: lbArn })).Attributes,
181 ),
182 ).toMatchObject(lbAttributes);
183 expect(
184 toRecord(
185 (yield* elbv2.describeListenerAttributes({ ListenerArn: listenerArn })).Attributes,
186 ),
187 ).toMatchObject(listenerAttributes);
188
189 const settled = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
190 expect(settled.resources.DriftLb?.action).toBe("unchanged");
191 expect(settled.resources.DriftLbListener?.action).toBe("unchanged");
192
193 yield* stack.destroy();
194 }).pipe(logLevel),
195 {
196 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elbv2", "live"],
197 timeout: 600_000,
198 },
199 );