2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import moment from 'moment';
|
|
|
|
import angular from 'angular';
|
|
|
|
import { ElasticDatasource } from '../datasource';
|
2018-07-03 06:33:30 -05:00
|
|
|
import { jestTimeSrvStub, TemplateSrvStub } from 'test/specs/helpers';
|
2017-12-20 05:33:33 -06:00
|
|
|
|
|
|
|
describe('ElasticDatasource', function() {
|
2018-07-03 03:12:07 -05:00
|
|
|
let backendSrv = {
|
2018-07-03 03:47:50 -05:00
|
|
|
datasourceRequest: jest.fn(),
|
2018-07-03 03:12:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let $rootScope = {
|
|
|
|
$on: jest.fn(),
|
|
|
|
appEvent: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2018-07-03 06:33:30 -05:00
|
|
|
let templateSrv = new TemplateSrvStub();
|
2018-07-03 03:47:50 -05:00
|
|
|
|
2018-07-03 04:55:23 -05:00
|
|
|
let timeSrv = new jestTimeSrvStub();
|
2018-07-03 03:12:07 -05:00
|
|
|
|
|
|
|
let ctx = <any>{
|
|
|
|
$rootScope,
|
|
|
|
backendSrv,
|
|
|
|
};
|
2016-01-09 06:21:16 -06:00
|
|
|
|
|
|
|
function createDatasource(instanceSettings) {
|
|
|
|
instanceSettings.jsonData = instanceSettings.jsonData || {};
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv);
|
2016-01-09 06:21:16 -06:00
|
|
|
}
|
2015-09-28 09:28:19 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('When testing datasource with index pattern', function() {
|
2015-09-28 09:28:19 -05:00
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
2017-12-20 05:33:33 -06:00
|
|
|
url: 'http://es.com',
|
|
|
|
index: '[asd-]YYYY.MM.DD',
|
|
|
|
jsonData: { interval: 'Daily', esVersion: '2' },
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should translate index pattern to current day', function() {
|
2015-09-28 09:28:19 -05:00
|
|
|
var requestOptions;
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
2015-09-28 09:28:19 -05:00
|
|
|
requestOptions = options;
|
2018-07-03 03:12:07 -05:00
|
|
|
return Promise.resolve({ data: {} });
|
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
|
|
|
|
ctx.ds.testDatasource();
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
var today = moment.utc().format('YYYY.MM.DD');
|
2018-07-03 03:47:50 -05:00
|
|
|
expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping');
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-13 12:48:37 -05:00
|
|
|
describe('When issuing metric query with interval pattern', function() {
|
2015-10-22 15:23:21 -05:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
2018-07-03 03:55:48 -05:00
|
|
|
beforeEach(() => {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
2017-12-20 05:33:33 -06:00
|
|
|
url: 'http://es.com',
|
|
|
|
index: '[asd-]YYYY.MM.DD',
|
|
|
|
jsonData: { interval: 'Daily', esVersion: '2' },
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
2015-09-28 09:28:19 -05:00
|
|
|
requestOptions = options;
|
2018-07-03 03:12:07 -05:00
|
|
|
return Promise.resolve({ data: { responses: [] } });
|
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
|
2018-07-03 03:55:48 -05:00
|
|
|
ctx.ds.query({
|
2015-09-28 09:28:19 -05:00
|
|
|
range: {
|
2016-08-07 05:01:53 -05:00
|
|
|
from: moment.utc([2015, 4, 30, 10]),
|
2017-12-20 05:33:33 -06:00
|
|
|
to: moment.utc([2015, 5, 1, 10]),
|
2015-09-28 09:28:19 -05:00
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
bucketAggs: [],
|
2017-12-20 05:33:33 -06:00
|
|
|
metrics: [{ type: 'raw_document' }],
|
|
|
|
query: 'escape\\:test',
|
|
|
|
},
|
|
|
|
],
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
parts = requestOptions.data.split('\n');
|
2015-10-22 15:23:21 -05:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should translate index pattern to current day', function() {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
2015-10-22 15:23:21 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should json escape lucene query', function() {
|
2015-10-22 15:23:21 -05:00
|
|
|
var body = angular.fromJson(parts[1]);
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');
|
2015-10-22 15:23:21 -05:00
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
2015-11-05 01:36:51 -06:00
|
|
|
|
2018-04-13 12:48:37 -05:00
|
|
|
describe('When issuing document query', function() {
|
2015-11-05 01:36:51 -06:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
2017-12-20 05:33:33 -06:00
|
|
|
url: 'http://es.com',
|
|
|
|
index: 'test',
|
|
|
|
jsonData: { esVersion: '2' },
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2015-11-05 01:36:51 -06:00
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
2015-11-05 01:36:51 -06:00
|
|
|
requestOptions = options;
|
2018-07-03 03:12:07 -05:00
|
|
|
return Promise.resolve({ data: { responses: [] } });
|
|
|
|
});
|
2015-11-05 01:36:51 -06:00
|
|
|
|
|
|
|
ctx.ds.query({
|
2017-12-19 09:06:54 -06:00
|
|
|
range: {
|
|
|
|
from: moment([2015, 4, 30, 10]),
|
2017-12-20 05:33:33 -06:00
|
|
|
to: moment([2015, 5, 1, 10]),
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
bucketAggs: [],
|
2017-12-20 05:33:33 -06:00
|
|
|
metrics: [{ type: 'raw_document' }],
|
|
|
|
query: 'test',
|
|
|
|
},
|
|
|
|
],
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
parts = requestOptions.data.split('\n');
|
2015-11-05 01:36:51 -06:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should set search type to query_then_fetch', function() {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(header.search_type).toEqual('query_then_fetch');
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should set size', function() {
|
2015-11-05 01:36:51 -06:00
|
|
|
var body = angular.fromJson(parts[1]);
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(body.size).toBe(500);
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('When getting fields', function() {
|
2018-07-03 03:47:50 -05:00
|
|
|
beforeEach(() => {
|
2017-12-20 05:33:33 -06:00
|
|
|
createDatasource({ url: 'http://es.com', index: 'metricbeat' });
|
2016-09-15 00:30:08 -05:00
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
2018-07-03 03:12:07 -05:00
|
|
|
return Promise.resolve({
|
2017-12-19 09:06:54 -06:00
|
|
|
data: {
|
|
|
|
metricbeat: {
|
|
|
|
mappings: {
|
|
|
|
metricsets: {
|
|
|
|
_all: {},
|
|
|
|
properties: {
|
2017-12-20 05:33:33 -06:00
|
|
|
'@timestamp': { type: 'date' },
|
2017-12-19 09:06:54 -06:00
|
|
|
beat: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2017-12-20 05:33:33 -06:00
|
|
|
fields: { raw: { type: 'keyword' } },
|
|
|
|
type: 'string',
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
hostname: { type: 'string' },
|
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
|
|
|
system: {
|
|
|
|
properties: {
|
|
|
|
cpu: {
|
|
|
|
properties: {
|
2017-12-20 05:33:33 -06:00
|
|
|
system: { type: 'float' },
|
|
|
|
user: { type: 'float' },
|
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
|
|
|
process: {
|
|
|
|
properties: {
|
|
|
|
cpu: {
|
|
|
|
properties: {
|
2017-12-20 05:33:33 -06:00
|
|
|
total: { type: 'float' },
|
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
name: { type: 'string' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2018-07-03 03:47:50 -05:00
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should return nested fields', function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds
|
|
|
|
.getFields({
|
2017-12-20 05:33:33 -06:00
|
|
|
find: 'fields',
|
|
|
|
query: '*',
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
|
|
|
.then(fieldObjects => {
|
2017-12-20 05:33:33 -06:00
|
|
|
var fields = _.map(fieldObjects, 'text');
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(fields).toEqual([
|
2017-12-20 05:33:33 -06:00
|
|
|
'@timestamp',
|
|
|
|
'beat.name.raw',
|
|
|
|
'beat.name',
|
|
|
|
'beat.hostname',
|
|
|
|
'system.cpu.system',
|
|
|
|
'system.cpu.user',
|
|
|
|
'system.process.cpu.total',
|
|
|
|
'system.process.name',
|
2017-12-19 09:06:54 -06:00
|
|
|
]);
|
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should return fields related to query type', function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds
|
|
|
|
.getFields({
|
2017-12-20 05:33:33 -06:00
|
|
|
find: 'fields',
|
|
|
|
query: '*',
|
|
|
|
type: 'number',
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
|
|
|
.then(fieldObjects => {
|
2017-12-20 05:33:33 -06:00
|
|
|
var fields = _.map(fieldObjects, 'text');
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']);
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds
|
|
|
|
.getFields({
|
2017-12-20 05:33:33 -06:00
|
|
|
find: 'fields',
|
|
|
|
query: '*',
|
|
|
|
type: 'date',
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
|
|
|
.then(fieldObjects => {
|
2017-12-20 05:33:33 -06:00
|
|
|
var fields = _.map(fieldObjects, 'text');
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(fields).toEqual(['@timestamp']);
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
});
|
|
|
|
});
|
2016-09-19 13:10:45 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('When issuing aggregation query on es5.x', function() {
|
2016-06-19 17:40:16 -05:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
2017-12-20 05:33:33 -06:00
|
|
|
url: 'http://es.com',
|
|
|
|
index: 'test',
|
|
|
|
jsonData: { esVersion: '5' },
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
2016-06-19 17:40:16 -05:00
|
|
|
requestOptions = options;
|
2018-07-03 03:47:50 -05:00
|
|
|
return Promise.resolve({ data: { responses: [] } });
|
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
|
|
|
|
ctx.ds.query({
|
2017-12-19 09:06:54 -06:00
|
|
|
range: {
|
|
|
|
from: moment([2015, 4, 30, 10]),
|
2017-12-20 05:33:33 -06:00
|
|
|
to: moment([2015, 5, 1, 10]),
|
2017-12-19 09:06:54 -06:00
|
|
|
},
|
|
|
|
targets: [
|
|
|
|
{
|
2017-12-21 01:39:31 -06:00
|
|
|
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
|
2017-12-20 05:33:33 -06:00
|
|
|
metrics: [{ type: 'count' }],
|
|
|
|
query: 'test',
|
|
|
|
},
|
|
|
|
],
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
parts = requestOptions.data.split('\n');
|
2016-06-19 17:40:16 -05:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should not set search type to count', function() {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(header.search_type).not.toEqual('count');
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should set size to 0', function() {
|
2016-06-19 17:40:16 -05:00
|
|
|
var body = angular.fromJson(parts[1]);
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(body.size).toBe(0);
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('When issuing metricFind query on es5.x', function() {
|
2017-07-12 00:43:12 -05:00
|
|
|
var requestOptions, parts, header, body, results;
|
2016-06-19 17:40:16 -05:00
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
beforeEach(() => {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
2017-12-20 05:33:33 -06:00
|
|
|
url: 'http://es.com',
|
|
|
|
index: 'test',
|
|
|
|
jsonData: { esVersion: '5' },
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
2016-06-19 17:40:16 -05:00
|
|
|
requestOptions = options;
|
2018-07-03 03:47:50 -05:00
|
|
|
return Promise.resolve({
|
2017-07-12 00:43:12 -05:00
|
|
|
data: {
|
|
|
|
responses: [
|
|
|
|
{
|
|
|
|
aggregations: {
|
2017-12-20 05:33:33 -06:00
|
|
|
'1': {
|
2017-07-12 00:43:12 -05:00
|
|
|
buckets: [
|
2017-12-20 05:33:33 -06:00
|
|
|
{ doc_count: 1, key: 'test' },
|
2017-12-19 09:06:54 -06:00
|
|
|
{
|
|
|
|
doc_count: 2,
|
2017-12-20 05:33:33 -06:00
|
|
|
key: 'test2',
|
|
|
|
key_as_string: 'test2_as_string',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
2018-07-03 03:47:50 -05:00
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
|
2017-07-12 00:43:12 -05:00
|
|
|
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => {
|
|
|
|
results = res;
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
parts = requestOptions.data.split('\n');
|
2016-06-19 17:40:16 -05:00
|
|
|
header = angular.fromJson(parts[0]);
|
2016-07-26 15:05:49 -05:00
|
|
|
body = angular.fromJson(parts[1]);
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
it('should get results', () => {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(results.length).toEqual(2);
|
2017-07-12 00:43:12 -05:00
|
|
|
});
|
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
it('should use key or key_as_string', () => {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(results[0].text).toEqual('test');
|
|
|
|
expect(results[1].text).toEqual('test2_as_string');
|
2017-07-12 00:43:12 -05:00
|
|
|
});
|
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
it('should not set search type to count', () => {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(header.search_type).not.toEqual('count');
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
it('should set size to 0', () => {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(body.size).toBe(0);
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2018-07-03 03:47:50 -05:00
|
|
|
it('should not set terms aggregation size to 0', () => {
|
2018-07-03 03:12:07 -05:00
|
|
|
expect(body['aggs']['1']['terms'].size).not.toBe(0);
|
2016-10-02 09:59:25 -05:00
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|