mirror of https://github.com/grafana/grafana
prometheushacktoberfestmetricsmonitoringalertinggrafanagoinfluxdbmysqlpostgresanalyticsdata-visualizationdashboardbusiness-intelligenceelasticsearch
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
360 lines
9.6 KiB
360 lines
9.6 KiB
![]()
8 years ago
|
import _ from 'lodash';
|
||
|
import moment from 'moment';
|
||
|
import angular from 'angular';
|
||
|
import { ElasticDatasource } from '../datasource';
|
||
![]()
7 years ago
|
|
||
|
import * as dateMath from 'app/core/utils/datemath';
|
||
![]()
8 years ago
|
|
||
|
describe('ElasticDatasource', function() {
|
||
![]()
7 years ago
|
let backendSrv = {
|
||
![]()
7 years ago
|
datasourceRequest: jest.fn(),
|
||
![]()
7 years ago
|
};
|
||
|
|
||
|
let $rootScope = {
|
||
|
$on: jest.fn(),
|
||
|
appEvent: jest.fn(),
|
||
|
};
|
||
|
|
||
![]()
7 years ago
|
let templateSrv = {
|
||
|
replace: jest.fn(text => text),
|
||
|
getAdhocFilters: jest.fn(() => []),
|
||
|
};
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
let timeSrv = {
|
||
|
time: { from: 'now-1h', to: 'now' },
|
||
![]()
7 years ago
|
timeRange: jest.fn(() => {
|
||
![]()
7 years ago
|
return {
|
||
|
from: dateMath.parse(this.time.from, false),
|
||
|
to: dateMath.parse(this.time.to, true),
|
||
|
};
|
||
|
}),
|
||
|
setTime: jest.fn(time => {
|
||
|
this.time = time;
|
||
|
}),
|
||
|
};
|
||
![]()
7 years ago
|
|
||
|
let ctx = <any>{
|
||
|
$rootScope,
|
||
|
backendSrv,
|
||
|
};
|
||
![]()
10 years ago
|
|
||
|
function createDatasource(instanceSettings) {
|
||
|
instanceSettings.jsonData = instanceSettings.jsonData || {};
|
||
![]()
7 years ago
|
ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv);
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
describe('When testing datasource with index pattern', function() {
|
||
![]()
10 years ago
|
beforeEach(function() {
|
||
![]()
8 years ago
|
createDatasource({
|
||
![]()
8 years ago
|
url: 'http://es.com',
|
||
|
index: '[asd-]YYYY.MM.DD',
|
||
|
jsonData: { interval: 'Daily', esVersion: '2' },
|
||
![]()
8 years ago
|
});
|
||
![]()
10 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should translate index pattern to current day', function() {
|
||
![]()
10 years ago
|
var requestOptions;
|
||
![]()
7 years ago
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
||
![]()
10 years ago
|
requestOptions = options;
|
||
![]()
7 years ago
|
return Promise.resolve({ data: {} });
|
||
|
});
|
||
![]()
10 years ago
|
|
||
|
ctx.ds.testDatasource();
|
||
|
|
||
![]()
8 years ago
|
var today = moment.utc().format('YYYY.MM.DD');
|
||
![]()
7 years ago
|
expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping');
|
||
![]()
10 years ago
|
});
|
||
|
});
|
||
|
|
||
![]()
7 years ago
|
describe('When issuing metric query with interval pattern', function() {
|
||
![]()
10 years ago
|
var requestOptions, parts, header;
|
||
|
|
||
![]()
7 years ago
|
beforeEach(() => {
|
||
![]()
8 years ago
|
createDatasource({
|
||
![]()
8 years ago
|
url: 'http://es.com',
|
||
|
index: '[asd-]YYYY.MM.DD',
|
||
|
jsonData: { interval: 'Daily', esVersion: '2' },
|
||
![]()
8 years ago
|
});
|
||
![]()
10 years ago
|
|
||
![]()
7 years ago
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
||
![]()
10 years ago
|
requestOptions = options;
|
||
![]()
7 years ago
|
return Promise.resolve({ data: { responses: [] } });
|
||
|
});
|
||
![]()
10 years ago
|
|
||
![]()
7 years ago
|
ctx.ds.query({
|
||
![]()
10 years ago
|
range: {
|
||
![]()
9 years ago
|
from: moment.utc([2015, 4, 30, 10]),
|
||
![]()
8 years ago
|
to: moment.utc([2015, 5, 1, 10]),
|
||
![]()
10 years ago
|
},
|
||
![]()
8 years ago
|
targets: [
|
||
|
{
|
||
|
bucketAggs: [],
|
||
![]()
8 years ago
|
metrics: [{ type: 'raw_document' }],
|
||
|
query: 'escape\\:test',
|
||
|
},
|
||
|
],
|
||
![]()
10 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
parts = requestOptions.data.split('\n');
|
||
![]()
10 years ago
|
header = angular.fromJson(parts[0]);
|
||
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should translate index pattern to current day', function() {
|
||
![]()
7 years ago
|
expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
|
||
![]()
10 years ago
|
});
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
it('should json escape lucene query', function() {
|
||
![]()
10 years ago
|
var body = angular.fromJson(parts[1]);
|
||
![]()
7 years ago
|
expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');
|
||
![]()
10 years ago
|
});
|
||
![]()
10 years ago
|
});
|
||
![]()
10 years ago
|
|
||
![]()
7 years ago
|
describe('When issuing document query', function() {
|
||
![]()
10 years ago
|
var requestOptions, parts, header;
|
||
|
|
||
|
beforeEach(function() {
|
||
![]()
8 years ago
|
createDatasource({
|
||
![]()
8 years ago
|
url: 'http://es.com',
|
||
|
index: 'test',
|
||
|
jsonData: { esVersion: '2' },
|
||
![]()
8 years ago
|
});
|
||
![]()
10 years ago
|
|
||
![]()
7 years ago
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
||
![]()
10 years ago
|
requestOptions = options;
|
||
![]()
7 years ago
|
return Promise.resolve({ data: { responses: [] } });
|
||
|
});
|
||
![]()
10 years ago
|
|
||
|
ctx.ds.query({
|
||
![]()
8 years ago
|
range: {
|
||
|
from: moment([2015, 4, 30, 10]),
|
||
![]()
8 years ago
|
to: moment([2015, 5, 1, 10]),
|
||
![]()
8 years ago
|
},
|
||
|
targets: [
|
||
|
{
|
||
|
bucketAggs: [],
|
||
![]()
8 years ago
|
metrics: [{ type: 'raw_document' }],
|
||
|
query: 'test',
|
||
|
},
|
||
|
],
|
||
![]()
10 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
parts = requestOptions.data.split('\n');
|
||
![]()
10 years ago
|
header = angular.fromJson(parts[0]);
|
||
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should set search type to query_then_fetch', function() {
|
||
![]()
7 years ago
|
expect(header.search_type).toEqual('query_then_fetch');
|
||
![]()
10 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should set size', function() {
|
||
![]()
10 years ago
|
var body = angular.fromJson(parts[1]);
|
||
![]()
7 years ago
|
expect(body.size).toBe(500);
|
||
![]()
10 years ago
|
});
|
||
|
});
|
||
|
|
||
![]()
8 years ago
|
describe('When getting fields', function() {
|
||
![]()
7 years ago
|
beforeEach(() => {
|
||
![]()
8 years ago
|
createDatasource({ url: 'http://es.com', index: 'metricbeat' });
|
||
![]()
9 years ago
|
|
||
![]()
7 years ago
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
||
![]()
7 years ago
|
return Promise.resolve({
|
||
![]()
8 years ago
|
data: {
|
||
|
metricbeat: {
|
||
|
mappings: {
|
||
|
metricsets: {
|
||
|
_all: {},
|
||
|
properties: {
|
||
![]()
8 years ago
|
'@timestamp': { type: 'date' },
|
||
![]()
8 years ago
|
beat: {
|
||
|
properties: {
|
||
|
name: {
|
||
![]()
8 years ago
|
fields: { raw: { type: 'keyword' } },
|
||
|
type: 'string',
|
||
![]()
8 years ago
|
},
|
||
![]()
8 years ago
|
hostname: { type: 'string' },
|
||
|
},
|
||
![]()
8 years ago
|
},
|
||
|
system: {
|
||
|
properties: {
|
||
|
cpu: {
|
||
|
properties: {
|
||
![]()
8 years ago
|
system: { type: 'float' },
|
||
|
user: { type: 'float' },
|
||
|
},
|
||
![]()
8 years ago
|
},
|
||
|
process: {
|
||
|
properties: {
|
||
|
cpu: {
|
||
|
properties: {
|
||
![]()
8 years ago
|
total: { type: 'float' },
|
||
|
},
|
||
![]()
8 years ago
|
},
|
||
![]()
8 years ago
|
name: { type: 'string' },
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
![]()
8 years ago
|
});
|
||
![]()
7 years ago
|
});
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should return nested fields', function() {
|
||
![]()
8 years ago
|
ctx.ds
|
||
|
.getFields({
|
||
![]()
8 years ago
|
find: 'fields',
|
||
|
query: '*',
|
||
![]()
8 years ago
|
})
|
||
|
.then(fieldObjects => {
|
||
![]()
8 years ago
|
var fields = _.map(fieldObjects, 'text');
|
||
![]()
7 years ago
|
expect(fields).toEqual([
|
||
![]()
8 years ago
|
'@timestamp',
|
||
|
'beat.name.raw',
|
||
|
'beat.name',
|
||
|
'beat.hostname',
|
||
|
'system.cpu.system',
|
||
|
'system.cpu.user',
|
||
|
'system.process.cpu.total',
|
||
|
'system.process.name',
|
||
![]()
8 years ago
|
]);
|
||
|
});
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should return fields related to query type', function() {
|
||
![]()
8 years ago
|
ctx.ds
|
||
|
.getFields({
|
||
![]()
8 years ago
|
find: 'fields',
|
||
|
query: '*',
|
||
|
type: 'number',
|
||
![]()
8 years ago
|
})
|
||
|
.then(fieldObjects => {
|
||
![]()
8 years ago
|
var fields = _.map(fieldObjects, 'text');
|
||
![]()
7 years ago
|
expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']);
|
||
![]()
8 years ago
|
});
|
||
![]()
9 years ago
|
|
||
![]()
8 years ago
|
ctx.ds
|
||
|
.getFields({
|
||
![]()
8 years ago
|
find: 'fields',
|
||
|
query: '*',
|
||
|
type: 'date',
|
||
![]()
8 years ago
|
})
|
||
|
.then(fieldObjects => {
|
||
![]()
8 years ago
|
var fields = _.map(fieldObjects, 'text');
|
||
![]()
7 years ago
|
expect(fields).toEqual(['@timestamp']);
|
||
![]()
8 years ago
|
});
|
||
![]()
9 years ago
|
});
|
||
|
});
|
||
![]()
9 years ago
|
|
||
![]()
8 years ago
|
describe('When issuing aggregation query on es5.x', function() {
|
||
![]()
9 years ago
|
var requestOptions, parts, header;
|
||
|
|
||
|
beforeEach(function() {
|
||
![]()
8 years ago
|
createDatasource({
|
||
![]()
8 years ago
|
url: 'http://es.com',
|
||
|
index: 'test',
|
||
|
jsonData: { esVersion: '5' },
|
||
![]()
8 years ago
|
});
|
||
![]()
9 years ago
|
|
||
![]()
7 years ago
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
||
![]()
9 years ago
|
requestOptions = options;
|
||
![]()
7 years ago
|
return Promise.resolve({ data: { responses: [] } });
|
||
|
});
|
||
![]()
9 years ago
|
|
||
|
ctx.ds.query({
|
||
![]()
8 years ago
|
range: {
|
||
|
from: moment([2015, 4, 30, 10]),
|
||
![]()
8 years ago
|
to: moment([2015, 5, 1, 10]),
|
||
![]()
8 years ago
|
},
|
||
|
targets: [
|
||
|
{
|
||
![]()
8 years ago
|
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
|
||
![]()
8 years ago
|
metrics: [{ type: 'count' }],
|
||
|
query: 'test',
|
||
|
},
|
||
|
],
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
parts = requestOptions.data.split('\n');
|
||
![]()
9 years ago
|
header = angular.fromJson(parts[0]);
|
||
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should not set search type to count', function() {
|
||
![]()
7 years ago
|
expect(header.search_type).not.toEqual('count');
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
8 years ago
|
it('should set size to 0', function() {
|
||
![]()
9 years ago
|
var body = angular.fromJson(parts[1]);
|
||
![]()
7 years ago
|
expect(body.size).toBe(0);
|
||
![]()
9 years ago
|
});
|
||
|
});
|
||
|
|
||
![]()
8 years ago
|
describe('When issuing metricFind query on es5.x', function() {
|
||
![]()
8 years ago
|
var requestOptions, parts, header, body, results;
|
||
![]()
9 years ago
|
|
||
![]()
7 years ago
|
beforeEach(() => {
|
||
![]()
8 years ago
|
createDatasource({
|
||
![]()
8 years ago
|
url: 'http://es.com',
|
||
|
index: 'test',
|
||
|
jsonData: { esVersion: '5' },
|
||
![]()
8 years ago
|
});
|
||
![]()
9 years ago
|
|
||
![]()
7 years ago
|
ctx.backendSrv.datasourceRequest = jest.fn(options => {
|
||
![]()
9 years ago
|
requestOptions = options;
|
||
![]()
7 years ago
|
return Promise.resolve({
|
||
![]()
8 years ago
|
data: {
|
||
|
responses: [
|
||
|
{
|
||
|
aggregations: {
|
||
![]()
8 years ago
|
'1': {
|
||
![]()
8 years ago
|
buckets: [
|
||
![]()
8 years ago
|
{ doc_count: 1, key: 'test' },
|
||
![]()
8 years ago
|
{
|
||
|
doc_count: 2,
|
||
![]()
8 years ago
|
key: 'test2',
|
||
|
key_as_string: 'test2_as_string',
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
},
|
||
![]()
9 years ago
|
});
|
||
![]()
7 years ago
|
});
|
||
![]()
9 years ago
|
|
||
![]()
8 years ago
|
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => {
|
||
|
results = res;
|
||
|
});
|
||
|
|
||
![]()
8 years ago
|
parts = requestOptions.data.split('\n');
|
||
![]()
9 years ago
|
header = angular.fromJson(parts[0]);
|
||
![]()
9 years ago
|
body = angular.fromJson(parts[1]);
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
7 years ago
|
it('should get results', () => {
|
||
![]()
7 years ago
|
expect(results.length).toEqual(2);
|
||
![]()
8 years ago
|
});
|
||
|
|
||
![]()
7 years ago
|
it('should use key or key_as_string', () => {
|
||
![]()
7 years ago
|
expect(results[0].text).toEqual('test');
|
||
|
expect(results[1].text).toEqual('test2_as_string');
|
||
![]()
8 years ago
|
});
|
||
|
|
||
![]()
7 years ago
|
it('should not set search type to count', () => {
|
||
![]()
7 years ago
|
expect(header.search_type).not.toEqual('count');
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
7 years ago
|
it('should set size to 0', () => {
|
||
![]()
7 years ago
|
expect(body.size).toBe(0);
|
||
![]()
9 years ago
|
});
|
||
|
|
||
![]()
7 years ago
|
it('should not set terms aggregation size to 0', () => {
|
||
![]()
7 years ago
|
expect(body['aggs']['1']['terms'].size).not.toBe(0);
|
||
![]()
9 years ago
|
});
|
||
![]()
9 years ago
|
});
|
||
![]()
10 years ago
|
});
|