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

[Stack Monitoring] improve unit tests in fetch functions in alerts #124033

Merged
merged 6 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.
*/

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks';
import { fetchCCRReadExceptions } from './fetch_ccr_read_exceptions';

jest.mock('../../static_globals', () => ({
Globals: {
app: {
config: {
ui: {
ccs: { enabled: true },
},
},
},
},
}));
import { Globals } from '../../static_globals';

describe('fetchCCReadExceptions', () => {
const esRes = {
aggregations: {
remote_clusters: {
buckets: [],
},
},
};
const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser;
esClient.search.mockReturnValue(
// @ts-expect-error not full response interface
elasticsearchClientMock.createSuccessTransportRequestPromise(esRes)
);
it('should call ES with correct query', async () => {
await fetchCCRReadExceptions(esClient, 1643306331418, 1643309869056, 10000);
expect(esClient.search).toHaveBeenCalledWith({
index:
'*:.monitoring-es-*,.monitoring-es-*,*:metrics-elasticsearch.ccr-*,metrics-elasticsearch.ccr-*',
filter_path: ['aggregations.remote_clusters.buckets'],
body: {
size: 0,
query: {
bool: {
filter: [
{
nested: {
path: 'ccr_stats.read_exceptions',
query: { exists: { field: 'ccr_stats.read_exceptions.exception' } },
},
},
{
bool: {
should: [
{ term: { type: 'ccr_stats' } },
{ term: { 'data_stream.dataset': 'elasticsearch.ccr' } },
],
minimum_should_match: 1,
},
},
{
range: {
timestamp: { format: 'epoch_millis', gte: 1643306331418, lte: 1643309869056 },
},
},
],
},
},
aggs: {
remote_clusters: {
terms: { field: 'ccr_stats.remote_cluster', size: 10000 },
aggs: {
follower_indices: {
terms: { field: 'ccr_stats.follower_index', size: 10000 },
aggs: {
hits: {
top_hits: {
sort: [{ timestamp: { order: 'desc', unmapped_type: 'long' } }],
_source: {
includes: [
'cluster_uuid',
'ccr_stats.read_exceptions',
'ccr_stats.shard_id',
'ccr_stats.leader_index',
],
},
size: 1,
},
},
},
},
},
},
},
},
});
});
it('should call ES with correct query when ccs disabled', async () => {
// @ts-ignore
Globals.app.config.ui.ccs.enabled = false;
let params = null;
esClient.search.mockImplementation((...args) => {
params = args[0];
return elasticsearchClientMock.createSuccessTransportRequestPromise(esRes as any);
});

await fetchCCRReadExceptions(esClient, 1643306331418, 1643309869056, 10000);

// @ts-ignore
expect(params.index).toBe('.monitoring-es-*,metrics-elasticsearch.ccr-*');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks';
import { fetchClusterHealth } from './fetch_cluster_health';

jest.mock('../../static_globals', () => ({
Globals: {
app: {
Expand All @@ -21,12 +20,14 @@ jest.mock('../../static_globals', () => ({
},
},
}));
import { Globals } from '../../static_globals';

describe('fetchClusterHealth', () => {
it('should return the cluster health', async () => {
const status = 'green';
const clusterUuid = 'sdfdsaj34434';
const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser;
const clusterUuid = 'sdfdsaj34434';
const clusters = [{ clusterUuid, clusterName: 'foo' }];
const status = 'green';
esClient.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
Expand All @@ -45,8 +46,6 @@ describe('fetchClusterHealth', () => {
} as estypes.SearchResponse)
);

const clusters = [{ clusterUuid, clusterName: 'foo' }];

const health = await fetchClusterHealth(esClient, clusters);
expect(health).toEqual([
{
Expand All @@ -56,4 +55,58 @@ describe('fetchClusterHealth', () => {
},
]);
});
it('should call ES with correct query', async () => {
const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser;
await fetchClusterHealth(esClient, [
{ clusterUuid: '1', clusterName: 'foo1' },
{ clusterUuid: '2', clusterName: 'foo2' },
]);
expect(esClient.search).toHaveBeenCalledWith({
index:
'*:.monitoring-es-*,.monitoring-es-*,*:metrics-elasticsearch.cluster_stats-*,metrics-elasticsearch.cluster_stats-*',
filter_path: [
'hits.hits._source.cluster_state.status',
'hits.hits._source.cluster_uuid',
'hits.hits._index',
],
body: {
size: 2,
sort: [{ timestamp: { order: 'desc', unmapped_type: 'long' } }],
query: {
bool: {
filter: [
{ terms: { cluster_uuid: ['1', '2'] } },
{
bool: {
should: [
{ term: { type: 'cluster_stats' } },
{ term: { 'data_stream.dataset': 'elasticsearch.cluster_stats' } },
],
minimum_should_match: 1,
},
},
{ range: { timestamp: { gte: 'now-2m' } } },
],
},
},
collapse: { field: 'cluster_uuid' },
},
});
});

it('should call ES with correct query when ccs disabled', async () => {
// @ts-ignore
Globals.app.config.ui.ccs.enabled = false;
const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser;
let params = null;
esClient.search.mockImplementation((...args) => {
params = args[0];
return elasticsearchClientMock.createSuccessTransportRequestPromise({} as any);
});

await fetchClusterHealth(esClient, [{ clusterUuid: '1', clusterName: 'foo1' }]);

// @ts-ignore
expect(params.index).toBe('.monitoring-es-*,metrics-elasticsearch.cluster_stats-*');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jest.mock('../../static_globals', () => ({
},
},
}));
import { Globals } from '../../static_globals';

describe('fetchClusters', () => {
const clusterUuid = '1sdfds734';
Expand Down Expand Up @@ -81,4 +82,51 @@ describe('fetchClusters', () => {
const params = esClient.search.mock.calls[0][0] as any;
expect(params?.body?.query.bool.filter[1].range.timestamp.gte).toBe('now-2m');
});

it('should call ES with correct query', async () => {
const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser;
await fetchClusters(esClient);
expect(esClient.search).toHaveBeenCalledWith({
index:
'*:.monitoring-es-*,.monitoring-es-*,*:metrics-elasticsearch.cluster_stats-*,metrics-elasticsearch.cluster_stats-*',
filter_path: [
'hits.hits._source.cluster_settings.cluster.metadata.display_name',
'hits.hits._source.cluster_uuid',
'hits.hits._source.cluster_name',
],
body: {
size: 1000,
query: {
bool: {
filter: [
{
bool: {
should: [
{ term: { type: 'cluster_stats' } },
{ term: { 'data_stream.dataset': 'elasticsearch.cluster_stats' } },
],
minimum_should_match: 1,
},
},
{ range: { timestamp: { gte: 'now-2m' } } },
],
},
},
collapse: { field: 'cluster_uuid' },
},
});
});
it('should call ES with correct query when ccs disabled', async () => {
// @ts-ignore
Globals.app.config.ui.ccs.enabled = false;
const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser;
let params = null;
esClient.search.mockImplementation((...args) => {
params = args[0];
return elasticsearchClientMock.createSuccessTransportRequestPromise({} as any);
});
await fetchClusters(esClient);
// @ts-ignore
expect(params.index).toBe('.monitoring-es-*,metrics-elasticsearch.cluster_stats-*');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function fetchClusters(
const indexPatterns = getNewIndexPatterns({
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'cluster_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
});
const params = {
Expand Down
Loading