The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as gkehub from "@distilled.cloud/gcp/gkehub_v2"; |
| 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: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const membership = process.env.GCP_TEST_GKE_MEMBERSHIP; |
| 15 | // The full lifecycle needs a registered Fleet membership; set |
| 16 | // GCP_TEST_GKE_MEMBERSHIP to run it. |
| 17 | const runLifecycle = !!membership; |
| 18 | |
| 19 | const missingMembershipOf = (project: string) => |
| 20 | `projects/${project}/locations/global/memberships/alchemy-missing-membership`; |
| 21 | const missingFeatureOf = (project: string) => |
| 22 | `${missingMembershipOf(project)}/features/configmanagement`; |
| 23 | |
| 24 | const waitUntilGone = (name: string) => |
| 25 | gkehub.getProjectsLocationsMembershipsFeatures({ name }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("2 seconds"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "getProjectsLocationsMembershipsFeatures on a missing feature fails with NotFound", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | const missingFeature = missingFeatureOf(project); |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const error = yield* Effect.flip( |
| 44 | gkehub.getProjectsLocationsMembershipsFeatures({ |
| 45 | name: missingFeature, |
| 46 | }), |
| 47 | ); |
| 48 | expect(error._tag).toEqual("NotFound"); |
| 49 | |
| 50 | const page = yield* gkehub.listProjectsLocationsMembershipsFeatures({ |
| 51 | parent: `projects/${project}/locations/global/memberships/-`, |
| 52 | pageSize: 10, |
| 53 | }); |
| 54 | expect((page.membershipFeatures ?? []).map((feature) => feature.name)).not.toContain( |
| 55 | missingFeature, |
| 56 | ); |
| 57 | |
| 58 | yield* stack.destroy(); |
| 59 | }).pipe(logLevel), |
| 60 | { tags: ["provider:gcp", "provider:gcp:gkehub", "live"], timeout: 90_000 }, |
| 61 | ); |
| 62 | |
| 63 | test.provider.skipIf(runLifecycle)( |
| 64 | "create against a missing membership fails with NotFound", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | const { project } = yield* GcpEnvironment.current; |
| 68 | const missingMembership = missingMembershipOf(project); |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const error = yield* Effect.flip( |
| 72 | stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | return yield* GCP.GKEHub.MembershipsFeature("ConfigSync", { |
| 75 | membership: missingMembership, |
| 76 | featureId: "configmanagement", |
| 77 | spec: { |
| 78 | configmanagement: { |
| 79 | configSync: { enabled: false }, |
| 80 | }, |
| 81 | }, |
| 82 | labels: { env: "test" }, |
| 83 | }); |
| 84 | }), |
| 85 | ), |
| 86 | ); |
| 87 | expect(error._tag).toEqual("NotFound"); |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | }).pipe(logLevel), |
| 91 | { tags: ["provider:gcp", "provider:gcp:gkehub", "live"], timeout: 90_000 }, |
| 92 | ); |
| 93 | |
| 94 | test.provider.skipIf(!runLifecycle)( |
| 95 | "create, update, and delete a membership feature", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const created = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | return yield* GCP.GKEHub.MembershipsFeature("ConfigSync", { |
| 103 | membership: membership!, |
| 104 | featureId: "configmanagement", |
| 105 | spec: { |
| 106 | configmanagement: { |
| 107 | configSync: { enabled: false }, |
| 108 | }, |
| 109 | }, |
| 110 | labels: { env: "test" }, |
| 111 | }); |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | expect(created.name).toContain("/features/"); |
| 116 | expect(created.featureId).toEqual("configmanagement"); |
| 117 | expect(created.membership).toEqual( |
| 118 | membership!.includes("/") |
| 119 | ? membership!.replace(/\/+$/, "") |
| 120 | : expect.stringContaining(`/memberships/${membership}`), |
| 121 | ); |
| 122 | expect(created.labels).toMatchObject({ env: "test" }); |
| 123 | |
| 124 | const fetched = yield* gkehub.getProjectsLocationsMembershipsFeatures({ |
| 125 | name: created.name, |
| 126 | }); |
| 127 | expect(fetched.name).toEqual(created.name); |
| 128 | expect(fetched.labels?.env).toEqual("test"); |
| 129 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 130 | |
| 131 | const updated = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | return yield* GCP.GKEHub.MembershipsFeature("ConfigSync", { |
| 134 | membership: created.membership, |
| 135 | featureId: created.featureId, |
| 136 | location: created.location, |
| 137 | spec: { |
| 138 | configmanagement: { |
| 139 | configSync: { enabled: false, preventDrift: true }, |
| 140 | }, |
| 141 | }, |
| 142 | labels: { env: "prod", role: "hub" }, |
| 143 | }); |
| 144 | }), |
| 145 | ); |
| 146 | |
| 147 | expect(updated.name).toEqual(created.name); |
| 148 | expect(updated.labels).toMatchObject({ env: "prod", role: "hub" }); |
| 149 | |
| 150 | yield* stack.destroy(); |
| 151 | const gone = yield* waitUntilGone(created.name); |
| 152 | expect(gone).toEqual("gone"); |
| 153 | }).pipe(logLevel), |
| 154 | { tags: ["provider:gcp", "provider:gcp:gkehub", "live"], timeout: 120_000 }, |
| 155 | ); |