Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Racer159 committed May 9, 2024
1 parent 0531a3c commit a8f2838
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/pepr/operator/controllers/istio/service-entry.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from "@jest/globals";
import { UDSConfig } from "../../../config";
import { generateServiceEntry } from "./service-entry";
import { Expose, Gateway, IstioLocation, IstioResolution } from "../../crd";

describe("test generate service entry", () => {
const ownerRefs = [
{
apiVersion: "uds.dev/v1alpha1",
kind: "Package",
name: "test",
uid: "f50120aa-2713-4502-9496-566b102b1174",
},
];

const host = "test";
const port = 8080;
const service = "test-service"

const namespace = "test";
const pkgName = "test";
const generation = "1";

it("should create a simple ServiceEntry object", () => {
const expose: Expose = {
host,
port,
service,
};

const payload = generateServiceEntry(expose, namespace, pkgName, generation, ownerRefs);

expect(payload).toBeDefined();
expect(payload.metadata?.name).toEqual(`${pkgName}-${Gateway.Tenant}-${host}`);
expect(payload.metadata?.namespace).toEqual(namespace);

expect(payload.spec?.hosts).toBeDefined();
expect(payload.spec!.hosts![0]).toEqual(`${host}.${UDSConfig.domain}`);

expect(payload.spec!.location).toEqual(IstioLocation.MeshInternal);
expect(payload.spec!.resolution).toEqual(IstioResolution.DNS);

expect(payload.spec?.ports).toBeDefined();
expect(payload.spec!.ports![0].name).toEqual("https");
expect(payload.spec!.ports![0].number).toEqual(443);
expect(payload.spec!.ports![0].protocol).toEqual("HTTPS");

expect(payload.spec?.endpoints).toBeDefined();
expect(payload.spec!.endpoints![0].address).toEqual(`${Gateway.Tenant}-ingressgateway.istio-${Gateway.Tenant}-gateway.svc.cluster.local`);
});
});
104 changes: 104 additions & 0 deletions src/pepr/operator/controllers/istio/virtual-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { describe, expect, it } from "@jest/globals";
import { UDSConfig } from "../../../config";
import { generateVirtualService } from "./virtual-service";
import { Expose, Gateway } from "../../crd";

describe("test generate virtual service", () => {
const ownerRefs = [
{
apiVersion: "uds.dev/v1alpha1",
kind: "Package",
name: "test",
uid: "f50120aa-2713-4502-9496-566b102b1174",
},
];

const host = "test";
const port = 8080;
const service = "test-service"

const namespace = "test";
const pkgName = "test";
const generation = "1";

it("should create a simple VirtualService object", () => {
const expose: Expose = {
host,
port,
service,
};

const payload = generateVirtualService(expose, namespace, pkgName, generation, ownerRefs);

expect(payload).toBeDefined();
expect(payload.metadata?.name).toEqual(`${pkgName}-${Gateway.Tenant}-${host}-${port}-${service}`);
expect(payload.metadata?.namespace).toEqual(namespace);

expect(payload.spec?.hosts).toBeDefined();
expect(payload.spec!.hosts![0]).toEqual(`${host}.${UDSConfig.domain}`);

expect(payload.spec?.http).toBeDefined();
expect(payload.spec!.http![0].route).toBeDefined();
expect(payload.spec!.http![0].route![0].destination?.host).toEqual(`${service}.${namespace}.svc.cluster.local`);
expect(payload.spec!.http![0].route![0].destination?.port?.number).toEqual(port);

expect(payload.spec?.gateways).toBeDefined();
expect(payload.spec!.gateways![0]).toEqual(`istio-${Gateway.Tenant}-gateway/${Gateway.Tenant}-gateway`);
});

it("should create an admin VirtualService object", () => {
const gateway = Gateway.Admin;
const expose: Expose = {
gateway,
host,
port,
service,
};

const payload = generateVirtualService(expose, namespace, pkgName, generation, ownerRefs);

expect(payload).toBeDefined();
expect(payload.spec?.hosts).toBeDefined();
expect(payload.spec!.hosts![0]).toEqual(`${host}.admin.${UDSConfig.domain}`);
});

it("should create an advancedHttp VirtualService object", () => {
const advancedHTTP = {
directResponse: { status: 404 }
}
const expose: Expose = {
host,
port,
service,
advancedHTTP,
};

const payload = generateVirtualService(expose, namespace, pkgName, generation, ownerRefs);

expect(payload).toBeDefined();
expect(payload.spec?.http).toBeDefined();
expect(payload.spec!.http![0].route).not.toBeDefined();
expect(payload.spec!.http![0].directResponse?.status).toEqual(404);
});

it("should create a passthrough VirtualService object", () => {
const gateway = Gateway.Passthrough;
const expose: Expose = {
gateway,
host,
port,
service,
};

const payload = generateVirtualService(expose, namespace, pkgName, generation, ownerRefs);

expect(payload).toBeDefined();
expect(payload.spec?.tls).toBeDefined();
expect(payload.spec!.tls![0].match).toBeDefined();
expect(payload.spec!.tls![0].match![0].port).toEqual(443);
expect(payload.spec!.tls![0].match![0].sniHosts![0]).toEqual(`${host}.${UDSConfig.domain}`);
expect(payload.spec!.tls![0].route).toBeDefined();
expect(payload.spec!.http![0].route![0].destination?.host).toEqual(`${service}.${namespace}.svc.cluster.local`);
expect(payload.spec!.http![0].route![0].destination?.port?.number).toEqual(port);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "@jest/globals";
import { generateServiceMonitor } from "./service-monitor";
import { Monitor } from "../../crd";

describe("test generate service monitor", () => {
it("should return a valid Service Monitor object", () => {
Expand All @@ -14,7 +15,7 @@ describe("test generate service monitor", () => {
const portName = "http-metrics";
const metricsPath = "/test";
const selectorApp = "test";
const monitor = {
const monitor: Monitor = {
portName: portName,
path: metricsPath,
targetPort: 1234,
Expand Down
3 changes: 1 addition & 2 deletions src/pepr/operator/controllers/monitoring/service-monitor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { K8s, Log } from "pepr";

import { V1OwnerReference } from "@kubernetes/client-node";
import { Prometheus, UDSPackage } from "../../crd";
import { Monitor } from "../../crd/generated/package-v1alpha1";
import { Prometheus, UDSPackage, Monitor } from "../../crd";
import { getOwnerRef, sanitizeResourceName } from "../utils";

/**
Expand Down
1 change: 1 addition & 0 deletions src/pepr/operator/crd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {
Allow,
Direction,
Expose,
Monitor,
Gateway,
Phase,
Status as PkgStatus,
Expand Down

0 comments on commit a8f2838

Please sign in to comment.