2016-09-15 00:30:08 -05:00
|
|
|
import _ from 'lodash';
|
2015-09-28 09:28:19 -05:00
|
|
|
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
|
2015-12-21 09:00:58 -06:00
|
|
|
import moment from 'moment';
|
|
|
|
import angular from 'angular';
|
|
|
|
import helpers from 'test/specs/helpers';
|
2016-02-03 09:31:07 -06:00
|
|
|
import {ElasticDatasource} from "../datasource";
|
2015-09-28 09:28:19 -05:00
|
|
|
|
|
|
|
describe('ElasticDatasource', function() {
|
|
|
|
var ctx = new helpers.ServiceTestContext();
|
2016-01-09 06:21:16 -06:00
|
|
|
var instanceSettings: any = {jsonData: {}};
|
2015-09-28 09:28:19 -05:00
|
|
|
|
2015-10-30 09:58:20 -05:00
|
|
|
beforeEach(angularMocks.module('grafana.core'));
|
2015-09-28 09:28:19 -05:00
|
|
|
beforeEach(angularMocks.module('grafana.services'));
|
2017-01-13 10:37:53 -06:00
|
|
|
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv']));
|
2016-03-28 13:44:32 -05:00
|
|
|
|
2016-01-09 06:21:16 -06:00
|
|
|
beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
|
|
|
|
ctx.$q = $q;
|
|
|
|
ctx.$httpBackend = $httpBackend;
|
|
|
|
ctx.$rootScope = $rootScope;
|
|
|
|
ctx.$injector = $injector;
|
2016-03-28 13:44:32 -05:00
|
|
|
$httpBackend.when('GET', /\.html$/).respond('');
|
2016-01-09 06:21:16 -06:00
|
|
|
}));
|
|
|
|
|
|
|
|
function createDatasource(instanceSettings) {
|
|
|
|
instanceSettings.jsonData = instanceSettings.jsonData || {};
|
2016-02-03 09:31:07 -06:00
|
|
|
ctx.ds = ctx.$injector.instantiate(ElasticDatasource, {instanceSettings: instanceSettings});
|
2016-01-09 06:21:16 -06:00
|
|
|
}
|
2015-09-28 09:28:19 -05:00
|
|
|
|
|
|
|
describe('When testing datasource with index pattern', function() {
|
|
|
|
beforeEach(function() {
|
2016-12-06 07:17:03 -06:00
|
|
|
createDatasource({url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: {interval: 'Daily', esVersion: '2'}});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should translate index pattern to current day', function() {
|
|
|
|
var requestOptions;
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
2016-03-01 03:51:39 -06:00
|
|
|
return ctx.$q.when({data: {}});
|
2015-09-28 09:28:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.testDatasource();
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
2015-10-22 15:58:31 -05:00
|
|
|
var today = moment.utc().format("YYYY.MM.DD");
|
2017-03-29 09:33:44 -05:00
|
|
|
expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_mapping');
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When issueing metric query with interval pattern', function() {
|
2015-10-22 15:23:21 -05:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
2015-09-28 09:28:19 -05:00
|
|
|
beforeEach(function() {
|
2016-12-06 07:17:03 -06:00
|
|
|
createDatasource({url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: {interval: 'Daily', esVersion: '2'}});
|
2015-09-28 09:28:19 -05:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({data: {responses: []}});
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.query({
|
|
|
|
range: {
|
2016-08-07 05:01:53 -05:00
|
|
|
from: moment.utc([2015, 4, 30, 10]),
|
|
|
|
to: moment.utc([2015, 5, 1, 10])
|
2015-09-28 09:28:19 -05:00
|
|
|
},
|
2017-06-08 04:53:12 -05:00
|
|
|
targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'escape\\:test' }]
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
2015-10-22 15:23:21 -05:00
|
|
|
|
|
|
|
parts = requestOptions.data.split('\n');
|
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should translate index pattern to current day', function() {
|
2015-09-28 09:28:19 -05:00
|
|
|
expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
|
|
|
|
});
|
2015-10-22 15:23:21 -05:00
|
|
|
|
|
|
|
it('should json escape lucene query', function() {
|
|
|
|
var body = angular.fromJson(parts[1]);
|
2017-01-21 01:31:29 -06:00
|
|
|
expect(body.query.bool.filter[1].query_string.query).to.be('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
|
|
|
|
|
|
|
describe('When issueing document query', function() {
|
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2016-12-06 07:17:03 -06:00
|
|
|
createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '2'}});
|
2015-11-05 01:36:51 -06:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({data: {responses: []}});
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.query({
|
|
|
|
range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
|
|
|
|
targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'test' }]
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
parts = requestOptions.data.split('\n');
|
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set search type to query_then_fetch', function() {
|
|
|
|
expect(header.search_type).to.eql('query_then_fetch');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set size', function() {
|
|
|
|
var body = angular.fromJson(parts[1]);
|
2015-11-05 02:56:19 -06:00
|
|
|
expect(body.size).to.be(500);
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-15 00:30:08 -05:00
|
|
|
describe('When getting fields', function() {
|
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
createDatasource({url: 'http://es.com', index: 'metricbeat'});
|
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({data: {
|
|
|
|
metricbeat: {
|
|
|
|
mappings: {
|
|
|
|
metricsets: {
|
|
|
|
_all: {},
|
|
|
|
properties: {
|
|
|
|
'@timestamp': {type: 'date'},
|
|
|
|
beat: {
|
|
|
|
properties: {
|
|
|
|
name: {type: 'string'},
|
|
|
|
hostname: {type: 'string'},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
system: {
|
|
|
|
properties: {
|
|
|
|
cpu: {
|
|
|
|
properties: {
|
|
|
|
system: {type: 'float'},
|
|
|
|
user: {type: 'float'},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
process: {
|
|
|
|
properties: {
|
|
|
|
cpu: {
|
|
|
|
properties: {
|
|
|
|
total: {type: 'float'}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
name: {type: 'string'},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return nested fields', function() {
|
|
|
|
ctx.ds.getFields({
|
|
|
|
find: 'fields',
|
|
|
|
query: '*'
|
|
|
|
}).then((fieldObjects) => {
|
|
|
|
var fields = _.map(fieldObjects, 'text');
|
|
|
|
expect(fields).to.eql([
|
|
|
|
'@timestamp',
|
|
|
|
'beat.name',
|
|
|
|
'beat.hostname',
|
|
|
|
'system.cpu.system',
|
|
|
|
'system.cpu.user',
|
|
|
|
'system.process.cpu.total',
|
|
|
|
'system.process.name'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return fields related to query type', function() {
|
|
|
|
ctx.ds.getFields({
|
|
|
|
find: 'fields',
|
|
|
|
query: '*',
|
|
|
|
type: 'number'
|
|
|
|
}).then((fieldObjects) => {
|
|
|
|
var fields = _.map(fieldObjects, 'text');
|
|
|
|
expect(fields).to.eql([
|
|
|
|
'system.cpu.system',
|
|
|
|
'system.cpu.user',
|
|
|
|
'system.process.cpu.total'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.ds.getFields({
|
|
|
|
find: 'fields',
|
|
|
|
query: '*',
|
|
|
|
type: 'date'
|
|
|
|
}).then((fieldObjects) => {
|
|
|
|
var fields = _.map(fieldObjects, 'text');
|
|
|
|
expect(fields).to.eql([
|
|
|
|
'@timestamp'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
});
|
2016-09-19 13:10:45 -05:00
|
|
|
|
2016-10-02 09:59:25 -05:00
|
|
|
describe('When issuing aggregation query on es5.x', function() {
|
2016-06-19 17:40:16 -05:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
|
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({data: {responses: []}});
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.query({
|
|
|
|
range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
|
|
|
|
targets: [{
|
|
|
|
bucketAggs: [
|
|
|
|
{type: 'date_histogram', field: '@timestamp', id: '2'}
|
|
|
|
],
|
|
|
|
metrics: [
|
|
|
|
{type: 'count'}], query: 'test' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
parts = requestOptions.data.split('\n');
|
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set search type to count', function() {
|
|
|
|
expect(header.search_type).to.not.eql('count');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set size to 0', function() {
|
|
|
|
var body = angular.fromJson(parts[1]);
|
|
|
|
expect(body.size).to.be(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When issuing metricFind query on es5.x', function() {
|
2016-07-26 15:05:49 -05:00
|
|
|
var requestOptions, parts, header, body;
|
2016-06-19 17:40:16 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
|
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({
|
|
|
|
data: {
|
|
|
|
responses: [{aggregations: {"1": [{buckets: {text: 'test', value: '1'}}]}}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}');
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
|
|
|
parts = requestOptions.data.split('\n');
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set search type to count', function() {
|
|
|
|
expect(header.search_type).to.not.eql('count');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set size to 0', function() {
|
|
|
|
expect(body.size).to.be(0);
|
|
|
|
});
|
|
|
|
|
2016-07-26 15:05:49 -05:00
|
|
|
it('should not set terms aggregation size to 0', function() {
|
|
|
|
expect(body['aggs']['1']['terms'].size).to.not.be(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
|
|
|
});
|
2016-10-02 09:59:25 -05:00
|
|
|
|