[Security Solution] fix flaky tests (#152867)

This commit is contained in:
Joey F. Poon 2023-03-08 09:30:17 -06:00 committed by GitHub
parent 68e1658a37
commit 08c2b56e6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 9 deletions

View file

@ -85,4 +85,13 @@ describe('retryTransientErrors', () => {
await expect(retryTransientEsErrors(esCallMock)).rejects.toThrow(error);
expect(esCallMock).toHaveBeenCalledTimes(1);
});
it('retries with additionalResponseStatuses', async () => {
const error = new EsErrors.ResponseError({ statusCode: 123, meta: {} as any, warnings: [] });
const esCallMock = jest.fn().mockRejectedValueOnce(error).mockResolvedValue('success');
expect(await retryTransientEsErrors(esCallMock, { additionalResponseStatuses: [123] })).toEqual(
'success'
);
expect(esCallMock).toHaveBeenCalledTimes(2);
});
});

View file

@ -17,11 +17,12 @@ const retryResponseStatuses = [
410, // Gone
];
const isRetryableError = (e: any) =>
const isRetryableError = (e: any, additionalResponseStatuses: number[] = []) =>
e instanceof EsErrors.NoLivingConnectionsError ||
e instanceof EsErrors.ConnectionError ||
e instanceof EsErrors.TimeoutError ||
(e instanceof EsErrors.ResponseError && retryResponseStatuses.includes(e?.statusCode!));
(e instanceof EsErrors.ResponseError &&
[...retryResponseStatuses, ...additionalResponseStatuses].includes(e?.statusCode!));
/**
* Retries any transient network or configuration issues encountered from Elasticsearch with an exponential backoff.
@ -29,12 +30,16 @@ const isRetryableError = (e: any) =>
*/
export const retryTransientEsErrors = async <T>(
esCall: () => Promise<T>,
{ logger, attempt = 0 }: { logger?: Logger; attempt?: number } = {}
{
logger,
attempt = 0,
additionalResponseStatuses = [],
}: { logger?: Logger; attempt?: number; additionalResponseStatuses?: number[] } = {}
): Promise<T> => {
try {
return await esCall();
} catch (e) {
if (attempt < MAX_ATTEMPTS && isRetryableError(e)) {
if (attempt < MAX_ATTEMPTS && isRetryableError(e, additionalResponseStatuses)) {
const retryCount = attempt + 1;
const retryDelaySec = Math.min(Math.pow(2, retryCount), 64); // 2s, 4s, 8s, 16s, 32s, 64s, 64s, 64s ...

View file

@ -703,9 +703,13 @@ async function handleTransformInstall({
// start transform by default if not set in yml file
// else, respect the setting
if (startTransform === undefined || startTransform === true) {
await esClient.transform.startTransform(
{ transform_id: transform.installationName },
{ ignore: [409] }
await retryTransientEsErrors(
() =>
esClient.transform.startTransform(
{ transform_id: transform.installationName },
{ ignore: [409] }
),
{ logger, additionalResponseStatuses: [400] }
);
logger.debug(`Started transform: ${transform.installationName}`);
}

View file

@ -37,8 +37,7 @@ export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const endpointTestResources = getService('endpointTestResources');
// Failing: See https://github.com/elastic/kibana/issues/151854
describe.skip('test metadata apis', () => {
describe('test metadata apis', () => {
before(async () => {
await endpointTestResources.setMetadataTransformFrequency('1s');
});