From 62bb1fee073cbfcd43157fa2cf5b13e6f9d8ca1e Mon Sep 17 00:00:00 2001 From: Sam Mayer Date: Mon, 4 Nov 2024 13:51:55 -0600 Subject: [PATCH] Add test cases to start increasing code coverage --- src/lib/validate-processor.test.ts | 34 ++++++++++++++++++++++++------ src/lib/validate-processor.ts | 10 ++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/lib/validate-processor.test.ts b/src/lib/validate-processor.test.ts index 4be184f52..814541861 100644 --- a/src/lib/validate-processor.test.ts +++ b/src/lib/validate-processor.test.ts @@ -2,21 +2,28 @@ import { describe, expect, it } from "@jest/globals"; import { validateProcessor } from "./validate-processor"; import { Capability } from "./capability"; import { KubernetesObject } from "kubernetes-fluent-client"; -import { AdmissionRequest } from "./types"; +import { AdmissionRequest, CapabilityCfg } from "./types"; import { Operation } from "./enums"; describe("validate-processor tests", () => { + const defaultCapabilityConfig: CapabilityCfg = { + name: "test-capability", + description: "Test capability description", + namespaces: ["default"], + }; + const defaultModuleConfig = { uuid: "some-uuid", alwaysIgnore: { namespaces: [] } }; - const defaultCapabilities: Capability[] = []; + const defaultCapabilities: Capability[] = [new Capability(defaultCapabilityConfig)]; const defaultRequestMetadata = {}; + const defaultKind = { + group: "", + version: "v1", + kind: "Pod", + }; const defaultRequest: AdmissionRequest = { operation: Operation.CREATE, uid: "test-uid", - kind: { - group: "", - version: "v1", - kind: "Pod", - }, + kind: defaultKind, resource: { group: "", version: "v1", @@ -51,4 +58,17 @@ describe("validate-processor tests", () => { ); expect(result).toStrictEqual([]); }); + + it("TODO: should do something when secret", async () => { + const request = { ...defaultRequest, kind: { group: "", kind: "Secret", version: "v1" } }; + const result = await validateProcessor(defaultModuleConfig, defaultCapabilities, request, defaultRequestMetadata); + expect(result).toStrictEqual([]); + }); + + it("TODO should do something with bindings", async () => { + const capabilities: Capability[] = [new Capability({ ...defaultCapabilityConfig })]; + const request = { ...defaultRequest, kind: { group: "", kind: "Secret", version: "v1" } }; + const result = await validateProcessor(defaultModuleConfig, capabilities, request, defaultRequestMetadata); + expect(result).toStrictEqual([]); + }); }); diff --git a/src/lib/validate-processor.ts b/src/lib/validate-processor.ts index 43571a330..3e94d7bfa 100644 --- a/src/lib/validate-processor.ts +++ b/src/lib/validate-processor.ts @@ -32,9 +32,9 @@ export async function validateProcessor( for (const { name, bindings, namespaces } of capabilities) { const actionMetadata = { ...reqMetadata, name }; - for (const action of bindings) { + for (const binding of bindings) { // Skip this action if it's not a validation action - if (!action.validateCallback) { + if (!binding.validateCallback) { continue; } @@ -44,18 +44,18 @@ export async function validateProcessor( }; // Continue to the next action without doing anything if this one should be skipped - const shouldSkip = shouldSkipRequest(action, req, namespaces, config?.alwaysIgnore?.namespaces); + const shouldSkip = shouldSkipRequest(binding, req, namespaces, config?.alwaysIgnore?.namespaces); if (shouldSkip !== "") { Log.debug(shouldSkip); continue; } - const label = action.validateCallback.name; + const label = binding.validateCallback.name; Log.info(actionMetadata, `Processing validation action (${label})`); try { // Run the validation callback, if it fails set allowed to false - const resp = await action.validateCallback(wrapped); + const resp = await binding.validateCallback(wrapped); localResponse.allowed = resp.allowed; // If the validation callback returned a status code or message, set it in the Response