Skip to content

Commit

Permalink
Add test cases to start increasing code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
samayer12 committed Nov 4, 2024
1 parent 72f4311 commit 62bb1fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
34 changes: 27 additions & 7 deletions src/lib/validate-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KubernetesObject> = {
operation: Operation.CREATE,
uid: "test-uid",
kind: {
group: "",
version: "v1",
kind: "Pod",
},
kind: defaultKind,
resource: {
group: "",
version: "v1",
Expand Down Expand Up @@ -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([]);
});
});
10 changes: 5 additions & 5 deletions src/lib/validate-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);

Check warning on line 47 in src/lib/validate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/validate-processor.ts#L47

Added line #L47 was not covered by tests
if (shouldSkip !== "") {
Log.debug(shouldSkip);
continue;
}

const label = action.validateCallback.name;
const label = binding.validateCallback.name;

Check warning on line 53 in src/lib/validate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/validate-processor.ts#L53

Added line #L53 was not covered by tests
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);

Check warning on line 58 in src/lib/validate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/validate-processor.ts#L58

Added line #L58 was not covered by tests
localResponse.allowed = resp.allowed;

// If the validation callback returned a status code or message, set it in the Response
Expand Down

0 comments on commit 62bb1fe

Please sign in to comment.