Skip to content

Commit

Permalink
[Alerting] Adding functional tests for alerting and actions telemetry (
Browse files Browse the repository at this point in the history
…#126528)

* Renaming getTestAlertData to getTestRuleData

* Adding run now route to alerting_api_integration tests

* Renaming getTestAlertData to getTestRuleData

* Adding alerting telemetry functional test

* Adding actions telemetry functional test

* Changing order of tests

* Clearing telemetry task state before running tests

* Debugging

* Cleanup

* Moving to own file

* Merge

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
ymao1 and kibanamachine committed Mar 7, 2022
1 parent cb70888 commit c0a653d
Show file tree
Hide file tree
Showing 57 changed files with 865 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
* 2.0.
*/

import { getTestAlertData } from '../../../common/lib';
import { getTestRuleData } from '../../../common/lib';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default function basicAlertTest({ getService }: FtrProviderContext) {
export default function basicRuleTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

describe('basic alert', () => {
it('should return 200 when creating a basic license alert', async () => {
describe('basic rule', () => {
it('should return 200 when creating a basic license rule', async () => {
await supertest
.post(`/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData())
.send(getTestRuleData())
.expect(200);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
* 2.0.
*/

import { getTestAlertData } from '../../../common/lib';
import { getTestRuleData } from '../../../common/lib';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default function emailTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

describe('create gold noop alert', () => {
it('should return 403 when creating an gold alert', async () => {
describe('create gold noop rule', () => {
it('should return 403 when creating an gold rule', async () => {
await supertest
.post(`/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData({ rule_type_id: 'test.gold.noop' }))
.send(getTestRuleData({ rule_type_id: 'test.gold.noop' }))
.expect(403, {
statusCode: 403,
error: 'Forbidden',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
* 2.0.
*/

import { Plugin, CoreSetup, Logger, PluginInitializerContext } from 'kibana/server';
import { Plugin, CoreSetup, CoreStart, Logger, PluginInitializerContext } from 'kibana/server';
import { Subject } from 'rxjs';
import { first } from 'rxjs/operators';
import { PluginSetupContract as ActionsPluginSetup } from '../../../../../../../plugins/actions/server/plugin';
import { PluginSetupContract as AlertingPluginSetup } from '../../../../../../../plugins/alerting/server/plugin';
import {
TaskManagerSetupContract,
TaskManagerStartContract,
} from '../../../../../../../plugins/task_manager/server/plugin';
import { EncryptedSavedObjectsPluginStart } from '../../../../../../../plugins/encrypted_saved_objects/server';
import { PluginSetupContract as FeaturesPluginSetup } from '../../../../../../../plugins/features/server';
import { defineAlertTypes } from './alert_types';
Expand All @@ -21,18 +27,25 @@ export interface FixtureSetupDeps {
features: FeaturesPluginSetup;
actions: ActionsPluginSetup;
alerting: AlertingPluginSetup;
taskManager: TaskManagerSetupContract;
}

export interface FixtureStartDeps {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
security?: SecurityPluginStart;
spaces?: SpacesPluginStart;
actions: ActionsPluginStart;
taskManager: TaskManagerStartContract;
}

export class FixturePlugin implements Plugin<void, void, FixtureSetupDeps, FixtureStartDeps> {
private readonly logger: Logger;

taskManagerStart$: Subject<TaskManagerStartContract> = new Subject<TaskManagerStartContract>();
taskManagerStart: Promise<TaskManagerStartContract> = this.taskManagerStart$
.pipe(first())
.toPromise();

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get('fixtures', 'plugins', 'alerts');
}
Expand Down Expand Up @@ -127,9 +140,12 @@ export class FixturePlugin implements Plugin<void, void, FixtureSetupDeps, Fixtu

defineActionTypes(core, { actions });
defineAlertTypes(core, { alerting });
defineRoutes(core, { logger: this.logger });
defineRoutes(core, this.taskManagerStart, { logger: this.logger });
}

public start() {}
public start(core: CoreStart, { taskManager }: FixtureStartDeps) {
this.taskManagerStart$.next(taskManager);
this.taskManagerStart$.complete();
}
public stop() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import { RawRule } from '../../../../../../../plugins/alerting/server/types';
import {
ConcreteTaskInstance,
TaskInstance,
TaskManagerStartContract,
} from '../../../../../../../plugins/task_manager/server';
import { FixtureStartDeps } from './plugin';
import { retryIfConflicts } from './lib/retry_if_conflicts';

export function defineRoutes(core: CoreSetup<FixtureStartDeps>, { logger }: { logger: Logger }) {
export function defineRoutes(
core: CoreSetup<FixtureStartDeps>,
taskManagerStart: Promise<TaskManagerStartContract>,
{ logger }: { logger: Logger }
) {
const router = core.http.createRouter();
router.put(
{
Expand Down Expand Up @@ -324,4 +329,39 @@ export function defineRoutes(core: CoreSetup<FixtureStartDeps>, { logger }: { lo
}
}
);

router.post(
{
path: `/api/alerting_actions_telemetry/run_now`,
validate: {
body: schema.object({
taskId: schema.string({
validate: (telemetryTaskId: string) => {
if (
['Alerting-alerting_telemetry', 'Actions-actions_telemetry'].includes(
telemetryTaskId
)
) {
return;
}
return 'invalid telemetry task id';
},
}),
}),
},
},
async function (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> {
const { taskId } = req.body;
try {
const taskManager = await taskManagerStart;
return res.ok({ body: await taskManager.runNow(taskId) });
} catch (err) {
return res.ok({ body: { id: taskId, error: `${err}` } });
}
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Space, User } from '../types';
import { ObjectRemover } from './object_remover';
import { getUrlPrefix } from './space_test_utils';
import { ES_TEST_INDEX_NAME } from './es_test_index_tool';
import { getTestAlertData } from './get_test_alert_data';
import { getTestRuleData } from './get_test_rule_data';

export interface AlertUtilsOpts {
user?: User;
Expand Down Expand Up @@ -293,7 +293,7 @@ export class AlertUtils {
request = request.auth(this.user.username, this.user.password);
}
const response = await request.send({
...getTestAlertData(),
...getTestRuleData(),
...overwrites,
});
if (response.statusCode === 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

export function getTestAlertData(overwrites = {}) {
export function getTestRuleData(overwrites = {}) {
return {
enabled: true,
name: 'abc',
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/alerting_api_integration/common/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export { ObjectRemover } from './object_remover';
export { getUrlPrefix } from './space_test_utils';
export { ES_TEST_INDEX_NAME, ESTestIndexTool } from './es_test_index_tool';
export { getTestAlertData } from './get_test_alert_data';
export { getTestRuleData } from './get_test_rule_data';
export {
AlertUtils,
getConsumerUnauthorizedErrorMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import expect from '@kbn/expect';
import { UserAtSpaceScenarios } from '../../scenarios';
import { getUrlPrefix, getTestAlertData, ObjectRemover } from '../../../common/lib';
import { getUrlPrefix, getTestRuleData, ObjectRemover } from '../../../common/lib';
import { FtrProviderContext } from '../../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
Expand Down Expand Up @@ -134,7 +134,7 @@ export default function getAllActionTests({ getService }: FtrProviderContext) {
.post(`${getUrlPrefix(space.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(
getTestAlertData({
getTestRuleData({
actions: [
{
group: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ESTestIndexTool,
ES_TEST_INDEX_NAME,
getUrlPrefix,
getTestAlertData,
getTestRuleData,
ObjectRemover,
AlertUtils,
getConsumerUnauthorizedErrorMessage,
Expand Down Expand Up @@ -494,7 +494,7 @@ instanceStateValue: true
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.always-firing',
params: {
index: ES_TEST_INDEX_NAME,
Expand Down Expand Up @@ -603,7 +603,7 @@ instanceStateValue: true
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.authorization',
params: {
callClusterAuthorizationIndex: authorizationIndex,
Expand Down Expand Up @@ -711,7 +711,7 @@ instanceStateValue: true
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.always-firing',
params: {
index: ES_TEST_INDEX_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import expect from '@kbn/expect';
import { UserAtSpaceScenarios } from '../../scenarios';
import {
checkAAD,
getTestAlertData,
getTestRuleData,
getConsumerUnauthorizedErrorMessage,
getUrlPrefix,
ObjectRemover,
Expand Down Expand Up @@ -57,7 +57,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
actions: [
{
id: createdAction.id,
Expand Down Expand Up @@ -155,7 +155,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.restricted-noop',
consumer: 'alertsRestrictedFixture',
})
Expand Down Expand Up @@ -194,7 +194,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.unrestricted-noop',
consumer: 'alertsFixture',
})
Expand Down Expand Up @@ -244,7 +244,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.noop',
consumer: 'alerts',
})
Expand Down Expand Up @@ -290,7 +290,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.noop',
consumer: 'some consumer patrick invented',
})
Expand Down Expand Up @@ -325,7 +325,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.post(`${getUrlPrefix(space.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(getTestAlertData({ enabled: false }));
.send(getTestRuleData({ enabled: false }));

switch (scenario.id) {
case 'no_kibana_privileges at space1':
Expand Down Expand Up @@ -361,7 +361,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
name: ' leading and trailing whitespace ',
})
);
Expand Down Expand Up @@ -400,7 +400,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.unregistered-alert-type',
})
);
Expand Down Expand Up @@ -458,7 +458,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(
getTestAlertData({
getTestRuleData({
rule_type_id: 'test.validation',
})
);
Expand Down Expand Up @@ -500,7 +500,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.post(`${getUrlPrefix(space.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(getTestAlertData(getTestAlertData({ schedule: { interval: '10x' } })));
.send(getTestRuleData({ schedule: { interval: '10x' } }));

switch (scenario.id) {
case 'no_kibana_privileges at space1':
Expand All @@ -527,7 +527,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
.post(`${getUrlPrefix(space.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(getTestAlertData(getTestAlertData({ schedule: { interval: '0s' } })));
.send(getTestRuleData({ schedule: { interval: '0s' } }));

switch (scenario.id) {
case 'no_kibana_privileges at space1':
Expand Down
Loading

0 comments on commit c0a653d

Please sign in to comment.