2017-12-21 01:39:31 -06:00
|
|
|
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
|
2017-12-20 05:33:33 -06:00
|
|
|
import helpers from 'test/specs/helpers';
|
|
|
|
import { GraphiteDatasource } from '../datasource';
|
|
|
|
import moment from 'moment';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
describe('graphiteDatasource', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let ctx = new helpers.ServiceTestContext();
|
2017-12-20 05:33:33 -06:00
|
|
|
let instanceSettings: any = { url: [''], name: 'graphiteProd', jsonData: {} };
|
2017-12-19 09:06:54 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
beforeEach(angularMocks.module('grafana.core'));
|
|
|
|
beforeEach(angularMocks.module('grafana.services'));
|
|
|
|
beforeEach(ctx.providePhase(['backendSrv', 'templateSrv']));
|
2017-12-19 09:06:54 -06:00
|
|
|
beforeEach(
|
|
|
|
angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
|
|
|
|
ctx.$q = $q;
|
|
|
|
ctx.$httpBackend = $httpBackend;
|
|
|
|
ctx.$rootScope = $rootScope;
|
|
|
|
ctx.$injector = $injector;
|
2017-12-20 05:33:33 -06:00
|
|
|
$httpBackend.when('GET', /\.html$/).respond('');
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
|
|
|
);
|
2015-10-30 09:58:20 -05:00
|
|
|
|
2015-09-28 08:23:53 -05:00
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds = ctx.$injector.instantiate(GraphiteDatasource, {
|
2017-12-20 05:33:33 -06:00
|
|
|
instanceSettings: instanceSettings,
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('When querying graphite with one target using query editor target spec', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let query = {
|
2016-07-23 05:54:11 -05:00
|
|
|
panelId: 3,
|
2017-12-20 05:33:33 -06:00
|
|
|
rangeRaw: { from: 'now-1h', to: 'now' },
|
|
|
|
targets: [{ target: 'prod1.count' }, { target: 'prod2.count' }],
|
|
|
|
maxDataPoints: 500,
|
2015-09-28 08:23:53 -05:00
|
|
|
};
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
let results;
|
|
|
|
let requestOptions;
|
2015-09-28 08:23:53 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
2017-12-19 09:06:54 -06:00
|
|
|
return ctx.$q.when({
|
2017-12-20 05:33:33 -06:00
|
|
|
data: [{ target: 'prod1.count', datapoints: [[10, 1], [12, 1]] }],
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2015-09-28 08:23:53 -05:00
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds.query(query).then(function(data) {
|
|
|
|
results = data;
|
|
|
|
});
|
2015-09-28 08:23:53 -05:00
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should generate the correct query', function() {
|
|
|
|
expect(requestOptions.url).to.be('/render');
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should set unique requestId', function() {
|
|
|
|
expect(requestOptions.requestId).to.be('graphiteProd.panelId.3');
|
2016-07-23 05:54:11 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should query correctly', function() {
|
|
|
|
let params = requestOptions.data.split('&');
|
|
|
|
expect(params).to.contain('target=prod1.count');
|
|
|
|
expect(params).to.contain('target=prod2.count');
|
|
|
|
expect(params).to.contain('from=-1h');
|
|
|
|
expect(params).to.contain('until=now');
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should exclude undefined params', function() {
|
|
|
|
let params = requestOptions.data.split('&');
|
|
|
|
expect(params).to.not.contain('cacheTimeout=undefined');
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should return series list', function() {
|
2015-09-28 08:23:53 -05:00
|
|
|
expect(results.data.length).to.be(1);
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(results.data[0].target).to.be('prod1.count');
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should convert to millisecond resolution', function() {
|
2015-09-28 08:23:53 -05:00
|
|
|
expect(results.data[0].datapoints[0][0]).to.be(10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('when fetching Graphite Events as annotations', () => {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
annotation: {
|
2017-12-20 05:33:33 -06:00
|
|
|
tags: 'tag1',
|
2017-10-07 03:31:39 -05:00
|
|
|
},
|
|
|
|
range: {
|
|
|
|
from: moment(1432288354),
|
2017-12-20 05:33:33 -06:00
|
|
|
to: moment(1432288401),
|
2017-10-07 03:31:39 -05:00
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
rangeRaw: { from: 'now-24h', to: 'now' },
|
2017-10-07 03:31:39 -05:00
|
|
|
};
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('and tags are returned as string', () => {
|
2017-10-07 03:31:39 -05:00
|
|
|
const response = {
|
|
|
|
data: [
|
2017-12-19 09:06:54 -06:00
|
|
|
{
|
|
|
|
when: 1507222850,
|
2017-12-20 05:33:33 -06:00
|
|
|
tags: 'tag1 tag2',
|
|
|
|
data: 'some text',
|
2017-12-19 09:06:54 -06:00
|
|
|
id: 2,
|
2017-12-20 05:33:33 -06:00
|
|
|
what: 'Event - deploy',
|
|
|
|
},
|
|
|
|
],
|
2017-12-19 09:06:54 -06:00
|
|
|
};
|
2017-10-07 03:31:39 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
return ctx.$q.when(response);
|
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds.annotationQuery(options).then(function(data) {
|
|
|
|
results = data;
|
|
|
|
});
|
2017-10-07 03:31:39 -05:00
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should parse the tags string into an array', () => {
|
2017-10-07 03:31:39 -05:00
|
|
|
expect(_.isArray(results[0].tags)).to.eql(true);
|
|
|
|
expect(results[0].tags.length).to.eql(2);
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(results[0].tags[0]).to.eql('tag1');
|
|
|
|
expect(results[0].tags[1]).to.eql('tag2');
|
2017-10-07 03:31:39 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('and tags are returned as an array', () => {
|
2017-10-07 03:31:39 -05:00
|
|
|
const response = {
|
|
|
|
data: [
|
2017-12-19 09:06:54 -06:00
|
|
|
{
|
|
|
|
when: 1507222850,
|
2017-12-20 05:33:33 -06:00
|
|
|
tags: ['tag1', 'tag2'],
|
|
|
|
data: 'some text',
|
2017-12-19 09:06:54 -06:00
|
|
|
id: 2,
|
2017-12-20 05:33:33 -06:00
|
|
|
what: 'Event - deploy',
|
|
|
|
},
|
|
|
|
],
|
2017-12-19 09:06:54 -06:00
|
|
|
};
|
2017-10-07 03:31:39 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
return ctx.$q.when(response);
|
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds.annotationQuery(options).then(function(data) {
|
|
|
|
results = data;
|
|
|
|
});
|
2017-10-07 03:31:39 -05:00
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should parse the tags string into an array', () => {
|
2017-10-07 03:31:39 -05:00
|
|
|
expect(_.isArray(results[0].tags)).to.eql(true);
|
|
|
|
expect(results[0].tags.length).to.eql(2);
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(results[0].tags[0]).to.eql('tag1');
|
|
|
|
expect(results[0].tags[1]).to.eql('tag2');
|
2017-10-07 03:31:39 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
describe('building graphite params', function() {
|
|
|
|
it('should return empty array if no targets', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-20 05:33:33 -06:00
|
|
|
targets: [{}],
|
2015-12-14 11:33:44 -06:00
|
|
|
});
|
|
|
|
expect(results.length).to.be(0);
|
|
|
|
});
|
2015-09-28 08:23:53 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should uri escape targets', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-20 05:33:33 -06:00
|
|
|
targets: [{ target: 'prod1.{test,test2}' }, { target: 'prod2.count' }],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should replace target placeholder', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-21 01:39:31 -06:00
|
|
|
targets: [{ target: 'series1' }, { target: 'series2' }, { target: 'asPercent(#A,#B)' }],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should replace target placeholder for hidden series', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-19 09:06:54 -06:00
|
|
|
targets: [
|
2017-12-20 05:33:33 -06:00
|
|
|
{ target: 'series1', hide: true },
|
|
|
|
{ target: 'sumSeries(#A)', hide: true },
|
|
|
|
{ target: 'asPercent(#A,#B)' },
|
|
|
|
],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
2017-12-21 01:39:31 -06:00
|
|
|
expect(results[0]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should replace target placeholder when nesting query references', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-21 01:39:31 -06:00
|
|
|
targets: [{ target: 'series1' }, { target: 'sumSeries(#A)' }, { target: 'asPercent(#A,#B)' }],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
2017-12-21 01:39:31 -06:00
|
|
|
expect(results[2]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should fix wrong minute interval parameters', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-20 05:33:33 -06:00
|
|
|
targets: [{ target: "summarize(prod.25m.count, '25m', 'sum')" }],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
2017-12-21 01:39:31 -06:00
|
|
|
expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should fix wrong month interval parameters', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-20 05:33:33 -06:00
|
|
|
targets: [{ target: "summarize(prod.5M.count, '5M', 'sum')" }],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
2017-12-21 01:39:31 -06:00
|
|
|
expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
it('should ignore empty targets', function() {
|
2017-10-07 03:31:39 -05:00
|
|
|
let results = ctx.ds.buildGraphiteParams({
|
2017-12-20 05:33:33 -06:00
|
|
|
targets: [{ target: 'series1' }, { target: '' }],
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|
|
|
|
expect(results.length).to.be(2);
|
|
|
|
});
|
|
|
|
});
|
2018-04-17 11:39:59 -05:00
|
|
|
|
|
|
|
describe('querying for template variables', () => {
|
|
|
|
let results;
|
|
|
|
let requestOptions;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({
|
|
|
|
data: [{ target: 'prod1.count', datapoints: [[10, 1], [12, 1]] }],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tags query', () => {
|
|
|
|
ctx.ds.metricFindQuery('tags()').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/tags');
|
|
|
|
expect(requestOptions.params.expr).to.eql([]);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tags query with a filter expression', () => {
|
|
|
|
ctx.ds.metricFindQuery('tags(server=backend_01)').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/tags');
|
|
|
|
expect(requestOptions.params.expr).to.eql(['server=backend_01']);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tag query for an expression with whitespace after', () => {
|
|
|
|
ctx.ds.metricFindQuery('tags(server=backend_01 )').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/tags');
|
|
|
|
expect(requestOptions.params.expr).to.eql(['server=backend_01']);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tag values query for one tag', () => {
|
|
|
|
ctx.ds.metricFindQuery('tag_values(server)').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/values');
|
|
|
|
expect(requestOptions.params.tag).to.be('server');
|
|
|
|
expect(requestOptions.params.expr).to.eql([]);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tag values query for a tag and expression', () => {
|
|
|
|
ctx.ds.metricFindQuery('tag_values(server,server=~backend*)').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/values');
|
|
|
|
expect(requestOptions.params.tag).to.be('server');
|
|
|
|
expect(requestOptions.params.expr).to.eql(['server=~backend*']);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tag values query for a tag with whitespace after', () => {
|
|
|
|
ctx.ds.metricFindQuery('tag_values(server )').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/values');
|
|
|
|
expect(requestOptions.params.tag).to.be('server');
|
|
|
|
expect(requestOptions.params.expr).to.eql([]);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate tag values query for a tag and expression with whitespace after', () => {
|
|
|
|
ctx.ds.metricFindQuery('tag_values(server , server=~backend* )').then(data => {
|
|
|
|
results = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
expect(requestOptions.url).to.be('/tags/autoComplete/values');
|
|
|
|
expect(requestOptions.params.tag).to.be('server');
|
|
|
|
expect(requestOptions.params.expr).to.eql(['server=~backend*']);
|
|
|
|
expect(results).not.to.be(null);
|
|
|
|
});
|
|
|
|
});
|
2015-09-28 08:23:53 -05:00
|
|
|
});
|