1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as compute from "@distilled.cloud/gcp/compute_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const region = "us-central1";
18
19const waitUntilGone = (nodeTemplate: string) =>
20 GcpEnvironment.current.pipe(
21 Effect.flatMap(({ project }) =>
22 compute.getNodeTemplates({ project, region, nodeTemplate }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 ),
31 ),
32 );
33
34test.provider(
35 "getNodeTemplates on a missing template fails with NotFound",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 compute.getNodeTemplates({
43 project,
44 region,
45 nodeTemplate: "alchemy-missing-nt",
46 }),
47 );
48 expect(error._tag).toBe("NotFound");
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
53);
54
55test.provider(
56 "create and delete a node template",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Compute.NodeTemplate("SoleTenant", {
64 region,
65 nodeType: "n2-node-80-640",
66 description: "prod sole tenant",
67 });
68 }),
69 );
70
71 expect(created.nodeTemplateName).toEqual(expect.any(String));
72 expect(created.region).toEqual(region);
73 expect(created.description).toEqual("prod sole tenant");
74 expect(created.nodeType).toEqual("n2-node-80-640");
75
76 const fetched = yield* compute.getNodeTemplates({
77 project: created.project,
78 region,
79 nodeTemplate: created.nodeTemplateName,
80 });
81 expect(fetched.name).toEqual(created.nodeTemplateName);
82 expect(fetched.description).toContain("[alchemy ");
83
84 yield* stack.destroy();
85 const gone = yield* waitUntilGone(created.nodeTemplateName);
86 expect(gone).toEqual("gone");
87 }).pipe(logLevel),
88 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
89);