1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as config from "@distilled.cloud/gcp/config_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// A preview runs Terraform through Cloud Build as a service account that
18// needs Infra Manager roles. Set GCP_TEST_CONFIG=1 on a project prepared for
19// it to run the full lifecycle.
20const entitled = process.env.GCP_TEST_CONFIG === "1";
21const runLifecycle = entitled && !process.env.FAST;
22
23const waitUntilGone = (name: string) =>
24 config.getProjectsLocationsPreviews({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("2 seconds"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34test.provider(
35 "getProjectsLocationsPreviews on a missing preview fails with a typed tag",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 config.getProjectsLocationsPreviews({
43 name: `projects/${project}/locations/us-central1/previews/alchemy-missing-preview`,
44 }),
45 );
46 expect(error._tag).toEqual("NotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:config", "live"], timeout: 90_000 },
51);
52
53test.provider(
54 "create preview without a blueprint is rejected with BadRequest",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 const serviceAccount = `projects/${project}/serviceAccounts/alchemy-testing@${project}.iam.gserviceaccount.com`;
59 yield* stack.destroy();
60
61 const error = yield* Effect.flip(
62 stack.deploy(
63 Effect.gen(function* () {
64 return yield* GCP.Config.Preview("Plan", {
65 serviceAccount,
66 });
67 }),
68 ),
69 );
70 // "invalid preview: missing required terraform blueprint specification"
71 expect(error._tag).toEqual("BadRequest");
72
73 yield* stack.destroy();
74 }).pipe(logLevel),
75 { tags: ["provider:gcp", "provider:gcp:config", "live"], timeout: 90_000 },
76);
77
78test.provider.skipIf(!runLifecycle)(
79 "create and delete a preview",
80 (stack) =>
81 Effect.gen(function* () {
82 const { project } = yield* GcpEnvironment.current;
83 const serviceAccount = `projects/${project}/serviceAccounts/alchemy-testing@${project}.iam.gserviceaccount.com`;
84 yield* stack.destroy();
85
86 const created = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.Config.Preview("Plan", {
89 serviceAccount,
90 terraformBlueprint: {
91 gitSource: {
92 repo: "https://github.com/terraform-google-modules/terraform-docs-samples.git",
93 directory: "storage/quickstart",
94 },
95 },
96 labels: { env: "test" },
97 });
98 }),
99 );
100
101 expect(created.name).toContain("/previews/");
102 expect(created.previewId).toEqual(expect.any(String));
103 expect(created.location).toEqual("us-central1");
104 expect(created.serviceAccount).toEqual(serviceAccount);
105 expect(created.labels).toMatchObject({ env: "test" });
106
107 const fetched = yield* config.getProjectsLocationsPreviews({
108 name: created.name,
109 });
110 expect(fetched.name).toEqual(created.name);
111 expect(fetched.labels?.env).toEqual("test");
112
113 yield* stack.destroy();
114
115 const gone = yield* waitUntilGone(created.name);
116 expect(gone).toEqual("gone");
117 }).pipe(logLevel),
118 { tags: ["provider:gcp", "provider:gcp:config", "live"], timeout: 120_000 },
119);