kibana/x-pack/plugins/monitoring/server/lib/create_query.test.ts
Sandra G eb17b10203
[Stack Monitoring] compatibility for agent data streams (#119112)
* update queries for elasticsearch package

* fix unit test

* add gitCcs helper function

* modify rest of es queries

* update logstash and kibana queries to use new createQuery

* change beats and apm to use new createQuery

* update changeQuery and remove old one

* make getIndexPattern take request to check for ccs

* fix unit test

* fix unit tests

* update queries and createQuery

* don't add metric constant without dataset in query

* fix types

* fix type

* comment out mb tests

* fix unit test

* fix unit test

* fix

* fix function param

* change to getMetrics name

* change to node_stats

* comment out metricbeat tests

* fix types

* improve types and readability for test

* remove passing of data stream type for now

* add tests for createQuery changes

* update getNewIndexPatterns to take one dataset

* add unit test for getNewIndexPatterns

* fix types

* remove metrics from filter, update tests

* update createNewIndexPatterns to accept new config instead of legacy

* update alert queries to include datas stream index patterns

* update comment

* fix defaulting ccs to * for non cluster requests

* update elasticsearch enterprise module

* update unit test

* remove data_stream.type from queries

* change entsearch to metricbeat module name enterprisesearch

* undo ccs cluster stats change

* fix import

* update alert queries

* fix unit test

* update unit test

* change shard size query to use filter

* change must to filter fix

* update findSupportedBasicLicenseCluster index pattern

* add ccs param to cluster request functions

* update queries for ccs in get_clusters_from_request

* update getBeatsForClusters query

* update clusters apm query

* update enterprisesearch query

* move index pattern to query in fetch for alerts, fix ccs

* remove metricbeat config from alert tests

* fix ts

* add metricset.name back to queries

* comment tests back in

* remove enterprise search checking for standalone cluster to fix test

* update es index metricset name from index_stats to index for mb data

* fix type

* fetchClusters creates index pattern

* fix type

* remove monitoring.ui.metricbeat.index from config and usage in getCollectionStatus

* fix type

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2022-01-20 17:13:23 -05:00

231 lines
6.4 KiB
TypeScript

/*
* 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 { MissingRequiredError } from './error_missing_required';
import { ElasticsearchMetric } from './metrics';
import { createQuery } from './create_query';
interface Metric {
uuidField?: string;
timestampField: string;
}
let metric: Metric;
describe('Create Query', () => {
beforeEach(() => {
metric = ElasticsearchMetric.getMetricFields();
});
it('Allows UUID to not be passed', () => {
const options = { metric, clusterUuid: 'cuid123' };
expect(createQuery(options)).toEqual({
bool: { filter: [{ term: { cluster_uuid: 'cuid123' } }] },
});
});
it('Uses Elasticsearch timestamp field for start and end time range by default', () => {
const options = {
clusterUuid: 'cuid123',
uuid: 'abc123',
start: 1456826400000,
end: 14568264010000,
metric,
};
expect(createQuery(options)).toEqual({
bool: {
filter: [
{ term: { cluster_uuid: 'cuid123' } },
{ term: { 'source_node.uuid': 'abc123' } },
{
range: {
timestamp: { format: 'epoch_millis', gte: 1456826400000, lte: 14568264010000 },
},
},
],
},
});
});
it('Injects uuid and timestamp fields dynamically, based on metric', () => {
const options = {
clusterUuid: 'cuid123',
uuid: 'abc123',
start: 1456826400000,
end: 14568264010000,
metric: {
uuidField: 'testUuidField',
timestampField: 'testTimestampField',
},
};
expect(createQuery(options)).toEqual({
bool: {
filter: [
{ term: { cluster_uuid: 'cuid123' } },
{ term: { testUuidField: 'abc123' } },
{
range: {
testTimestampField: {
format: 'epoch_millis',
gte: 1456826400000,
lte: 14568264010000,
},
},
},
],
},
});
});
it('Throws if missing metric.timestampField', () => {
function callCreateQuery() {
const options = { clusterUuid: 'cuid123' }; // missing metric object
return createQuery(options);
}
expect(callCreateQuery).toThrowError(MissingRequiredError);
});
it('Throws if given uuid but missing metric.uuidField', () => {
function callCreateQuery() {
const options = { uuid: 'abc123', clusterUuid: 'cuid123', metric };
delete options.metric.uuidField;
return createQuery(options);
}
expect(callCreateQuery).toThrowError(MissingRequiredError);
});
it('Uses `type` option to add type filter with minimal fields', () => {
const options = { type: 'cluster_stats', clusterUuid: 'cuid123', metric };
expect(createQuery(options)).toEqual({
bool: {
filter: [
{ bool: { should: [{ term: { type: 'cluster_stats' } }] } },
{ term: { cluster_uuid: 'cuid123' } },
],
},
});
});
it('Uses `type` option to add type filter with all other option fields and no data stream fields', () => {
const options = {
type: 'cluster_stats',
clusterUuid: 'cuid123',
uuid: 'abc123',
start: 1456826400000,
end: 14568264000000,
metric,
};
expect(createQuery(options)).toEqual({
bool: {
filter: [
{ bool: { should: [{ term: { type: 'cluster_stats' } }] } },
{ term: { cluster_uuid: 'cuid123' } },
{ term: { 'source_node.uuid': 'abc123' } },
{
range: {
timestamp: { format: 'epoch_millis', gte: 1456826400000, lte: 14568264000000 },
},
},
],
},
});
});
it('Uses `dsType` option to add filter with all other option fields', () => {
const options = {
dsDataset: 'elasticsearch.cluster_stats',
clusterUuid: 'cuid123',
uuid: 'abc123',
start: 1456826400000,
end: 14568264000000,
metric,
};
expect(createQuery(options)).toEqual({
bool: {
filter: [
{
bool: {
should: [{ term: { 'data_stream.dataset': 'elasticsearch.cluster_stats' } }],
},
},
{ term: { cluster_uuid: 'cuid123' } },
{ term: { 'source_node.uuid': 'abc123' } },
{
range: {
timestamp: { format: 'epoch_millis', gte: 1456826400000, lte: 14568264000000 },
},
},
],
},
});
});
it('Uses legacy `type`, `dsDataset`, `metricset` options to add type filters and data stream filters with minimal fields that defaults to `metrics` data_stream', () => {
const options = {
type: 'cluster_stats',
metricset: 'cluster_stats',
dsDataset: 'elasticsearch.cluster_stats',
clusterUuid: 'cuid123',
metric,
};
expect(createQuery(options)).toEqual({
bool: {
filter: [
{
bool: {
should: [
{
term: {
'data_stream.dataset': 'elasticsearch.cluster_stats',
},
},
{
term: {
'metricset.name': 'cluster_stats',
},
},
{ term: { type: 'cluster_stats' } },
],
},
},
{ term: { cluster_uuid: 'cuid123' } },
],
},
});
});
it('Uses legacy `type`, `metricset`, `dsDataset`, and `filters` options', () => {
const options = {
type: 'cluster_stats',
metricset: 'cluster_stats',
dsDataset: 'elasticsearch.cluster_stats',
clusterUuid: 'cuid123',
metric,
filters: [
{
term: { 'source_node.uuid': `nuid123` },
},
],
};
expect(createQuery(options)).toEqual({
bool: {
filter: [
{
bool: {
should: [
{ term: { 'data_stream.dataset': 'elasticsearch.cluster_stats' } },
{ term: { 'metricset.name': 'cluster_stats' } },
{ term: { type: 'cluster_stats' } },
],
},
},
{ term: { cluster_uuid: 'cuid123' } },
{ term: { 'source_node.uuid': 'nuid123' } },
],
},
});
});
});