Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix unparsable lines in monitor command #907

Merged
merged 8 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 40 additions & 37 deletions src/cli/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import stream from "stream";
import { ResponseItem } from "../lib/types";
import { RootCmd } from "./root";

import { sleep } from "../lib/helpers";
export default function (program: RootCmd) {
program
.command("monitor [module-uuid]")
Expand Down Expand Up @@ -42,51 +42,54 @@
const log = new K8sLog(kc);

const logStream = new stream.PassThrough();

logStream.on("data", chunk => {
const respMsg = `"msg":"Check response"`;
// Split the chunk into lines
const lines = chunk.toString().split("\n");

for (const line of lines) {
// Check for `"msg":"Hello Pepr"`
if (line.includes(respMsg)) {
try {
const payload = JSON.parse(line);
const isMutate = payload.res.patchType || payload.res.warnings;

const name = `${payload.namespace}${payload.name}`;
const uid = payload.uid;

if (isMutate) {
const plainPatch = atob(payload.res.patch) || "";
const patch = JSON.stringify(JSON.parse(plainPatch), null, 2);

const patchType = payload.res.patchType || payload.res.warnings || "";

const allowOrDeny = payload.res.allowed ? "🔀" : "🚫";
console.log(`\n${allowOrDeny} MUTATE ${name} (${uid})`);
if (patchType.length > 0) {
console.log(`\n\u001b[1;34m${patch}\u001b[0m`);
}
} else {
const failures = Array.isArray(payload.res) ? payload.res : [payload.res];

const filteredFailures = failures
.filter((r: ResponseItem) => !r.allowed)
.map((r: ResponseItem) => r.status.message);
if (filteredFailures.length > 0) {
console.log(`\n❌ VALIDATE ${name} (${uid})`);
console.log(`\u001b[1;31m${filteredFailures}\u001b[0m`);
sleep(2, () => {

Check warning on line 49 in src/cli/monitor.ts

View workflow job for this annotation

GitHub Actions / format

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
for (const line of lines) {
// Check for `"msg":"Hello Pepr"`
if (line.includes(respMsg)) {
try {
const payload = JSON.parse(line.trim());
const isMutate = payload.res.patchType || payload.res.warnings;

const name = `${payload.namespace}${payload.name}`;
const uid = payload.res.uid;

if (isMutate) {
const plainPatch =
payload.res?.patch !== undefined && payload.res?.patch !== null
? atob(payload.res.patch)
: "";

const patch =
plainPatch !== "" && JSON.stringify(JSON.parse(plainPatch), null, 2);
const patchType = payload.res.patchType || payload.res.warnings || "";
const allowOrDeny = payload.res.allowed ? "🔀" : "🚫";
console.log(`\n${allowOrDeny} MUTATE ${name} (${uid})`);
if (patchType.length > 0) {
console.log(`\n\u001b[1;34m${patch}\u001b[0m`);
}
} else {
console.log(`\n✅ VALIDATE ${name} (${uid})`);
const failures = Array.isArray(payload.res) ? payload.res : [payload.res];

const filteredFailures = failures
.filter((r: ResponseItem) => !r.allowed)
.map((r: ResponseItem) => r.status.message);
if (filteredFailures.length > 0) {
console.log(`\n❌ VALIDATE ${name} (${uid})`);
console.log(`\u001b[1;31m${filteredFailures}\u001b[0m`);
} else {
console.log(`\n✅ VALIDATE ${name} (${uid})`);
}
}
} catch {
console.warn(`\nIGNORED - Unable to parse line: ${line}.`);
}
} catch {
console.warn(`\nIGNORED - Unable to parse line: ${line}.`);
}
}
}
});
});

for (const podName of podNames) {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
validateHash,
ValidationError,
validateCapabilityNames,
sleep,
} from "./helpers";
import { expect, describe, test, jest, beforeEach, afterEach } from "@jest/globals";
import { parseTimeout, secretOverLimit, replaceString } from "./helpers";
Expand Down Expand Up @@ -292,6 +293,22 @@ const mockCapabilities: CapabilityExport[] = JSON.parse(`[
]
}
]`);

describe("sleep function tests", () => {
jest.useFakeTimers();

test("sleep function delays for the specified time and executes callback before resolving", () => {
const callback = jest.fn();
const sleepPromise = sleep(1, callback);
jest.advanceTimersByTime(1000);

return sleepPromise.then(() => {
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});
});
});

describe("validateCapabilityNames", () => {
test("should return true if all capability names are valid", () => {
const capabilities = mockCapabilities;
Expand Down
9 changes: 9 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { sanitizeResourceName } from "../sdk/sdk";

export class ValidationError extends Error {}

export function sleep(seconds: number, callback: () => void) {
return new Promise<void>(resolve => {
setTimeout(() => {
callback();
resolve();
}, seconds * 1000);
});
}

export function validateCapabilityNames(capabilities: CapabilityExport[] | undefined): void {
if (capabilities && capabilities.length > 0) {
for (let i = 0; i < capabilities.length; i++) {
Expand Down
Loading