diff --git a/x-pack/test/api_integration/apis/slos/get_slo.ts b/x-pack/test/api_integration/apis/slos/get_slo.ts index 4fa2eec47acd6..e61bab90e4cac 100644 --- a/x-pack/test/api_integration/apis/slos/get_slo.ts +++ b/x-pack/test/api_integration/apis/slos/get_slo.ts @@ -14,8 +14,7 @@ import { SloEsClient } from './helper/es'; import { sloData } from './fixtures/create_slo'; export default function ({ getService }: FtrProviderContext) { - // FLAKY: https://github.com/elastic/kibana/issues/177806 - describe.skip('Get SLOs', function () { + describe('Get SLOs', function () { this.tags('skipCloud'); const supertestAPI = getService('supertest'); diff --git a/x-pack/test/api_integration/apis/slos/helper/wait_for_index_state.ts b/x-pack/test/api_integration/apis/slos/helper/wait_for_index_state.ts new file mode 100644 index 0000000000000..eaf091ad2fd52 --- /dev/null +++ b/x-pack/test/api_integration/apis/slos/helper/wait_for_index_state.ts @@ -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({ + esClient, + indexName, + docCountTarget = 1, +}: { + esClient: Client; + indexName: string; + docCountTarget?: number; +}): Promise>> { + return pRetry( + async () => { + const response = await esClient.search({ 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({ + esClient, + indexName, +}: { + esClient: Client; + indexName: string; +}): Promise>> { + return pRetry( + async () => { + const response = await esClient.search({ 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 } + ); +} diff --git a/x-pack/test/api_integration/services/slo.ts b/x-pack/test/api_integration/services/slo.ts index 7d7d1daab99f7..baa30067f4edf 100644 --- a/x-pack/test/api_integration/services/slo.ts +++ b/x-pack/test/api_integration/services/slo.ts @@ -6,10 +6,13 @@ */ import { CreateSLOInput, FindSLODefinitionsResponse } from '@kbn/slo-schema'; +import { SLO_SUMMARY_DESTINATION_INDEX_NAME } from '@kbn/observability-plugin/common/slo/constants'; +import { waitForIndexToBeEmpty } from '../apis/slos/helper/wait_for_index_state'; import { FtrProviderContext } from '../ftr_provider_context'; export function SloApiProvider({ getService }: FtrProviderContext) { const supertest = getService('supertest'); + const esClient = getService('es'); return { async create(params: CreateSLOInput) { @@ -44,6 +47,7 @@ export function SloApiProvider({ getService }: FtrProviderContext) { .send() .expect(204); } + await waitForIndexToBeEmpty({ esClient, indexName: SLO_SUMMARY_DESTINATION_INDEX_NAME }); }, }; }