Skip to content

Commit

Permalink
templating: fix queries contained regex with braces, fixes #432
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Jul 25, 2017
1 parent b1fafc2 commit 996bc9e
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 7 deletions.
50 changes: 50 additions & 0 deletions dist/datasource-zabbix/specs/utils_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,54 @@ describe('Utils', () => {
done();
});
});

describe('splitTemplateQuery()', () => {

// Backward compatibility
it('should properly split query in old format', (done) => {
let test_cases = [
{
query: `/alu/./tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9{2}/`,
expected: ['/alu/', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9{2}/']
},
{
query: `a.b.c.d`,
expected: ['a', 'b', 'c', 'd']
}
];

_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
});
done();
});

it('should properly split query', (done) => {
let test_cases = [
{
query: `{alu}{/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]*/}`,
expected: ['alu', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]*/']
},
{
query: `{alu}{/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]{2}/}`,
expected: ['alu', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]{2}/']
},
{
query: `{a}{b}{c}{d}`,
expected: ['a', 'b', 'c', 'd']
},
{
query: `{a}{b.c.d}`,
expected: ['a', 'b.c.d']
}
];

_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
});
done();
});
});
});
5 changes: 3 additions & 2 deletions dist/datasource-zabbix/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/datasource-zabbix/utils.js.map

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions dist/test/datasource-zabbix/specs/utils_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,46 @@ describe('Utils', function () {
done();
});
});

describe('splitTemplateQuery()', function () {

// Backward compatibility
it('should properly split query in old format', function (done) {
var test_cases = [{
query: '/alu/./tw-(nyc|que|brx|dwt|brk)-sta_(w|d)*-alu-[0-9{2}/',
expected: ['/alu/', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9{2}/']
}, {
query: 'a.b.c.d',
expected: ['a', 'b', 'c', 'd']
}];

_lodash2.default.each(test_cases, function (test_case) {
var splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
});
done();
});

it('should properly split query', function (done) {
var test_cases = [{
query: '{alu}{/tw-(nyc|que|brx|dwt|brk)-sta_(w|d)*-alu-[0-9]*/}',
expected: ['alu', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]*/']
}, {
query: '{alu}{/tw-(nyc|que|brx|dwt|brk)-sta_(w|d)*-alu-[0-9]{2}/}',
expected: ['alu', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]{2}/']
}, {
query: '{a}{b}{c}{d}',
expected: ['a', 'b', 'c', 'd']
}, {
query: '{a}{b.c.d}',
expected: ['a', 'b.c.d']
}];

_lodash2.default.each(test_cases, function (test_case) {
var splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
});
done();
});
});
});
5 changes: 3 additions & 2 deletions dist/test/datasource-zabbix/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function escapeMacro(macro) {
* {group}{host.com} -> [group, host.com]
*/
function splitTemplateQuery(query) {
var splitPattern = /{[^{}]*}/g;
var splitPattern = /\{[^\{\}]*\}|\{\/.*\/\}/g;
var split = void 0;

if (isContainsBraces(query)) {
Expand All @@ -136,7 +136,8 @@ function splitTemplateQuery(query) {
}

function isContainsBraces(query) {
return query.includes('{') && query.includes('}');
var bracesPattern = /^\{.+\}$/;
return bracesPattern.test(query);
}

// Pattern for testing regex
Expand Down
50 changes: 50 additions & 0 deletions src/datasource-zabbix/specs/utils_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,54 @@ describe('Utils', () => {
done();
});
});

describe('splitTemplateQuery()', () => {

// Backward compatibility
it('should properly split query in old format', (done) => {
let test_cases = [
{
query: `/alu/./tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9{2}/`,
expected: ['/alu/', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9{2}/']
},
{
query: `a.b.c.d`,
expected: ['a', 'b', 'c', 'd']
}
];

_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
});
done();
});

it('should properly split query', (done) => {
let test_cases = [
{
query: `{alu}{/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]*/}`,
expected: ['alu', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]*/']
},
{
query: `{alu}{/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]{2}/}`,
expected: ['alu', '/tw-(nyc|que|brx|dwt|brk)-sta_(\w|\d)*-alu-[0-9]{2}/']
},
{
query: `{a}{b}{c}{d}`,
expected: ['a', 'b', 'c', 'd']
},
{
query: `{a}{b.c.d}`,
expected: ['a', 'b.c.d']
}
];

_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
});
done();
});
});
});
5 changes: 3 additions & 2 deletions src/datasource-zabbix/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function escapeMacro(macro) {
* {group}{host.com} -> [group, host.com]
*/
export function splitTemplateQuery(query) {
let splitPattern = /{[^{}]*}/g;
let splitPattern = /\{[^\{\}]*\}|\{\/.*\/\}/g;
let split;

if (isContainsBraces(query)) {
Expand All @@ -109,7 +109,8 @@ export function splitTemplateQuery(query) {
}

function isContainsBraces(query) {
return query.includes('{') && query.includes('}');
let bracesPattern = /^\{.+\}$/;
return bracesPattern.test(query);
}

// Pattern for testing regex
Expand Down

0 comments on commit 996bc9e

Please sign in to comment.