2014-08-28 05:44:01 -05:00
|
|
|
define([
|
|
|
|
'mocks/dashboard-mock',
|
2014-09-05 05:07:48 -05:00
|
|
|
'./helpers',
|
2014-09-05 06:31:34 -05:00
|
|
|
'moment',
|
2014-08-28 05:44:01 -05:00
|
|
|
'services/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'));
|
2014-09-05 05:07:48 -05:00
|
|
|
beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv']));
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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) {
|
|
|
|
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));
|
|
|
|
ctx.datasourceSrv.get = sinon.stub().returns(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 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' };
|
|
|
|
scenario.variable.current = { value: 'backend2'};
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep variable value', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.current.value).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('and existing value still exists in options', function(scenario) {
|
|
|
|
scenario.setup(function() {
|
|
|
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
|
|
|
scenario.variable.current = { value: 'backend2'};
|
|
|
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
|
2014-09-01 04:13:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep variable value', function() {
|
2014-09-05 05:07:48 -05:00
|
|
|
expect(scenario.variable.current.value).to.be('backend2');
|
2014-09-01 04:13:18 -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-08-28 05:44:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|