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 baremetalsolution from "@distilled.cloud/gcp/baremetalsolution_v2"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { isServiceEnabled } from "../ServiceState.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Bare Metal Solution is contracted physical hardware (real cost) and the API |
| 16 | // is off on the testing project (ServiceDisabled: "Bare Metal Solution API has not |
| 17 | // been used in project ... or it is disabled."). Set |
| 18 | // GCP_TEST_BAREMETALSOLUTION_NETWORK to a provisioned BMS client network on an |
| 19 | // entitled project to run the lifecycle. |
| 20 | const bmsNetwork = process.env.GCP_TEST_BAREMETALSOLUTION_NETWORK; |
| 21 | const runLifecycle = !!bmsNetwork; |
| 22 | |
| 23 | const missingNetwork = (projectId: string) => |
| 24 | `projects/${projectId}/locations/us-central1/networks/alchemy-missing-bms-net`; |
| 25 | |
| 26 | const dummyClientsFor = (project: string) => [ |
| 27 | { |
| 28 | network: missingNetwork(project), |
| 29 | allowedClientsCidr: "10.200.0.0/28", |
| 30 | mountPermissions: "READ_WRITE" as const, |
| 31 | noRootSquash: true, |
| 32 | }, |
| 33 | ]; |
| 34 | |
| 35 | const waitUntilGone = (name: string) => |
| 36 | baremetalsolution.getProjectsLocationsNfsShares({ name }).pipe( |
| 37 | Effect.as("found" as const), |
| 38 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 39 | Effect.repeat({ |
| 40 | schedule: Schedule.spaced("2 seconds"), |
| 41 | until: (status) => status === "gone", |
| 42 | times: 10, |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | test.provider( |
| 47 | "getProjectsLocationsNfsShares on a missing share fails with a typed tag", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | const { project } = yield* GcpEnvironment.current; |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const error = yield* Effect.flip( |
| 54 | baremetalsolution.getProjectsLocationsNfsShares({ |
| 55 | name: `projects/${project}/locations/us-central1/nfsShares/alchemy-bms-missing`, |
| 56 | }), |
| 57 | ); |
| 58 | expect(error._tag).toEqual("ServiceDisabled"); |
| 59 | |
| 60 | yield* stack.destroy(); |
| 61 | }).pipe(logLevel), |
| 62 | { |
| 63 | tags: ["provider:gcp", "provider:gcp:baremetalsolution", "live"], |
| 64 | timeout: 90_000, |
| 65 | }, |
| 66 | ); |
| 67 | |
| 68 | test.provider.skipIf(runLifecycle)( |
| 69 | "create is rejected with ServiceDisabled when the Bare Metal Solution API is disabled", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | // Only meaningful while the API is disabled: enabled, the create would |
| 73 | // provision a real resource. |
| 74 | if (yield* isServiceEnabled("baremetalsolution.googleapis.com")) return; |
| 75 | const { project } = yield* GcpEnvironment.current; |
| 76 | yield* stack.destroy(); |
| 77 | |
| 78 | const error = yield* Effect.flip( |
| 79 | stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | return yield* GCP.BareMetalSolution.NfsShare("Share", { |
| 82 | requestedSizeGib: 100, |
| 83 | storageType: "SSD", |
| 84 | allowedClients: dummyClientsFor(project), |
| 85 | labels: { env: "test" }, |
| 86 | }); |
| 87 | }), |
| 88 | ), |
| 89 | ); |
| 90 | expect(error._tag).toEqual("ServiceDisabled"); |
| 91 | |
| 92 | yield* stack.destroy(); |
| 93 | }).pipe(logLevel), |
| 94 | { |
| 95 | tags: ["provider:gcp", "provider:gcp:baremetalsolution", "live"], |
| 96 | timeout: 90_000, |
| 97 | }, |
| 98 | ); |
| 99 | |
| 100 | test.provider.skipIf(!runLifecycle)( |
| 101 | "create, update, and delete an NFS share", |
| 102 | (stack) => |
| 103 | Effect.gen(function* () { |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const network = bmsNetwork!; |
| 107 | |
| 108 | const created = yield* stack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | return yield* GCP.BareMetalSolution.NfsShare("Share", { |
| 111 | requestedSizeGib: 100, |
| 112 | storageType: "SSD", |
| 113 | allowedClients: [ |
| 114 | { |
| 115 | network, |
| 116 | allowedClientsCidr: "10.200.0.0/28", |
| 117 | mountPermissions: "READ_WRITE", |
| 118 | noRootSquash: true, |
| 119 | }, |
| 120 | ], |
| 121 | labels: { env: "test" }, |
| 122 | }); |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | expect(created.name).toContain("/nfsShares/"); |
| 127 | expect(created.nfsShareId).toEqual(expect.any(String)); |
| 128 | expect(created.location).toEqual("us-central1"); |
| 129 | expect(created.labels).toMatchObject({ env: "test" }); |
| 130 | |
| 131 | const fetched = yield* baremetalsolution.getProjectsLocationsNfsShares({ |
| 132 | name: created.name, |
| 133 | }); |
| 134 | expect(fetched.name).toEqual(created.name); |
| 135 | expect(fetched.labels?.env).toEqual("test"); |
| 136 | |
| 137 | const updated = yield* stack.deploy( |
| 138 | Effect.gen(function* () { |
| 139 | return yield* GCP.BareMetalSolution.NfsShare("Share", { |
| 140 | nfsShareId: created.nfsShareId, |
| 141 | location: created.location, |
| 142 | requestedSizeGib: created.requestedSizeGib, |
| 143 | storageType: created.storageType, |
| 144 | allowedClients: [ |
| 145 | { |
| 146 | network, |
| 147 | allowedClientsCidr: "10.200.0.0/28", |
| 148 | mountPermissions: "READ_ONLY", |
| 149 | noRootSquash: true, |
| 150 | }, |
| 151 | ], |
| 152 | labels: { env: "prod", team: "storage" }, |
| 153 | }); |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | expect(updated.name).toEqual(created.name); |
| 158 | expect(updated.labels).toMatchObject({ env: "prod", team: "storage" }); |
| 159 | |
| 160 | const refetched = yield* baremetalsolution.getProjectsLocationsNfsShares({ |
| 161 | name: created.name, |
| 162 | }); |
| 163 | expect(refetched.labels?.env).toEqual("prod"); |
| 164 | expect(refetched.labels?.team).toEqual("storage"); |
| 165 | |
| 166 | yield* stack.destroy(); |
| 167 | |
| 168 | const gone = yield* waitUntilGone(created.name); |
| 169 | expect(gone).toEqual("gone"); |
| 170 | }).pipe(logLevel), |
| 171 | { |
| 172 | tags: ["provider:gcp", "provider:gcp:baremetalsolution", "live"], |
| 173 | timeout: 120_000, |
| 174 | }, |
| 175 | ); |