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";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (project: string, zone: string, instance: string) =>
17 compute.getInstances({ project, zone, instance }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("3 seconds"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27test.provider.skipIf(!!process.env.FAST)(
28 "create, update, and delete an instance",
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.Compute.Instance("Vm", {
36 zone: "us-central1-a",
37 machineType: "e2-micro",
38 labels: { env: "test" },
39 tags: ["alchemy-test"],
40 metadata: { role: "test" },
41 associatePublicIp: false,
42 });
43 }),
44 );
45
46 expect(created.instanceName).toEqual(expect.any(String));
47 expect(created.zone).toEqual("us-central1-a");
48 expect(created.machineType).toEqual("e2-micro");
49 expect(created.labels).toMatchObject({ env: "test" });
50 expect(created.tags).toEqual(["alchemy-test"]);
51 expect(created.metadata).toMatchObject({ role: "test" });
52
53 const fetched = yield* compute.getInstances({
54 project: created.project,
55 zone: created.zone,
56 instance: created.instanceName,
57 });
58 expect(fetched.name).toEqual(created.instanceName);
59 expect(fetched.labels?.env).toEqual("test");
60 expect(fetched.tags?.items).toEqual(["alchemy-test"]);
61
62 const updated = yield* stack.deploy(
63 Effect.gen(function* () {
64 return yield* GCP.Compute.Instance("Vm", {
65 instanceName: created.instanceName,
66 zone: "us-central1-a",
67 machineType: "e2-micro",
68 labels: { env: "prod", role: "web" },
69 tags: ["alchemy-prod"],
70 metadata: { role: "prod" },
71 associatePublicIp: false,
72 });
73 }),
74 );
75
76 expect(updated.instanceName).toEqual(created.instanceName);
77 expect(updated.instanceId).toEqual(created.instanceId);
78 expect(updated.labels).toMatchObject({ env: "prod", role: "web" });
79 expect(updated.tags).toEqual(["alchemy-prod"]);
80 expect(updated.metadata).toMatchObject({ role: "prod" });
81
82 yield* stack.destroy();
83
84 const gone = yield* waitUntilGone(
85 created.project,
86 created.zone,
87 created.instanceName,
88 );
89 expect(gone).toEqual("gone");
90 }).pipe(logLevel),
91 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 240_000 },
92);