Tests: added tests for template variables replacing.
This commit is contained in:
@@ -449,7 +449,7 @@ function formatMetric(metricObj) {
|
|||||||
* template variables, for example
|
* template variables, for example
|
||||||
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
|
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
|
||||||
*/
|
*/
|
||||||
function zabbixTemplateFormat(value) {
|
export function zabbixTemplateFormat(value) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return utils.escapeRegex(value);
|
return utils.escapeRegex(value);
|
||||||
}
|
}
|
||||||
@@ -468,7 +468,7 @@ function zabbixTemplateFormat(value) {
|
|||||||
*/
|
*/
|
||||||
function replaceTemplateVars(templateSrv, target, scopedVars) {
|
function replaceTemplateVars(templateSrv, target, scopedVars) {
|
||||||
var replacedTarget = templateSrv.replace(target, scopedVars, zabbixTemplateFormat);
|
var replacedTarget = templateSrv.replace(target, scopedVars, zabbixTemplateFormat);
|
||||||
if (target !== replacedTarget && !utils.regexPattern.test(replacedTarget)) {
|
if (target !== replacedTarget && !utils.isRegex(replacedTarget)) {
|
||||||
replacedTarget = '/^' + replacedTarget + '$/';
|
replacedTarget = '/^' + replacedTarget + '$/';
|
||||||
}
|
}
|
||||||
return replacedTarget;
|
return replacedTarget;
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import {Datasource} from "../module";
|
import {Datasource} from "../module";
|
||||||
|
import {zabbixTemplateFormat} from "../datasource";
|
||||||
import Q from "q";
|
import Q from "q";
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
describe('ZabbixDatasource', function() {
|
describe('ZabbixDatasource', () => {
|
||||||
var ctx = {};
|
var ctx = {};
|
||||||
var defined = sinon.match.defined;
|
var defined = sinon.match.defined;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(() => {
|
||||||
ctx.instanceSettings = {
|
ctx.instanceSettings = {
|
||||||
jsonData: {
|
jsonData: {
|
||||||
username: 'zabbix',
|
username: 'zabbix',
|
||||||
@@ -19,19 +20,19 @@ describe('ZabbixDatasource', function() {
|
|||||||
ctx.$q = Q;
|
ctx.$q = Q;
|
||||||
ctx.templateSrv = {};
|
ctx.templateSrv = {};
|
||||||
ctx.alertSrv = {};
|
ctx.alertSrv = {};
|
||||||
ctx.zabbixAPIService = function() {};
|
ctx.zabbixAPIService = () => {};
|
||||||
ctx.ZabbixCachingProxy = function() {};
|
ctx.ZabbixCachingProxy = () => {};
|
||||||
ctx.QueryProcessor = function() {};
|
ctx.QueryProcessor = () => {};
|
||||||
|
|
||||||
ctx.ds = new Datasource(ctx.instanceSettings, ctx.$q, ctx.templateSrv, ctx.alertSrv,
|
ctx.ds = new Datasource(ctx.instanceSettings, ctx.$q, ctx.templateSrv, ctx.alertSrv,
|
||||||
ctx.zabbixAPIService, ctx.ZabbixCachingProxy, ctx.QueryProcessor);
|
ctx.zabbixAPIService, ctx.ZabbixCachingProxy, ctx.QueryProcessor);
|
||||||
|
|
||||||
ctx.ds.replaceTemplateVars = function(str) {
|
|
||||||
return str;
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When querying data', function() {
|
describe('When querying data', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
ctx.ds.replaceTemplateVars = (str) => { return str; };
|
||||||
|
});
|
||||||
|
|
||||||
ctx.options = {
|
ctx.options = {
|
||||||
targets: [
|
targets: [
|
||||||
{
|
{
|
||||||
@@ -83,10 +84,62 @@ describe('ZabbixDatasource', function() {
|
|||||||
expect(ctx.ds.queryNumericData)
|
expect(ctx.ds.queryNumericData)
|
||||||
.to.have.been.calledWith(defined, defined, defined, false);
|
.to.have.been.calledWith(defined, defined, defined, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('When replacing template variables', () => {
|
||||||
|
|
||||||
|
function testReplacingVariable(target, varValue, expectedResult, done) {
|
||||||
|
ctx.ds.templateSrv.replace = () => {
|
||||||
|
return zabbixTemplateFormat(varValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = ctx.ds.replaceTemplateVars(target);
|
||||||
|
expect(result).to.equal(expectedResult);
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Alphanumerics, spaces, dots, dashes and underscores
|
||||||
|
* are allowed in Zabbix host name.
|
||||||
|
* 'AaBbCc0123 .-_'
|
||||||
|
*/
|
||||||
|
it('should return properly escaped regex', (done) => {
|
||||||
|
let target = '$host';
|
||||||
|
let template_var_value = 'AaBbCc0123 .-_';
|
||||||
|
let expected_result = '/^AaBbCc0123 \\.-_$/';
|
||||||
|
|
||||||
|
testReplacingVariable(target, template_var_value, expected_result, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Single-value variable
|
||||||
|
* $host = backend01
|
||||||
|
* $host => /^backend01|backend01$/
|
||||||
|
*/
|
||||||
|
it('should return proper regex for single value', (done) => {
|
||||||
|
let target = '$host';
|
||||||
|
let template_var_value = 'backend01';
|
||||||
|
let expected_result = '/^backend01$/';
|
||||||
|
|
||||||
|
testReplacingVariable(target, template_var_value, expected_result, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Multi-value variable
|
||||||
|
* $host = [backend01, backend02]
|
||||||
|
* $host => /^(backend01|backend01)$/
|
||||||
|
*/
|
||||||
|
it('should return proper regex for multi-value', (done) => {
|
||||||
|
let target = '$host';
|
||||||
|
let template_var_value = ['backend01', 'backend02'];
|
||||||
|
let expected_result = '/^(backend01|backend02)$/';
|
||||||
|
|
||||||
|
testReplacingVariable(target, template_var_value, expected_result, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user