Skip to content

Commit

Permalink
use services mock in siem, monitoring and uptime
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Apr 15, 2020
1 parent 6276786 commit 84d65da
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 144 deletions.
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ import {
} from './__mocks__/es_results';
import { searchAfterAndBulkCreate } from './search_after_bulk_create';
import { DEFAULT_SIGNALS_INDEX } from '../../../../common/constants';
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { alertsMock, AlertServicesMock } from '../../../../../../../plugins/alerting/server/mocks';
import uuid from 'uuid';

export const mockService = {
callCluster: jest.fn(),
alertInstanceFactory: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};

describe('searchAfterAndBulkCreate', () => {
let mockService: AlertServicesMock;
let inputIndexPattern: string[] = [];
beforeEach(() => {
jest.clearAllMocks();
inputIndexPattern = ['auditbeat-*'];
mockService = alertsMock.createAlertServices();
});

test('if successful with empty search results', async () => {
Expand Down Expand Up @@ -65,7 +61,7 @@ describe('searchAfterAndBulkCreate', () => {
const sampleParams = sampleRuleAlertParams(30);
const someGuids = Array.from({ length: 13 }).map(x => uuid.v4());
mockService.callCluster
.mockReturnValueOnce({
.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand All @@ -79,8 +75,8 @@ describe('searchAfterAndBulkCreate', () => {
},
],
})
.mockReturnValueOnce(repeatedSearchResultsWithSortId(3, 1, someGuids.slice(0, 3)))
.mockReturnValueOnce({
.mockResolvedValueOnce(repeatedSearchResultsWithSortId(3, 1, someGuids.slice(0, 3)))
.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand All @@ -94,8 +90,8 @@ describe('searchAfterAndBulkCreate', () => {
},
],
})
.mockReturnValueOnce(repeatedSearchResultsWithSortId(3, 1, someGuids.slice(3, 6)))
.mockReturnValueOnce({
.mockResolvedValueOnce(repeatedSearchResultsWithSortId(3, 1, someGuids.slice(3, 6)))
.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand Down Expand Up @@ -139,7 +135,7 @@ describe('searchAfterAndBulkCreate', () => {
test('if unsuccessful first bulk create', async () => {
const someGuids = Array.from({ length: 4 }).map(x => uuid.v4());
const sampleParams = sampleRuleAlertParams(10);
mockService.callCluster.mockReturnValue(sampleBulkCreateDuplicateResult);
mockService.callCluster.mockResolvedValue(sampleBulkCreateDuplicateResult);
const { success, createdSignalsCount } = await searchAfterAndBulkCreate({
someResult: repeatedSearchResultsWithSortId(4, 1, someGuids),
ruleParams: sampleParams,
Expand Down Expand Up @@ -169,7 +165,7 @@ describe('searchAfterAndBulkCreate', () => {

test('if unsuccessful iteration of searchAfterAndBulkCreate due to empty sort ids', async () => {
const sampleParams = sampleRuleAlertParams();
mockService.callCluster.mockReturnValueOnce({
mockService.callCluster.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand Down Expand Up @@ -212,7 +208,7 @@ describe('searchAfterAndBulkCreate', () => {

test('if unsuccessful iteration of searchAfterAndBulkCreate due to empty sort ids and 0 total hits', async () => {
const sampleParams = sampleRuleAlertParams();
mockService.callCluster.mockReturnValueOnce({
mockService.callCluster.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand Down Expand Up @@ -256,7 +252,7 @@ describe('searchAfterAndBulkCreate', () => {
const sampleParams = sampleRuleAlertParams(10);
const someGuids = Array.from({ length: 4 }).map(x => uuid.v4());
mockService.callCluster
.mockReturnValueOnce({
.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand All @@ -270,7 +266,7 @@ describe('searchAfterAndBulkCreate', () => {
},
],
})
.mockReturnValueOnce(sampleDocSearchResultsNoSortId());
.mockResolvedValueOnce(sampleDocSearchResultsNoSortId());
const { success, createdSignalsCount } = await searchAfterAndBulkCreate({
someResult: repeatedSearchResultsWithSortId(4, 1, someGuids),
ruleParams: sampleParams,
Expand Down Expand Up @@ -301,7 +297,7 @@ describe('searchAfterAndBulkCreate', () => {
const sampleParams = sampleRuleAlertParams(10);
const someGuids = Array.from({ length: 4 }).map(x => uuid.v4());
mockService.callCluster
.mockReturnValueOnce({
.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand All @@ -315,7 +311,7 @@ describe('searchAfterAndBulkCreate', () => {
},
],
})
.mockReturnValueOnce(sampleEmptyDocSearchResults());
.mockResolvedValueOnce(sampleEmptyDocSearchResults());
const { success, createdSignalsCount } = await searchAfterAndBulkCreate({
someResult: repeatedSearchResultsWithSortId(4, 1, someGuids),
ruleParams: sampleParams,
Expand Down Expand Up @@ -346,7 +342,7 @@ describe('searchAfterAndBulkCreate', () => {
const sampleParams = sampleRuleAlertParams(10);
const someGuids = Array.from({ length: 4 }).map(x => uuid.v4());
mockService.callCluster
.mockReturnValueOnce({
.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ import {
sampleBulkCreateErrorResult,
sampleDocWithAncestors,
} from './__mocks__/es_results';
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { DEFAULT_SIGNALS_INDEX } from '../../../../common/constants';
import { singleBulkCreate, filterDuplicateRules } from './single_bulk_create';

export const mockService = {
callCluster: jest.fn(),
alertInstanceFactory: jest.fn(),
savedObjectsClient: savedObjectsClientMock.create(),
};
import { alertsMock, AlertServicesMock } from '../../../../../../../plugins/alerting/server/mocks';

describe('singleBulkCreate', () => {
const mockService: AlertServicesMock = alertsMock.createAlertServices();

beforeEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -135,7 +131,7 @@ describe('singleBulkCreate', () => {

test('create successful bulk create', async () => {
const sampleParams = sampleRuleAlertParams();
mockService.callCluster.mockReturnValueOnce({
mockService.callCluster.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand Down Expand Up @@ -169,7 +165,7 @@ describe('singleBulkCreate', () => {

test('create successful bulk create with docs with no versioning', async () => {
const sampleParams = sampleRuleAlertParams();
mockService.callCluster.mockReturnValueOnce({
mockService.callCluster.mockResolvedValueOnce({
took: 100,
errors: false,
items: [
Expand Down Expand Up @@ -203,7 +199,7 @@ describe('singleBulkCreate', () => {

test('create unsuccessful bulk create due to empty search results', async () => {
const sampleParams = sampleRuleAlertParams();
mockService.callCluster.mockReturnValue(false);
mockService.callCluster.mockResolvedValue(false);
const { success, createdItemsCount } = await singleBulkCreate({
someResult: sampleEmptyDocSearchResults(),
ruleParams: sampleParams,
Expand All @@ -230,7 +226,7 @@ describe('singleBulkCreate', () => {
test('create successful bulk create when bulk create has duplicate errors', async () => {
const sampleParams = sampleRuleAlertParams();
const sampleSearchResult = sampleDocSearchResultsNoSortId;
mockService.callCluster.mockReturnValue(sampleBulkCreateDuplicateResult);
mockService.callCluster.mockResolvedValue(sampleBulkCreateDuplicateResult);
const { success, createdItemsCount } = await singleBulkCreate({
someResult: sampleSearchResult(),
ruleParams: sampleParams,
Expand Down Expand Up @@ -259,7 +255,7 @@ describe('singleBulkCreate', () => {
test('create successful bulk create when bulk create has multiple error statuses', async () => {
const sampleParams = sampleRuleAlertParams();
const sampleSearchResult = sampleDocSearchResultsNoSortId;
mockService.callCluster.mockReturnValue(sampleBulkCreateErrorResult);
mockService.callCluster.mockResolvedValue(sampleBulkCreateErrorResult);
const { success, createdItemsCount } = await singleBulkCreate({
someResult: sampleSearchResult(),
ruleParams: sampleParams,
Expand Down Expand Up @@ -354,7 +350,7 @@ describe('singleBulkCreate', () => {

test('create successful and returns proper createdItemsCount', async () => {
const sampleParams = sampleRuleAlertParams();
mockService.callCluster.mockReturnValue(sampleBulkCreateDuplicateResult);
mockService.callCluster.mockResolvedValue(sampleBulkCreateDuplicateResult);
const { success, createdItemsCount } = await singleBulkCreate({
someResult: sampleDocSearchResultsNoSortId(),
ruleParams: sampleParams,
Expand Down
Loading

0 comments on commit 84d65da

Please sign in to comment.