2014-08-28 05:44:01 -05:00
|
|
|
define([
|
|
|
|
'mocks/dashboard-mock',
|
2014-12-23 14:01:50 -06:00
|
|
|
'helpers',
|
2014-09-05 06:31:34 -05:00
|
|
|
'moment',
|
2014-12-31 02:34:54 -06:00
|
|
|
'features/templating/templateValuesSrv'
|
2014-09-05 06:31:34 -05:00
|
|
|
], function(dashboardMock, helpers, moment) {
|
2014-08-28 05:44:01 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('templateValuesSrv', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
var ctx = new helpers.ServiceTestContext();
|
2014-08-28 05:44:01 -05:00
|
|
|
|
|
|
|
beforeEach(module('grafana.services'));
|
2015-05-04 03:13:12 -05:00
|
|
|
beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location']));
|
2014-09-05 05:07:48 -05:00
|
|
|
beforeEach(ctx.createService('templateValuesSrv'));
|
2014-08-28 05:44:01 -05:00
|
|
|
|
2014-09-05 06:31:34 -05:00
|
|
|
describe('update interval variable options', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
var variable = { type: 'interval', query: 'auto,1s,2h,5h,1d', name: 'test' };
|
2014-08-28 05:44:01 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
ctx.service.updateOptions(variable);
|
2014-08-28 05:44:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should update options array', function() {
|
|
|
|
expect(variable.options.length).to.be(5);
|
|
|
|
expect(variable.options[1].text).to.be('1s');
|
|
|
|
expect(variable.options[1].value).to.be('1s');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-04 03:13:12 -05:00
|
|
|
describe('when template variable is present in url', function() {
|
|
|
|
var variable = {
|
|
|
|
name: 'apps',
|
|
|
|
current: {text: "test", value: "test"},
|
|
|
|
options: [{text: "test", value: "test"}]
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
var dashboard = { templating: { list: [variable] } };
|
|
|
|
var urlParams = {};
|
|
|
|
urlParams["var-apps"] = "new";
|
|
|
|
ctx.$location.search = sinon.stub().returns(urlParams);
|
|
|
|
ctx.service.init(dashboard);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update current value', function() {
|
|
|
|
expect(variable.current.value).to.be("new");
|
|
|
|
expect(variable.current.text).to.be("new");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when template variable is present in url multiple times', function() {
|
|
|
|
var variable = {
|
|
|
|
name: 'apps',
|
|
|
|
multi: true,
|
|
|
|
current: {text: "test", value: "test"},
|
|
|
|
options: [{text: "test", value: "test"}]
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
var dashboard = { templating: { list: [variable] } };
|
|
|
|
var urlParams = {};
|
|
|
|
urlParams["var-apps"] = ["new", "other"];
|
|
|
|
ctx.$location.search = sinon.stub().returns(urlParams);
|
|
|
|
ctx.service.init(dashboard);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update current value', function() {
|
|
|
|
expect(variable.current.value.length).to.be(2);
|
|
|
|
expect(variable.current.value[0]).to.be("new");
|
|
|
|
expect(variable.current.value[1]).to.be("other");
|
|
|
|
expect(variable.current.text).to.be("new, other");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-09-01 04:13:18 -05:00
|
|
|
function describeUpdateVariable(desc, fn) {
|
|
|
|
describe(desc, function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
var scenario = {};
|
|
|
|
scenario.setup = function(setupFn) {
|
2015-05-04 03:13:12 -05:00
|
|
|
scenario.setupFn = setupFn;
|
2014-09-01 04:13:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
scenario.setupFn();
|
2014-09-01 04:13:18 -05:00
|
|
|
var ds = {};
|
2014-09-05 05:07:48 -05:00
|
|
|
ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
|
2015-02-28 03:52:25 -06:00
|
|
|
ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
|
2014-09-01 04:13:18 -05:00
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
ctx.service.updateOptions(scenario.variable);
|
|
|
|
ctx.$rootScope.$digest();
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
fn(scenario);
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-05 06:31:34 -05:00
|
|
|
describeUpdateVariable('interval variable without auto', function(scenario) {
|
2014-09-05 05:07:48 -05:00
|
|
|
scenario.setup(function() {
|
2014-09-05 06:31:34 -05:00
|
|
|
scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update options array', function() {
|
|
|
|
expect(scenario.variable.options.length).to.be(4);
|
|
|
|
expect(scenario.variable.options[0].text).to.be('1s');
|
|
|
|
expect(scenario.variable.options[0].value).to.be('1s');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describeUpdateVariable('interval variable with auto', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
|
|
|
|
|
|
|
|
var range = {
|
|
|
|
from: moment(new Date()).subtract(7, 'days').toDate(),
|
|
|
|
to: new Date()
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.timeSrv.timeRange = sinon.stub().returns(range);
|
|
|
|
ctx.templateSrv.setGrafanaVariable = sinon.spy();
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should update options array', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options.length).to.be(5);
|
2014-09-05 06:31:34 -05:00
|
|
|
expect(scenario.variable.options[0].text).to.be('auto');
|
|
|
|
expect(scenario.variable.options[0].value).to.be('$__auto_interval');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set $__auto_interval', function() {
|
|
|
|
var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
|
|
|
|
expect(call.args[0]).to.be('$__auto_interval');
|
|
|
|
expect(call.args[1]).to.be('12h');
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 07:03:36 -05:00
|
|
|
describeUpdateVariable('update custom variable', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update options array', function() {
|
|
|
|
expect(scenario.variable.options.length).to.be(3);
|
|
|
|
expect(scenario.variable.options[0].text).to.be('hej');
|
|
|
|
expect(scenario.variable.options[1].value).to.be('hop');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set $__auto_interval', function() {
|
|
|
|
var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
|
|
|
|
expect(call.args[0]).to.be('$__auto_interval');
|
|
|
|
expect(call.args[1]).to.be('12h');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('basic query variable', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should update options array', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options.length).to.be(2);
|
|
|
|
expect(scenario.variable.options[0].text).to.be('backend1');
|
|
|
|
expect(scenario.variable.options[0].value).to.be('backend1');
|
|
|
|
expect(scenario.variable.options[1].value).to.be('backend2');
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should select first option as value', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.current.value).to.be('backend1');
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('and existing value still exists in options', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
2014-09-08 04:03:14 -05:00
|
|
|
scenario.variable.current = { text: 'backend2'};
|
2014-09-05 05:07:48 -05:00
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep variable value', function() {
|
2014-09-08 04:03:14 -05:00
|
|
|
expect(scenario.variable.current.text).to.be('backend2');
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('and regex pattern exists', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
|
|
|
scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
|
|
|
|
scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract and use match group', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options[0].value).to.be('backend_01');
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
|
|
|
scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
|
|
|
|
scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add non matching items', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options.length).to.be(0);
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('regex pattern without slashes', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
|
|
|
scenario.variable.regex = 'backend_01';
|
|
|
|
scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return matches options', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options.length).to.be(1);
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
|
|
|
scenario.variable.regex = 'backend_01';
|
|
|
|
scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
|
2014-09-02 00:05:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return matches options', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options.length).to.be(1);
|
2014-09-02 00:05:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('with include All glob syntax', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add All Glob option', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('with include all wildcard', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
|
2014-09-02 00:05:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add All wildcard option', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options[0].value).to.be('*');
|
2014-09-02 00:05:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('with include all wildcard', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
|
2014-09-02 00:05:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add All wildcard option', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options[0].value).to.be('.*');
|
2014-09-02 00:05:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('with include all regex values', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
|
2014-09-01 06:40:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add All wildcard option', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options[0].value).to.be('*');
|
2014-09-01 06:40:34 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
describeUpdateVariable('with include all glob no values', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
|
|
|
|
scenario.queryResult = [];
|
2014-09-05 00:02:59 -05:00
|
|
|
});
|
2014-09-01 06:40:34 -05:00
|
|
|
|
2014-09-05 00:02:59 -05:00
|
|
|
it('should add empty glob', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.options[0].value).to.be('{}');
|
2014-09-05 00:02:59 -05:00
|
|
|
});
|
|
|
|
});
|
2014-09-01 06:40:34 -05:00
|
|
|
|
2014-09-05 08:46:29 -05:00
|
|
|
describeUpdateVariable('with include all regex all values', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add empty glob', function() {
|
|
|
|
expect(scenario.variable.options[0].value).to.be('(backend1|backend2|backend3)');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|