| 1 | import * as floatingIps from "@distilled.cloud/hetzner/floating_ips"; |
| 2 | import * as Hetzner from "@/Hetzner"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as servers from "@distilled.cloud/hetzner/servers"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Hetzner.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const hasHetznerCreds = !!process.env.HCLOUD_TOKEN; |
| 19 | |
| 20 | const waitUntilFloatingIpGone = (id: number) => |
| 21 | floatingIps.getFloatingIp({ id }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | const waitUntilServerGone = (id: number) => |
| 32 | servers.getServer({ id }).pipe( |
| 33 | Effect.as("found" as const), |
| 34 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 35 | Effect.repeat({ |
| 36 | schedule: Schedule.spaced("1 second"), |
| 37 | until: (status) => status === "gone", |
| 38 | times: 10, |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | test.provider.skipIf(!hasHetznerCreds)( |
| 43 | "create update replace and delete assignment", |
| 44 | (stack) => |
| 45 | Effect.gen(function* () { |
| 46 | yield* stack.destroy(); |
| 47 | |
| 48 | const created = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | const server = yield* Hetzner.Server("Web", { |
| 51 | serverType: "cx23", |
| 52 | image: "ubuntu-24.04", |
| 53 | location: "nbg1", |
| 54 | }); |
| 55 | const ip = yield* Hetzner.FloatingIp("PublicIp", { |
| 56 | type: "ipv4", |
| 57 | homeLocation: "nbg1", |
| 58 | }); |
| 59 | const assignment = yield* Hetzner.FloatingIpAssignment("Assign", { |
| 60 | floatingIp: ip, |
| 61 | server, |
| 62 | }); |
| 63 | return { server, ip, assignment }; |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(created.assignment.floatingIpId).toEqual(created.ip.id); |
| 68 | expect(created.assignment.serverId).toEqual(created.server.serverId); |
| 69 | |
| 70 | const fetched = yield* floatingIps.getFloatingIp({ |
| 71 | id: created.ip.id, |
| 72 | }); |
| 73 | expect(fetched.floating_ip.server).toEqual(created.server.serverId); |
| 74 | |
| 75 | const provider = yield* Provider.findProvider( |
| 76 | Hetzner.FloatingIpAssignment, |
| 77 | ); |
| 78 | const listed = yield* provider.list(); |
| 79 | const found = listed.find((row) => row.floatingIpId === created.ip.id); |
| 80 | expect(found).toBeDefined(); |
| 81 | expect(found?.serverId).toEqual(created.server.serverId); |
| 82 | |
| 83 | const updated = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | const server = yield* Hetzner.Server("Web", { |
| 86 | serverType: "cx23", |
| 87 | image: "ubuntu-24.04", |
| 88 | location: "nbg1", |
| 89 | }); |
| 90 | const ip = yield* Hetzner.FloatingIp("PublicIp", { |
| 91 | type: "ipv4", |
| 92 | homeLocation: "nbg1", |
| 93 | }); |
| 94 | const assignment = yield* Hetzner.FloatingIpAssignment("Assign", { |
| 95 | floatingIp: ip, |
| 96 | server, |
| 97 | }); |
| 98 | return { server, ip, assignment }; |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | expect(updated.assignment.floatingIpId).toEqual(created.ip.id); |
| 103 | expect(updated.assignment.serverId).toEqual(created.server.serverId); |
| 104 | expect(updated.server.id).toEqual(created.server.id); |
| 105 | expect(updated.ip.id).toEqual(created.ip.id); |
| 106 | |
| 107 | const stillAssigned = yield* floatingIps.getFloatingIp({ |
| 108 | id: created.ip.id, |
| 109 | }); |
| 110 | expect(stillAssigned.floating_ip.server).toEqual(created.server.serverId); |
| 111 | |
| 112 | const replaced = yield* stack.deploy( |
| 113 | Effect.gen(function* () { |
| 114 | const server = yield* Hetzner.Server("Web", { |
| 115 | serverType: "cx23", |
| 116 | image: "ubuntu-24.04", |
| 117 | location: "nbg1", |
| 118 | }); |
| 119 | const ip = yield* Hetzner.FloatingIp("PublicIp", { |
| 120 | type: "ipv4", |
| 121 | homeLocation: "nbg1", |
| 122 | }); |
| 123 | const nextIp = yield* Hetzner.FloatingIp("PublicIp2", { |
| 124 | type: "ipv4", |
| 125 | homeLocation: "nbg1", |
| 126 | }); |
| 127 | const assignment = yield* Hetzner.FloatingIpAssignment("Assign", { |
| 128 | floatingIp: nextIp, |
| 129 | server, |
| 130 | }); |
| 131 | return { server, ip, nextIp, assignment }; |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | expect(replaced.assignment.floatingIpId).toEqual(replaced.nextIp.id); |
| 136 | expect(replaced.assignment.serverId).toEqual(created.server.serverId); |
| 137 | expect(replaced.nextIp.id).not.toEqual(created.ip.id); |
| 138 | |
| 139 | const oldIp = yield* floatingIps.getFloatingIp({ |
| 140 | id: created.ip.id, |
| 141 | }); |
| 142 | expect(oldIp.floating_ip.server).toBeNull(); |
| 143 | |
| 144 | const newIp = yield* floatingIps.getFloatingIp({ |
| 145 | id: replaced.nextIp.id, |
| 146 | }); |
| 147 | expect(newIp.floating_ip.server).toEqual(created.server.serverId); |
| 148 | |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | const ipGone = yield* waitUntilFloatingIpGone(created.ip.id); |
| 152 | expect(ipGone).toEqual("gone"); |
| 153 | const nextIpGone = yield* waitUntilFloatingIpGone(replaced.nextIp.id); |
| 154 | expect(nextIpGone).toEqual("gone"); |
| 155 | const serverGone = yield* waitUntilServerGone(created.server.id); |
| 156 | expect(serverGone).toEqual("gone"); |
| 157 | }).pipe(logLevel), |
| 158 | { |
| 159 | tags: [ |
| 160 | "provider:hetzner", |
| 161 | "provider:hetzner:floatingip", |
| 162 | "provider:hetzner:floatingipassignment", |
| 163 | "provider:hetzner:server", |
| 164 | "provider:hetzner:service", |
| 165 | "live", |
| 166 | ], |
| 167 | timeout: 180_000, |
| 168 | exclusive: true, |
| 169 | }, |
| 170 | ); |