sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ GCP/ NetworkServices/ LbTrafficExtension.test.ts

import * as GCP from "@alchemy.run/gcp";
import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as networkservices from "@distilled.cloud/gcp/networkservices_v1";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
import { defaultNetworkSelfLink } from "../networkQuota.ts";

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

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

// The fixture's INTERNAL_MANAGED address currently fails with BadRequest
// "Invalid value for field 'resource.network': ... The field cannot be
// specified with subnetwork address." — it needs a subnetwork plus a
// proxy-only subnet. Set GCP_TEST_LB_TRAFFIC_EXTENSION=1 once reworked.
const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_LB_TRAFFIC_EXTENSION;

const waitUntilGone = (name: string) =>
  networkservices.getProjectsLocationsLbTrafficExtensions({ name }).pipe(
    Effect.as("found" as const),
    Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
    Effect.repeat({
      schedule: Schedule.spaced("2 seconds"),
      until: (status) => status === "gone",
      times: 10,
    }),
  );

test.provider(
  "getProjectsLocationsLbTrafficExtensions on a missing extension fails with a typed tag",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const { project } = yield* GcpEnvironment.current;
      const error = yield* Effect.flip(
        networkservices.getProjectsLocationsLbTrafficExtensions({
          name: `projects/${project}/locations/us-central1/lbTrafficExtensions/alchemy-missing`,
        }),
      );
      expect(error._tag).toEqual("NotFound");

      yield* stack.destroy();
    }).pipe(logLevel),
  {
    tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
    timeout: 90_000,
  },
);

test.provider.skipIf(!runLifecycle)(
  "create, update, and delete an lb traffic extension",
  (stack) =>
    Effect.gen(function* () {
      const { project } = yield* GcpEnvironment.current;
      yield* stack.destroy();

      const created = yield* stack.deploy(
        Effect.gen(function* () {
          const healthCheck = yield* GCP.Compute.HealthCheck("Hc", {
            type: "HTTP",
            httpHealthCheck: { port: 80, requestPath: "/" },
          });
          const backend = yield* GCP.Compute.RegionBackendService("Backend", {
            region: "us-central1",
            loadBalancingScheme: "INTERNAL_MANAGED",
            protocol: "HTTP",
            healthChecks: [healthCheck.selfLink.as<string>()],
          });
          const urlMap = yield* GCP.Compute.RegionUrlMap("Map", {
            region: "us-central1",
            defaultService: backend.selfLink.as<string>(),
          });
          const proxy = yield* GCP.Compute.RegionTargetHttpProxy("Proxy", {
            region: "us-central1",
            urlMap: urlMap.selfLink.as<string>(),
          });
          const address = yield* GCP.Compute.Address("Ip", {
            region: "us-central1",
            addressType: "INTERNAL",
            purpose: "SHARED_LOADBALANCER_VIP",
            network: defaultNetworkSelfLink(project),
          });
          const rule = yield* GCP.Compute.ForwardingRule("Rule", {
            region: "us-central1",
            loadBalancingScheme: "INTERNAL_MANAGED",
            ipProtocol: "TCP",
            portRange: "80",
            target: proxy.selfLink.as<string>(),
            ipAddress: address.selfLink.as<string>(),
            network: defaultNetworkSelfLink(project),
          });
          const callout = yield* GCP.Compute.RegionBackendService("Callout", {
            region: "us-central1",
            loadBalancingScheme: "INTERNAL_MANAGED",
            protocol: "HTTP2",
            healthChecks: [healthCheck.selfLink.as<string>()],
          });
          const extension = yield* GCP.NetworkServices.LbTrafficExtension("Inspect", {
            location: "us-central1",
            description: "traffic ext a",
            labels: { env: "test" },
            loadBalancingScheme: "INTERNAL_MANAGED",
            forwardingRules: [rule.selfLink.as<string>()],
            extensionChains: [
              {
                name: "all-traffic",
                matchCondition: { celExpression: "true" },
                extensions: [
                  {
                    name: "header-rewriter",
                    service: callout.selfLink.as<string>(),
                    authority: "ext.example.com",
                    timeout: "0.1s",
                    failOpen: true,
                    supportedEvents: ["REQUEST_HEADERS"],
                  },
                ],
              },
            ],
          });
          return { extension };
        }),
      );

      expect(created.extension.name).toContain("/lbTrafficExtensions/");
      expect(created.extension.lbTrafficExtensionId).toEqual(expect.any(String));
      expect(created.extension.location).toEqual("us-central1");
      expect(created.extension.description).toEqual("traffic ext a");
      expect(created.extension.loadBalancingScheme).toEqual("INTERNAL_MANAGED");
      expect(created.extension.labels).toMatchObject({ env: "test" });

      const fetched = yield* networkservices.getProjectsLocationsLbTrafficExtensions({
        name: created.extension.name,
      });
      expect(fetched.name).toEqual(created.extension.name);
      expect(fetched.description).toEqual("traffic ext a");
      expect(fetched.labels?.env).toEqual("test");
      expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
        true,
      );

      yield* stack.destroy();

      const gone = yield* waitUntilGone(created.extension.name);
      expect(gone).toEqual("gone");
    }).pipe(logLevel),
  {
    tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
    timeout: 900_000,
  },
);