Skip to content

Commit

Permalink
Initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Apr 24, 2020
1 parent a145aa9 commit b21d4bd
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ jest.mock('./lib/send_email', () => ({
}));

import { Logger } from '../../../../../src/core/server';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';

import { ActionType, ActionTypeExecutorOptions } from '../types';
import { actionsConfigMock } from '../actions_config.mock';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { createActionTypeRegistry } from './index.test';
import { sendEmail } from './lib/send_email';
import { actionsMock } from '../mocks';
import {
ActionParamsType,
ActionTypeConfigType,
Expand All @@ -26,13 +26,8 @@ import {
const sendEmailMock = sendEmail as jest.Mock;

const ACTION_TYPE_ID = '.email';
const NO_OP_FN = () => {};

const services = {
log: NO_OP_FN,
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};
const services = actionsMock.createServices();

let actionType: ActionType;
let mockedLogger: jest.Mocked<Logger>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ jest.mock('./lib/send_email', () => ({

import { ActionType, ActionTypeExecutorOptions } from '../types';
import { validateConfig, validateParams } from '../lib';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { createActionTypeRegistry } from './index.test';
import { ActionParamsType, ActionTypeConfigType } from './es_index';
import { actionsMock } from '../mocks';

const ACTION_TYPE_ID = '.index';
const NO_OP_FN = () => {};

const services = {
log: NO_OP_FN,
callCluster: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};
const services = actionsMock.createServices();

let actionType: ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ jest.mock('./lib/post_pagerduty', () => ({
import { getActionType } from './pagerduty';
import { ActionType, Services, ActionTypeExecutorOptions } from '../types';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { postPagerduty } from './lib/post_pagerduty';
import { createActionTypeRegistry } from './index.test';
import { Logger } from '../../../../../src/core/server';
import { actionsConfigMock } from '../actions_config.mock';
import { actionsMock } from '../mocks';

const postPagerdutyMock = postPagerduty as jest.Mock;

const ACTION_TYPE_ID = '.pagerduty';

const services: Services = {
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};
const services: Services = actionsMock.createServices();

let actionType: ActionType;
let mockedLogger: jest.Mocked<Logger>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { ActionType } from '../types';
import { validateParams } from '../lib';
import { Logger } from '../../../../../src/core/server';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { createActionTypeRegistry } from './index.test';
import { actionsMock } from '../mocks';

const ACTION_TYPE_ID = '.server-log';

Expand Down Expand Up @@ -90,10 +90,7 @@ describe('execute()', () => {
const actionId = 'some-id';
await actionType.executor({
actionId,
services: {
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: savedObjectsClientMock.create(),
},
services: actionsMock.createServices(),
params: { message: 'message text here', level: 'info' },
config: {},
secrets: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import { getActionType } from '.';
import { ActionType, Services, ActionTypeExecutorOptions } from '../../types';
import { validateConfig, validateSecrets, validateParams } from '../../lib';
import { savedObjectsClientMock } from '../../../../../../src/core/server/mocks';
import { createActionTypeRegistry } from '../index.test';
import { actionsConfigMock } from '../../actions_config.mock';
import { actionsMock } from '../../mocks';

import { ACTION_TYPE_ID } from './constants';
import * as i18n from './translations';
Expand All @@ -21,10 +21,7 @@ jest.mock('./action_handlers');

const handleIncidentMock = handleIncident as jest.Mock;

const services: Services = {
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};
const services: Services = actionsMock.createServices();

let actionType: ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
*/

import { ActionType, Services, ActionTypeExecutorOptions } from '../types';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { validateParams, validateSecrets } from '../lib';
import { getActionType } from './slack';
import { actionsConfigMock } from '../actions_config.mock';
import { actionsMock } from '../mocks';

const ACTION_TYPE_ID = '.slack';

const services: Services = {
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};
const services: Services = actionsMock.createServices();

let actionType: ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ jest.mock('axios', () => ({
import { getActionType } from './webhook';
import { ActionType, Services } from '../types';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { actionsConfigMock } from '../actions_config.mock';
import { createActionTypeRegistry } from './index.test';
import { Logger } from '../../../../../src/core/server';
import { actionsMock } from '../mocks';
import axios from 'axios';

const axiosRequestMock = axios.request as jest.Mock;

const ACTION_TYPE_ID = '.webhook';

const services: Services = {
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};
const services: Services = actionsMock.createServices();

let actionType: ActionType;
let mockedLogger: jest.Mocked<Logger>;
Expand Down
18 changes: 6 additions & 12 deletions x-pack/plugins/actions/server/lib/action_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@ import { schema } from '@kbn/config-schema';
import { ActionExecutor } from './action_executor';
import { actionTypeRegistryMock } from '../action_type_registry.mock';
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks';
import { savedObjectsClientMock, loggingServiceMock } from '../../../../../src/core/server/mocks';
import { loggingServiceMock } from '../../../../../src/core/server/mocks';
import { eventLoggerMock } from '../../../event_log/server/mocks';
import { spacesServiceMock } from '../../../spaces/server/spaces_service/spaces_service.mock';
import { ActionType } from '../types';
import { actionsMock } from '../mocks';

const actionExecutor = new ActionExecutor({ isESOUsingEphemeralEncryptionKey: false });
const savedObjectsClient = savedObjectsClientMock.create();

function getServices() {
return {
savedObjectsClient,
log: jest.fn(),
callCluster: jest.fn(),
};
}
const services = actionsMock.createServices();
const savedObjectsClient = services.savedObjectsClient;
const encryptedSavedObjectsPlugin = encryptedSavedObjectsMock.createStart();
const actionTypeRegistry = actionTypeRegistryMock.create();

Expand All @@ -39,7 +33,7 @@ const spacesMock = spacesServiceMock.createSetupContract();
actionExecutor.initialize({
logger: loggingServiceMock.create().get(),
spaces: spacesMock,
getServices,
getServices: () => services,
actionTypeRegistry,
encryptedSavedObjectsPlugin,
eventLogger: eventLoggerMock.create(),
Expand Down Expand Up @@ -229,7 +223,7 @@ test('throws an error when passing isESOUsingEphemeralEncryptionKey with value o
customActionExecutor.initialize({
logger: loggingServiceMock.create().get(),
spaces: spacesMock,
getServices,
getServices: () => services,
actionTypeRegistry,
encryptedSavedObjectsPlugin,
eventLogger: eventLoggerMock.create(),
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/actions/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import { actionsClientMock } from './actions_client.mock';
import { PluginSetupContract, PluginStartContract } from './plugin';
import { Services } from './types';
import { savedObjectsClientMock } from '../../../../src/core/server/mocks';

export { actionsClientMock };

Expand All @@ -26,7 +28,19 @@ const createStartMock = () => {
return mock;
};

const createServicesMock = () => {
const mock: jest.Mocked<Services & {
savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;
}> = {
callCluster: jest.fn(),
getScopedClusterClient: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};
return mock;
};

export const actionsMock = {
createServices: createServicesMock,
createSetup: createSetupMock,
createStart: createStartMock,
};
8 changes: 8 additions & 0 deletions x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
IContextProvider,
SavedObjectsServiceStart,
ElasticsearchServiceStart,
IClusterClient,
ClusterClient,
} from '../../../../src/core/server';

import {
Expand Down Expand Up @@ -294,6 +296,12 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
return request => ({
callCluster: elasticsearch.legacy.client.asScoped(request).callAsCurrentUser,
savedObjectsClient: savedObjects.getScopedClient(request),
getScopedClusterClient(clusterClient: IClusterClient) {
if (!(clusterClient instanceof ClusterClient)) {
throw new Error('given clusterClient is not an instance of ClusterClient');
}
return clusterClient.asScoped(request);
},
});
}

Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/actions/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract, SavedObjectAttributes } from '../../../../src/core/server';
import { ActionTypeRegistry } from './action_type_registry';
import { PluginSetupContract, PluginStartContract } from './plugin';
import { ActionsClient } from './actions_client';
import { LicenseType } from '../../licensing/common/types';
import {
IClusterClient,
IScopedClusterClient,
SavedObjectsClientContract,
SavedObjectAttributes,
} from '../../../../src/core/server';

export type WithoutQueryAndParams<T> = Pick<T, Exclude<keyof T, 'query' | 'params'>>;
export type GetServicesFunction = (request: any) => Services;
Expand All @@ -19,6 +24,7 @@ export type SpaceIdToNamespaceFunction = (spaceId?: string) => string | undefine
export interface Services {
callCluster(path: string, opts: any): Promise<any>;
savedObjectsClient: SavedObjectsClientContract;
getScopedClusterClient(clusterClient: IClusterClient): IScopedClusterClient;
}

declare module 'src/core/server' {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const createAlertServicesMock = () => {
.fn<jest.Mocked<AlertInstance>, [string]>()
.mockReturnValue(alertInstanceFactoryMock),
callCluster: jest.fn(),
getScopedClusterClient: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};
};
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
RequestHandler,
SharedGlobalConfig,
ElasticsearchServiceStart,
IClusterClient,
ClusterClient,
} from '../../../../src/core/server';

import {
Expand Down Expand Up @@ -267,6 +269,12 @@ export class AlertingPlugin {
return request => ({
callCluster: elasticsearch.legacy.client.asScoped(request).callAsCurrentUser,
savedObjectsClient: savedObjects.getScopedClient(request),
getScopedClusterClient(clusterClient: IClusterClient) {
if (!(clusterClient instanceof ClusterClient)) {
throw new Error('given clusterClient is not an instance of ClusterClient');
}
return clusterClient.asScoped(request);
},
});
}

Expand Down
11 changes: 4 additions & 7 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { ConcreteTaskInstance, TaskStatus } from '../../../../plugins/task_manag
import { TaskRunnerContext } from './task_runner_factory';
import { TaskRunner } from './task_runner';
import { encryptedSavedObjectsMock } from '../../../../plugins/encrypted_saved_objects/server/mocks';
import { savedObjectsClientMock, loggingServiceMock } from '../../../../../src/core/server/mocks';
import { loggingServiceMock } from '../../../../../src/core/server/mocks';
import { PluginStartContract as ActionsPluginStart } from '../../../actions/server';
import { actionsMock } from '../../../actions/server/mocks';
import { alertsMock } from '../mocks';
import { eventLoggerMock } from '../../../event_log/server/event_logger.mock';
import { IEventLogger } from '../../../event_log/server';
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
Expand Down Expand Up @@ -52,13 +53,9 @@ describe('Task Runner', () => {

afterAll(() => fakeTimer.restore());

const savedObjectsClient = savedObjectsClientMock.create();
const encryptedSavedObjectsPlugin = encryptedSavedObjectsMock.createStart();
const services = {
log: jest.fn(),
callCluster: jest.fn(),
savedObjectsClient,
};
const services = alertsMock.createAlertServices();
const savedObjectsClient = services.savedObjectsClient;

const taskRunnerFactoryInitializerParams: jest.Mocked<TaskRunnerContext> & {
actionsPlugin: jest.Mocked<ActionsPluginStart>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import sinon from 'sinon';
import { ConcreteTaskInstance, TaskStatus } from '../../../../plugins/task_manager/server';
import { TaskRunnerContext, TaskRunnerFactory } from './task_runner_factory';
import { encryptedSavedObjectsMock } from '../../../../plugins/encrypted_saved_objects/server/mocks';
import { savedObjectsClientMock, loggingServiceMock } from '../../../../../src/core/server/mocks';
import { loggingServiceMock } from '../../../../../src/core/server/mocks';
import { actionsMock } from '../../../actions/server/mocks';
import { alertsMock } from '../mocks';
import { eventLoggerMock } from '../../../event_log/server/event_logger.mock';

const alertType = {
Expand Down Expand Up @@ -48,13 +49,8 @@ describe('Task Runner Factory', () => {

afterAll(() => fakeTimer.restore());

const savedObjectsClient = savedObjectsClientMock.create();
const encryptedSavedObjectsPlugin = encryptedSavedObjectsMock.createStart();
const services = {
log: jest.fn(),
callCluster: jest.fn(),
savedObjectsClient,
};
const services = alertsMock.createAlertServices();

const taskRunnerFactoryInitializerParams: jest.Mocked<TaskRunnerContext> = {
getServices: jest.fn().mockReturnValue(services),
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import { AlertInstance } from './alert_instance';
import { AlertTypeRegistry as OrigAlertTypeRegistry } from './alert_type_registry';
import { PluginSetupContract, PluginStartContract } from './plugin';
import { SavedObjectAttributes, SavedObjectsClientContract } from '../../../../src/core/server';
import { Alert, AlertActionParams, ActionGroup } from '../common';
import { AlertsClient } from './alerts_client';
export * from '../common';
import {
IClusterClient,
IScopedClusterClient,
SavedObjectAttributes,
SavedObjectsClientContract,
} from '../../../../src/core/server';

export type State = Record<string, any>;
export type Context = Record<string, any>;
Expand All @@ -31,6 +36,7 @@ declare module 'src/core/server' {
export interface Services {
callCluster(path: string, opts: any): Promise<any>;
savedObjectsClient: SavedObjectsClientContract;
getScopedClusterClient(clusterClient: IClusterClient): IScopedClusterClient;
}

export interface AlertServices extends Services {
Expand Down

0 comments on commit b21d4bd

Please sign in to comment.