Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test_serverless/api_integration] convert alerting_wait_for_helpers into FTR service #162623

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions x-pack/test_serverless/api_integration/services/alerting_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 {
AggregationsAggregate,
SearchResponse,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import { FtrProviderContext } from '../ftr_provider_context';

export function AlertingApiProvider({ getService }: FtrProviderContext) {
const retry = getService('retry');
const supertest = getService('supertest');
const es = getService('es');
const requestTimeout = 30 * 1000;
const retryTimeout = 120 * 1000;

return {
async waitForRuleStatus({
ruleId,
expectedStatus,
}: {
ruleId: string;
expectedStatus: string;
}) {
if (!ruleId) {
throw new Error(`'ruleId' is undefined`);
}
return await retry.tryForTime(retryTimeout, async () => {
const response = await supertest
.get(`/api/alerting/rule/${ruleId}`)
.timeout(requestTimeout);
const { execution_status: executionStatus } = response.body || {};
const { status } = executionStatus || {};
if (status !== expectedStatus) {
throw new Error(`waitForStatus(${expectedStatus}): got ${status}`);
}
return executionStatus?.status;
});
},

async waitForDocumentInIndex<T>({
indexName,
}: {
indexName: string;
}): Promise<SearchResponse<T, Record<string, AggregationsAggregate>>> {
return await retry.tryForTime(retryTimeout, async () => {
const response = await es.search<T>({ index: indexName });
if (response.hits.hits.length === 0) {
throw new Error('No hits found');
}
return response;
});
},

async waitForAlertInIndex<T>({
indexName,
ruleId,
}: {
indexName: string;
ruleId: string;
}): Promise<SearchResponse<T, Record<string, AggregationsAggregate>>> {
if (!ruleId) {
throw new Error(`'ruleId' is undefined`);
}
return await retry.tryForTime(retryTimeout, async () => {
const response = await es.search<T>({
index: indexName,
body: {
query: {
term: {
'kibana.alert.rule.uuid': ruleId,
},
},
},
});
if (response.hits.hits.length === 0) {
throw new Error('No hits found');
}
return response;
});
},
};
}
2 changes: 2 additions & 0 deletions x-pack/test_serverless/api_integration/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { services as xpackApiIntegrationServices } from '../../../test/api_integ
import { services as svlSharedServices } from '../../shared/services';

import { SvlCommonApiServiceProvider } from './svl_common_api';
import { AlertingApiProvider } from './alerting_api';

export const services = {
...xpackApiIntegrationServices,
...svlSharedServices,

svlCommonApi: SvlCommonApiServiceProvider,
alertingApi: AlertingApiProvider,
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { OBSERVABILITY_THRESHOLD_RULE_TYPE_ID } from '@kbn/observability-plugin/
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createIndexConnector, createRule } from '../helpers/alerting_api_helper';
import { createDataView, deleteDataView } from '../helpers/data_view';
import { waitForAlertInIndex, waitForRuleStatus } from '../helpers/alerting_wait_for_helpers';

export default function ({ getService }: FtrProviderContext) {
const esClient = getService('es');
const supertest = getService('supertest');
const esDeleteAllIndices = getService('esDeleteAllIndices');
const alertingApi = getService('alertingApi');
const logger = getService('log');

describe('Threshold rule - AVG - PCT - FIRED', () => {
Expand Down Expand Up @@ -119,17 +119,15 @@ export default function ({ getService }: FtrProviderContext) {
});

it('should be active', async () => {
const executionStatus = await waitForRuleStatus({
id: ruleId,
const executionStatus = await alertingApi.waitForRuleStatus({
ruleId,
expectedStatus: 'active',
supertest,
});
expect(executionStatus.status).to.be('active');
expect(executionStatus).to.be('active');
});

it('should set correct information in the alert document', async () => {
const resp = await waitForAlertInIndex({
esClient,
const resp = await alertingApi.waitForAlertInIndex({
indexName: THRESHOLD_RULE_ALERT_INDEX,
ruleId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { OBSERVABILITY_THRESHOLD_RULE_TYPE_ID } from '@kbn/observability-plugin/
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createIndexConnector, createRule } from '../helpers/alerting_api_helper';
import { createDataView, deleteDataView } from '../helpers/data_view';
import { waitForAlertInIndex, waitForRuleStatus } from '../helpers/alerting_wait_for_helpers';

export default function ({ getService }: FtrProviderContext) {
const esClient = getService('es');
const supertest = getService('supertest');
const alertingApi = getService('alertingApi');

describe('Threshold rule - AVG - PCT - NoData', () => {
const THRESHOLD_RULE_ALERT_INDEX = '.alerts-observability.threshold.alerts-default';
Expand Down Expand Up @@ -112,17 +112,15 @@ export default function ({ getService }: FtrProviderContext) {
});

it('should be active', async () => {
const executionStatus = await waitForRuleStatus({
id: ruleId,
const executionStatus = await alertingApi.waitForRuleStatus({
ruleId,
expectedStatus: 'active',
supertest,
});
expect(executionStatus.status).to.be('active');
expect(executionStatus).to.be('active');
});

it('should set correct information in the alert document', async () => {
const resp = await waitForAlertInIndex({
esClient,
const resp = await alertingApi.waitForAlertInIndex({
indexName: THRESHOLD_RULE_ALERT_INDEX,
ruleId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import { OBSERVABILITY_THRESHOLD_RULE_TYPE_ID } from '@kbn/observability-plugin/
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createIndexConnector, createRule } from '../helpers/alerting_api_helper';
import { createDataView, deleteDataView } from '../helpers/data_view';
import { waitForAlertInIndex, waitForRuleStatus } from '../helpers/alerting_wait_for_helpers';

export default function ({ getService }: FtrProviderContext) {
const esClient = getService('es');
const supertest = getService('supertest');
const esDeleteAllIndices = getService('esDeleteAllIndices');
const logger = getService('log');
const alertingApi = getService('alertingApi');

describe('Threshold rule - CUSTOM_EQ - AVG - BYTES - FIRED', () => {
const THRESHOLD_RULE_ALERT_INDEX = '.alerts-observability.threshold.alerts-default';
Expand Down Expand Up @@ -127,17 +127,15 @@ export default function ({ getService }: FtrProviderContext) {
});

it('should be active', async () => {
const executionStatus = await waitForRuleStatus({
id: ruleId,
const executionStatus = await alertingApi.waitForRuleStatus({
ruleId,
expectedStatus: 'active',
supertest,
});
expect(executionStatus.status).to.be('active');
expect(executionStatus).to.be('active');
});

it('should set correct information in the alert document', async () => {
const resp = await waitForAlertInIndex({
esClient,
const resp = await alertingApi.waitForAlertInIndex({
indexName: THRESHOLD_RULE_ALERT_INDEX,
ruleId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { OBSERVABILITY_THRESHOLD_RULE_TYPE_ID } from '@kbn/observability-plugin/
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createIndexConnector, createRule } from '../helpers/alerting_api_helper';
import { createDataView, deleteDataView } from '../helpers/data_view';
import { waitForAlertInIndex, waitForRuleStatus } from '../helpers/alerting_wait_for_helpers';

export default function ({ getService }: FtrProviderContext) {
const esClient = getService('es');
const supertest = getService('supertest');
const esDeleteAllIndices = getService('esDeleteAllIndices');
const logger = getService('log');
const alertingApi = getService('alertingApi');

describe('Threshold rule - DOCUMENTS_COUNT - FIRED', () => {
const THRESHOLD_RULE_ALERT_INDEX = '.alerts-observability.threshold.alerts-default';
Expand Down Expand Up @@ -117,17 +117,15 @@ export default function ({ getService }: FtrProviderContext) {
});

it('should be active', async () => {
const executionStatus = await waitForRuleStatus({
id: ruleId,
const executionStatus = await alertingApi.waitForRuleStatus({
ruleId,
expectedStatus: 'active',
supertest,
});
expect(executionStatus.status).to.be('active');
expect(executionStatus).to.be('active');
});

it('should set correct information in the alert document', async () => {
const resp = await waitForAlertInIndex({
esClient,
const resp = await alertingApi.waitForAlertInIndex({
indexName: THRESHOLD_RULE_ALERT_INDEX,
ruleId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ import { OBSERVABILITY_THRESHOLD_RULE_TYPE_ID } from '@kbn/observability-plugin/
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createIndexConnector, createRule } from '../helpers/alerting_api_helper';
import { createDataView, deleteDataView } from '../helpers/data_view';
import {
waitForAlertInIndex,
waitForDocumentInIndex,
waitForRuleStatus,
} from '../helpers/alerting_wait_for_helpers';

export default function ({ getService }: FtrProviderContext) {
const esClient = getService('es');
const supertest = getService('supertest');
const esDeleteAllIndices = getService('esDeleteAllIndices');
const logger = getService('log');
const alertingApi = getService('alertingApi');
let alertId: string;
let startedAt: string;

Expand Down Expand Up @@ -137,17 +133,15 @@ export default function ({ getService }: FtrProviderContext) {
});

it('should be active', async () => {
const executionStatus = await waitForRuleStatus({
id: ruleId,
const executionStatus = await alertingApi.waitForRuleStatus({
ruleId,
expectedStatus: 'active',
supertest,
});
expect(executionStatus.status).to.be('active');
expect(executionStatus).to.be('active');
});

it('should set correct information in the alert document', async () => {
const resp = await waitForAlertInIndex({
esClient,
const resp = await alertingApi.waitForAlertInIndex({
indexName: THRESHOLD_RULE_ALERT_INDEX,
ruleId,
});
Expand Down Expand Up @@ -208,14 +202,13 @@ export default function ({ getService }: FtrProviderContext) {

it('should set correct action variables', async () => {
const rangeFrom = moment(startedAt).subtract('5', 'minute').toISOString();
const resp = await waitForDocumentInIndex<{
const resp = await alertingApi.waitForDocumentInIndex<{
ruleType: string;
alertDetailsUrl: string;
reason: string;
value: string;
host: string;
}>({
esClient,
indexName: ALERT_ACTION_INDEX,
});

Expand Down