| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as bigqueryreservation from "@distilled.cloud/gcp/bigqueryreservation_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | // Flex commitments are end of sale (create fails with BadRequest, asserted |
| 18 | // by the probe below) and any commitment bills slot-hours. Set |
| 19 | // GCP_TEST_BIGQUERY_CAPACITY_COMMITMENT=1 on a project that can still buy |
| 20 | // flex slots. |
| 21 | const runLifecycle = |
| 22 | !process.env.FAST && |
| 23 | process.env.GCP_TEST_BIGQUERY_CAPACITY_COMMITMENT === "1"; |
| 24 | |
| 25 | const waitUntilGone = (name: string) => |
| 26 | bigqueryreservation.getProjectsLocationsCapacityCommitments({ name }).pipe( |
| 27 | Effect.as("found" as const), |
| 28 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("2 seconds"), |
| 31 | until: (status) => status === "gone", |
| 32 | times: 10, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | test.provider( |
| 37 | "getProjectsLocationsCapacityCommitments on a missing commitment fails with NotFound", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | const { project } = yield* GcpEnvironment.current; |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const error = yield* Effect.flip( |
| 44 | bigqueryreservation.getProjectsLocationsCapacityCommitments({ |
| 45 | name: `projects/${project}/locations/us-central1/capacityCommitments/alchemy-bq-capacity-missing`, |
| 46 | }), |
| 47 | ); |
| 48 | expect(error._tag).toBe("NotFound"); |
| 49 | |
| 50 | const page = |
| 51 | yield* bigqueryreservation.listProjectsLocationsCapacityCommitments({ |
| 52 | parent: `projects/${project}/locations/us-central1`, |
| 53 | pageSize: 10, |
| 54 | }); |
| 55 | expect( |
| 56 | (page.capacityCommitments ?? []).map((item) => |
| 57 | item.name?.split("/").pop(), |
| 58 | ), |
| 59 | ).not.toContain("alchemy-bq-capacity-missing"); |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | }).pipe(logLevel), |
| 63 | { |
| 64 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 65 | timeout: 90_000, |
| 66 | }, |
| 67 | ); |
| 68 | |
| 69 | test.provider( |
| 70 | "createProjectsLocationsCapacityCommitments flex plans are end of sale or invalid for editions", |
| 71 | (stack) => |
| 72 | Effect.gen(function* () { |
| 73 | const { project } = yield* GcpEnvironment.current; |
| 74 | yield* stack.destroy(); |
| 75 | |
| 76 | const error = yield* Effect.flip( |
| 77 | bigqueryreservation.createProjectsLocationsCapacityCommitments({ |
| 78 | parent: `projects/${project}/locations/us-central1`, |
| 79 | capacityCommitmentId: "alchemy-bq-capacity-probe", |
| 80 | body: { |
| 81 | slotCount: "50", |
| 82 | plan: "FLEX_FLAT_RATE", |
| 83 | edition: "ENTERPRISE", |
| 84 | }, |
| 85 | }), |
| 86 | ); |
| 87 | expect(error._tag).toEqual("CommitmentPlanNotSupported"); |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | }).pipe(logLevel), |
| 91 | { |
| 92 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 93 | timeout: 90_000, |
| 94 | }, |
| 95 | ); |
| 96 | |
| 97 | test.provider.skipIf(!runLifecycle)( |
| 98 | "create, update renewal plan, and delete a flex capacity commitment", |
| 99 | (stack) => |
| 100 | Effect.gen(function* () { |
| 101 | yield* stack.destroy(); |
| 102 | |
| 103 | const created = yield* stack.deploy( |
| 104 | Effect.gen(function* () { |
| 105 | return yield* GCP.BigQueryReservation.CapacityCommitment("Flex", { |
| 106 | location: "us-central1", |
| 107 | slotCount: "50", |
| 108 | plan: "FLEX_FLAT_RATE", |
| 109 | edition: "ENTERPRISE", |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(created.name).toContain("/capacityCommitments/"); |
| 115 | expect(created.capacityCommitmentId).toEqual(expect.any(String)); |
| 116 | expect(created.capacityCommitmentId.startsWith("alch-")).toEqual(true); |
| 117 | expect(created.location).toEqual("us-central1"); |
| 118 | expect(created.slotCount).toEqual("50"); |
| 119 | expect(created.plan).toEqual("FLEX_FLAT_RATE"); |
| 120 | expect(created.edition).toEqual("ENTERPRISE"); |
| 121 | |
| 122 | const fetched = |
| 123 | yield* bigqueryreservation.getProjectsLocationsCapacityCommitments({ |
| 124 | name: created.name, |
| 125 | }); |
| 126 | expect(fetched.name).toEqual(created.name); |
| 127 | expect(fetched.slotCount).toEqual("50"); |
| 128 | expect(fetched.plan).toEqual("FLEX_FLAT_RATE"); |
| 129 | |
| 130 | const listed = |
| 131 | yield* bigqueryreservation.listProjectsLocationsCapacityCommitments({ |
| 132 | parent: `projects/${created.project}/locations/${created.location}`, |
| 133 | }); |
| 134 | expect( |
| 135 | (listed.capacityCommitments ?? []).some( |
| 136 | (item) => item.name === created.name, |
| 137 | ), |
| 138 | ).toEqual(true); |
| 139 | |
| 140 | const updated = yield* stack.deploy( |
| 141 | Effect.gen(function* () { |
| 142 | return yield* GCP.BigQueryReservation.CapacityCommitment("Flex", { |
| 143 | capacityCommitmentId: created.capacityCommitmentId, |
| 144 | location: "us-central1", |
| 145 | slotCount: "50", |
| 146 | plan: "FLEX_FLAT_RATE", |
| 147 | edition: "ENTERPRISE", |
| 148 | renewalPlan: "FLEX_FLAT_RATE", |
| 149 | }); |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | expect(updated.name).toEqual(created.name); |
| 154 | expect(updated.plan).toEqual("FLEX_FLAT_RATE"); |
| 155 | expect( |
| 156 | updated.renewalPlan === "FLEX_FLAT_RATE" || |
| 157 | updated.renewalPlan === undefined, |
| 158 | ).toEqual(true); |
| 159 | |
| 160 | yield* stack.destroy(); |
| 161 | |
| 162 | const gone = yield* waitUntilGone(created.name); |
| 163 | expect(gone).toEqual("gone"); |
| 164 | }).pipe(logLevel), |
| 165 | { |
| 166 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 167 | timeout: 120_000, |
| 168 | }, |
| 169 | ); |