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 privateca from "@distilled.cloud/gcp/privateca_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 | const waitUntilGone = (name: string) => |
| 15 | privateca.getProjectsLocationsCaPools({ name }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("1 second"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | test.provider( |
| 26 | "getProjectsLocationsCaPools on a missing pool fails with NotFound", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const { project } = yield* GcpEnvironment.current; |
| 30 | |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const error = yield* Effect.flip( |
| 34 | privateca.getProjectsLocationsCaPools({ |
| 35 | name: `projects/${project}/locations/us-central1/caPools/alchemy-capool-missing`, |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toBe("NotFound"); |
| 39 | |
| 40 | const page = yield* privateca.listProjectsLocationsCaPools({ |
| 41 | parent: `projects/${project}/locations/-`, |
| 42 | pageSize: 10, |
| 43 | }); |
| 44 | expect((page.caPools ?? []).map((pool) => pool.name)).not.toContain( |
| 45 | `projects/${project}/locations/us-central1/caPools/alchemy-capool-missing`, |
| 46 | ); |
| 47 | |
| 48 | yield* stack.destroy(); |
| 49 | }).pipe(logLevel), |
| 50 | { tags: ["provider:gcp", "provider:gcp:privateca", "live"], timeout: 90_000 }, |
| 51 | ); |
| 52 | |
| 53 | test.provider( |
| 54 | "create, update, and delete a ca pool", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const created = yield* stack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | return yield* GCP.PrivateCA.CaPool("AppCa", { |
| 62 | location: "us-central1", |
| 63 | tier: "DEVOPS", |
| 64 | labels: { env: "test" }, |
| 65 | publishingOptions: { |
| 66 | publishCaCert: false, |
| 67 | publishCrl: false, |
| 68 | }, |
| 69 | }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(created.name).toContain("/caPools/"); |
| 74 | expect(created.caPoolId).toEqual(expect.any(String)); |
| 75 | expect(created.location).toEqual("us-central1"); |
| 76 | expect(created.tier).toEqual("DEVOPS"); |
| 77 | expect(created.labels).toMatchObject({ env: "test" }); |
| 78 | expect(created.publishingOptions?.publishCaCert ?? false).toEqual(false); |
| 79 | expect(created.publishingOptions?.publishCrl ?? false).toEqual(false); |
| 80 | |
| 81 | const fetched = yield* privateca.getProjectsLocationsCaPools({ |
| 82 | name: created.name, |
| 83 | }); |
| 84 | expect(fetched.name).toEqual(created.name); |
| 85 | expect(fetched.tier).toEqual("DEVOPS"); |
| 86 | expect(fetched.labels?.env).toEqual("test"); |
| 87 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 88 | true, |
| 89 | ); |
| 90 | |
| 91 | const trust = yield* privateca.fetchCaCertsProjectsLocationsCaPools({ |
| 92 | caPool: created.name, |
| 93 | body: {}, |
| 94 | }); |
| 95 | // A pool without certificate authorities has no trust anchors. |
| 96 | expect(trust.caCerts ?? []).toEqual([]); |
| 97 | |
| 98 | const updated = yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | return yield* GCP.PrivateCA.CaPool("AppCa", { |
| 101 | caPoolId: created.caPoolId, |
| 102 | location: "us-central1", |
| 103 | tier: "DEVOPS", |
| 104 | labels: { env: "prod", role: "ca" }, |
| 105 | publishingOptions: { |
| 106 | publishCaCert: false, |
| 107 | publishCrl: false, |
| 108 | encodingFormat: "PEM", |
| 109 | }, |
| 110 | issuancePolicy: { |
| 111 | maximumLifetime: "2592000s", |
| 112 | allowedIssuanceModes: { |
| 113 | allowConfigBasedIssuance: true, |
| 114 | allowCsrBasedIssuance: true, |
| 115 | }, |
| 116 | }, |
| 117 | }); |
| 118 | }), |
| 119 | ); |
| 120 | |
| 121 | expect(updated.name).toEqual(created.name); |
| 122 | expect(updated.labels).toMatchObject({ env: "prod", role: "ca" }); |
| 123 | expect(updated.publishingOptions?.publishCaCert ?? false).toEqual(false); |
| 124 | expect(updated.issuancePolicy?.maximumLifetime).toEqual("2592000s"); |
| 125 | |
| 126 | const refetched = yield* privateca.getProjectsLocationsCaPools({ |
| 127 | name: created.name, |
| 128 | }); |
| 129 | expect(refetched.labels?.env).toEqual("prod"); |
| 130 | expect(refetched.labels?.role).toEqual("ca"); |
| 131 | expect(refetched.issuancePolicy?.maximumLifetime).toEqual("2592000s"); |
| 132 | expect(refetched.issuancePolicy?.allowedIssuanceModes?.allowConfigBasedIssuance).toEqual( |
| 133 | true, |
| 134 | ); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const gone = yield* waitUntilGone(created.name); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { |
| 142 | tags: ["provider:gcp", "provider:gcp:privateca", "live"], |
| 143 | timeout: 120_000, |
| 144 | }, |
| 145 | ); |