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
| 1 | import { describe, expect } from "bun:test"; |
| 2 | import * as GCP from "@alchemy.run/gcp"; |
| 3 | import * as Kubernetes from "@alchemy.run/kubernetes"; |
| 4 | import * as Test from "@alchemy.run/test/Bun"; |
| 5 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 7 | import * as container from "@distilled.cloud/gcp/container_v1"; |
| 8 | import * as firestore from "@distilled.cloud/gcp/firestore_v1"; |
| 9 | import * as Alchemy from "alchemy"; |
| 10 | import * as Data from "effect/Data"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 13 | import * as HttpClient from "effect/http/HttpClient"; |
| 14 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | import * as Schedule from "effect/Schedule"; |
| 17 | import Stack from "../alchemy.run.ts"; |
| 18 | import { seedEntries } from "../src/SeedJob.ts"; |
| 19 | |
| 20 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 21 | providers: Layer.mergeAll(GCP.providers(), Kubernetes.providers()), |
| 22 | state: Alchemy.localState(), |
| 23 | }); |
| 24 | |
| 25 | // Out-of-band calls to the Google APIs resolve the same stored credentials |
| 26 | // the deploy uses, so the test runs against the configured profile. |
| 27 | const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer); |
| 28 | |
| 29 | // The project comes from the same credential the deploy uses. |
| 30 | const currentProject = GCP.GcpEnvironment.current.pipe( |
| 31 | Effect.map((env) => env.project), |
| 32 | Effect.provide(GCP.fromCredentials().pipe(Layer.provide(GcpHttp))), |
| 33 | ); |
| 34 | |
| 35 | // Deploy, tests and destroy all sit behind the same Docker guard. |
| 36 | describe("gcp-gke", () => { |
| 37 | // Autopilot cluster creation dominates the deploy (~5–10 min). |
| 38 | const stack = beforeAll(deploy(Stack), { timeout: 1_800_000 }); |
| 39 | |
| 40 | const found = Effect.as("found" as const); |
| 41 | const gone = () => Effect.succeed("gone" as const); |
| 42 | |
| 43 | /** Poll until a deleted resource reads as gone. */ |
| 44 | const expectGone = <E, R>(what: string, status: Effect.Effect<"found" | "gone", E, R>) => |
| 45 | status.pipe( |
| 46 | Effect.repeat({ |
| 47 | schedule: Schedule.spaced("5 seconds"), |
| 48 | until: (status) => status === "gone", |
| 49 | times: 12, |
| 50 | }), |
| 51 | Effect.tap((status) => Effect.sync(() => expect(`${what}: ${status}`).toBe(`${what}: gone`))), |
| 52 | ); |
| 53 | |
| 54 | const projectNumber = (project: string) => |
| 55 | resourcemanager |
| 56 | .getProjects({ name: `projects/${project}` }) |
| 57 | .pipe(Effect.map((p) => (p.name ?? "").split("/").pop() ?? "")); |
| 58 | |
| 59 | /** The Workload Identity Federation principal of a Kubernetes ServiceAccount. */ |
| 60 | const ksaPrincipal = (project: string, number: string, namespace: string, ksa: string) => |
| 61 | `principal://iam.googleapis.com/projects/${number}/locations/global/` + |
| 62 | `workloadIdentityPools/${project}.svc.id.goog/subject/ns/${namespace}/sa/${ksa}`; |
| 63 | |
| 64 | /** Project IAM bindings (v3, with conditions) that include `member`. */ |
| 65 | const projectBindingsOf = (member: string) => |
| 66 | currentProject.pipe( |
| 67 | Effect.flatMap((project) => |
| 68 | resourcemanager.getIamPolicyProjects({ |
| 69 | resource: `projects/${project}`, |
| 70 | body: { options: { requestedPolicyVersion: 3 } }, |
| 71 | }), |
| 72 | ), |
| 73 | Effect.map((policy) => |
| 74 | (policy.bindings ?? []).filter((binding) => (binding.members ?? []).includes(member)), |
| 75 | ), |
| 76 | ); |
| 77 | |
| 78 | /** Forwarding rules GKE created for Services in the `guestbook` namespace. */ |
| 79 | const guestbookForwardingRules = currentProject.pipe( |
| 80 | Effect.flatMap((project) => compute.listForwardingRules({ project, region: "us-central1" })), |
| 81 | Effect.map((page) => |
| 82 | (page.items ?? []).filter((rule) => |
| 83 | (rule.description ?? "").includes('"kubernetes.io/service-name":"guestbook/'), |
| 84 | ), |
| 85 | ), |
| 86 | ); |
| 87 | |
| 88 | class NotReady extends Data.TaggedError("NotReady")<{ detail: string }> {} |
| 89 | |
| 90 | /** |
| 91 | * Re-run `attempt` every 10s until it succeeds with a value `ready` |
| 92 | * accepts. Transport errors count as not ready: a fresh load balancer |
| 93 | * refuses connections until its backends are healthy. |
| 94 | */ |
| 95 | const until = <A, E, R>( |
| 96 | attempt: Effect.Effect<A, E, R>, |
| 97 | ready: (value: A) => boolean, |
| 98 | times: number, |
| 99 | ) => |
| 100 | attempt.pipe( |
| 101 | Effect.filterOrFail(ready, (value) => new NotReady({ detail: JSON.stringify(value) })), |
| 102 | Effect.retry({ schedule: Schedule.spaced("10 seconds"), times }), |
| 103 | ); |
| 104 | |
| 105 | const get = (url: string) => |
| 106 | HttpClient.execute(HttpClientRequest.get(url)).pipe( |
| 107 | Effect.flatMap((response) => |
| 108 | response.text.pipe(Effect.map((text) => ({ status: response.status, text }))), |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | /** The principals of the stack's two workloads. */ |
| 113 | const principalsOf = (outputs: { |
| 114 | namespace: string; |
| 115 | apiServiceAccount: string; |
| 116 | seedJobServiceAccount: string; |
| 117 | }) => |
| 118 | Effect.gen(function* () { |
| 119 | const project = yield* currentProject; |
| 120 | const number = yield* projectNumber(project); |
| 121 | return [ |
| 122 | ksaPrincipal(project, number, outputs.namespace, outputs.apiServiceAccount), |
| 123 | ksaPrincipal(project, number, outputs.namespace, outputs.seedJobServiceAccount), |
| 124 | ]; |
| 125 | }).pipe(Effect.provide(GcpHttp)); |
| 126 | |
| 127 | afterAll.skipIf(!!process.env.NO_DESTROY)( |
| 128 | Effect.gen(function* () { |
| 129 | const outputs = yield* stack; |
| 130 | const principals = outputs === undefined ? [] : yield* principalsOf(outputs); |
| 131 | yield* destroy(Stack); |
| 132 | if (outputs === undefined) return; |
| 133 | |
| 134 | // Every grant is revoked from the workloads' principals. |
| 135 | for (const principal of principals) { |
| 136 | const bindings = yield* projectBindingsOf(principal).pipe(Effect.provide(GcpHttp)); |
| 137 | expect(bindings).toEqual([]); |
| 138 | } |
| 139 | // The LoadBalancer Services were drained before the cluster went, so |
| 140 | // no load balancer leaked. |
| 141 | expect(yield* guestbookForwardingRules.pipe(Effect.provide(GcpHttp))).toEqual([]); |
| 142 | yield* expectGone( |
| 143 | "cluster", |
| 144 | container |
| 145 | .getProjectsLocationsClusters({ name: outputs.clusterName }) |
| 146 | .pipe(found, Effect.catchTag("NotFound", gone), Effect.provide(GcpHttp)), |
| 147 | ); |
| 148 | yield* expectGone( |
| 149 | "database", |
| 150 | firestore |
| 151 | .getProjectsDatabases({ name: outputs.databaseName }) |
| 152 | .pipe(found, Effect.catchTag("NotFound", gone), Effect.provide(GcpHttp)), |
| 153 | ); |
| 154 | }), |
| 155 | { timeout: 1_800_000 }, |
| 156 | ); |
| 157 | |
| 158 | const baseUrlOf = (url: string | undefined) => { |
| 159 | if (url === undefined) throw new Error("the service has no URL"); |
| 160 | return url.replace(/\/+$/, ""); |
| 161 | }; |
| 162 | |
| 163 | interface Entry { |
| 164 | id: string; |
| 165 | author: string; |
| 166 | message: string; |
| 167 | } |
| 168 | |
| 169 | test( |
| 170 | "grants each binding to its Kubernetes ServiceAccount principal", |
| 171 | Effect.gen(function* () { |
| 172 | const outputs = yield* stack; |
| 173 | for (const principal of yield* principalsOf(outputs)) { |
| 174 | const bindings = yield* projectBindingsOf(principal).pipe(Effect.provide(GcpHttp)); |
| 175 | // `roles/datastore.user` on the project, conditioned to the |
| 176 | // guestbook database only. |
| 177 | expect(bindings).toHaveLength(1); |
| 178 | expect(bindings[0]!.role).toEqual("roles/datastore.user"); |
| 179 | expect(bindings[0]!.condition?.expression).toContain( |
| 180 | `resource.name == "${outputs.databaseName}"`, |
| 181 | ); |
| 182 | } |
| 183 | }), |
| 184 | { timeout: 120_000 }, |
| 185 | ); |
| 186 | |
| 187 | test( |
| 188 | "the SeedJob seeded the guestbook through Workload Identity", |
| 189 | Effect.gen(function* () { |
| 190 | const { apiUrl } = yield* stack; |
| 191 | const baseUrl = baseUrlOf(apiUrl); |
| 192 | expect(baseUrl).toMatch(/^http:\/\/[^/]+:3000$/); |
| 193 | |
| 194 | // Autopilot provisions nodes for the Job's pod on demand, and a fresh |
| 195 | // Firestore grant takes a few minutes to propagate; the Job retries |
| 196 | // with backoff meanwhile. |
| 197 | const listed = yield* until( |
| 198 | get(`${baseUrl}/entries`).pipe( |
| 199 | Effect.map((response) => |
| 200 | response.status === 200 |
| 201 | ? (JSON.parse(response.text) as { entries: Entry[] }) |
| 202 | : { entries: [] as Entry[] }, |
| 203 | ), |
| 204 | ), |
| 205 | (body) => seedEntries.every((seed) => body.entries.some((entry) => entry.id === seed.id)), |
| 206 | 60, |
| 207 | ); |
| 208 | for (const seed of seedEntries) { |
| 209 | expect(listed.entries).toContainEqual({ |
| 210 | id: seed.id, |
| 211 | author: seed.id, |
| 212 | message: seed.message, |
| 213 | }); |
| 214 | } |
| 215 | |
| 216 | const ada = yield* get(`${baseUrl}/entries/ada`); |
| 217 | expect(ada.status).toBe(200); |
| 218 | expect(JSON.parse(ada.text)).toMatchObject({ id: "ada", author: "ada" }); |
| 219 | }), |
| 220 | { timeout: 720_000 }, |
| 221 | ); |
| 222 | |
| 223 | test( |
| 224 | "signs the guestbook and reads the entry back", |
| 225 | Effect.gen(function* () { |
| 226 | const { apiUrl, databaseName } = yield* stack; |
| 227 | const baseUrl = baseUrlOf(apiUrl); |
| 228 | |
| 229 | // Until the Firestore grant has propagated the API answers 500. |
| 230 | const created = yield* until( |
| 231 | HttpClient.execute( |
| 232 | HttpClientRequest.post(`${baseUrl}/entries?author=integ&message=hello%20from%20gke`), |
| 233 | ).pipe( |
| 234 | Effect.flatMap((response) => |
| 235 | response.text.pipe(Effect.map((text) => ({ status: response.status, text }))), |
| 236 | ), |
| 237 | ), |
| 238 | (response) => response.status !== 500, |
| 239 | 42, |
| 240 | ); |
| 241 | expect(created.status).toBe(200); |
| 242 | const entry = JSON.parse(created.text) as Entry; |
| 243 | expect(entry).toMatchObject({ |
| 244 | author: "integ", |
| 245 | message: "hello from gke", |
| 246 | }); |
| 247 | |
| 248 | const read = yield* get(`${baseUrl}/entries/${entry.id}`); |
| 249 | expect(read.status).toBe(200); |
| 250 | expect(JSON.parse(read.text)).toEqual(entry); |
| 251 | |
| 252 | // The write is a real Firestore document. |
| 253 | const document = yield* firestore |
| 254 | .getProjectsDatabasesDocuments({ |
| 255 | name: `${databaseName}/documents/entries/${entry.id}`, |
| 256 | }) |
| 257 | .pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 258 | expect(document.fields?.message?.stringValue).toEqual("hello from gke"); |
| 259 | |
| 260 | const missing = yield* get(`${baseUrl}/entries/nope-nope`); |
| 261 | expect(missing.status).toBe(404); |
| 262 | }), |
| 263 | { timeout: 600_000 }, |
| 264 | ); |
| 265 | |
| 266 | test( |
| 267 | "serves the external nginx Web deployment", |
| 268 | Effect.gen(function* () { |
| 269 | const { webUrl } = yield* stack; |
| 270 | const baseUrl = baseUrlOf(webUrl); |
| 271 | expect(baseUrl).toMatch(/^http:\/\/[^/:]+$/); |
| 272 | const res = yield* until(get(`${baseUrl}/`), (r) => r.status === 200, 24); |
| 273 | expect(res.text).toContain("nginx"); |
| 274 | }), |
| 275 | { timeout: 300_000 }, |
| 276 | ); |
| 277 | }); |