From 2ce85859454a0d5614ab15114bf12ec6e4320738 Mon Sep 17 00:00:00 2001 From: re-Tick Date: Mon, 26 Feb 2024 16:56:32 +0530 Subject: [PATCH 1/3] fix: adds configuration to set maxTimeout for keploy test And it also calls the keploy graph handler to stop the ebpf hooks Signed-off-by: re-Tick --- keployCli.ts | 57 ++++++++++++++++++++++++++++++++++++++--------- package-lock.json | 4 ++-- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/keployCli.ts b/keployCli.ts index e328c1b..004bd07 100644 --- a/keployCli.ts +++ b/keployCli.ts @@ -13,6 +13,11 @@ export enum TestRunStatus { FAILED = 'FAILED' } +interface TestOptions { + maxTimeout: number; + +} + let hasTestRunCompleted = false; export const setTestRunCompletionStatus = (status: boolean) => { @@ -21,12 +26,18 @@ export const setTestRunCompletionStatus = (status: boolean) => { let userCommandPID: any = 0; -export const Test = async (appCmd: string, options: any, callback: (err: Error | null, result?: boolean) => void) => { +export const Test = async (appCmd: string, options: TestOptions, callback: (err: Error | null, result?: boolean) => void) => { + // set default values if (appCmd == "") { appCmd = "npm start" } + if (options.maxTimeout === 0 || options.maxTimeout === undefined || options.maxTimeout === null) { + console.log("setting the max timeout for the test to 30 seconds") + options.maxTimeout = 30000; + } + console.log("max timeout: ", options); + let testResult = true; - const MAX_TIMEOUT = 10000; let startTime = Date.now(); try { const testSets = await FetchTestSets(); @@ -41,17 +52,18 @@ export const Test = async (appCmd: string, options: any, callback: (err: Error | const testRunId = await RunTestSet(testset); let testRunStatus; while (true) { - await new Promise(res => setTimeout(res, 10000)); + await new Promise(res => setTimeout(res, 2000)); testRunStatus = await FetchTestSetStatus(testRunId); - if (testRunStatus === TestRunStatus.RUNNING) { - console.log("testRun still in progress"); - if (Date.now() - startTime > MAX_TIMEOUT) { - console.log("Timeout reached, exiting loop"); - break; - } - continue; + // break the loop if the testRunStatus is not running or if it's been more than `maxTimeout` milliseconds + if (testRunStatus !== TestRunStatus.RUNNING) { + break; } - break; + if (Date.now() - startTime > options.maxTimeout) { + console.log("Timeout reached, exiting loop", Date.now() - startTime, options.maxTimeout); + break; + } + console.log("testRun still in progress"); + // break; } if (testRunStatus === TestRunStatus.FAILED || testRunStatus === TestRunStatus.RUNNING) { @@ -66,6 +78,8 @@ export const Test = async (appCmd: string, options: any, callback: (err: Error | StopUserApplication() await new Promise(res => setTimeout(res, 5000)); // wait for the application to stop } + // stop the ebpf hooks + stopTest(); callback(null, testResult); // Callback with no error and the test result } catch (error) { callback(error as Error); // Callback with the error cast to an Error object @@ -247,6 +261,27 @@ export const FetchTestSets = async (): Promise => { return null; }; +const stopTest = async (): Promise => { + try { + const client = await setHttpClient(); + if (!client) throw new Error("Could not initialize HTTP client."); + const response = await client.post('', { + query: `{ stopTest }` + }); + if (response.status >= 200 && response.status < 300) { + if (response.data && response.data.data) { + return response.data.data.stopTest; + } else { + console.error('Unexpected response structure', response.data); + return false; + } + } + } catch (error) { + console.error('Error stopping the test', error); + } + return false; +}; + export const FetchTestSetStatus = async (testRunId: string): Promise => { try { const client = await setHttpClient(); diff --git a/package-lock.json b/package-lock.json index 8a031b7..985e791 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@keploy/sdk", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@keploy/sdk", - "version": "2.0.0", + "version": "2.0.1", "license": "MIT", "dependencies": { "@types/express": "^4.17.21", From eb6d7a320793e1ac8c666addfc0892ed0b3b2012 Mon Sep 17 00:00:00 2001 From: re-Tick Date: Mon, 26 Feb 2024 16:59:58 +0530 Subject: [PATCH 2/3] style: removes debug log statements Signed-off-by: re-Tick --- keployCli.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/keployCli.ts b/keployCli.ts index 004bd07..6500f68 100644 --- a/keployCli.ts +++ b/keployCli.ts @@ -32,10 +32,8 @@ export const Test = async (appCmd: string, options: TestOptions, callback: (err: appCmd = "npm start" } if (options.maxTimeout === 0 || options.maxTimeout === undefined || options.maxTimeout === null) { - console.log("setting the max timeout for the test to 30 seconds") options.maxTimeout = 30000; } - console.log("max timeout: ", options); let testResult = true; let startTime = Date.now(); From ece81c89673fa49950ae13a710e62a4127644d0a Mon Sep 17 00:00:00 2001 From: re-Tick Date: Mon, 26 Feb 2024 17:04:39 +0530 Subject: [PATCH 3/3] style: logs the maxTimeout when test exection exceeds Signed-off-by: re-Tick --- keployCli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keployCli.ts b/keployCli.ts index 6500f68..dd9761c 100644 --- a/keployCli.ts +++ b/keployCli.ts @@ -57,7 +57,7 @@ export const Test = async (appCmd: string, options: TestOptions, callback: (err: break; } if (Date.now() - startTime > options.maxTimeout) { - console.log("Timeout reached, exiting loop", Date.now() - startTime, options.maxTimeout); + console.log("Timeout reached, exiting loop. maxTimeout: ", options.maxTimeout); break; } console.log("testRun still in progress");