| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as appengine from "@distilled.cloud/gcp/appengine_v1"; |
| 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 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | // Needs an App Engine application in the project, which is permanent once |
| 18 | // created (it can never be deleted). The testing project has none and the |
| 19 | // App Engine Admin API is off (ServiceDisabled: "App Engine Admin API has not been |
| 20 | // used in project ... or it is disabled."). Set GCP_TEST_APPENGINE_APP=1 on a |
| 21 | // project with an app to run the lifecycle. |
| 22 | const runLifecycle = !!process.env.GCP_TEST_APPENGINE_APP; |
| 23 | |
| 24 | const sourceUrl = process.env.GCP_TEST_APPENGINE_SOURCE_URL ?? ""; |
| 25 | |
| 26 | const waitUntilGone = ( |
| 27 | appsId: string, |
| 28 | servicesId: string, |
| 29 | versionsId: string, |
| 30 | ) => |
| 31 | appengine |
| 32 | .getAppsServicesVersions({ |
| 33 | appsId, |
| 34 | servicesId, |
| 35 | versionsId, |
| 36 | }) |
| 37 | .pipe( |
| 38 | Effect.as("found" as const), |
| 39 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 40 | Effect.repeat({ |
| 41 | schedule: Schedule.spaced("1 second"), |
| 42 | until: (status) => status === "gone", |
| 43 | times: 10, |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "getAppsServicesVersions on a missing version fails with a typed tag", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | const { project } = yield* GcpEnvironment.current; |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const error = yield* Effect.flip( |
| 55 | appengine.getAppsServicesVersions({ |
| 56 | appsId: project, |
| 57 | servicesId: "default", |
| 58 | versionsId: "alchemy-missing", |
| 59 | }), |
| 60 | ); |
| 61 | expect(error._tag).toEqual("ServiceDisabled"); |
| 62 | |
| 63 | yield* stack.destroy(); |
| 64 | }).pipe(logLevel), |
| 65 | { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 }, |
| 66 | ); |
| 67 | |
| 68 | test.provider.skipIf(runLifecycle)( |
| 69 | "createAppsServicesVersions without an App Engine app fails with a typed tag", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | const { project } = yield* GcpEnvironment.current; |
| 73 | yield* stack.destroy(); |
| 74 | |
| 75 | const error = yield* Effect.flip( |
| 76 | appengine.createAppsServicesVersions({ |
| 77 | appsId: project, |
| 78 | servicesId: "default", |
| 79 | body: { |
| 80 | id: "alchemy-probe", |
| 81 | runtime: "python311", |
| 82 | }, |
| 83 | }), |
| 84 | ); |
| 85 | expect(error._tag).toEqual("ServiceDisabled"); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | }).pipe(logLevel), |
| 89 | { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 }, |
| 90 | ); |
| 91 | |
| 92 | test.provider.skipIf(!runLifecycle || sourceUrl.length === 0)( |
| 93 | "create, update, and delete a service version", |
| 94 | (stack) => |
| 95 | Effect.gen(function* () { |
| 96 | yield* stack.destroy(); |
| 97 | |
| 98 | const created = yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | return yield* GCP.AppEngine.AppsServicesVersion("Api", { |
| 101 | runtime: "python311", |
| 102 | servingStatus: "SERVING", |
| 103 | automaticScaling: { |
| 104 | standardSchedulerSettings: { |
| 105 | minInstances: 0, |
| 106 | maxInstances: 1, |
| 107 | }, |
| 108 | }, |
| 109 | deployment: { |
| 110 | zip: { sourceUrl }, |
| 111 | }, |
| 112 | }); |
| 113 | }), |
| 114 | ); |
| 115 | |
| 116 | expect(created.versionId.length).toBeGreaterThan(0); |
| 117 | expect(created.runtime).toEqual("python311"); |
| 118 | expect(created.serviceId).toEqual("default"); |
| 119 | |
| 120 | const fetched = yield* appengine.getAppsServicesVersions({ |
| 121 | appsId: created.appsId, |
| 122 | servicesId: created.serviceId, |
| 123 | versionsId: created.versionId, |
| 124 | view: "FULL", |
| 125 | }); |
| 126 | expect(fetched.id).toEqual(created.versionId); |
| 127 | expect(fetched.envVariables?.ALCHEMY_OWNERSHIP).toContain("[alchemy "); |
| 128 | |
| 129 | const updated = yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | return yield* GCP.AppEngine.AppsServicesVersion("Api", { |
| 132 | versionId: created.versionId, |
| 133 | runtime: "python311", |
| 134 | servingStatus: "STOPPED", |
| 135 | deployment: { |
| 136 | zip: { sourceUrl }, |
| 137 | }, |
| 138 | }); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | expect(updated.versionId).toEqual(created.versionId); |
| 143 | expect(updated.servingStatus).toEqual("STOPPED"); |
| 144 | |
| 145 | yield* stack.destroy(); |
| 146 | |
| 147 | const gone = yield* waitUntilGone( |
| 148 | created.appsId, |
| 149 | created.serviceId, |
| 150 | created.versionId, |
| 151 | ); |
| 152 | expect(gone).toEqual("gone"); |
| 153 | }).pipe(logLevel), |
| 154 | { |
| 155 | tags: ["provider:gcp", "provider:gcp:appengine", "live"], |
| 156 | timeout: 120_000, |
| 157 | }, |
| 158 | ); |