The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Hetzner from "@alchemy.run/hetzner"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as loadBalancers from "@distilled.cloud/hetzner/load_balancers"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: Hetzner.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const hasHetznerCreds = !!process.env.HCLOUD_TOKEN; |
| 15 | |
| 16 | const waitUntilGone = (id: number) => |
| 17 | loadBalancers.getLoadBalancer({ id }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | const tcpService = (listenPort: number, destinationPort: number) => ({ |
| 28 | protocol: "tcp" as const, |
| 29 | listenPort, |
| 30 | destinationPort, |
| 31 | healthCheck: { |
| 32 | protocol: "tcp" as const, |
| 33 | port: destinationPort, |
| 34 | interval: 15, |
| 35 | timeout: 10, |
| 36 | retries: 3, |
| 37 | }, |
| 38 | }); |
| 39 | |
| 40 | test.provider.skipIf(!hasHetznerCreds)( |
| 41 | "create, update, and delete a load balancer", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const created = yield* stack.deploy( |
| 47 | Effect.gen(function* () { |
| 48 | const server = yield* Hetzner.Server("Target", { |
| 49 | serverType: "cx23", |
| 50 | image: "ubuntu-24.04", |
| 51 | location: "nbg1", |
| 52 | startAfterCreate: false, |
| 53 | }); |
| 54 | const network = yield* Hetzner.Network("LbNet", { |
| 55 | ipRange: "10.30.0.0/16", |
| 56 | subnets: [ |
| 57 | { |
| 58 | type: "cloud", |
| 59 | ipRange: "10.30.1.0/24", |
| 60 | networkZone: "eu-central", |
| 61 | }, |
| 62 | ], |
| 63 | }); |
| 64 | const lb = yield* Hetzner.LoadBalancer("Edge", { |
| 65 | location: "nbg1", |
| 66 | loadBalancerType: "lb11", |
| 67 | algorithm: "round_robin", |
| 68 | services: [tcpService(80, 80)], |
| 69 | targets: [{ type: "server", server }], |
| 70 | networks: [network], |
| 71 | labels: { env: "test" }, |
| 72 | }); |
| 73 | return { |
| 74 | lb, |
| 75 | serverId: server.serverId, |
| 76 | networkId: network.networkId, |
| 77 | }; |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(created.lb.id).toEqual(expect.any(Number)); |
| 82 | expect(created.lb.name).toEqual(expect.any(String)); |
| 83 | expect(created.lb.loadBalancerType).toEqual("lb11"); |
| 84 | expect(created.lb.location).toEqual("nbg1"); |
| 85 | expect(created.lb.networkZone).toEqual("eu-central"); |
| 86 | expect(created.lb.algorithm).toEqual("round_robin"); |
| 87 | expect(created.lb.publicInterface).toEqual(true); |
| 88 | expect(created.lb.deleteProtection).toEqual(false); |
| 89 | expect(created.lb.ipv4).toEqual(expect.any(String)); |
| 90 | expect(created.lb.labels).toMatchObject({ env: "test" }); |
| 91 | expect(created.lb.services).toEqual( |
| 92 | expect.arrayContaining([ |
| 93 | expect.objectContaining({ |
| 94 | protocol: "tcp", |
| 95 | listenPort: 80, |
| 96 | destinationPort: 80, |
| 97 | }), |
| 98 | ]), |
| 99 | ); |
| 100 | expect(created.lb.targets).toEqual( |
| 101 | expect.arrayContaining([ |
| 102 | expect.objectContaining({ |
| 103 | type: "server", |
| 104 | serverId: created.serverId, |
| 105 | }), |
| 106 | ]), |
| 107 | ); |
| 108 | expect(created.lb.privateNetworks).toEqual( |
| 109 | expect.arrayContaining([expect.objectContaining({ networkId: created.networkId })]), |
| 110 | ); |
| 111 | |
| 112 | const fetched = yield* loadBalancers.getLoadBalancer({ |
| 113 | id: created.lb.id, |
| 114 | }); |
| 115 | expect(fetched.load_balancer.id).toEqual(created.lb.id); |
| 116 | expect(fetched.load_balancer.algorithm.type).toEqual("round_robin"); |
| 117 | expect(fetched.load_balancer.load_balancer_type.name).toEqual("lb11"); |
| 118 | expect(fetched.load_balancer.location.name).toEqual("nbg1"); |
| 119 | expect(fetched.load_balancer.labels.env).toEqual("test"); |
| 120 | expect(fetched.load_balancer.public_net.enabled).toEqual(true); |
| 121 | expect(fetched.load_balancer.private_net).toEqual( |
| 122 | expect.arrayContaining([expect.objectContaining({ network: created.networkId })]), |
| 123 | ); |
| 124 | expect(fetched.load_balancer.targets).toEqual( |
| 125 | expect.arrayContaining([ |
| 126 | expect.objectContaining({ |
| 127 | type: "server", |
| 128 | server: expect.objectContaining({ id: created.serverId }), |
| 129 | }), |
| 130 | ]), |
| 131 | ); |
| 132 | |
| 133 | const updated = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | const server = yield* Hetzner.Server("Target", { |
| 136 | serverType: "cx23", |
| 137 | image: "ubuntu-24.04", |
| 138 | location: "nbg1", |
| 139 | startAfterCreate: false, |
| 140 | }); |
| 141 | const network = yield* Hetzner.Network("LbNet", { |
| 142 | ipRange: "10.30.0.0/16", |
| 143 | subnets: [ |
| 144 | { |
| 145 | type: "cloud", |
| 146 | ipRange: "10.30.1.0/24", |
| 147 | networkZone: "eu-central", |
| 148 | }, |
| 149 | ], |
| 150 | }); |
| 151 | return yield* Hetzner.LoadBalancer("Edge", { |
| 152 | location: "nbg1", |
| 153 | loadBalancerType: "lb11", |
| 154 | algorithm: "least_connections", |
| 155 | services: [tcpService(80, 8080)], |
| 156 | targets: [{ type: "server", server }], |
| 157 | networks: [network], |
| 158 | deleteProtection: true, |
| 159 | labels: { env: "prod", role: "lb" }, |
| 160 | }); |
| 161 | }), |
| 162 | ); |
| 163 | |
| 164 | expect(updated.id).toEqual(created.lb.id); |
| 165 | expect(updated.algorithm).toEqual("least_connections"); |
| 166 | expect(updated.deleteProtection).toEqual(true); |
| 167 | expect(updated.labels).toMatchObject({ env: "prod", role: "lb" }); |
| 168 | expect(updated.services).toEqual( |
| 169 | expect.arrayContaining([ |
| 170 | expect.objectContaining({ |
| 171 | protocol: "tcp", |
| 172 | listenPort: 80, |
| 173 | destinationPort: 8080, |
| 174 | }), |
| 175 | ]), |
| 176 | ); |
| 177 | expect(updated.targets).toEqual( |
| 178 | expect.arrayContaining([ |
| 179 | expect.objectContaining({ |
| 180 | type: "server", |
| 181 | serverId: created.serverId, |
| 182 | }), |
| 183 | ]), |
| 184 | ); |
| 185 | |
| 186 | const refetched = yield* loadBalancers.getLoadBalancer({ |
| 187 | id: updated.id, |
| 188 | }); |
| 189 | expect(refetched.load_balancer.algorithm.type).toEqual("least_connections"); |
| 190 | expect(refetched.load_balancer.protection.delete).toEqual(true); |
| 191 | expect(refetched.load_balancer.labels.env).toEqual("prod"); |
| 192 | expect(refetched.load_balancer.labels.role).toEqual("lb"); |
| 193 | expect(refetched.load_balancer.services).toEqual( |
| 194 | expect.arrayContaining([ |
| 195 | expect.objectContaining({ |
| 196 | protocol: "tcp", |
| 197 | listen_port: 80, |
| 198 | destination_port: 8080, |
| 199 | }), |
| 200 | ]), |
| 201 | ); |
| 202 | |
| 203 | const provider = yield* Provider.findProvider(Hetzner.LoadBalancer); |
| 204 | const all = yield* provider.list(); |
| 205 | const listed = all.find((item) => item.id === updated.id); |
| 206 | expect(listed).toBeDefined(); |
| 207 | expect(listed?.algorithm).toEqual("least_connections"); |
| 208 | expect(listed?.loadBalancerType).toEqual("lb11"); |
| 209 | |
| 210 | yield* stack.destroy(); |
| 211 | |
| 212 | const gone = yield* waitUntilGone(created.lb.id); |
| 213 | expect(gone).toEqual("gone"); |
| 214 | }).pipe(logLevel), |
| 215 | { |
| 216 | tags: [ |
| 217 | "provider:hetzner", |
| 218 | "provider:hetzner:loadbalancer", |
| 219 | "provider:hetzner:network", |
| 220 | "provider:hetzner:server", |
| 221 | "provider:hetzner:service", |
| 222 | "live", |
| 223 | ], |
| 224 | timeout: 180_000, |
| 225 | }, |
| 226 | ); |
| 227 | |
| 228 | test.provider.skipIf(!hasHetznerCreds)( |
| 229 | "replace when location changes", |
| 230 | (stack) => |
| 231 | Effect.gen(function* () { |
| 232 | yield* stack.destroy(); |
| 233 | |
| 234 | const created = yield* stack.deploy( |
| 235 | Effect.gen(function* () { |
| 236 | return yield* Hetzner.LoadBalancer("ReplaceEdge", { |
| 237 | location: "nbg1", |
| 238 | loadBalancerType: "lb11", |
| 239 | services: [tcpService(80, 80)], |
| 240 | }); |
| 241 | }), |
| 242 | ); |
| 243 | |
| 244 | expect(created.location).toEqual("nbg1"); |
| 245 | |
| 246 | const replaced = yield* stack.deploy( |
| 247 | Effect.gen(function* () { |
| 248 | return yield* Hetzner.LoadBalancer("ReplaceEdge", { |
| 249 | location: "fsn1", |
| 250 | loadBalancerType: "lb11", |
| 251 | services: [tcpService(80, 80)], |
| 252 | }); |
| 253 | }), |
| 254 | ); |
| 255 | |
| 256 | expect(replaced.id).not.toEqual(created.id); |
| 257 | expect(replaced.location).toEqual("fsn1"); |
| 258 | |
| 259 | const fetched = yield* loadBalancers.getLoadBalancer({ |
| 260 | id: replaced.id, |
| 261 | }); |
| 262 | expect(fetched.load_balancer.location.name).toEqual("fsn1"); |
| 263 | |
| 264 | const oldGone = yield* waitUntilGone(created.id); |
| 265 | expect(oldGone).toEqual("gone"); |
| 266 | |
| 267 | yield* stack.destroy(); |
| 268 | |
| 269 | const gone = yield* waitUntilGone(replaced.id); |
| 270 | expect(gone).toEqual("gone"); |
| 271 | }).pipe(logLevel), |
| 272 | { |
| 273 | tags: ["provider:hetzner", "provider:hetzner:loadbalancer", "provider:hetzner:service", "live"], |
| 274 | timeout: 180_000, |
| 275 | }, |
| 276 | ); |