Skip to content

Commit

Permalink
[Alerting] Saved object remover for all e2e triggers_actions_ui tests (
Browse files Browse the repository at this point in the history
…elastic#86837) (elastic#87158)

* wip

* Using object remover for alerts list

* wip - using supertest instead of axios

* wip - using supertest instead of axios

* Removing custom services in favor of supertest

* Fixing test

* Fixing test

* Fixing types check

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
ymao1 and kibanamachine authored Jan 4, 2021
1 parent 46bbbd1 commit 43a0cfd
Show file tree
Hide file tree
Showing 15 changed files with 320 additions and 572 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import uuid from 'uuid';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

function generateUniqueKey() {
return uuid.v4().replace(/-/g, '');
}
import { generateUniqueKey } from '../../lib/get_test_data';

export default ({ getPageObjects, getService }: FtrProviderContext) => {
const testSubjects = getService('testSubjects');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,50 @@
* you may not use this file except in compliance with the Elastic License.
*/

import uuid from 'uuid';
import { times } from 'lodash';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

function generateUniqueKey() {
return uuid.v4().replace(/-/g, '');
}
import { ObjectRemover } from '../../lib/object_remover';
import { generateUniqueKey, getTestAlertData, getTestActionData } from '../../lib/get_test_data';

export default ({ getPageObjects, getService }: FtrProviderContext) => {
const alerting = getService('alerting');
const testSubjects = getService('testSubjects');
const find = getService('find');
const pageObjects = getPageObjects(['common', 'triggersActionsUI', 'header']);
const supertest = getService('supertest');
const retry = getService('retry');
const objectRemover = new ObjectRemover(supertest);

async function deleteAlerts(alertIds: string[]) {
alertIds.forEach(async (alertId: string) => {
await supertest.delete(`/api/alerts/alert/${alertId}`).set('kbn-xsrf', 'foo').expect(204, '');
});
}

async function createAlert(overwrites: Record<string, any> = {}) {
async function createAlertManualCleanup(overwrites: Record<string, any> = {}) {
const { body: createdAlert } = await supertest
.post(`/api/alerts/alert`)
.set('kbn-xsrf', 'foo')
.send({
enabled: true,
name: generateUniqueKey(),
tags: ['foo', 'bar'],
alertTypeId: 'test.noop',
consumer: 'alerts',
schedule: { interval: '1m' },
throttle: '1m',
actions: [],
params: {},
...overwrites,
})
.send(getTestAlertData(overwrites))
.expect(200);
return createdAlert;
}

async function createFailingAlert(overwrites: Record<string, any> = {}) {
const { body: createdAlert } = await supertest
.post(`/api/alerts/alert`)
async function createFailingAlert() {
return await createAlert({
alertTypeId: 'test.failing',
schedule: { interval: '30s' },
});
}

async function createAlert(overwrites: Record<string, any> = {}) {
const createdAlert = await createAlertManualCleanup(overwrites);
objectRemover.add(createdAlert.id, 'alert', 'alerts');
return createdAlert;
}

async function createAction(overwrites: Record<string, any> = {}) {
const { body: createdAction } = await supertest
.post(`/api/actions/action`)
.set('kbn-xsrf', 'foo')
.send({
enabled: true,
name: generateUniqueKey(),
tags: ['foo', 'bar'],
alertTypeId: 'test.failing',
consumer: 'alerts',
schedule: { interval: '30s' },
throttle: '1m',
actions: [],
params: {},
...overwrites,
})
.send(getTestActionData(overwrites))
.expect(200);
return createdAlert;
objectRemover.add(createdAction.id, 'action', 'actions');
return createdAction;
}

async function refreshAlertsList() {
Expand All @@ -77,11 +60,15 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
await testSubjects.click('alertsTab');
});

afterEach(async () => {
await objectRemover.removeAll();
});

it('should display alerts in alphabetical order', async () => {
const uniqueKey = generateUniqueKey();
const a = await createAlert({ name: 'b', tags: [uniqueKey] });
const b = await createAlert({ name: 'c', tags: [uniqueKey] });
const c = await createAlert({ name: 'a', tags: [uniqueKey] });
await createAlert({ name: 'b', tags: [uniqueKey] });
await createAlert({ name: 'c', tags: [uniqueKey] });
await createAlert({ name: 'a', tags: [uniqueKey] });
await refreshAlertsList();
await pageObjects.triggersActionsUI.searchAlerts(uniqueKey);

Expand All @@ -90,8 +77,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
expect(searchResults[0].name).to.eql('a');
expect(searchResults[1].name).to.eql('b');
expect(searchResults[2].name).to.eql('c');

await deleteAlerts([a.id, b.id, c.id]);
});

it('should search for alert', async () => {
Expand All @@ -108,7 +93,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
interval: '1m',
},
]);
await deleteAlerts([createdAlert.id]);
});

it('should search for tags', async () => {
Expand All @@ -125,16 +109,14 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
interval: '1m',
},
]);
await deleteAlerts([createdAlert.id]);
});

it('should display an empty list when search did not return any alerts', async () => {
const createdAlert = await createAlert();
await createAlert();
await refreshAlertsList();
await pageObjects.triggersActionsUI.searchAlerts(`An Alert That For Sure Doesn't Exist!`);

expect(await pageObjects.triggersActionsUI.isAlertsListDisplayed()).to.eql(true);
await deleteAlerts([createdAlert.id]);
});

it('should disable single alert', async () => {
Expand All @@ -153,7 +135,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const disableSwitchAfterDisable = await testSubjects.find('disableSwitch');
const isChecked = await disableSwitchAfterDisable.getAttribute('aria-checked');
expect(isChecked).to.eql('true');
await deleteAlerts([createdAlert.id]);
});

it('should re-enable single alert', async () => {
Expand All @@ -178,7 +159,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const disableSwitchAfterReEnable = await testSubjects.find('disableSwitch');
const isChecked = await disableSwitchAfterReEnable.getAttribute('aria-checked');
expect(isChecked).to.eql('false');
await deleteAlerts([createdAlert.id]);
});

it('should mute single alert', async () => {
Expand All @@ -197,7 +177,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const muteSwitchAfterMute = await testSubjects.find('muteSwitch');
const isChecked = await muteSwitchAfterMute.getAttribute('aria-checked');
expect(isChecked).to.eql('true');
await deleteAlerts([createdAlert.id]);
});

it('should unmute single alert', async () => {
Expand All @@ -222,12 +201,11 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const muteSwitchAfterUnmute = await testSubjects.find('muteSwitch');
const isChecked = await muteSwitchAfterUnmute.getAttribute('aria-checked');
expect(isChecked).to.eql('false');
await deleteAlerts([createdAlert.id]);
});

it('should delete single alert', async () => {
const firstAlert = await createAlert();
const secondAlert = await createAlert();
await createAlert();
const secondAlert = await createAlertManualCleanup();
await refreshAlertsList();
await pageObjects.triggersActionsUI.searchAlerts(secondAlert.name);

Expand All @@ -247,7 +225,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
await pageObjects.triggersActionsUI.searchAlerts(secondAlert.name);
const searchResultsAfterDelete = await pageObjects.triggersActionsUI.getAlertsList();
expect(searchResultsAfterDelete.length).to.eql(0);
await deleteAlerts([firstAlert.id]);
});

it('should mute all selection', async () => {
Expand All @@ -271,7 +248,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const muteSwitch = await testSubjects.find('muteSwitch');
const isChecked = await muteSwitch.getAttribute('aria-checked');
expect(isChecked).to.eql('true');
await deleteAlerts([createdAlert.id]);
});

it('should unmute all selection', async () => {
Expand All @@ -297,7 +273,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const muteSwitch = await testSubjects.find('muteSwitch');
const isChecked = await muteSwitch.getAttribute('aria-checked');
expect(isChecked).to.eql('false');
await deleteAlerts([createdAlert.id]);
});

it('should disable all selection', async () => {
Expand All @@ -321,7 +296,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const disableSwitch = await testSubjects.find('disableSwitch');
const isChecked = await disableSwitch.getAttribute('aria-checked');
expect(isChecked).to.eql('true');
await deleteAlerts([createdAlert.id]);
});

it('should enable all selection', async () => {
Expand All @@ -347,14 +321,13 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const disableSwitch = await testSubjects.find('disableSwitch');
const isChecked = await disableSwitch.getAttribute('aria-checked');
expect(isChecked).to.eql('false');
await deleteAlerts([createdAlert.id]);
});

it.skip('should delete all selection', async () => {
const namePrefix = generateUniqueKey();
let count = 0;
const createdAlertsFirstPage = await Promise.all(
times(2, () => createAlert({ name: `${namePrefix}-0${count++}` }))
times(2, () => createAlertManualCleanup({ name: `${namePrefix}-0${count++}` }))
);
await refreshAlertsList();
await pageObjects.triggersActionsUI.searchAlerts(namePrefix);
Expand All @@ -381,30 +354,28 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
});

it('should filter alerts by the status', async () => {
const createdAlert = await createAlert();
const failinfAlert = await createFailingAlert();
await createAlert();
const failingAlert = await createFailingAlert();
// initialy alert get Pending status, so we need to retry refresh list logic to get the post execution statuses
await retry.try(async () => {
await refreshAlertsList();
const refreshResults = await pageObjects.triggersActionsUI.getAlertsListWithStatus();
expect(refreshResults.map((item) => item.status).sort()).to.eql(['Error', 'Ok']);
expect(refreshResults.map((item: any) => item.status).sort()).to.eql(['Error', 'Ok']);
});
await testSubjects.click('alertStatusFilterButton');
await testSubjects.click('alertStatuserrorFilerOption'); // select Error status filter
await retry.try(async () => {
const filterErrorOnlyResults = await pageObjects.triggersActionsUI.getAlertsListWithStatus();
expect(filterErrorOnlyResults).to.eql([
{
name: failinfAlert.name,
name: failingAlert.name,
tagsText: 'foo, bar',
alertType: 'Test: Failing',
interval: '30s',
status: 'Error',
},
]);
});

await deleteAlerts([createdAlert.id, failinfAlert.id]);
});

it('should display total alerts by status and error banner only when exists alerts with status error', async () => {
Expand All @@ -428,7 +399,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
);
expect(alertsErrorBannerWhenNoErrors).to.have.length(0);

const failingAlert = await createFailingAlert();
await createFailingAlert();
await retry.try(async () => {
await refreshAlertsList();
const alertsErrorBannerExistErrors = await find.allByCssSelector(
Expand All @@ -450,13 +421,11 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
expect(await testSubjects.getVisibleText('totalErrorAlertsCount')).to.be('Error: 1');
expect(await testSubjects.getVisibleText('totalPendingAlertsCount')).to.be('Pending: 0');
expect(await testSubjects.getVisibleText('totalUnknownAlertsCount')).to.be('Unknown: 0');

await deleteAlerts([createdAlert.id, failingAlert.id]);
});

it('should filter alerts by the alert type', async () => {
const noopAlert = await createAlert();
const failinfAlert = await createFailingAlert();
await createAlert();
const failingAlert = await createFailingAlert();
await refreshAlertsList();
await testSubjects.click('alertTypeFilterButton');
expect(await (await testSubjects.find('alertType0Group')).getVisibleText()).to.eql('Alerts');
Expand All @@ -466,27 +435,18 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
const filterFailingAlertOnlyResults = await pageObjects.triggersActionsUI.getAlertsList();
expect(filterFailingAlertOnlyResults).to.eql([
{
name: failinfAlert.name,
name: failingAlert.name,
tagsText: 'foo, bar',
alertType: 'Test: Failing',
interval: '30s',
},
]);
});

await deleteAlerts([noopAlert.id, failinfAlert.id]);
});

it('should filter alerts by the action type', async () => {
const noopAlert = await createAlert();
const action = await alerting.actions.createAction({
name: `slack-${Date.now()}`,
actionTypeId: '.slack',
config: {},
secrets: {
webhookUrl: 'https://test',
},
});
await createAlert();
const action = await createAction();
const noopAlertWithAction = await createAlert({
actions: [
{
Expand All @@ -512,8 +472,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
},
]);
});

await deleteAlerts([noopAlertWithAction.id, noopAlert.id]);
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
* you may not use this file except in compliance with the Elastic License.
*/

import uuid from 'uuid';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

function generateUniqueKey() {
return uuid.v4().replace(/-/g, '');
}
import { ObjectRemover } from '../../lib/object_remover';
import { generateUniqueKey, getTestActionData } from '../../lib/get_test_data';

export default ({ getPageObjects, getService }: FtrProviderContext) => {
const alerting = getService('alerting');
const testSubjects = getService('testSubjects');
const pageObjects = getPageObjects(['common', 'triggersActionsUI', 'header']);
const find = getService('find');
const retry = getService('retry');
const comboBox = getService('comboBox');
const supertest = getService('supertest');

describe('Connectors', function () {
before(async () => {
await alerting.actions.createAction({
name: `slack-${Date.now()}`,
actionTypeId: '.slack',
config: {},
secrets: {
webhookUrl: 'https://test',
},
});
const objectRemover = new ObjectRemover(supertest);

before(async () => {
const { body: createdAction } = await supertest
.post(`/api/actions/action`)
.set('kbn-xsrf', 'foo')
.send(getTestActionData())
.expect(200);
await pageObjects.common.navigateToApp('triggersActions');
await testSubjects.click('connectorsTab');
objectRemover.add(createdAction.id, 'action', 'actions');
});

after(async () => {
await objectRemover.removeAll();
});

it('should create a connector', async () => {
Expand Down
Loading

0 comments on commit 43a0cfd

Please sign in to comment.