From 49cdc9066df35d7fae0e5fc55cf3c4be81f9f40d Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Thu, 22 Apr 2021 17:16:54 +0200 Subject: [PATCH] [ML] Improve functional tests for Anomaly detection alert rule (#97998) * [ML] ensureAdvancedSectionOpen for assertion * [ML] delete alert rules after tests execution * [ML] add isAdvancedSectionOpened --- .../test/functional/services/ml/alerting.ts | 30 ++++++++++++++++--- .../apps/ml/alert_flyout.ts | 1 + 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/x-pack/test/functional/services/ml/alerting.ts b/x-pack/test/functional/services/ml/alerting.ts index 8d27a75b7b48..327a0e574f0f 100644 --- a/x-pack/test/functional/services/ml/alerting.ts +++ b/x-pack/test/functional/services/ml/alerting.ts @@ -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, @@ -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() { @@ -103,6 +107,7 @@ export function MachineLearningAlertingProvider( }, async assertLookbackInterval(expectedValue: string) { + await this.ensureAdvancedSectionOpen(); const actualValue = await testSubjects.getAttribute( 'mlAnomalyAlertLookbackInterval', 'value' @@ -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, @@ -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 + >) { + await supertest.delete(`/api/alerting/rule/${rule.id}`).set('kbn-xsrf', 'foo').expect(204); + } + }, }; } diff --git a/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts b/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts index cc0dcff52866..ee30f3a9eab0 100644 --- a/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts +++ b/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts @@ -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', () => {