1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as appengine from "@distilled.cloud/gcp/appengine_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { CERT_A, CERT_B, KEY_A, KEY_B } from "./fixtures/cert.ts";
9import { GcpEnvironment } from "@/GCP/Environment";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// Needs an App Engine application in the project, which is permanent once
19// created (it can never be deleted). The testing project has none and the
20// App Engine Admin API is off (ServiceDisabled: "App Engine Admin API has not been
21// used in project ... or it is disabled."). Set GCP_TEST_APPENGINE_APP=1 on a
22// project with an app to run the lifecycle.
23const runLifecycle = !!process.env.GCP_TEST_APPENGINE_APP;
24
25const location = "us-central";
26
27const waitUntilGone = (
28 projectsId: string,
29 locationsId: string,
30 applicationsId: string,
31 certificateId: string,
32) =>
33 appengine
34 .getProjectsLocationsApplicationsAuthorizedCertificates({
35 projectsId,
36 locationsId,
37 applicationsId,
38 authorizedCertificatesId: certificateId,
39 })
40 .pipe(
41 Effect.as("found" as const),
42 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
43 Effect.repeat({
44 schedule: Schedule.spaced("1 second"),
45 until: (status) => status === "gone",
46 times: 10,
47 }),
48 );
49
50test.provider(
51 "getProjectsLocationsApplicationsAuthorizedCertificates on a missing certificate fails with a typed tag",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const error = yield* Effect.flip(
58 appengine.getProjectsLocationsApplicationsAuthorizedCertificates({
59 projectsId: project,
60 locationsId: location,
61 applicationsId: project,
62 authorizedCertificatesId: "missing",
63 }),
64 );
65 expect(error._tag).toEqual("ServiceDisabled");
66
67 yield* stack.destroy();
68 }).pipe(logLevel),
69 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
70);
71
72test.provider.skipIf(runLifecycle)(
73 "createProjectsLocationsApplicationsAuthorizedCertificates without an App Engine app fails with a typed tag",
74 (stack) =>
75 Effect.gen(function* () {
76 const { project } = yield* GcpEnvironment.current;
77 yield* stack.destroy();
78
79 const error = yield* Effect.flip(
80 appengine.createProjectsLocationsApplicationsAuthorizedCertificates({
81 projectsId: project,
82 locationsId: location,
83 applicationsId: project,
84 body: {
85 displayName: "Alchemy Appengine Probe",
86 certificateRawData: {
87 publicCertificate: CERT_A,
88 privateKey: KEY_A,
89 },
90 },
91 }),
92 );
93 expect(error._tag).toEqual("ServiceDisabled");
94
95 yield* stack.destroy();
96 }).pipe(logLevel),
97 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
98);
99
100test.provider.skipIf(!runLifecycle)(
101 "create, update, and delete an applications authorized certificate",
102 (stack) =>
103 Effect.gen(function* () {
104 const { project } = yield* GcpEnvironment.current;
105 yield* stack.destroy();
106
107 const created = yield* stack.deploy(
108 Effect.gen(function* () {
109 return yield* GCP.AppEngine.ApplicationsAuthorizedCertificate(
110 "FrontendTls",
111 {
112 displayName: "frontend",
113 publicCertificate: CERT_A,
114 privateKey: KEY_A,
115 },
116 );
117 }),
118 );
119
120 expect(created.certificateId.length).toBeGreaterThan(0);
121 expect(created.displayName).toEqual("frontend");
122 expect(created.applicationsId).toEqual(project);
123
124 const fetched =
125 yield* appengine.getProjectsLocationsApplicationsAuthorizedCertificates(
126 {
127 projectsId: created.project,
128 locationsId: created.location,
129 applicationsId: created.applicationsId,
130 authorizedCertificatesId: created.certificateId,
131 },
132 );
133 expect(fetched.id).toEqual(created.certificateId);
134 expect(fetched.displayName).toContain("[alchemy ");
135
136 const updated = yield* stack.deploy(
137 Effect.gen(function* () {
138 return yield* GCP.AppEngine.ApplicationsAuthorizedCertificate(
139 "FrontendTls",
140 {
141 certificateId: created.certificateId,
142 location: created.location,
143 displayName: "frontend-prod",
144 publicCertificate: CERT_B,
145 privateKey: KEY_B,
146 },
147 );
148 }),
149 );
150
151 expect(updated.certificateId).toEqual(created.certificateId);
152 expect(updated.displayName).toEqual("frontend-prod");
153
154 yield* stack.destroy();
155
156 const gone = yield* waitUntilGone(
157 created.project,
158 created.location,
159 created.applicationsId,
160 created.certificateId,
161 );
162 expect(gone).toEqual("gone");
163 }).pipe(logLevel),
164 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
165);