Commit 7e434fc0 by Torkel Ödegaard

refactor: moved graphite specs into plugins directory

parent cb7424ce
...@@ -25,6 +25,7 @@ module.exports = function(config) { ...@@ -25,6 +25,7 @@ module.exports = function(config) {
browsers: ['PhantomJS'], browsers: ['PhantomJS'],
captureTimeout: 60000, captureTimeout: 60000,
singleRun: true, singleRun: true,
autoWatchBatchDelay: 1000,
}); });
......
...@@ -5,9 +5,9 @@ define([ ...@@ -5,9 +5,9 @@ define([
'config', 'config',
'app/core/utils/datemath', 'app/core/utils/datemath',
'./directives', './directives',
'./queryCtrl', './query_ctrl',
'./funcEditor', './func_editor',
'./addGraphiteFunc', './add_graphite_func',
], ],
function (angular, _, $, config, dateMath) { function (angular, _, $, config, dateMath) {
'use strict'; 'use strict';
......
define([ ///<amd-dependency path="app/plugins/datasource/graphite/datasource" />
'./helpers', ///<amd-dependency path="test/specs/helpers" name="helpers" />
'app/plugins/datasource/graphite/datasource'
], function(helpers) {
'use strict';
describe('graphiteDatasource', function() { import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
var ctx = new helpers.ServiceTestContext(); declare var helpers: any;
beforeEach(module('grafana.services')); describe('graphiteDatasource', function() {
beforeEach(ctx.providePhase(['backendSrv'])); var ctx = new helpers.ServiceTestContext();
beforeEach(ctx.createService('GraphiteDatasource')); beforeEach(angularMocks.module('grafana.services'));
beforeEach(function() { beforeEach(ctx.providePhase(['backendSrv']));
ctx.ds = new ctx.service({ url: [''] });
});
describe('When querying influxdb with one target using query editor target spec', function() { beforeEach(ctx.createService('GraphiteDatasource'));
var query = { beforeEach(function() {
rangeRaw: { from: 'now-1h', to: 'now' }, ctx.ds = new ctx.service({ url: [''] });
targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}], });
maxDataPoints: 500,
};
var results; describe('When querying influxdb with one target using query editor target spec', function() {
var requestOptions; var query = {
rangeRaw: { from: 'now-1h', to: 'now' },
targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}],
maxDataPoints: 500,
};
beforeEach(function() { var results;
ctx.backendSrv.datasourceRequest = function(options) { var requestOptions;
requestOptions = options;
return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
};
ctx.ds.query(query).then(function(data) { results = data; }); beforeEach(function() {
ctx.$rootScope.$apply(); ctx.backendSrv.datasourceRequest = function(options) {
}); requestOptions = options;
return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
};
it('should generate the correct query', function() { ctx.ds.query(query).then(function(data) { results = data; });
expect(requestOptions.url).to.be('/render'); ctx.$rootScope.$apply();
}); });
it('should query correctly', function() { it('should generate the correct query', function() {
var params = requestOptions.data.split('&'); expect(requestOptions.url).to.be('/render');
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');
});
it('should exclude undefined params', function() { it('should query correctly', function() {
var params = requestOptions.data.split('&'); var params = requestOptions.data.split('&');
expect(params).to.not.contain('cacheTimeout=undefined'); 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');
});
it('should return series list', function() { it('should exclude undefined params', function() {
expect(results.data.length).to.be(1); var params = requestOptions.data.split('&');
expect(results.data[0].target).to.be('prod1.count'); expect(params).to.not.contain('cacheTimeout=undefined');
}); });
it('should convert to millisecond resolution', function() { it('should return series list', function() {
expect(results.data[0].datapoints[0][0]).to.be(10); expect(results.data.length).to.be(1);
}); expect(results.data[0].target).to.be('prod1.count');
});
it('should convert to millisecond resolution', function() {
expect(results.data[0].datapoints[0][0]).to.be(10);
}); });
describe('building graphite params', function() { });
it('should uri escape targets', function() { describe('building graphite params', function() {
var results = ctx.ds.buildGraphiteParams({
targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
});
expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
});
it('should replace target placeholder', function() { it('should uri escape targets', function() {
var results = ctx.ds.buildGraphiteParams({ var results = ctx.ds.buildGraphiteParams({
targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}] targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
});
expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
}); });
expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
});
it('should replace target placeholder for hidden series', function() { it('should replace target placeholder', function() {
var results = ctx.ds.buildGraphiteParams({ var results = ctx.ds.buildGraphiteParams({
targets: [{target: 'series1', hide: true}, {target: 'sumSeries(#A)', hide: true}, {target: 'asPercent(#A,#B)'}] targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}]
});
expect(results[0]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
}); });
expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
});
it('should replace target placeholder when nesting query references', function() { it('should replace target placeholder for hidden series', function() {
var results = ctx.ds.buildGraphiteParams({ var results = ctx.ds.buildGraphiteParams({
targets: [{target: 'series1'}, {target: 'sumSeries(#A)'}, {target: 'asPercent(#A,#B)'}] targets: [{target: 'series1', hide: true}, {target: 'sumSeries(#A)', hide: true}, {target: 'asPercent(#A,#B)'}]
});
expect(results[2]).to.be('target=' + encodeURIComponent("asPercent(series1,sumSeries(series1))"));
}); });
expect(results[0]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
});
it('should fix wrong minute interval parameters', function() { it('should replace target placeholder when nesting query references', function() {
var results = ctx.ds.buildGraphiteParams({ var results = ctx.ds.buildGraphiteParams({
targets: [{target: "summarize(prod.25m.count, '25m', 'sum')" }] targets: [{target: 'series1'}, {target: 'sumSeries(#A)'}, {target: 'asPercent(#A,#B)'}]
});
expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
}); });
expect(results[2]).to.be('target=' + encodeURIComponent("asPercent(series1,sumSeries(series1))"));
});
it('should fix wrong month interval parameters', function() { it('should fix wrong minute interval parameters', function() {
var results = ctx.ds.buildGraphiteParams({ var results = ctx.ds.buildGraphiteParams({
targets: [{target: "summarize(prod.5M.count, '5M', 'sum')" }] targets: [{target: "summarize(prod.25m.count, '25m', 'sum')" }]
});
expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
}); });
expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
});
it('should ignore empty targets', function() { it('should fix wrong month interval parameters', function() {
var results = ctx.ds.buildGraphiteParams({ var results = ctx.ds.buildGraphiteParams({
targets: [{target: 'series1'}, {target: ''}] targets: [{target: "summarize(prod.5M.count, '5M', 'sum')" }]
});
expect(results.length).to.be(2);
}); });
expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
});
it('should ignore empty targets', function() {
var results = ctx.ds.buildGraphiteParams({
targets: [{target: 'series1'}, {target: ''}]
});
expect(results.length).to.be(2);
}); });
}); });
......
define([ ///<amd-dependency path="app/plugins/datasource/graphite/gfunc" name="gfunc" />
'app/plugins/datasource/graphite/gfunc'
], function(gfunc) { import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
'use strict';
declare var gfunc: any;
describe('when creating func instance from func names', function() {
describe('when creating func instance from func names', function() {
it('should return func instance', function() { it('should return func instance', function() {
var func = gfunc.createFuncInstance('sumSeries'); var func = gfunc.createFuncInstance('sumSeries');
expect(func).to.be.ok(); expect(func).to.be.ok();
expect(func.def.name).to.equal('sumSeries'); expect(func.def.name).to.equal('sumSeries');
expect(func.def.params.length).to.equal(5); expect(func.def.params.length).to.equal(5);
expect(func.def.defaultParams.length).to.equal(1); expect(func.def.defaultParams.length).to.equal(1);
}); });
it('should return func instance with shortName', function() { it('should return func instance with shortName', function() {
var func = gfunc.createFuncInstance('sum'); var func = gfunc.createFuncInstance('sum');
expect(func).to.be.ok(); expect(func).to.be.ok();
}); });
it('should return func instance from funcDef', function() { it('should return func instance from funcDef', function() {
var func = gfunc.createFuncInstance('sum'); var func = gfunc.createFuncInstance('sum');
var func2 = gfunc.createFuncInstance(func.def); var func2 = gfunc.createFuncInstance(func.def);
expect(func2).to.be.ok(); expect(func2).to.be.ok();
}); });
it('func instance should have text representation', function() { it('func instance should have text representation', function() {
var func = gfunc.createFuncInstance('groupByNode'); var func = gfunc.createFuncInstance('groupByNode');
func.params[0] = 5; func.params[0] = 5;
func.params[1] = 'avg'; func.params[1] = 'avg';
func.updateText(); func.updateText();
expect(func.text).to.equal("groupByNode(5, avg)"); expect(func.text).to.equal("groupByNode(5, avg)");
}); });
});
});
describe('when rendering func instance', function() {
describe('when rendering func instance', function() {
it('should handle single metric param', function() {
it('should handle single metric param', function() { var func = gfunc.createFuncInstance('sumSeries');
var func = gfunc.createFuncInstance('sumSeries'); expect(func.render('hello.metric')).to.equal("sumSeries(hello.metric)");
expect(func.render('hello.metric')).to.equal("sumSeries(hello.metric)"); });
});
it('should include default params if options enable it', function() {
it('should include default params if options enable it', function() { var func = gfunc.createFuncInstance('scaleToSeconds', { withDefaultParams: true });
var func = gfunc.createFuncInstance('scaleToSeconds', { withDefaultParams: true }); expect(func.render('hello')).to.equal("scaleToSeconds(hello, 1)");
expect(func.render('hello')).to.equal("scaleToSeconds(hello, 1)"); });
});
it('should handle int or interval params with number', function() {
it('should handle int or interval params with number', function() { var func = gfunc.createFuncInstance('movingMedian');
var func = gfunc.createFuncInstance('movingMedian'); func.params[0] = '5';
func.params[0] = '5'; expect(func.render('hello')).to.equal("movingMedian(hello, 5)");
expect(func.render('hello')).to.equal("movingMedian(hello, 5)"); });
});
it('should handle int or interval params with interval string', function() {
it('should handle int or interval params with interval string', function() { var func = gfunc.createFuncInstance('movingMedian');
var func = gfunc.createFuncInstance('movingMedian'); func.params[0] = '5min';
func.params[0] = '5min'; expect(func.render('hello')).to.equal("movingMedian(hello, '5min')");
expect(func.render('hello')).to.equal("movingMedian(hello, '5min')"); });
});
it('should handle metric param and int param and string param', function() {
it('should handle metric param and int param and string param', function() { var func = gfunc.createFuncInstance('groupByNode');
var func = gfunc.createFuncInstance('groupByNode'); func.params[0] = 5;
func.params[0] = 5; func.params[1] = 'avg';
func.params[1] = 'avg'; expect(func.render('hello.metric')).to.equal("groupByNode(hello.metric, 5, 'avg')");
expect(func.render('hello.metric')).to.equal("groupByNode(hello.metric, 5, 'avg')"); });
});
it('should handle function with no metric param', function() {
it('should handle function with no metric param', function() { var func = gfunc.createFuncInstance('randomWalk');
var func = gfunc.createFuncInstance('randomWalk'); func.params[0] = 'test';
func.params[0] = 'test'; expect(func.render(undefined)).to.equal("randomWalk('test')");
expect(func.render(undefined)).to.equal("randomWalk('test')"); });
});
it('should handle function multiple series params', function() {
it('should handle function multiple series params', function() { var func = gfunc.createFuncInstance('asPercent');
var func = gfunc.createFuncInstance('asPercent'); func.params[0] = '#B';
func.params[0] = '#B'; expect(func.render('#A')).to.equal("asPercent(#A, #B)");
expect(func.render('#A')).to.equal("asPercent(#A, #B)"); });
});
});
});
describe('when requesting function categories', function() {
describe('when requesting function categories', function() { it('should return function categories', function() {
it('should return function categories', function() { var catIndex = gfunc.getCategories();
var catIndex = gfunc.getCategories(); expect(catIndex.Special.length).to.be.greaterThan(8);
expect(catIndex.Special.length).to.be.greaterThan(8); });
}); });
});
describe('when updating func param', function() {
describe('when updating func param', function() { it('should update param value and update text representation', function() {
it('should update param value and update text representation', function() { var func = gfunc.createFuncInstance('summarize', { withDefaultParams: true });
var func = gfunc.createFuncInstance('summarize', { withDefaultParams: true }); func.updateParam('1h', 0);
func.updateParam('1h', 0); expect(func.params[0]).to.be('1h');
expect(func.params[0]).to.be('1h'); expect(func.text).to.be('summarize(1h, sum, false)');
expect(func.text).to.be('summarize(1h, sum, false)'); });
});
it('should parse numbers as float', function() {
it('should parse numbers as float', function() { var func = gfunc.createFuncInstance('scale');
var func = gfunc.createFuncInstance('scale'); func.updateParam('0.001', 0);
func.updateParam('0.001', 0); expect(func.params[0]).to.be('0.001');
expect(func.params[0]).to.be('0.001'); });
}); });
});
describe('when updating func param with optional second parameter', function() {
describe('when updating func param with optional second parameter', function() { it('should update value and text', function() {
it('should update value and text', function() { var func = gfunc.createFuncInstance('aliasByNode');
var func = gfunc.createFuncInstance('aliasByNode'); func.updateParam('1', 0);
func.updateParam('1', 0); expect(func.params[0]).to.be('1');
expect(func.params[0]).to.be('1');
});
it('should slit text and put value in second param', function() {
var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('4,-5', 0);
expect(func.params[0]).to.be('4');
expect(func.params[1]).to.be('-5');
expect(func.text).to.be('aliasByNode(4, -5)');
});
it('should remove second param when empty string is set', function() {
var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('4,-5', 0);
func.updateParam('', 1);
expect(func.params[0]).to.be('4');
expect(func.params[1]).to.be(undefined);
expect(func.text).to.be('aliasByNode(4)');
});
}); });
it('should slit text and put value in second param', function() {
var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('4,-5', 0);
expect(func.params[0]).to.be('4');
expect(func.params[1]).to.be('-5');
expect(func.text).to.be('aliasByNode(4, -5)');
});
it('should remove second param when empty string is set', function() {
var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('4,-5', 0);
func.updateParam('', 1);
expect(func.params[0]).to.be('4');
expect(func.params[1]).to.be(undefined);
expect(func.text).to.be('aliasByNode(4)');
});
}); });
...@@ -7,10 +7,15 @@ var it = _global.it; ...@@ -7,10 +7,15 @@ var it = _global.it;
var sinon = _global.sinon; var sinon = _global.sinon;
var expect = _global.expect; var expect = _global.expect;
var angularMocks = {
module: _global.module,
};
export { export {
beforeEach, beforeEach,
describe, describe,
it, it,
sinon, sinon,
expect expect,
angularMocks,
} }
...@@ -113,7 +113,7 @@ require([ ...@@ -113,7 +113,7 @@ require([
var specs = []; var specs = [];
for (var file in window.__karma__.files) { for (var file in window.__karma__.files) {
if (/base\/test\/specs.*/.test(file)) { if (/specs.*/.test(file)) {
file = file2moduleName(file); file = file2moduleName(file);
specs.push(file); specs.push(file);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment