From 3cc7a4dc70ef9309f25052ca936531be6734ab7f Mon Sep 17 00:00:00 2001 From: Case Wylie Date: Mon, 16 Dec 2024 14:51:12 -0500 Subject: [PATCH 1/4] chore: return types for logger,tls,validate-request Signed-off-by: Case Wylie --- src/lib/telemetry/logger.ts | 4 +++- src/lib/tls.ts | 6 +++++- src/lib/validate-request.ts | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib/telemetry/logger.ts b/src/lib/telemetry/logger.ts index 6f92d5dd2..f2113c5ed 100644 --- a/src/lib/telemetry/logger.ts +++ b/src/lib/telemetry/logger.ts @@ -18,7 +18,9 @@ const pretty = { const transport = isPrettyLog ? pretty : undefined; // epochTime is the pino default const pinoTimeFunction = - process.env.PINO_TIME_STAMP === "iso" ? () => stdTimeFunctions.isoTime() : () => stdTimeFunctions.epochTime(); + process.env.PINO_TIME_STAMP === "iso" + ? (): string => stdTimeFunctions.isoTime() + : (): string => stdTimeFunctions.epochTime(); const Log = pino({ transport, timestamp: pinoTimeFunction, diff --git a/src/lib/tls.ts b/src/lib/tls.ts index 66118cd2a..2e25c44a7 100644 --- a/src/lib/tls.ts +++ b/src/lib/tls.ts @@ -62,7 +62,11 @@ export function genTLS(name: string): TLSOut { return { ca, key, crt, pem }; } -function genCert(key: forge.pki.rsa.KeyPair, name: string, issuer: forge.pki.CertificateField[]) { +function genCert( + key: forge.pki.rsa.KeyPair, + name: string, + issuer: forge.pki.CertificateField[], +): forge.pki.Certificate { const crt = forge.pki.createCertificate(); crt.publicKey = key.publicKey; crt.serialNumber = "01"; diff --git a/src/lib/validate-request.ts b/src/lib/validate-request.ts index d4a64bc1d..4f6b94984 100644 --- a/src/lib/validate-request.ts +++ b/src/lib/validate-request.ts @@ -23,7 +23,7 @@ export class PeprValidateRequest { * Provides access to the old resource in the request if available. * @returns The old Kubernetes resource object or null if not available. */ - get OldResource() { + get OldResource(): T | undefined { return this.#input.oldObject; } @@ -31,7 +31,7 @@ export class PeprValidateRequest { * Provides access to the request object. * @returns The request object containing the Kubernetes resource. */ - get Request() { + get Request(): AdmissionRequest { return this.#input; } @@ -61,7 +61,7 @@ export class PeprValidateRequest { * @param key the label key to check * @returns */ - HasLabel = (key: string) => { + HasLabel = (key: string): boolean => { return this.Raw.metadata?.labels?.[key] !== undefined; }; @@ -71,7 +71,7 @@ export class PeprValidateRequest { * @param key the annotation key to check * @returns */ - HasAnnotation = (key: string) => { + HasAnnotation = (key: string): boolean => { return this.Raw.metadata?.annotations?.[key] !== undefined; }; From 001e434c3420d4289e885cdcb7787497cf2abf22 Mon Sep 17 00:00:00 2001 From: Case Wylie Date: Mon, 16 Dec 2024 17:00:37 -0500 Subject: [PATCH 2/4] chore: increase coverage for utils Signed-off-by: Case Wylie --- src/cli/init/utils.test.ts | 67 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/src/cli/init/utils.test.ts b/src/cli/init/utils.test.ts index 4f679bf05..76cb2f83d 100644 --- a/src/cli/init/utils.test.ts +++ b/src/cli/init/utils.test.ts @@ -1,9 +1,16 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The Pepr Authors -import { expect, it } from "@jest/globals"; +import { expect, it, jest, describe, afterEach } from "@jest/globals"; +import { promises as fs } from "fs"; +import { sanitizeName, createDir, write } from "./utils"; -import { sanitizeName } from "./utils"; +jest.mock("fs", () => ({ + promises: { + mkdir: jest.fn(), + writeFile: jest.fn(), + }, +})); it.each([ //Test sanitizeName() with ["$BAD_INPUT", "$SANITIZED_INPUT"] @@ -17,3 +24,59 @@ it.each([ it("sanitizeName() should throw TypeError when given a non-string", () => { expect(() => sanitizeName({ input: 0 } as unknown as string)).toThrow(TypeError); }); + +describe("createDir", () => { + const mockedMkdir = fs.mkdir as jest.Mock; + + afterEach(() => { + jest.clearAllMocks(); // Clear mocks between tests + }); + + it("should create the pepr-test-module dir", async () => { + await createDir("pepr-test-module"); + expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module"); + }); + + it("should throw an error if the directory already exists", async () => { + const error = new Error("Directory already exists") as NodeJS.ErrnoException; + error.code = "EEXIST"; + mockedMkdir.mockRejectedValueOnce(error as unknown as never); + + await expect(createDir("pepr-test-module")).rejects.toThrow( + "Directory pepr-test-module already exists", + ); + expect(mockedMkdir).toHaveBeenCalledTimes(1); + expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module"); + }); + + it("should throw the error if there is another error other than already exists", async () => { + const error = new Error("Directory already exists") as NodeJS.ErrnoException; + error.code = "ENOENT"; + mockedMkdir.mockRejectedValueOnce(error as unknown as never); + + await expect(createDir("pepr-test-module")).rejects.toThrow(error); + expect(mockedMkdir).toHaveBeenCalledTimes(1); + expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module"); + }); +}); + +describe("write", () => { + const mockedWriteFile = fs.writeFile as jest.Mock; + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should write data to a file", async () => { + await write("package.json", "pepr-test-module"); + expect(mockedWriteFile).toHaveBeenCalledWith("package.json", "pepr-test-module"); + }); + + it("should stringify data if it is not a string", async () => { + await write("package.json", { name: "pepr-test-module" }); + expect(mockedWriteFile).toHaveBeenCalledWith( + "package.json", + JSON.stringify({ name: "pepr-test-module" }, null, 2), + ); + }); +}); From 86edbf89d9461082763696c5eac2e3dc56edcf47 Mon Sep 17 00:00:00 2001 From: Case Wylie Date: Mon, 16 Dec 2024 17:02:41 -0500 Subject: [PATCH 3/4] chore: change back - should be in a different pr Signed-off-by: Case Wylie --- src/cli/init/utils.test.ts | 67 ++------------------------------------ 1 file changed, 2 insertions(+), 65 deletions(-) diff --git a/src/cli/init/utils.test.ts b/src/cli/init/utils.test.ts index 76cb2f83d..4f679bf05 100644 --- a/src/cli/init/utils.test.ts +++ b/src/cli/init/utils.test.ts @@ -1,16 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The Pepr Authors -import { expect, it, jest, describe, afterEach } from "@jest/globals"; -import { promises as fs } from "fs"; -import { sanitizeName, createDir, write } from "./utils"; +import { expect, it } from "@jest/globals"; -jest.mock("fs", () => ({ - promises: { - mkdir: jest.fn(), - writeFile: jest.fn(), - }, -})); +import { sanitizeName } from "./utils"; it.each([ //Test sanitizeName() with ["$BAD_INPUT", "$SANITIZED_INPUT"] @@ -24,59 +17,3 @@ it.each([ it("sanitizeName() should throw TypeError when given a non-string", () => { expect(() => sanitizeName({ input: 0 } as unknown as string)).toThrow(TypeError); }); - -describe("createDir", () => { - const mockedMkdir = fs.mkdir as jest.Mock; - - afterEach(() => { - jest.clearAllMocks(); // Clear mocks between tests - }); - - it("should create the pepr-test-module dir", async () => { - await createDir("pepr-test-module"); - expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module"); - }); - - it("should throw an error if the directory already exists", async () => { - const error = new Error("Directory already exists") as NodeJS.ErrnoException; - error.code = "EEXIST"; - mockedMkdir.mockRejectedValueOnce(error as unknown as never); - - await expect(createDir("pepr-test-module")).rejects.toThrow( - "Directory pepr-test-module already exists", - ); - expect(mockedMkdir).toHaveBeenCalledTimes(1); - expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module"); - }); - - it("should throw the error if there is another error other than already exists", async () => { - const error = new Error("Directory already exists") as NodeJS.ErrnoException; - error.code = "ENOENT"; - mockedMkdir.mockRejectedValueOnce(error as unknown as never); - - await expect(createDir("pepr-test-module")).rejects.toThrow(error); - expect(mockedMkdir).toHaveBeenCalledTimes(1); - expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module"); - }); -}); - -describe("write", () => { - const mockedWriteFile = fs.writeFile as jest.Mock; - - afterEach(() => { - jest.clearAllMocks(); - }); - - it("should write data to a file", async () => { - await write("package.json", "pepr-test-module"); - expect(mockedWriteFile).toHaveBeenCalledWith("package.json", "pepr-test-module"); - }); - - it("should stringify data if it is not a string", async () => { - await write("package.json", { name: "pepr-test-module" }); - expect(mockedWriteFile).toHaveBeenCalledWith( - "package.json", - JSON.stringify({ name: "pepr-test-module" }, null, 2), - ); - }); -}); From e31be47d185b1812331e833a5dbf6eef939a3876 Mon Sep 17 00:00:00 2001 From: Case Wylie Date: Tue, 17 Dec 2024 09:04:27 -0500 Subject: [PATCH 4/4] chore: remove generics Signed-off-by: Case Wylie --- src/lib/validate-request.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/validate-request.ts b/src/lib/validate-request.ts index 4f6b94984..67bf45288 100644 --- a/src/lib/validate-request.ts +++ b/src/lib/validate-request.ts @@ -23,7 +23,7 @@ export class PeprValidateRequest { * Provides access to the old resource in the request if available. * @returns The old Kubernetes resource object or null if not available. */ - get OldResource(): T | undefined { + get OldResource(): KubernetesObject | undefined { return this.#input.oldObject; } @@ -31,7 +31,7 @@ export class PeprValidateRequest { * Provides access to the request object. * @returns The request object containing the Kubernetes resource. */ - get Request(): AdmissionRequest { + get Request(): AdmissionRequest { return this.#input; }