Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Sep 17, 2024
1 parent f191876 commit 0860b34
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function getApmEventClient({
const includeFrozen = await coreContext.uiSettings.client.get<boolean>(
UI_SETTINGS.SEARCH_INCLUDE_FROZEN
);
const excludedDataTiers = await coreContext.uiSettings.client.get<IndexLifeCycleDataTier>(
const excludedDataTiers = await coreContext.uiSettings.client.get<IndexLifeCycleDataTier[]>(
searchExcludedDataTiers
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { APMEventESSearchRequestParams, alertingEsClient } from './alerting_es_client';
import { RuleExecutorServices } from '@kbn/alerting-plugin/server';
import { ElasticsearchClient, IUiSettingsClient } from '@kbn/core/server';
import { ESSearchResponse } from '@kbn/es-types';

describe('alertingEsClient', () => {
let scopedClusterClientMock: jest.Mocked<{
asCurrentUser: jest.Mocked<ElasticsearchClient>;
}>;

let uiSettingsClientMock: jest.Mocked<IUiSettingsClient>;

beforeEach(() => {
scopedClusterClientMock = {
asCurrentUser: {
search: jest.fn(),
} as unknown as jest.Mocked<ElasticsearchClient>,
};

uiSettingsClientMock = {
get: jest.fn(),
} as unknown as jest.Mocked<IUiSettingsClient>;
});

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

it('should call search with filters containing excluded data tiers', async () => {
const excludedDataTiers = ['data_warm', 'data_cold'];
uiSettingsClientMock.get.mockResolvedValue(excludedDataTiers);

const params = {
body: {
size: 10,
track_total_hits: true,
query: {
match: { field: 'value' },
},
},
};

scopedClusterClientMock.asCurrentUser.search.mockResolvedValue({
hits: {
total: { value: 1, relation: 'eq' },
hits: [{ _source: {}, _index: '' }],
max_score: 1,
},
took: 1,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
timed_out: false,
} as unknown as ESSearchResponse<unknown, typeof params>);

await alertingEsClient({
scopedClusterClient: scopedClusterClientMock as unknown as RuleExecutorServices<
never,
never,
never
>['scopedClusterClient'],
uiSettingsClient: uiSettingsClientMock,
params,
});

const searchParams = scopedClusterClientMock.asCurrentUser.search.mock
.calls[0][0] as APMEventESSearchRequestParams;
expect(searchParams?.body?.query?.bool).toEqual({
filter: { bool: { must_not: [{ terms: { _tier: ['data_warm', 'data_cold'] } }] } },
must: [{ match: { field: 'value' } }],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function alertingEsClient<TParams extends APMEventESSearchRequestPa
uiSettingsClient: IUiSettingsClient;
params: TParams;
}): Promise<ESSearchResponse<unknown, TParams>> {
const excludedDataTiers = await uiSettingsClient.get<IndexLifeCycleDataTier>(
const excludedDataTiers = await uiSettingsClient.get<IndexLifeCycleDataTier[]>(
searchExcludedDataTiers
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export const createRuleTypeMocks = () => {
savedObjectsClient: {
get: () => ({ attributes: { consumer: APM_SERVER_FEATURE_ID } }),
},
uiSettingsClient: {
get: jest.fn(),
},
alertFactory: {
create: jest.fn(() => ({ scheduleActions, getUuid })),
done: {},
Expand Down
Loading

0 comments on commit 0860b34

Please sign in to comment.