| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as compute from "@distilled.cloud/gcp/compute_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 | // Resize requests only accept accelerator (GPU) templates — a CPU template |
| 18 | // fails with `Resize requests without accelerators are not supported.` GPUs |
| 19 | // need quota and cost real money, so set GCP_TEST_MIG_RESIZE_REQUEST=1 (with |
| 20 | // GPU quota) to opt in. |
| 21 | const runLifecycle = |
| 22 | !!process.env.GCP_TEST_MIG_RESIZE_REQUEST && !process.env.FAST; |
| 23 | |
| 24 | const zone = "us-central1-a"; |
| 25 | |
| 26 | const templateProps: GCP.Compute.InstanceTemplateProps = { |
| 27 | machineType: "e2-micro", |
| 28 | disks: [ |
| 29 | { |
| 30 | boot: true, |
| 31 | autoDelete: true, |
| 32 | sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 33 | diskSizeGb: 10, |
| 34 | }, |
| 35 | ], |
| 36 | networkInterfaces: [{ network: "global/networks/default" }], |
| 37 | }; |
| 38 | |
| 39 | const waitUntilGone = ( |
| 40 | project: string, |
| 41 | zoneName: string, |
| 42 | instanceGroupManager: string, |
| 43 | resizeRequest: string, |
| 44 | ) => |
| 45 | compute |
| 46 | .getInstanceGroupManagerResizeRequests({ |
| 47 | project, |
| 48 | zone: zoneName, |
| 49 | instanceGroupManager, |
| 50 | resizeRequest, |
| 51 | }) |
| 52 | .pipe( |
| 53 | Effect.as("found" as const), |
| 54 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 55 | Effect.repeat({ |
| 56 | schedule: Schedule.spaced("1 second"), |
| 57 | until: (status) => status === "gone", |
| 58 | times: 10, |
| 59 | }), |
| 60 | ); |
| 61 | |
| 62 | test.provider( |
| 63 | "insertInstanceGroupManagerResizeRequests on a missing manager fails with NotFound", |
| 64 | () => |
| 65 | Effect.gen(function* () { |
| 66 | const { project } = yield* GcpEnvironment.current; |
| 67 | const error = yield* Effect.flip( |
| 68 | compute.insertInstanceGroupManagerResizeRequests({ |
| 69 | project, |
| 70 | zone, |
| 71 | instanceGroupManager: "does-not-exist", |
| 72 | body: { |
| 73 | name: "alchemy-rr-probe", |
| 74 | description: "alchemy entitlement probe", |
| 75 | resizeBy: 1, |
| 76 | }, |
| 77 | }), |
| 78 | ); |
| 79 | expect(error._tag).toEqual("NotFound"); |
| 80 | }).pipe(logLevel), |
| 81 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 }, |
| 82 | ); |
| 83 | |
| 84 | test.provider.skipIf(!runLifecycle)( |
| 85 | "create and delete an instance group manager resize request", |
| 86 | (stack) => |
| 87 | Effect.gen(function* () { |
| 88 | yield* stack.destroy(); |
| 89 | |
| 90 | const created = yield* stack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | const template = yield* GCP.Compute.InstanceTemplate("Template", { |
| 93 | ...templateProps, |
| 94 | }); |
| 95 | const manager = yield* GCP.Compute.InstanceGroupManager("Manager", { |
| 96 | zone, |
| 97 | instanceTemplate: template.templateName, |
| 98 | targetSize: 0, |
| 99 | // Resize requests are rejected while automatic repair is on. |
| 100 | instanceLifecyclePolicy: { defaultActionOnFailure: "DO_NOTHING" }, |
| 101 | }); |
| 102 | const request = yield* GCP.Compute.InstanceGroupManagerResizeRequest( |
| 103 | "Burst", |
| 104 | { |
| 105 | zone, |
| 106 | instanceGroupManager: manager.managerName, |
| 107 | resizeBy: 1, |
| 108 | requestedRunDuration: { seconds: "600" }, |
| 109 | description: "queued burst", |
| 110 | }, |
| 111 | ); |
| 112 | return { template, manager, request }; |
| 113 | }), |
| 114 | ); |
| 115 | |
| 116 | expect(created.request.resizeRequestName).toEqual(expect.any(String)); |
| 117 | expect(created.request.resizeBy).toEqual(1); |
| 118 | expect(created.request.description).toEqual("queued burst"); |
| 119 | expect(created.request.instanceGroupManager).toEqual( |
| 120 | created.manager.managerName, |
| 121 | ); |
| 122 | |
| 123 | const fetched = yield* compute.getInstanceGroupManagerResizeRequests({ |
| 124 | project: created.request.project, |
| 125 | zone, |
| 126 | instanceGroupManager: created.manager.managerName, |
| 127 | resizeRequest: created.request.resizeRequestName, |
| 128 | }); |
| 129 | expect(fetched.name).toEqual(created.request.resizeRequestName); |
| 130 | expect(fetched.description).toContain("[alchemy "); |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | const gone = yield* waitUntilGone( |
| 134 | created.request.project, |
| 135 | zone, |
| 136 | created.manager.managerName, |
| 137 | created.request.resizeRequestName, |
| 138 | ); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 }, |
| 142 | ); |