kibana/x-pack/test/alerting_api_integration/common/retry.ts
Dzmitry Lemechko 8436f45fd1
FTR: enable ESLint mocha rules for api integration tests (#191267)
## Summary

Follow-up to #190690

Most of API integration tests does not match the path pattern set in the
original PR (thanks @pheyos for catching it) and where not updated.
This PR updates `.eslintrc.js` with explicit patterns to lint
api_integration tests. Hopefully it is final change, but I rely on code
owners to double check it.

Most of the changes are trivial adjustments:
- duplicated before/after hooks `mocha/no-sibling-hooks`
- duplicated test titles `mocha/no-identical-title`
- async function in describe() `mocha/no-async-describe`

---------

Co-authored-by: Ash <1849116+ashokaditya@users.noreply.github.com>
2024-08-30 18:50:35 +02:00

92 lines
2.8 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { RetryService } from '@kbn/ftr-common-functional-services';
import type { ToolingLog } from '@kbn/tooling-log';
/**
* Copied from x-pack/test/security_solution_api_integration/test_suites/detections_response/utils/retry.ts
*
* Retry wrapper for async supertests, with a maximum number of retries.
* You can pass in a function that executes a supertest test, and make assertions
* on the response. If the test fails, it will retry the test the number of retries
* that are passed in.
*
* Example usage:
* ```ts
const fleetResponse = await retry<InstallPackageResponse>({
testFn: async () => {
const testResponse = await supertest
.post(`/api/fleet/epm/packages/security_detection_engine`)
.set('kbn-xsrf', 'xxxx')
.set('elastic-api-version', '2023-10-31')
.type('application/json')
.send({ force: true })
.expect(200);
expect((testResponse.body as InstallPackageResponse).items).toBeDefined();
expect((testResponse.body as InstallPackageResponse).items.length).toBeGreaterThan(0);
return testResponse.body;
},
retryService,
retries: MAX_RETRIES,
timeout: ATTEMPT_TIMEOUT,
});
* ```
* @param test The function containing a test to run
* @param retryService The retry service to use
* @param retries The maximum number of retries
* @param timeout The timeout for each retry
* @param retryDelay The delay between each retry
* @returns The response from the test
*/
export const retry = async <T>({
testFn,
retryService,
utilityName,
retries = 3,
timeout = 30000,
retryDelay = 200,
logger,
}: {
testFn: () => Promise<T>;
utilityName: string;
retryService: RetryService;
retries?: number;
timeout?: number;
retryDelay?: number;
logger: ToolingLog;
}): Promise<T> => {
let retryAttempt = 0;
const response = await retryService.tryForTime(
timeout,
async () => {
if (retryAttempt > retries) {
// Log error message if we reached the maximum number of retries
// but don't throw an error, return it to break the retry loop.
const errorMessage = `Reached maximum number of retries for test ${utilityName}: ${
retryAttempt - 1
}/${retries}`;
logger.error(errorMessage);
return new Error(errorMessage);
}
retryAttempt = retryAttempt + 1;
return await testFn();
},
undefined,
retryDelay
);
// Now throw the error in order to fail the test.
if (response instanceof Error) {
throw response;
}
return response;
};