sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ GCP/ Compute/ TargetVpnGateway.test.ts

import * as GCP from "@alchemy.run/gcp";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as compute from "@distilled.cloud/gcp/compute_v1";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
import { DEFAULT_NETWORK } from "../networkQuota.ts";

const { test } = Test.make({ providers: GCP.providers() });

const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");

const waitUntilGone = (project: string, region: string, targetVpnGatewayName: string) =>
  compute
    .getTargetVpnGateways({
      project,
      region,
      targetVpnGateway: targetVpnGatewayName,
    })
    .pipe(
      Effect.as("found" as const),
      Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
      Effect.repeat({
        schedule: Schedule.spaced("1 second"),
        until: (status) => status === "gone",
        times: 10,
      }),
    );

test.provider.skipIf(!!process.env.FAST)(
  "create, update, and delete a Classic VPN gateway",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const created = yield* stack.deploy(
        Effect.gen(function* () {
          const gateway = yield* GCP.Compute.TargetVpnGateway("Gateway", {
            region: "us-central1",
            network: DEFAULT_NETWORK,
            description: "classic vpn",
            labels: { env: "test" },
          });
          return { gateway };
        }),
      );

      expect(created.gateway.targetVpnGatewayName).toEqual(expect.any(String));
      expect(created.gateway.region).toEqual("us-central1");
      expect(created.gateway.network).toEqual(
        expect.stringContaining(`/networks/${DEFAULT_NETWORK}`),
      );
      expect(created.gateway.description).toEqual("classic vpn");
      expect(created.gateway.labels).toMatchObject({ env: "test" });
      expect(created.gateway.status).toEqual("READY");

      const fetched = yield* compute.getTargetVpnGateways({
        project: created.gateway.project,
        region: created.gateway.region,
        targetVpnGateway: created.gateway.targetVpnGatewayName,
      });
      expect(fetched.name).toEqual(created.gateway.targetVpnGatewayName);
      expect(fetched.description).toEqual("classic vpn");
      expect(fetched.labels?.env).toEqual("test");
      expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
      expect(fetched.network).toEqual(expect.stringContaining(`/networks/${DEFAULT_NETWORK}`));

      const updated = yield* stack.deploy(
        Effect.gen(function* () {
          const gateway = yield* GCP.Compute.TargetVpnGateway("Gateway", {
            targetVpnGatewayName: created.gateway.targetVpnGatewayName,
            region: "us-central1",
            network: DEFAULT_NETWORK,
            description: "classic vpn",
            labels: { env: "prod", role: "vpn" },
          });
          return { gateway };
        }),
      );

      expect(updated.gateway.targetVpnGatewayName).toEqual(created.gateway.targetVpnGatewayName);
      expect(updated.gateway.targetVpnGatewayId).toEqual(created.gateway.targetVpnGatewayId);
      expect(updated.gateway.labels).toMatchObject({
        env: "prod",
        role: "vpn",
      });

      const fetchedUpdated = yield* compute.getTargetVpnGateways({
        project: updated.gateway.project,
        region: updated.gateway.region,
        targetVpnGateway: updated.gateway.targetVpnGatewayName,
      });
      expect(fetchedUpdated.labels?.env).toEqual("prod");
      expect(fetchedUpdated.labels?.role).toEqual("vpn");

      yield* stack.destroy();

      const gone = yield* waitUntilGone(
        created.gateway.project,
        created.gateway.region,
        created.gateway.targetVpnGatewayName,
      );
      expect(gone).toEqual("gone");
    }).pipe(logLevel),
  { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 },
);