| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as monitoring from "@distilled.cloud/gcp/monitoring_v3"; |
| 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 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | const waitUntilGone = (name: string) => |
| 17 | monitoring.getProjectsGroups({ name }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "create, update, and delete a monitoring group", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | return yield* GCP.Monitoring.Group("Prod", { |
| 36 | displayName: "production instances", |
| 37 | filter: 'resource.metadata.region="us-central1"', |
| 38 | }); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | expect(created.name).toContain("/groups/"); |
| 43 | expect(created.groupId).toEqual(expect.any(String)); |
| 44 | expect(created.displayName).toEqual("production instances"); |
| 45 | expect(created.filter).toEqual('resource.metadata.region="us-central1"'); |
| 46 | expect(created.isCluster).toEqual(false); |
| 47 | expect(created.parentName).toEqual(""); |
| 48 | |
| 49 | const fetched = yield* monitoring.getProjectsGroups({ |
| 50 | name: created.name, |
| 51 | }); |
| 52 | expect(fetched.name).toEqual(created.name); |
| 53 | expect(fetched.filter).toEqual('resource.metadata.region="us-central1"'); |
| 54 | expect(fetched.displayName).toContain("alchemy-id="); |
| 55 | expect(fetched.displayName).toContain("production instances"); |
| 56 | |
| 57 | const updated = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* GCP.Monitoring.Group("Prod", { |
| 60 | displayName: "east production", |
| 61 | filter: 'resource.metadata.region="us-east1"', |
| 62 | isCluster: true, |
| 63 | }); |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(updated.name).toEqual(created.name); |
| 68 | expect(updated.displayName).toEqual("east production"); |
| 69 | expect(updated.filter).toEqual('resource.metadata.region="us-east1"'); |
| 70 | expect(updated.isCluster).toEqual(true); |
| 71 | |
| 72 | const fetchedUpdate = yield* monitoring.getProjectsGroups({ |
| 73 | name: updated.name, |
| 74 | }); |
| 75 | expect(fetchedUpdate.filter).toEqual( |
| 76 | 'resource.metadata.region="us-east1"', |
| 77 | ); |
| 78 | expect(fetchedUpdate.isCluster).toEqual(true); |
| 79 | expect(fetchedUpdate.displayName).toContain("east production"); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const gone = yield* waitUntilGone(created.name); |
| 84 | expect(gone).toEqual("gone"); |
| 85 | }).pipe(logLevel), |
| 86 | { |
| 87 | tags: ["provider:gcp", "provider:gcp:monitoring", "live"], |
| 88 | timeout: 90_000, |
| 89 | }, |
| 90 | ); |