Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[alerting] Adds an alertServices mock and uses it in siem, monitoring and uptime #63489

Merged
merged 6 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/plugins/data/server/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export * from './search/mocks';
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,27 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { savedObjectsClientMock } from 'src/core/server/mocks';
import { loggerMock } from 'src/core/server/logging/logger.mock';
import { getResult } from '../routes/__mocks__/request_responses';
import { rulesNotificationAlertType } from './rules_notification_alert_type';
import { buildSignalsSearchQuery } from './build_signals_query';
import { AlertInstance } from '../../../../../../../plugins/alerting/server';
import { alertsMock, AlertServicesMock } from '../../../../../../../plugins/alerting/server/mocks';
import { NotificationExecutorOptions } from './types';
jest.mock('./build_signals_query');

describe('rules_notification_alert_type', () => {
let payload: NotificationExecutorOptions;
let alert: ReturnType<typeof rulesNotificationAlertType>;
let alertInstanceMock: Record<string, jest.Mock>;
let alertInstanceFactoryMock: () => AlertInstance;
let savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;
let logger: ReturnType<typeof loggerMock.create>;
let callClusterMock: jest.Mock;
let alertServices: AlertServicesMock;

beforeEach(() => {
alertInstanceMock = {
scheduleActions: jest.fn(),
replaceState: jest.fn(),
};
alertInstanceMock.replaceState.mockReturnValue(alertInstanceMock);
alertInstanceFactoryMock = jest.fn().mockReturnValue(alertInstanceMock);
callClusterMock = jest.fn();
savedObjectsClient = savedObjectsClientMock.create();
alertServices = alertsMock.createAlertServices();
logger = loggerMock.create();

payload = {
alertId: '1111',
services: {
savedObjectsClient,
alertInstanceFactory: alertInstanceFactoryMock,
callCluster: callClusterMock,
},
services: alertServices,
params: { ruleAlertId: '2222' },
state: {},
spaceId: '',
Expand All @@ -58,7 +43,7 @@ describe('rules_notification_alert_type', () => {

describe('executor', () => {
it('throws an error if rule alert was not found', async () => {
savedObjectsClient.get.mockResolvedValue({
alertServices.savedObjectsClient.get.mockResolvedValue({
id: 'id',
attributes: {},
type: 'type',
Expand All @@ -72,13 +57,13 @@ describe('rules_notification_alert_type', () => {

it('should call buildSignalsSearchQuery with proper params', async () => {
const ruleAlert = getResult();
savedObjectsClient.get.mockResolvedValue({
alertServices.savedObjectsClient.get.mockResolvedValue({
id: 'id',
type: 'type',
references: [],
attributes: ruleAlert,
});
callClusterMock.mockResolvedValue({
alertServices.callCluster.mockResolvedValue({
count: 0,
});

Expand All @@ -96,36 +81,38 @@ describe('rules_notification_alert_type', () => {

it('should not call alertInstanceFactory if signalsCount was 0', async () => {
const ruleAlert = getResult();
savedObjectsClient.get.mockResolvedValue({
alertServices.savedObjectsClient.get.mockResolvedValue({
id: 'id',
type: 'type',
references: [],
attributes: ruleAlert,
});
callClusterMock.mockResolvedValue({
alertServices.callCluster.mockResolvedValue({
count: 0,
});

await alert.executor(payload);

expect(alertInstanceFactoryMock).not.toHaveBeenCalled();
expect(alertServices.alertInstanceFactory).not.toHaveBeenCalled();
});

it('should call scheduleActions if signalsCount was greater than 0', async () => {
const ruleAlert = getResult();
savedObjectsClient.get.mockResolvedValue({
alertServices.savedObjectsClient.get.mockResolvedValue({
id: 'id',
type: 'type',
references: [],
attributes: ruleAlert,
});
callClusterMock.mockResolvedValue({
alertServices.callCluster.mockResolvedValue({
count: 10,
});

await alert.executor(payload);

expect(alertInstanceFactoryMock).toHaveBeenCalled();
expect(alertServices.alertInstanceFactory).toHaveBeenCalled();

const [{ value: alertInstanceMock }] = alertServices.alertInstanceFactory.mock.results;
expect(alertInstanceMock.replaceState).toHaveBeenCalledWith(
expect.objectContaining({ signals_count: 10 })
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,28 @@
*/

import { getQueryFilter, getFilter } from './get_filter';
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { PartialFilter } from '../types';
import { AlertServices } from '../../../../../../../plugins/alerting/server';
import { alertsMock, AlertServicesMock } from '../../../../../../../plugins/alerting/server/mocks';

describe('get_filter', () => {
let savedObjectsClient = savedObjectsClientMock.create();
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
attributes: {
query: { query: 'host.name: linux', language: 'kuery' },
filters: [],
},
}));
let servicesMock: AlertServices = {
savedObjectsClient,
callCluster: jest.fn(),
alertInstanceFactory: jest.fn(),
};
let servicesMock: AlertServicesMock;

beforeAll(() => {
jest.resetAllMocks();
});

beforeEach(() => {
savedObjectsClient = savedObjectsClientMock.create();
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock = alertsMock.createAlertServices();
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {
query: { query: 'host.name: linux', language: 'kuery' },
language: 'kuery',
filters: [],
},
}));
servicesMock = {
savedObjectsClient,
callCluster: jest.fn(),
alertInstanceFactory: jest.fn(),
};
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { savedObjectsClientMock } from 'src/core/server/mocks';
import { DEFAULT_INDEX_KEY } from '../../../../common/constants';
import { getInputIndex } from './get_input_output_index';
import { defaultIndexPattern } from '../../../../default_index_pattern';
import { AlertServices } from '../../../../../../../plugins/alerting/server';
import { alertsMock, AlertServicesMock } from '../../../../../../../plugins/alerting/server/mocks';

describe('get_input_output_index', () => {
let savedObjectsClient = savedObjectsClientMock.create();
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
attributes: {},
}));
let servicesMock: AlertServices = {
savedObjectsClient,
callCluster: jest.fn(),
alertInstanceFactory: jest.fn(),
};
let servicesMock: AlertServicesMock;

beforeAll(() => {
jest.resetAllMocks();
Expand All @@ -30,28 +21,32 @@ describe('get_input_output_index', () => {
});

beforeEach(() => {
savedObjectsClient = savedObjectsClientMock.create();
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock = alertsMock.createAlertServices();
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {},
}));
servicesMock = {
savedObjectsClient,
callCluster: jest.fn(),
alertInstanceFactory: jest.fn(),
};
});

describe('getInputOutputIndex', () => {
test('Returns inputIndex if inputIndex is passed in', async () => {
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {},
}));
const inputIndex = await getInputIndex(servicesMock, '8.0.0', ['test-input-index-1']);
expect(inputIndex).toEqual(['test-input-index-1']);
});

test('Returns a saved object inputIndex if passed in inputIndex is undefined', async () => {
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {
[DEFAULT_INDEX_KEY]: ['configured-index-1', 'configured-index-2'],
},
Expand All @@ -61,7 +56,10 @@ describe('get_input_output_index', () => {
});

test('Returns a saved object inputIndex if passed in inputIndex is null', async () => {
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {
[DEFAULT_INDEX_KEY]: ['configured-index-1', 'configured-index-2'],
},
Expand All @@ -71,7 +69,10 @@ describe('get_input_output_index', () => {
});

test('Returns a saved object inputIndex default from constants if inputIndex passed in is null and the key is also null', async () => {
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {
[DEFAULT_INDEX_KEY]: null,
},
Expand All @@ -81,7 +82,10 @@ describe('get_input_output_index', () => {
});

test('Returns a saved object inputIndex default from constants if inputIndex passed in is undefined and the key is also null', async () => {
savedObjectsClient.get = jest.fn().mockImplementation(() => ({
servicesMock.savedObjectsClient.get.mockImplementation(async (type: string, id: string) => ({
id,
type,
references: [],
attributes: {
[DEFAULT_INDEX_KEY]: null,
},
Expand Down
Loading