From afedf2dfc214893faed35d08eb1682ac3203ec3c Mon Sep 17 00:00:00 2001 From: Ying Mao Date: Fri, 1 Apr 2022 08:40:05 -0400 Subject: [PATCH] Adding functional tests --- .../plugins/alerts/server/alert_types.ts | 43 +++++++++++-------- .../tests/alerting/get_execution_log.ts | 26 +++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts index dd37f0cc8b67e..7a4506dc53a31 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts @@ -675,23 +675,32 @@ export function defineAlertTypes( throw new Error('this alert is intended to fail'); }, }; - const longRunningAlertType: RuleType<{}, {}, {}, {}, {}, 'default'> = { - id: 'test.longRunning', - name: 'Test: Long Running', - actionGroups: [ - { - id: 'default', - name: 'Default', + function getLongRunningRuleType() { + const paramsSchema = schema.object({ + delay: schema.maybe(schema.number({ defaultValue: 5000 })), + }); + type ParamsType = TypeOf; + + const result: RuleType = { + id: 'test.longRunning', + name: 'Test: Long Running', + actionGroups: [ + { + id: 'default', + name: 'Default', + }, + ], + producer: 'alertsFixture', + defaultActionGroupId: 'default', + minimumLicenseRequired: 'basic', + isExportable: true, + async executor(ruleExecutorOptions) { + const { params } = ruleExecutorOptions; + await new Promise((resolve) => setTimeout(resolve, params.delay ?? 5000)); }, - ], - producer: 'alertsFixture', - defaultActionGroupId: 'default', - minimumLicenseRequired: 'basic', - isExportable: true, - async executor() { - await new Promise((resolve) => setTimeout(resolve, 5000)); - }, - }; + }; + return result; + } const exampleAlwaysFiringAlertType: RuleType<{}, {}, {}, {}, {}, 'small' | 'medium' | 'large'> = { id: 'example.always-firing', name: 'Always firing', @@ -769,7 +778,7 @@ export function defineAlertTypes( alerting.registerType(onlyStateVariablesAlertType); alerting.registerType(getPatternFiringAlertType()); alerting.registerType(throwAlertType); - alerting.registerType(longRunningAlertType); + alerting.registerType(getLongRunningRuleType()); alerting.registerType(goldNoopAlertType); alerting.registerType(exampleAlwaysFiringAlertType); alerting.registerType(multipleSearchesRuleType); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts index 17e2a4c395989..7b4463996edf3 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts @@ -126,6 +126,32 @@ export default function createGetExecutionLogTests({ getService }: FtrProviderCo expect(response.body.errors).to.eql([]); }); + it('gets execution log for rule that is currently running', async () => { + const { body: createdRule } = await supertest + .post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`) + .set('kbn-xsrf', 'foo') + .send(getTestRuleData({ rule_type_id: 'test.longRunning', params: { delay: 120000 } })) + .expect(200); + objectRemover.add(Spaces.space1.id, createdRule.id, 'rule', 'alerting'); + + // wait for execute-start event that signals rule has started running + await waitForEvents(createdRule.id, 'alerting', new Map([['execute-start', { gte: 1 }]])); + + const response = await supertest.get( + `${getUrlPrefix(Spaces.space1.id)}/internal/alerting/rule/${ + createdRule.id + }/_execution_log?date_start=${dateStart}` + ); + + expect(response.status).to.eql(200); + + // since these events should have been excluded from the agg, should return empty + expect(response.body.total).to.eql(0); + expect(response.body.data).to.eql([]); + expect(response.body.totalErrors).to.eql(0); + expect(response.body.errors).to.eql([]); + }); + it('gets execution log for rule that performs ES searches', async () => { const { body: createdRule } = await supertest .post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)