-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SLO] Fix get_slo.ts api integration test (#178059)
## Summary This PR introduces a `waitForIndexToBeEmpty` function to allow for the SLO to be completely deleted before letting the test move on. Fixes #176763
- Loading branch information
1 parent
d69045b
commit 17c99b6
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
x-pack/test/api_integration/apis/slos/helper/wait_for_index_state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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 type { Client } from '@elastic/elasticsearch'; | ||
import type { | ||
AggregationsAggregate, | ||
SearchResponse, | ||
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import pRetry from 'p-retry'; | ||
|
||
export async function waitForDocumentInIndex<T>({ | ||
esClient, | ||
indexName, | ||
docCountTarget = 1, | ||
}: { | ||
esClient: Client; | ||
indexName: string; | ||
docCountTarget?: number; | ||
}): Promise<SearchResponse<T, Record<string, AggregationsAggregate>>> { | ||
return pRetry( | ||
async () => { | ||
const response = await esClient.search<T>({ index: indexName, rest_total_hits_as_int: true }); | ||
if (!response.hits.total || response.hits.total < docCountTarget) { | ||
throw new Error('No hits found'); | ||
} | ||
return response; | ||
}, | ||
{ retries: 10 } | ||
); | ||
} | ||
|
||
export async function waitForIndexToBeEmpty<T>({ | ||
esClient, | ||
indexName, | ||
}: { | ||
esClient: Client; | ||
indexName: string; | ||
}): Promise<SearchResponse<T, Record<string, AggregationsAggregate>>> { | ||
return pRetry( | ||
async () => { | ||
const response = await esClient.search<T>({ index: indexName, rest_total_hits_as_int: true }); | ||
if (response.hits.total != null && response.hits.total > 0) { | ||
throw new Error(`Found ${response.hits.total} docs.`); | ||
} | ||
return response; | ||
}, | ||
{ retries: 10 } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters