-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stack Monitoring] improve unit tests in fetch functions in alerts (#…
…124033) * add cluster_stats dataset to index pattern of query in fetchClusters * update unit tests to test the params being called with * add node_stats term filter * update test for node_stats filter in missing monitoring fetch rule Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
3bbbbb9
commit 6e7fcfe
Showing
14 changed files
with
1,246 additions
and
47 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-*'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.