| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as netapp from "@distilled.cloud/gcp/netapp_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 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 | // Volumes need a Private Service Access connection to |
| 18 | // `netapp.servicenetworking.goog` on the `default` network (otherwise volume |
| 19 | // create fails with `Please create Service Networking connection with |
| 20 | // service 'netapp.servicenetworking.goog' ...`), and each file provisions its |
| 21 | // own 2 TiB STANDARD pool while the default regional quota |
| 22 | // (StandardStoragePoolCapacityGiBPerRegion = 2048) fits one. Set |
| 23 | // GCP_TEST_NETAPP_VOLUMES=1 on a project with both. |
| 24 | const runLifecycle = !!process.env.GCP_TEST_NETAPP_VOLUMES && !process.env.FAST; |
| 25 | |
| 26 | const waitUntilGone = (name: string) => |
| 27 | netapp.getProjectsLocationsVolumesQuotaRules({ name }).pipe( |
| 28 | Effect.as("found" as const), |
| 29 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.spaced("2 seconds"), |
| 32 | until: (status) => status === "gone", |
| 33 | times: 10, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | test.provider( |
| 38 | "getProjectsLocationsVolumesQuotaRules on a missing rule fails with a typed tag", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const { project } = yield* GcpEnvironment.current; |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const error = yield* Effect.flip( |
| 46 | netapp.getProjectsLocationsVolumesQuotaRules({ |
| 47 | name: `projects/${project}/locations/us-central1/volumes/alchemy-missing-volume/quotaRules/alchemy-missing-rule`, |
| 48 | }), |
| 49 | ); |
| 50 | expect(error._tag).toEqual("NotFound"); |
| 51 | |
| 52 | const page = yield* netapp.listProjectsLocationsVolumesQuotaRules({ |
| 53 | parent: `projects/${project}/locations/-/volumes/-`, |
| 54 | pageSize: 10, |
| 55 | }); |
| 56 | expect((page.quotaRules ?? []).map((item) => item.name)).not.toContain( |
| 57 | `projects/${project}/locations/us-central1/volumes/alchemy-missing-volume/quotaRules/alchemy-missing-rule`, |
| 58 | ); |
| 59 | |
| 60 | yield* stack.destroy(); |
| 61 | }).pipe(logLevel), |
| 62 | { tags: ["provider:gcp", "provider:gcp:netapp", "live"], timeout: 90_000 }, |
| 63 | ); |
| 64 | |
| 65 | test.provider.skipIf(!runLifecycle)( |
| 66 | "create, update, and delete a quota rule", |
| 67 | (stack) => |
| 68 | Effect.gen(function* () { |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const created = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | const pool = yield* GCP.NetApp.StoragePool("Pool", { |
| 74 | network: "default", |
| 75 | serviceLevel: "STANDARD", |
| 76 | capacityGib: 2048, |
| 77 | labels: { env: "test" }, |
| 78 | }); |
| 79 | const volume = yield* GCP.NetApp.Volume("Share", { |
| 80 | storagePool: pool.name, |
| 81 | protocols: ["NFSV3"], |
| 82 | capacityGib: 100, |
| 83 | labels: { env: "test" }, |
| 84 | }); |
| 85 | return yield* GCP.NetApp.VolumesQuotaRule("DefaultUser", { |
| 86 | volume: volume.name, |
| 87 | type: "DEFAULT_USER_QUOTA", |
| 88 | diskLimitMib: 1024, |
| 89 | description: "alchemy-test-quota", |
| 90 | labels: { env: "test" }, |
| 91 | }); |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | expect(created.name).toContain("/quotaRules/"); |
| 96 | expect(created.diskLimitMib).toEqual(1024); |
| 97 | expect(created.labels).toMatchObject({ env: "test" }); |
| 98 | |
| 99 | const fetched = yield* netapp.getProjectsLocationsVolumesQuotaRules({ |
| 100 | name: created.name, |
| 101 | }); |
| 102 | expect(fetched.name).toEqual(created.name); |
| 103 | expect(fetched.diskLimitMib).toEqual(1024); |
| 104 | expect(fetched.labels?.env).toEqual("test"); |
| 105 | |
| 106 | const updated = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | const pool = yield* GCP.NetApp.StoragePool("Pool", { |
| 109 | network: "default", |
| 110 | serviceLevel: "STANDARD", |
| 111 | capacityGib: 2048, |
| 112 | labels: { env: "test" }, |
| 113 | }); |
| 114 | const volume = yield* GCP.NetApp.Volume("Share", { |
| 115 | storagePool: pool.name, |
| 116 | protocols: ["NFSV3"], |
| 117 | capacityGib: 100, |
| 118 | labels: { env: "test" }, |
| 119 | }); |
| 120 | return yield* GCP.NetApp.VolumesQuotaRule("DefaultUser", { |
| 121 | quotaRuleId: created.quotaRuleId, |
| 122 | volume: volume.name, |
| 123 | type: "DEFAULT_USER_QUOTA", |
| 124 | diskLimitMib: 2048, |
| 125 | description: "alchemy-prod-quota", |
| 126 | labels: { env: "prod", role: "quota" }, |
| 127 | }); |
| 128 | }), |
| 129 | ); |
| 130 | |
| 131 | expect(updated.name).toEqual(created.name); |
| 132 | expect(updated.diskLimitMib).toEqual(2048); |
| 133 | expect(updated.description).toEqual("alchemy-prod-quota"); |
| 134 | expect(updated.labels).toMatchObject({ env: "prod", role: "quota" }); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const gone = yield* waitUntilGone(created.name); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { tags: ["provider:gcp", "provider:gcp:netapp", "live"], timeout: 900_000 }, |
| 142 | ); |