Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 committed Dec 9, 2024
1 parent 9d62fcc commit 39c7c37
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
122 changes: 120 additions & 2 deletions src/cli/monitor.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { expect, describe, it, jest } from "@jest/globals";
import { getLabelsAndErrorMessage, getK8sLogFromKubeConfig } from "./monitor";
import { expect, describe, it, jest, beforeEach, afterEach } from "@jest/globals";
import {
getLabelsAndErrorMessage,
getK8sLogFromKubeConfig,
processMutateLog,
processValidateLog,
} from "./monitor";
import { KubeConfig, Log as K8sLog } from "@kubernetes/client-node";
import { SpiedFunction } from "jest-mock";

const payload = {
level: 30,
time: 1733751945893,
pid: 1,
hostname: "test-host",
uid: "test-uid",
namespace: "test-namespace",
name: "test-name",
res: {
allowed: true,
uid: "test-uid",
patch: btoa(JSON.stringify({ key: "value" })),
patchType: "test-patch-type",
warning: "test-warning",
status: { message: "test-message" },
},
msg: "Check response",
};

jest.mock("@kubernetes/client-node", () => {
const mockKubeConfig = jest.fn();
Expand Down Expand Up @@ -47,3 +72,96 @@ describe("getLabelsAndErrorMessage", () => {
expect(result).toEqual(expected);
});
});

describe("processMutateLog", () => {
let consoleLogSpy: SpiedFunction<{
(...data: unknown[]): void;
(message?: unknown, ...optionalParams: unknown[]): void;
}>;

beforeEach(() => {
consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {});
});

afterEach(() => {
jest.restoreAllMocks();
});

it("should log a mutation approval with patch details", () => {
processMutateLog(
{
...payload,
res: {
...payload.res,
patch: btoa(JSON.stringify({ key: "value" })),
patchType: "JSONPatch",
},
},
"test-name",
"test-uid",
);

expect(consoleLogSpy).toHaveBeenCalledWith("\n🔀 MUTATE test-name (test-uid)");
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining(JSON.stringify({ key: "value" }, null, 2)),
);
});

it("should log a mutation denial without patch details", () => {
processMutateLog(
{
...payload,
res: {
...payload.res,
allowed: false,
patch: btoa(JSON.stringify("something")),
},
},
"test-name",
"test-uid",
);

expect(consoleLogSpy).toHaveBeenCalledWith("\n🚫 MUTATE test-name (test-uid)");
expect(consoleLogSpy).not.toHaveBeenCalledWith(expect.stringContaining("{"));
});
});

describe("processValidateLog", () => {
let consoleLogSpy: SpiedFunction<{
(...data: unknown[]): void;
(message?: unknown, ...optionalParams: unknown[]): void;
}>;

beforeEach(() => {
consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {});
});

afterEach(() => {
jest.restoreAllMocks();
});

it("should log a successful validation", () => {
processValidateLog(payload, "test-name", "test-uid");

expect(consoleLogSpy).toHaveBeenCalledWith("\n✅ VALIDATE test-name (test-uid)");
expect(consoleLogSpy).not.toHaveBeenCalledWith(expect.stringContaining("❌"));
});

it("should log a validation failure with error messages", () => {
processValidateLog(
{
...payload,
res: {
...payload.res,
allowed: false,
status: { message: "Failure message 1" },
},
},
"test-name",
"test-uid",
);

expect(consoleLogSpy).toHaveBeenCalledWith("\n❌ VALIDATE test-name (test-uid)");
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining("Failure message 1"));
});
});
4 changes: 2 additions & 2 deletions src/cli/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function getK8sLogFromKubeConfig(): K8sLog {
return new K8sLog(kc);
}

export function createLogStream(): stream.PassThrough {
function createLogStream(): stream.PassThrough {
const logStream = new stream.PassThrough();

logStream.on("data", async chunk => {
Expand All @@ -97,7 +97,7 @@ export function createLogStream(): stream.PassThrough {
return logStream;
}

export function processLogLine(line: string): void {
function processLogLine(line: string): void {
try {
const payload: LogPayload = JSON.parse(line.trim());
const isMutate = payload.res.patchType || payload.res.warnings;
Expand Down

0 comments on commit 39c7c37

Please sign in to comment.