| 1 | import * as GCP from "@/GCP"; |
| 2 | import { findClusterAdapter } from "@/Kubernetes/ClusterAdapter.ts"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as container from "@distilled.cloud/gcp/container_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 10 | import { CAPACITY_ZONE, withGkeClusterSlot } from "../zones.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService( |
| 15 | MinimumLogLevel, |
| 16 | process.env.DEBUG ? "Debug" : "Info", |
| 17 | ); |
| 18 | |
| 19 | // GKE cluster create and delete each take 5-10 minutes. |
| 20 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 21 | |
| 22 | const waitUntilGone = (name: string) => |
| 23 | container.getProjectsLocationsClusters({ name }).pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("3 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "lists clusters and treats a missing cluster as NotFound", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | const page = yield* container.listProjectsLocationsClusters({ |
| 41 | parent: `projects/${project}/locations/-`, |
| 42 | }); |
| 43 | expect( |
| 44 | (page.clusters ?? []).map((cluster) => cluster.name), |
| 45 | ).not.toContain("alchemy-missing-cluster"); |
| 46 | |
| 47 | const missing = yield* container |
| 48 | .getProjectsLocationsClusters({ |
| 49 | name: `projects/${project}/locations/${CAPACITY_ZONE}/clusters/alchemy-missing-cluster`, |
| 50 | }) |
| 51 | .pipe( |
| 52 | Effect.as("found" as const), |
| 53 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 54 | ); |
| 55 | expect(missing).toEqual("gone"); |
| 56 | |
| 57 | yield* stack.destroy(); |
| 58 | }).pipe(logLevel), |
| 59 | { tags: ["provider:gcp", "provider:gcp:container", "live"], timeout: 90_000 }, |
| 60 | ); |
| 61 | |
| 62 | test.provider( |
| 63 | "registers the gcp-gke kubernetes adapter", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | yield* stack.destroy(); |
| 67 | |
| 68 | const adapter = yield* findClusterAdapter("gcp-gke"); |
| 69 | expect(adapter.kind).toEqual("Kubernetes.ClusterAdapter"); |
| 70 | expect(adapter.identity).toBeDefined(); |
| 71 | expect(adapter.registry).toBeDefined(); |
| 72 | expect(adapter.bootstrap).toBeDefined(); |
| 73 | expect(adapter.loadBalancerDefaults).toBeDefined(); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | }).pipe(logLevel), |
| 77 | { tags: ["provider:gcp", "provider:gcp:container", "live"], timeout: 90_000 }, |
| 78 | ); |
| 79 | |
| 80 | test.provider.skipIf(!runLifecycle)( |
| 81 | "create, update, and delete a cluster", |
| 82 | (stack) => |
| 83 | withGkeClusterSlot( |
| 84 | Effect.gen(function* () { |
| 85 | const { project } = yield* GcpEnvironment.current; |
| 86 | yield* stack.destroy(); |
| 87 | |
| 88 | const created = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* GCP.Container.Cluster("App", { |
| 91 | location: CAPACITY_ZONE, |
| 92 | machineType: "e2-medium", |
| 93 | initialNodeCount: 1, |
| 94 | diskSizeGb: 20, |
| 95 | spot: true, |
| 96 | description: "alchemy-test-cluster", |
| 97 | labels: { env: "test" }, |
| 98 | }); |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | expect(created.name).toContain("/clusters/"); |
| 103 | expect(created.clusterId).toEqual(expect.any(String)); |
| 104 | expect(created.location).toEqual(CAPACITY_ZONE); |
| 105 | expect(created.description).toEqual("alchemy-test-cluster"); |
| 106 | expect(created.labels).toMatchObject({ env: "test" }); |
| 107 | expect(created.status).toEqual("RUNNING"); |
| 108 | expect(created.autopilot).toEqual(false); |
| 109 | expect(created.endpoint).toEqual(expect.stringMatching(/^https:\/\//)); |
| 110 | expect(created.certificateAuthorityData).toEqual(expect.any(String)); |
| 111 | expect(created.connection.auth.kind).toEqual("gcp-gke"); |
| 112 | if (created.connection.auth.kind === "gcp-gke") { |
| 113 | expect(created.connection.auth.clusterId).toEqual(created.clusterId); |
| 114 | } |
| 115 | expect(created.kubernetesObjects).toEqual([]); |
| 116 | expect(created.workloadPool).toEqual(`${project}.svc.id.goog`); |
| 117 | |
| 118 | const fetched = yield* container.getProjectsLocationsClusters({ |
| 119 | name: created.name, |
| 120 | }); |
| 121 | expect(fetched.name).toEqual(created.clusterId); |
| 122 | expect(fetched.resourceLabels?.env).toEqual("test"); |
| 123 | expect(fetched.description).toEqual("alchemy-test-cluster"); |
| 124 | expect(fetched.status).toEqual("RUNNING"); |
| 125 | |
| 126 | const updated = yield* stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | return yield* GCP.Container.Cluster("App", { |
| 129 | clusterId: created.clusterId, |
| 130 | location: CAPACITY_ZONE, |
| 131 | machineType: "e2-medium", |
| 132 | initialNodeCount: 1, |
| 133 | diskSizeGb: 20, |
| 134 | spot: true, |
| 135 | description: "alchemy-test-cluster", |
| 136 | loggingService: "none", |
| 137 | labels: { env: "prod", role: "k8s" }, |
| 138 | }); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | expect(updated.name).toEqual(created.name); |
| 143 | expect(updated.clusterUid).toEqual(created.clusterUid); |
| 144 | expect(updated.labels).toMatchObject({ env: "prod", role: "k8s" }); |
| 145 | expect(updated.loggingService).toEqual("none"); |
| 146 | |
| 147 | const refetched = yield* container.getProjectsLocationsClusters({ |
| 148 | name: created.name, |
| 149 | }); |
| 150 | expect(refetched.resourceLabels?.env).toEqual("prod"); |
| 151 | expect(refetched.resourceLabels?.role).toEqual("k8s"); |
| 152 | expect(refetched.loggingService).toEqual("none"); |
| 153 | // An unspecified monitoring service keeps its observed value. |
| 154 | expect(refetched.monitoringService).toEqual(created.monitoringService); |
| 155 | |
| 156 | yield* stack.destroy(); |
| 157 | |
| 158 | const gone = yield* waitUntilGone(created.name); |
| 159 | expect(gone).toEqual("gone"); |
| 160 | }), |
| 161 | ).pipe(logLevel), |
| 162 | // Create ~6 min, logging update ~5 min, delete ~5 min. |
| 163 | { |
| 164 | tags: ["provider:gcp", "provider:gcp:container", "live"], |
| 165 | timeout: 1_800_000, |
| 166 | retry: 0, |
| 167 | }, |
| 168 | ); |