Skip to content

Commit

Permalink
[SLO] Fix get_slo.ts api integration test (#178059)
Browse files Browse the repository at this point in the history
## 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
simianhacker authored Mar 7, 2024
1 parent d69045b commit 17c99b6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
3 changes: 1 addition & 2 deletions x-pack/test/api_integration/apis/slos/get_slo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
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 }
);
}
4 changes: 4 additions & 0 deletions x-pack/test/api_integration/services/slo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -44,6 +47,7 @@ export function SloApiProvider({ getService }: FtrProviderContext) {
.send()
.expect(204);
}
await waitForIndexToBeEmpty({ esClient, indexName: SLO_SUMMARY_DESTINATION_INDEX_NAME });
},
};
}

0 comments on commit 17c99b6

Please sign in to comment.