Skip to content

Commit

Permalink
fix: adds configuration for maxTimeout of keployTest and stops the eb…
Browse files Browse the repository at this point in the history
…pf hooks after keploy-test completion (#103)

* 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 <[email protected]>

* style: removes debug log statements

Signed-off-by: re-Tick <[email protected]>

* style: logs the maxTimeout when test exection exceeds

Signed-off-by: re-Tick <[email protected]>

---------

Signed-off-by: re-Tick <[email protected]>
  • Loading branch information
re-Tick authored Feb 26, 2024
1 parent 38a0e68 commit f5f2b3f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
55 changes: 44 additions & 11 deletions keployCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export enum TestRunStatus {
FAILED = 'FAILED'
}

interface TestOptions {
maxTimeout: number;

}

let hasTestRunCompleted = false;

export const setTestRunCompletionStatus = (status: boolean) => {
Expand All @@ -21,12 +26,16 @@ 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) {
options.maxTimeout = 30000;
}

let testResult = true;
const MAX_TIMEOUT = 10000;
let startTime = Date.now();
try {
const testSets = await FetchTestSets();
Expand All @@ -41,17 +50,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. maxTimeout: ", options.maxTimeout);
break;
}
console.log("testRun still in progress");
// break;
}

if (testRunStatus === TestRunStatus.FAILED || testRunStatus === TestRunStatus.RUNNING) {
Expand All @@ -66,6 +76,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
Expand Down Expand Up @@ -247,6 +259,27 @@ export const FetchTestSets = async (): Promise<string[] | null> => {
return null;
};

const stopTest = async (): Promise<boolean> => {
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<TestRunStatus | null> => {
try {
const client = await setHttpClient();
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5f2b3f

Please sign in to comment.