| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as migrationcenter from "@distilled.cloud/gcp/migrationcenter_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 | const waitUntilGone = (name: string) => |
| 18 | migrationcenter.getProjectsLocationsImportJobs({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsImportJobs on a missing job fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | migrationcenter.getProjectsLocationsImportJobs({ |
| 37 | name: `projects/${project}/locations/us-central1/importJobs/alchemy-missing-import`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, and delete a migration center import job", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const source = yield* GCP.MigrationCenter.Source("Upload", { |
| 59 | location: "us-central1", |
| 60 | type: "SOURCE_TYPE_UPLOAD", |
| 61 | displayName: "import-source", |
| 62 | }); |
| 63 | const job = yield* GCP.MigrationCenter.ImportJob("Rvtools", { |
| 64 | location: "us-central1", |
| 65 | assetSource: source.name, |
| 66 | displayName: "rvtools-import", |
| 67 | labels: { env: "test" }, |
| 68 | }); |
| 69 | return { source, job }; |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(created.job.importJobId).toEqual(expect.any(String)); |
| 74 | expect(created.job.assetSource).toEqual(created.source.name); |
| 75 | expect(created.job.displayName).toEqual("rvtools-import"); |
| 76 | expect(created.job.labels).toMatchObject({ env: "test" }); |
| 77 | |
| 78 | const fetched = yield* migrationcenter.getProjectsLocationsImportJobs({ |
| 79 | name: created.job.name, |
| 80 | view: "IMPORT_JOB_VIEW_FULL", |
| 81 | }); |
| 82 | expect(fetched.name).toEqual(created.job.name); |
| 83 | expect(fetched.assetSource).toEqual(created.source.name); |
| 84 | expect(fetched.labels?.env).toEqual("test"); |
| 85 | |
| 86 | const updated = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | const source = yield* GCP.MigrationCenter.Source("Upload", { |
| 89 | sourceId: created.source.sourceId, |
| 90 | location: "us-central1", |
| 91 | type: "SOURCE_TYPE_UPLOAD", |
| 92 | displayName: "import-source", |
| 93 | }); |
| 94 | const job = yield* GCP.MigrationCenter.ImportJob("Rvtools", { |
| 95 | importJobId: created.job.importJobId, |
| 96 | location: "us-central1", |
| 97 | assetSource: source.name, |
| 98 | displayName: "rvtools-import-v2", |
| 99 | labels: { env: "prod" }, |
| 100 | }); |
| 101 | return { source, job }; |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(updated.job.name).toEqual(created.job.name); |
| 106 | expect(updated.job.displayName).toEqual("rvtools-import-v2"); |
| 107 | expect(updated.job.labels).toMatchObject({ env: "prod" }); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | const gone = yield* waitUntilGone(created.job.name); |
| 111 | expect(gone).toEqual("gone"); |
| 112 | }).pipe(logLevel), |
| 113 | { |
| 114 | tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"], |
| 115 | timeout: 180_000, |
| 116 | }, |
| 117 | ); |