grafana/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts

345 lines
9.3 KiB
TypeScript
Raw Normal View History

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,
};
function createDatasource(instanceSettings) {
instanceSettings.jsonData = instanceSettings.jsonData || {};
2018-07-03 03:47:50 -05:00
ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv);
}
2017-12-20 05:33:33 -06:00
describe('When testing datasource with index pattern', function() {
beforeEach(function() {
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-20 05:33:33 -06:00
it('should translate index pattern to current day', function() {
var requestOptions;
2018-07-03 03:47:50 -05:00
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
2018-07-03 03:12:07 -05:00
return Promise.resolve({ data: {} });
});
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');
});
});
2018-04-13 12:48:37 -05:00
describe('When issuing metric query with interval pattern', function() {
var requestOptions, parts, header;
2018-07-03 03:55:48 -05:00
beforeEach(() => {
createDatasource({
2017-12-20 05:33:33 -06:00
url: 'http://es.com',
index: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily', esVersion: '2' },
});
2018-07-03 03:47:50 -05:00
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
2018-07-03 03:12:07 -05:00
return Promise.resolve({ data: { responses: [] } });
});
2018-07-03 03:55:48 -05:00
ctx.ds.query({
range: {
from: moment.utc([2015, 4, 30, 10]),
2017-12-20 05:33:33 -06:00
to: moment.utc([2015, 5, 1, 10]),
},
targets: [
{
bucketAggs: [],
2017-12-20 05:33:33 -06:00
metrics: [{ type: 'raw_document' }],
query: 'escape\\:test',
},
],
});
2017-12-20 05:33:33 -06:00
parts = requestOptions.data.split('\n');
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']);
});
2017-12-20 05:33:33 -06:00
it('should json escape lucene query', function() {
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');
});
});
2018-04-13 12:48:37 -05:00
describe('When issuing document query', function() {
var requestOptions, parts, header;
beforeEach(function() {
createDatasource({
2017-12-20 05:33:33 -06:00
url: 'http://es.com',
index: 'test',
jsonData: { esVersion: '2' },
});
2018-07-03 03:47:50 -05:00
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
2018-07-03 03:12:07 -05:00
return Promise.resolve({ data: { responses: [] } });
});
ctx.ds.query({
range: {
from: moment([2015, 4, 30, 10]),
2017-12-20 05:33:33 -06:00
to: moment([2015, 5, 1, 10]),
},
targets: [
{
bucketAggs: [],
2017-12-20 05:33:33 -06:00
metrics: [{ type: 'raw_document' }],
query: 'test',
},
],
});
2017-12-20 05:33:33 -06:00
parts = requestOptions.data.split('\n');
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');
});
2017-12-20 05:33:33 -06:00
it('should set size', function() {
var body = angular.fromJson(parts[1]);
2018-07-03 03:12:07 -05:00
expect(body.size).toBe(500);
});
});
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' });
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({
data: {
metricbeat: {
mappings: {
metricsets: {
_all: {},
properties: {
2017-12-20 05:33:33 -06:00
'@timestamp': { type: 'date' },
beat: {
properties: {
name: {
2017-12-20 05:33:33 -06:00
fields: { raw: { type: 'keyword' } },
type: 'string',
},
2017-12-20 05:33:33 -06:00
hostname: { type: 'string' },
},
},
system: {
properties: {
cpu: {
properties: {
2017-12-20 05:33:33 -06:00
system: { type: 'float' },
user: { type: 'float' },
},
},
process: {
properties: {
cpu: {
properties: {
2017-12-20 05:33:33 -06:00
total: { type: 'float' },
},
},
2017-12-20 05:33:33 -06:00
name: { type: 'string' },
},
},
},
},
},
},
},
},
},
});
2018-07-03 03:47:50 -05:00
});
});
2017-12-20 05:33:33 -06:00
it('should return nested fields', function() {
ctx.ds
.getFields({
2017-12-20 05:33:33 -06:00
find: 'fields',
query: '*',
})
.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-20 05:33:33 -06:00
it('should return fields related to query type', function() {
ctx.ds
.getFields({
2017-12-20 05:33:33 -06:00
find: 'fields',
query: '*',
type: 'number',
})
.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']);
});
ctx.ds
.getFields({
2017-12-20 05:33:33 -06:00
find: 'fields',
query: '*',
type: 'date',
})
.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-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() {
createDatasource({
2017-12-20 05:33:33 -06:00
url: 'http://es.com',
index: 'test',
jsonData: { esVersion: '5' },
});
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({
range: {
from: moment([2015, 4, 30, 10]),
2017-12-20 05:33:33 -06:00
to: moment([2015, 5, 1, 10]),
},
targets: [
{
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() {
var requestOptions, parts, header, body, results;
2016-06-19 17:40:16 -05:00
2018-07-03 03:47:50 -05:00
beforeEach(() => {
createDatasource({
2017-12-20 05:33:33 -06:00
url: 'http://es.com',
index: 'test',
jsonData: { esVersion: '5' },
});
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: [
{
aggregations: {
2017-12-20 05:33:33 -06:00
'1': {
buckets: [
2017-12-20 05:33:33 -06:00
{ doc_count: 1, key: 'test' },
{
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
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]);
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);
});
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');
});
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-06-19 17:40:16 -05:00
});
});