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 5 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
3 changes: 2 additions & 1 deletion journey/pepr-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ export function peprDeploy() {
const stdout: String = data.toString()
state.accept = stdout.includes("✅") ? true : state.accept
state.reject = stdout.includes("❌") ? true : state.reject

expect(stdout.includes("IGNORED")).toBe(false)
if (state.accept && state.reject) {
proc.kill()
proc.stdin.destroy()
proc.stdout.destroy()
proc.stderr.destroy()
}
})

proc.on('exit', () => state.done = true);

await until(() => state.done)
Expand Down
19 changes: 10 additions & 9 deletions src/cli/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { K8s, kind } from "kubernetes-fluent-client";
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,28 +42,29 @@ export default function (program: RootCmd) {
const log = new K8sLog(kc);

const logStream = new stream.PassThrough();

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

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

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

if (isMutate) {
const plainPatch = atob(payload.res.patch) || "";
const patch = JSON.stringify(JSON.parse(plainPatch), null, 2);
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) {
Expand Down
15 changes: 15 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,20 @@ const mockCapabilities: CapabilityExport[] = JSON.parse(`[
]
}
]`);

test("sleep function", async () => {
const startTime = Date.now();
const sleepTimeSeconds = 2;

await sleep(2);

const endTime = Date.now();
const elapsedTime = endTime - startTime;

expect(elapsedTime).toBeGreaterThanOrEqual(sleepTimeSeconds);
expect(elapsedTime).toBeLessThan(3 * 1000); // milliseconds to second conversion
});

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

export class ValidationError extends Error {}

export function sleep(seconds: number) {
return new Promise<void>(resolve => {
setTimeout(() => {
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