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 managedidentities from "@distilled.cloud/gcp/managedidentities_v1"; |
| 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 | // Managed Microsoft AD takes 20-60 minutes to provision (~$0.40/hour). |
| 15 | // The API is enabled on the testing project; get-missing returns |
| 16 | // `NotFound`, and create with an invalid CIDR returns `BadRequest` |
| 17 | // (`CIDR "not-a-cidr" is invalid`). Set GCP_TEST_MANAGEDIDENTITIES=1 |
| 18 | // to run the full lifecycle. |
| 19 | const runLifecycle = !process.env.FAST && process.env.GCP_TEST_MANAGEDIDENTITIES === "1"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | managedidentities.getProjectsLocationsGlobalDomains({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("5 seconds"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "getProjectsLocationsGlobalDomains on a missing domain fails with a typed tag", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | const { project } = yield* GcpEnvironment.current; |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | managedidentities.getProjectsLocationsGlobalDomains({ |
| 41 | name: `projects/${project}/locations/global/domains/missing.alch.test`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { |
| 49 | tags: ["provider:gcp", "provider:gcp:managedidentities", "live"], |
| 50 | timeout: 90_000, |
| 51 | }, |
| 52 | ); |
| 53 | |
| 54 | test.provider.skipIf(runLifecycle)( |
| 55 | "create with an invalid reserved IP range fails with BadRequest", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const error = yield* Effect.flip( |
| 61 | stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* GCP.ManagedIdentities.Domain("Ad", { |
| 64 | reservedIpRange: "not-a-cidr", |
| 65 | locations: ["us-central1"], |
| 66 | labels: { env: "test" }, |
| 67 | }); |
| 68 | }), |
| 69 | ), |
| 70 | ); |
| 71 | expect(error._tag).toEqual("BadRequest"); |
| 72 | |
| 73 | yield* stack.destroy(); |
| 74 | }).pipe(logLevel), |
| 75 | { |
| 76 | tags: ["provider:gcp", "provider:gcp:managedidentities", "live"], |
| 77 | timeout: 90_000, |
| 78 | }, |
| 79 | ); |
| 80 | |
| 81 | test.provider.skipIf(!runLifecycle)( |
| 82 | "create, update, and delete a managed AD domain", |
| 83 | (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const created = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* GCP.ManagedIdentities.Domain("Ad", { |
| 90 | reservedIpRange: "172.16.0.0/24", |
| 91 | locations: ["us-central1"], |
| 92 | authorizedNetworks: ["default"], |
| 93 | labels: { env: "test" }, |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | expect(created.name).toContain("/domains/"); |
| 99 | expect(created.domainName).toEqual(expect.any(String)); |
| 100 | expect(created.location).toEqual("global"); |
| 101 | expect(created.labels).toMatchObject({ env: "test" }); |
| 102 | |
| 103 | const fetched = yield* managedidentities.getProjectsLocationsGlobalDomains({ |
| 104 | name: created.name, |
| 105 | }); |
| 106 | expect(fetched.name).toEqual(created.name); |
| 107 | expect(fetched.labels?.env).toEqual("test"); |
| 108 | |
| 109 | const updated = yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | return yield* GCP.ManagedIdentities.Domain("Ad", { |
| 112 | domainName: created.domainName, |
| 113 | reservedIpRange: "172.16.0.0/24", |
| 114 | locations: ["us-central1"], |
| 115 | authorizedNetworks: ["default"], |
| 116 | auditLogsEnabled: true, |
| 117 | labels: { env: "prod", role: "identity" }, |
| 118 | }); |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | expect(updated.name).toEqual(created.name); |
| 123 | expect(updated.auditLogsEnabled).toEqual(true); |
| 124 | expect(updated.labels).toMatchObject({ env: "prod", role: "identity" }); |
| 125 | |
| 126 | const refetched = yield* managedidentities.getProjectsLocationsGlobalDomains({ |
| 127 | name: created.name, |
| 128 | }); |
| 129 | expect(refetched.labels?.env).toEqual("prod"); |
| 130 | expect(refetched.labels?.role).toEqual("identity"); |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | |
| 134 | const gone = yield* waitUntilGone(created.name); |
| 135 | expect(gone).toEqual("gone"); |
| 136 | }).pipe(logLevel), |
| 137 | { |
| 138 | tags: ["provider:gcp", "provider:gcp:managedidentities", "live"], |
| 139 | timeout: 240_000, |
| 140 | }, |
| 141 | ); |