Skip to content

Commit

Permalink
[ML] Improve functional tests for Anomaly detection alert rule (#97998)
Browse files Browse the repository at this point in the history
* [ML] ensureAdvancedSectionOpen for assertion

* [ML] delete alert rules after tests execution

* [ML] add isAdvancedSectionOpened
  • Loading branch information
darnautov authored Apr 22, 2021
1 parent 2f679e6 commit 49cdc90
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
30 changes: 26 additions & 4 deletions x-pack/test/functional/services/ml/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
import { MlCommonUI } from './common_ui';
import { ML_ALERT_TYPES } from '../../../../plugins/ml/common/constants/alerts';
import { Alert } from '../../../../plugins/alerting/common';
import { MlAnomalyDetectionAlertParams } from '../../../../plugins/ml/common/types/alerts';

export function MachineLearningAlertingProvider(
{ getService }: FtrProviderContext,
Expand All @@ -17,6 +20,7 @@ export function MachineLearningAlertingProvider(
const comboBox = getService('comboBox');
const testSubjects = getService('testSubjects');
const find = getService('find');
const supertest = getService('supertest');

return {
async selectAnomalyDetectionAlertType() {
Expand Down Expand Up @@ -103,6 +107,7 @@ export function MachineLearningAlertingProvider(
},

async assertLookbackInterval(expectedValue: string) {
await this.ensureAdvancedSectionOpen();
const actualValue = await testSubjects.getAttribute(
'mlAnomalyAlertLookbackInterval',
'value'
Expand All @@ -114,6 +119,7 @@ export function MachineLearningAlertingProvider(
},

async assertTopNBuckets(expectedNumberOfBuckets: number) {
await this.ensureAdvancedSectionOpen();
const actualValue = await testSubjects.getAttribute('mlAnomalyAlertTopNBuckets', 'value');
expect(actualValue).to.eql(
expectedNumberOfBuckets,
Expand All @@ -133,15 +139,31 @@ export function MachineLearningAlertingProvider(
await this.assertTopNBuckets(numberOfBuckets);
},

async isAdvancedSectionOpened() {
return await find.existsByDisplayedByCssSelector('#mlAnomalyAlertAdvancedSettings');
},

async ensureAdvancedSectionOpen() {
await retry.tryForTime(5000, async () => {
const isVisible = await find.existsByDisplayedByCssSelector(
'#mlAnomalyAlertAdvancedSettings'
);
if (!isVisible) {
if (!(await this.isAdvancedSectionOpened())) {
await testSubjects.click('mlAnomalyAlertAdvancedSettingsTrigger');
expect(await this.isAdvancedSectionOpened()).to.eql(true);
}
});
},

async cleanAnomalyDetectionRules() {
const { body: anomalyDetectionRules } = await supertest
.get(`/api/alerting/rules/_find`)
.query({ filter: `alert.attributes.alertTypeId:${ML_ALERT_TYPES.ANOMALY_DETECTION}` })
.set('kbn-xsrf', 'foo')
.expect(200);

for (const rule of anomalyDetectionRules.data as Array<
Alert<MlAnomalyDetectionAlertParams>
>) {
await supertest.delete(`/api/alerting/rule/${rule.id}`).set('kbn-xsrf', 'foo').expect(204);
}
},
};
}
1 change: 1 addition & 0 deletions x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {

after(async () => {
await ml.api.cleanMlIndices();
await ml.alerting.cleanAnomalyDetectionRules();
});

describe('overview page alert flyout controls', () => {
Expand Down

0 comments on commit 49cdc90

Please sign in to comment.