diff --git a/x-pack/test/plugin_api_integration/plugins/event_log/server/init_routes.ts b/x-pack/test/plugin_api_integration/plugins/event_log/server/init_routes.ts index fde08ffb10122..4c0463da20b11 100644 --- a/x-pack/test/plugin_api_integration/plugins/event_log/server/init_routes.ts +++ b/x-pack/test/plugin_api_integration/plugins/event_log/server/init_routes.ts @@ -74,7 +74,7 @@ export const registerProviderActionsRoute = ( eventLogService.registerProviderActions(provider, actions); logger.info(`registered`); } catch (e) { - response.badRequest({ body: e }); + return response.badRequest({ body: e }); } return response.ok({ body: {} }); } @@ -117,16 +117,22 @@ export const getProviderActionsRoute = ( ) => { router.get( { - path: `/api/log_event_fixture/getProviderActions`, - validate: {}, + path: `/api/log_event_fixture/{provider}/getProviderActions`, + validate: { + params: (value: any, { ok }: RouteValidationResultFactory) => ok(value), + }, }, async function( context: RequestHandlerContext, req: KibanaRequest, res: KibanaResponseFactory ): Promise> { + const { provider } = req.params as { provider: string }; + logger.info(`test if get all provider actions is registered`); - return res.ok({ body: { actions: eventLogService.getProviderActions() } }); + return res.ok({ + body: { actions: [...(eventLogService.getProviderActions().get(provider) ?? [])] }, + }); } ); }; @@ -220,3 +226,61 @@ export const isEventLogServiceLoggingEntriesRoute = ( } ); }; + +export const startTimingEventLoggerRoute = ( + router: IRouter, + eventLogService: IEventLogService, + logger: Logger +) => { + router.post( + { + path: '/api/log_event_fixture/startTimingEventLogger', + validate: { + body: value => ({ value }), + }, + options: { authRequired: false }, + }, + (context, request, response) => { + const event: IValidatedEvent = request.body; + try { + logger.info(`started timing for event: ${event}`); + + const eventLogger = eventLogService.getLogger(event); + eventLogger.startTiming(event); + logger.info(`registered`); + } catch (e) { + return response.badRequest({ body: e }); + } + return response.ok({ body: {} }); + } + ); +}; + +export const stopTimingEventLoggerRoute = ( + router: IRouter, + eventLogService: IEventLogService, + logger: Logger +) => { + router.post( + { + path: '/api/log_event_fixture/stopTimingEventLogger', + validate: { + body: value => ({ value }), + }, + options: { authRequired: false }, + }, + (context, request, response) => { + const event: IValidatedEvent = request.body; + try { + logger.info(`started timing for event: ${event}`); + + const eventLogger = eventLogService.getLogger(event); + eventLogger.stopTiming(event); + logger.info(`registered`); + } catch (e) { + return response.badRequest({ body: e }); + } + return response.ok({ body: {} }); + } + ); +}; diff --git a/x-pack/test/plugin_api_integration/plugins/event_log/server/plugin.ts b/x-pack/test/plugin_api_integration/plugins/event_log/server/plugin.ts index 9c6f2e2735be5..7ff21ceeab50f 100644 --- a/x-pack/test/plugin_api_integration/plugins/event_log/server/plugin.ts +++ b/x-pack/test/plugin_api_integration/plugins/event_log/server/plugin.ts @@ -15,6 +15,8 @@ import { isIndexingEntriesRoute, isEventLogServiceLoggingEntriesRoute, isEventLogServiceEnabledRoute, + startTimingEventLoggerRoute, + stopTimingEventLoggerRoute, } from './init_routes'; // this plugin's dependendencies @@ -60,6 +62,8 @@ export class EventLogFixturePlugin isIndexingEntriesRoute(router, eventLog, this.logger); isEventLogServiceLoggingEntriesRoute(router, eventLog, this.logger); isEventLogServiceEnabledRoute(router, eventLog, this.logger); + startTimingEventLoggerRoute(router, eventLog, this.logger); + stopTimingEventLoggerRoute(router, eventLog, this.logger); } public start() {} diff --git a/x-pack/test/plugin_api_integration/test_suites/event_log/service_api_integration.ts b/x-pack/test/plugin_api_integration/test_suites/event_log/service_api_integration.ts index 19645aabcf521..237258bc14376 100644 --- a/x-pack/test/plugin_api_integration/test_suites/event_log/service_api_integration.ts +++ b/x-pack/test/plugin_api_integration/test_suites/event_log/service_api_integration.ts @@ -7,7 +7,6 @@ import * as Rx from 'rxjs'; import { startWith, switchMap, take } from 'rxjs/operators'; import expect from '@kbn/expect/expect.js'; import { IEvent } from '../../../../plugins/event_log/server'; -import { IEventLogger } from '../../../../plugins/event_log/server/types'; import { FtrProviderContext } from '../../ftr_provider_context'; export default function({ getService }: FtrProviderContext) { @@ -15,6 +14,7 @@ export default function({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const log = getService('log'); const config = getService('config'); + const retry = getService('retry'); describe('Event Log service API', () => { it('should check if it is enabled', async () => { @@ -45,74 +45,97 @@ export default function({ getService }: FtrProviderContext) { ); }); - it('should return error message if provider is regostered', async () => { - await registerProviderActions('duplication', ['action1', 'action2']); + it('should be able to check if provider actions is registered', async () => { + const initResult = await isProviderActionRegistered('provider3', 'action1'); - const result = await registerProviderActions('duplication', ['action3']); - expect(result.body).to.be.eql(''); + if (!initResult.body.isProviderActionRegistered) { + await registerProviderActions('provider3', ['action1']); + } + const result1 = await isProviderActionRegistered('provider3', 'action1'); + expect(result1.body.isProviderActionRegistered).to.be.eql(true); + + const result = await isProviderActionRegistered('provider3', 'action2'); + expect(result.body.isProviderActionRegistered).to.be.eql(false); }); - it('should allow to register provider actions and return all provider actions', async () => { - await registerProviderActions('provider1', ['action1', 'action2']); + it('should return error message if provider is registered', async () => { + const initResult = await isProviderActionRegistered('duplication', 'action1'); - const providerActions = await getProviderActions(); - expect(providerActions.body.actions).to.be.eql({ provider1: ['action1', 'action2'] }); + if (!initResult.body.isProviderActionRegistered) { + await registerProviderActions('duplication', ['action1', 'action2']); + } + + const result = await registerProviderActions('duplication', ['action1', 'action2']); + expect(result.badRequest).to.be.eql(true); }); - it('should allow to get event logger if it is registered', async () => { - await registerProviderActions('provider2', ['action1', 'action2']); - const eventLogger = await getEventLogger('provider2'); - expect(eventLogger.body.eventLogger).not.be(null); + it('should allow to register provider actions and return all provider actions', async () => { + const initResult = await isProviderActionRegistered('provider1', 'action1'); + + if (!initResult.body.isProviderActionRegistered) { + await registerProviderActions('provider1', ['action1', 'action2']); + } - const eventLoggerNotExist = await getEventLogger('000'); - expect(eventLoggerNotExist.body.eventLogger).to.be(null); + const providerActions = await getProviderActions('provider1'); + expect(providerActions.body.actions).to.be.eql(['action1', 'action2']); }); - it('should be able to check if provider actions is registered', async () => { - await registerProviderActions('provider3', ['action1']); - const result = await isProviderActionRegistered('provider3', 'action2'); - expect(result.body.isProviderActionRegistered).to.be.eql(false); + it('should allow to get event logger event log service', async () => { + const initResult = await isProviderActionRegistered('provider2', 'action1'); - const result1 = await isProviderActionRegistered('provider3', 'action1'); - expect(result1.body.isProviderActionRegistered).to.be.eql(true); + if (!initResult.body.isProviderActionRegistered) { + await registerProviderActions('provider2', ['action1', 'action2']); + } + const eventLogger = await getEventLogger('provider2'); + expect(eventLogger.body.eventLogger.initialProperties).to.be.eql({ + event: { provider: 'provider2' }, + }); }); it('should allow write an event to index document if indexing entries is enabled', async () => { - await registerProviderActions('provider4', ['action1', 'action2']); - const eventLogger = (await getEventLogger('provider4')).body.eventLogger as IEventLogger; + const initResult = await isProviderActionRegistered('provider4', 'action1'); + + if (!initResult.body.isProviderActionRegistered) { + await registerProviderActions('provider4', ['action1', 'action2']); + } const eventId = '1'; const event: IEvent = { event: { action: 'action1', provider: 'provider4' }, - kibana: { saved_objects: [{ type: 'action', id: eventId }] }, + kibana: { saved_objects: [{ type: 'event_log_test', id: eventId }] }, }; - eventLogger.startTiming(event); + await startTimingEventLogger(event); setTimeout(() => log.debug('doing some action'), 500); - eventLogger.stopTiming(event); - eventLogger.logEvent(event); - - const uri = `/api/event_log/provider4/${eventId}/_find`; - log.debug(`calling ${uri}`); - const result = await supertest - .get(uri) - .set('kbn-xsrf', 'foo') - .expect(200); - expect(result.body.data).to.be.eql([event]); + await stopTimingEventLogger(event); + await logTestEvent(eventId, event); + + await retry.try(async () => { + const uri = `/api/event_log/event_log_test/${eventId}/_find`; + log.debug(`calling ${uri}`); + const result = await supertest + .get(uri) + .set('kbn-xsrf', 'foo') + .expect(200); + expect(result.body.data.length).to.be.eql(1); + }); }); it('should allow write an event to system logger if logging entries is enabled', async () => { - await registerProviderActions('provider5', ['test']); - const eventLogger = (await getEventLogger('provider5')).body.eventLogger as IEventLogger; + const initResult = await isProviderActionRegistered('provider5', 'test'); + + if (!initResult.body.isProviderActionRegistered) { + await registerProviderActions('provider5', ['test']); + } const eventId = '1'; const event: IEvent = { event: { action: 'action1', provider: 'provider5' }, kibana: { saved_objects: [{ type: 'action', id: eventId }] }, }; - eventLogger.startTiming(event); + await startTimingEventLogger(event); setTimeout(() => log.debug('doing some action'), 500); - eventLogger.stopTiming(event); - eventLogger.logEvent(event); + await stopTimingEventLogger(event); + await logTestEvent(eventId, event); const promise = log .getWritten$() @@ -133,8 +156,7 @@ export default function({ getService }: FtrProviderContext) { return await supertest .post(`/api/log_event_fixture/${provider}/_registerProviderActions`) .set('kbn-xsrf', 'xxx') - .send(actions) - .expect(200); + .send(actions); } async function isProviderActionRegistered(provider: string, action: string) { @@ -145,10 +167,10 @@ export default function({ getService }: FtrProviderContext) { .expect(200); } - async function getProviderActions() { - log.debug(`getProviderActions`); + async function getProviderActions(provider: string) { + log.debug(`getProviderActions ${provider}`); return await supertest - .get(`/api/log_event_fixture/getProviderActions`) + .get(`/api/log_event_fixture/${provider}/getProviderActions`) .set('kbn-xsrf', 'xxx') .expect(200); } @@ -184,4 +206,31 @@ export default function({ getService }: FtrProviderContext) { .set('kbn-xsrf', 'foo') .expect(200); } + + async function startTimingEventLogger(event: IEvent) { + log.debug(`startTimingEventLogger ${event}`); + return await supertest + .post(`/api/log_event_fixture/startTimingEventLogger`) + .set('kbn-xsrf', 'xxx') + .send(event) + .expect(200); + } + + async function stopTimingEventLogger(event: IEvent) { + log.debug(`stopTimingEventLogger ${event}`); + return await supertest + .post(`/api/log_event_fixture/stopTimingEventLogger`) + .set('kbn-xsrf', 'xxx') + .send(event) + .expect(200); + } + + async function logTestEvent(id: string, event: IEvent) { + log.debug(`Logging Event for Saved Object ${id}`); + return await supertest + .post(`/api/log_event_fixture/${id}/_log`) + .set('kbn-xsrf', 'foo') + .send(event) + .expect(200); + } }