Skip to content

Commit

Permalink
test: add units for new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
btlghrants committed Nov 25, 2024
1 parent d995d9b commit 2724233
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 43 deletions.
45 changes: 2 additions & 43 deletions src/lib/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,13 @@ import { ModuleConfig, isWatchMode } from "../module";
import { mutateProcessor } from "../mutate-processor";
import { validateProcessor } from "../validate-processor";
import { StoreController } from "./store";
import { ResponseItem, AdmissionRequest } from "../types";
import { AdmissionRequest } from "../types";
import { karForMutate, karForValidate, KubeAdmissionReview } from "./index.util";

if (!process.env.PEPR_NODE_WARNINGS) {
process.removeAllListeners("warning");
}

interface KubeAdmissionReview {
apiVersion: string;
kind: string;
response: ValidateResponse[] | MutateResponse | ResponseItem;
}

function karForMutate(mr: MutateResponse): KubeAdmissionReview {
return {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: mr,
};
}

function karForValidate(ar: AdmissionRequest, vr: ValidateResponse[]): KubeAdmissionReview {
const isAllowed = vr.filter(r => !r.allowed).length === 0;

const resp: ValidateResponse =
vr.length === 0
? {
uid: ar.uid,
allowed: true,
status: { code: 200, message: "no in-scope validations -- allowed!" },
}
: {
uid: vr[0].uid,
allowed: isAllowed,
status: {
code: isAllowed ? 200 : 422,
message: vr
.filter(rl => !rl.allowed)
.map(curr => curr.status?.message)
.join("; "),
},
};
return {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: resp,
};
}

export class Controller {
// Track whether the server is running
#running = false;
Expand Down
103 changes: 103 additions & 0 deletions src/lib/controller/index.util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { describe, it, expect } from "@jest/globals";
import { MutateResponse, ValidateResponse } from "../k8s";
import * as sut from "./index.util";
import { AdmissionRequest } from "../types";

describe("karForMutate()", () => {
it("returns given MutateResponse wrapped in an KubeAdmissionReview", () => {
const mr: MutateResponse = { uid: "uid", allowed: true };
const kar: sut.KubeAdmissionReview = {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: mr,
};
const result = sut.karForMutate(mr);
expect(result).toEqual(kar);
});
});

describe("karForValidate()", () => {
describe("given 0 ValidationResponse[]'s", () => {
it("returns KubeAdmissionReview with abbreviated success message", () => {
const ar = { uid: "uid" } as unknown as AdmissionRequest;
const vrs: ValidateResponse[] = [];
const resp = {
uid: ar.uid,
allowed: true,
status: { code: 200, message: "no in-scope validations -- allowed!" },
};
const kar: sut.KubeAdmissionReview = {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: resp,
};
const result = sut.karForValidate(ar, vrs);
expect(result).toEqual(kar);
});
});

describe("given 1-or-more ValidationResponse[]'s", () => {
it("returns KubeAdmissionReview with detailed success message", () => {
const ar = { uid: "uid" } as unknown as AdmissionRequest;
const vrs: ValidateResponse[] = [
{
uid: "uid",
allowed: true,
status: {
code: 200,
message: "msg",
},
},
];
const resp = {
uid: ar.uid,
allowed: true,
status: { code: 200, message: "" },
};
const kar: sut.KubeAdmissionReview = {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: resp,
};
const result = sut.karForValidate(ar, vrs);
expect(result).toEqual(kar);
});

it("returns KubeAdmissionReview with detailed failure message", () => {
const ar = { uid: "uid" } as unknown as AdmissionRequest;
const vrs: ValidateResponse[] = [
{
uid: "uid",
allowed: false,
status: {
code: 422,
message: "mess",
},
},
{
uid: "uid",
allowed: false,
status: {
code: 422,
message: "age",
},
},
];
const resp = {
uid: ar.uid,
allowed: false,
status: { code: 422, message: "mess; age" },
};
const kar: sut.KubeAdmissionReview = {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: resp,
};
const result = sut.karForValidate(ar, vrs);
expect(result).toEqual(kar);
});
});
});
47 changes: 47 additions & 0 deletions src/lib/controller/index.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { MutateResponse, ValidateResponse } from "../k8s";
import { ResponseItem, AdmissionRequest } from "../types";

export interface KubeAdmissionReview {
apiVersion: string;
kind: string;
response: ValidateResponse[] | MutateResponse | ResponseItem;
}

export function karForMutate(mr: MutateResponse): KubeAdmissionReview {
return {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: mr,
};
}

export function karForValidate(ar: AdmissionRequest, vr: ValidateResponse[]): KubeAdmissionReview {
const isAllowed = vr.filter(r => !r.allowed).length === 0;

const resp: ValidateResponse =
vr.length === 0
? {
uid: ar.uid,
allowed: true,
status: { code: 200, message: "no in-scope validations -- allowed!" },
}
: {
uid: vr[0].uid,
allowed: isAllowed,
status: {
code: isAllowed ? 200 : 422,
message: vr
.filter(rl => !rl.allowed)
.map(curr => curr.status?.message)
.join("; "),
},
};
return {
apiVersion: "admission.k8s.io/v1",
kind: "AdmissionReview",
response: resp,
};
}

0 comments on commit 2724233

Please sign in to comment.