The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 compute from "@distilled.cloud/gcp/compute_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 region = "us-central1"; |
| 15 | |
| 16 | const waitUntilGone = (autoscaler: string) => |
| 17 | GcpEnvironment.current.pipe( |
| 18 | Effect.flatMap(({ project }) => |
| 19 | compute.getRegionAutoscalers({ project, region, autoscaler }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 15, |
| 26 | }), |
| 27 | ), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | const templateProps: GCP.Compute.InstanceTemplateProps = { |
| 32 | machineType: "e2-micro", |
| 33 | disks: [ |
| 34 | { |
| 35 | boot: true, |
| 36 | autoDelete: true, |
| 37 | sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 38 | diskSizeGb: 10, |
| 39 | }, |
| 40 | ], |
| 41 | networkInterfaces: [{ network: "global/networks/default" }], |
| 42 | }; |
| 43 | |
| 44 | test.provider.skipIf(!!process.env.FAST)( |
| 45 | "create, update, and delete a region autoscaler", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | const { project } = yield* GcpEnvironment.current; |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | const created = yield* stack.deploy( |
| 52 | Effect.gen(function* () { |
| 53 | const template = yield* GCP.Compute.InstanceTemplate("Tpl", templateProps); |
| 54 | const mig = yield* GCP.Compute.RegionInstanceGroupManager("Mig", { |
| 55 | region, |
| 56 | instanceTemplate: template.templateName.as<string>(), |
| 57 | targetSize: 0, |
| 58 | }); |
| 59 | const scaler = yield* GCP.Compute.RegionAutoscaler("Scale", { |
| 60 | region, |
| 61 | target: mig.selfLink.as<string>(), |
| 62 | description: "test scaler", |
| 63 | labels: { env: "test" }, |
| 64 | autoscalingPolicy: { |
| 65 | minNumReplicas: 0, |
| 66 | maxNumReplicas: 1, |
| 67 | coolDownPeriodSec: 60, |
| 68 | mode: "OFF", |
| 69 | cpuUtilization: { utilizationTarget: 0.6 }, |
| 70 | }, |
| 71 | }); |
| 72 | return { template, mig, scaler }; |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.scaler.autoscalerName).toEqual(expect.any(String)); |
| 77 | expect(created.scaler.region).toEqual(region); |
| 78 | expect(created.scaler.project).toEqual(project); |
| 79 | expect(created.scaler.description).toEqual("test scaler"); |
| 80 | expect(created.scaler.labels).toMatchObject({ env: "test" }); |
| 81 | expect(created.scaler.target).toContain(created.mig.managerName); |
| 82 | expect(created.scaler.autoscalingPolicy?.maxNumReplicas).toEqual(1); |
| 83 | expect(created.scaler.autoscalingPolicy?.minNumReplicas).toEqual(0); |
| 84 | expect(created.scaler.autoscalingPolicy?.mode).toEqual("OFF"); |
| 85 | |
| 86 | const fetched = yield* compute.getRegionAutoscalers({ |
| 87 | project, |
| 88 | region, |
| 89 | autoscaler: created.scaler.autoscalerName, |
| 90 | }); |
| 91 | expect(fetched.name).toEqual(created.scaler.autoscalerName); |
| 92 | expect(fetched.description).toContain("[alchemy "); |
| 93 | expect(fetched.description).toContain("test scaler"); |
| 94 | expect(fetched.description).toContain("env=test"); |
| 95 | expect(fetched.autoscalingPolicy?.maxNumReplicas).toEqual(1); |
| 96 | expect(fetched.autoscalingPolicy?.coolDownPeriodSec).toEqual(60); |
| 97 | expect(fetched.autoscalingPolicy?.cpuUtilization?.utilizationTarget).toEqual(0.6); |
| 98 | expect(fetched.target).toEqual(expect.any(String)); |
| 99 | expect(fetched.target).toContain(created.mig.managerName); |
| 100 | |
| 101 | const updated = yield* stack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | const template = yield* GCP.Compute.InstanceTemplate("Tpl", templateProps); |
| 104 | const mig = yield* GCP.Compute.RegionInstanceGroupManager("Mig", { |
| 105 | region, |
| 106 | instanceTemplate: template.templateName.as<string>(), |
| 107 | targetSize: 0, |
| 108 | }); |
| 109 | const scaler = yield* GCP.Compute.RegionAutoscaler("Scale", { |
| 110 | region, |
| 111 | target: mig.selfLink.as<string>(), |
| 112 | description: "updated scaler", |
| 113 | labels: { env: "prod", role: "scale" }, |
| 114 | autoscalingPolicy: { |
| 115 | minNumReplicas: 0, |
| 116 | maxNumReplicas: 2, |
| 117 | coolDownPeriodSec: 90, |
| 118 | mode: "OFF", |
| 119 | cpuUtilization: { utilizationTarget: 0.5 }, |
| 120 | }, |
| 121 | }); |
| 122 | return { template, mig, scaler }; |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | expect(updated.scaler.autoscalerName).toEqual(created.scaler.autoscalerName); |
| 127 | expect(updated.scaler.autoscalerId).toEqual(created.scaler.autoscalerId); |
| 128 | expect(updated.scaler.description).toEqual("updated scaler"); |
| 129 | expect(updated.scaler.labels).toMatchObject({ |
| 130 | env: "prod", |
| 131 | role: "scale", |
| 132 | }); |
| 133 | expect(updated.scaler.autoscalingPolicy?.maxNumReplicas).toEqual(2); |
| 134 | expect(updated.scaler.autoscalingPolicy?.coolDownPeriodSec).toEqual(90); |
| 135 | |
| 136 | const refetched = yield* compute.getRegionAutoscalers({ |
| 137 | project, |
| 138 | region, |
| 139 | autoscaler: updated.scaler.autoscalerName, |
| 140 | }); |
| 141 | expect(refetched.id).toEqual(created.scaler.autoscalerId); |
| 142 | expect(refetched.description).toContain("updated scaler"); |
| 143 | expect(refetched.description).toContain("env=prod"); |
| 144 | expect(refetched.autoscalingPolicy?.maxNumReplicas).toEqual(2); |
| 145 | expect(refetched.autoscalingPolicy?.cpuUtilization?.utilizationTarget).toEqual(0.5); |
| 146 | |
| 147 | yield* stack.destroy(); |
| 148 | |
| 149 | const gone = yield* waitUntilGone(created.scaler.autoscalerName); |
| 150 | expect(gone).toEqual("gone"); |
| 151 | }).pipe(logLevel), |
| 152 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 }, |
| 153 | ); |