From 3a352fb54d26feae762f2ebb1041aedbe5995d5e Mon Sep 17 00:00:00 2001 From: Andris Zeila Date: Thu, 16 Jul 2020 10:28:35 +0300 Subject: ........S. [ZBXNEXT-6042] optimized trigger, calculated item processing by resolving user macros during configuration cache synchronization * commit 'ae4645be5080ef59cfd0267bc960d99197f77ed3': .D........ [ZBXNEXT-6042] updated changelog entry ........S. [ZBXNEXT-6042] fixed use of uninitialised variable ........S. [ZBXNEXT-6042] removed unused function .......PS. [ZBXNEXT-6042] removed redundant initialization .......... [ZBXNEXT-6042] added dc_expand_user_macros_in_calcitem() function test suite .......... [ZBXNEXT-6042] renamed test suite to match the renamed function .......... [ZBXNEXT-6042] moved user macro mocks to separae configcache mocking library .......... [ZBXNEXT-6042] merged global and host macro mock code and moved to separate file .......... [ZBXNEXT-6042] add first test cases for dc_expand_user_macros_in_func_params() function ........S. [ZBXNEXT-6042] added user macro resolving in calculated item formulas during configuration cache sync ........S. [ZBXNEXT-6042] fixed internal/external (dc_/zbx_dc_) configuration cache function naming and expression macro expanding without chache locking when polling calculated items ........S. [ZBXNEXT-6042] fixed user macro resolving in simple macro function parameters .D........ [ZBXNEXT-6042] added changelog entry ........S. [ZBXNEXT-6042] removed macro resolving in user parameters during trigger processing because function parameter user macros are already resolved during conifguration cache sync ........S. [ZBXNEXT-6042] added function parameter user macro resolving during configuration cache sync (cherry picked from commit 7bd5435bd09ac8ef954fc1c913b61d70dffc81b2) --- tests/libs/zbxdbcache/Makefile.am | 40 ++++- .../zbxdbcache/dc_expand_user_macros_in_calcitem.c | 62 +++++++ .../dc_expand_user_macros_in_calcitem.yaml | 174 ++++++++++++++++++ .../dc_expand_user_macros_in_expression.c | 58 ++++++ .../dc_expand_user_macros_in_expression.yaml | 200 +++++++++++++++++++++ .../dc_expand_user_macros_in_func_params.c | 59 ++++++ .../dc_expand_user_macros_in_func_params.yaml | 147 +++++++++++++++ .../zbx_dc_expand_user_macros_in_expression.c | 187 ------------------- .../zbx_dc_expand_user_macros_in_expression.yaml | 200 --------------------- 9 files changed, 735 insertions(+), 392 deletions(-) create mode 100644 tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.c create mode 100644 tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.yaml create mode 100644 tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.c create mode 100644 tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.yaml create mode 100644 tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.c create mode 100644 tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.yaml delete mode 100644 tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.c delete mode 100644 tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.yaml (limited to 'tests/libs/zbxdbcache') diff --git a/tests/libs/zbxdbcache/Makefile.am b/tests/libs/zbxdbcache/Makefile.am index 519d45d11ee..326bb24073b 100644 --- a/tests/libs/zbxdbcache/Makefile.am +++ b/tests/libs/zbxdbcache/Makefile.am @@ -7,7 +7,9 @@ SERVER_tests = \ dc_check_maintenance_period \ is_item_processed_by_server \ dc_item_poller_type_update \ - zbx_dc_expand_user_macros_in_expression + dc_expand_user_macros_in_expression \ + dc_expand_user_macros_in_func_params \ + dc_expand_user_macros_in_calcitem endif noinst_PROGRAMS = $(SERVER_tests) @@ -148,12 +150,40 @@ dc_item_poller_type_update_LDADD = $(CACHE_LIBS) @SERVER_LIBS@ dc_item_poller_type_update_LDFLAGS = @SERVER_LDFLAGS@ dc_item_poller_type_update_CFLAGS = -I@top_srcdir@/tests -I@top_srcdir@/src/libs/zbxdbcache -zbx_dc_expand_user_macros_in_expression_CFLAGS = \ +dc_expand_user_macros_in_expression_CFLAGS = \ -I@top_srcdir@/tests \ + -I@top_srcdir@/tests/mocks/configcache \ -I@top_srcdir@/src/libs/zbxdbcache \ -Wl,--wrap=zbx_hashset_search -zbx_dc_expand_user_macros_in_expression_SOURCES = zbx_dc_expand_user_macros_in_expression.c -zbx_dc_expand_user_macros_in_expression_LDADD = $(CACHE_LIBS) @SERVER_LIBS@ -zbx_dc_expand_user_macros_in_expression_LDFLAGS = @SERVER_LDFLAGS@ +dc_expand_user_macros_in_expression_SOURCES = \ + dc_expand_user_macros_in_expression.c +dc_expand_user_macros_in_expression_LDADD = \ + $(top_srcdir)/tests/mocks/configcache/libconfigcachemock.a \ + $(CACHE_LIBS) @SERVER_LIBS@ +dc_expand_user_macros_in_expression_LDFLAGS = @SERVER_LDFLAGS@ + +dc_expand_user_macros_in_func_params_CFLAGS = \ + -I@top_srcdir@/tests \ + -I@top_srcdir@/tests/mocks/configcache \ + -I@top_srcdir@/src/libs/zbxdbcache \ + -Wl,--wrap=zbx_hashset_search +dc_expand_user_macros_in_func_params_SOURCES = \ + dc_expand_user_macros_in_func_params.c +dc_expand_user_macros_in_func_params_LDADD = \ + $(top_srcdir)/tests/mocks/configcache/libconfigcachemock.a \ + $(CACHE_LIBS) @SERVER_LIBS@ +dc_expand_user_macros_in_func_params_LDFLAGS = @SERVER_LDFLAGS@ + +dc_expand_user_macros_in_calcitem_CFLAGS = \ + -I@top_srcdir@/tests \ + -I@top_srcdir@/tests/mocks/configcache \ + -I@top_srcdir@/src/libs/zbxdbcache \ + -Wl,--wrap=zbx_hashset_search +dc_expand_user_macros_in_calcitem_SOURCES = \ + dc_expand_user_macros_in_calcitem.c +dc_expand_user_macros_in_calcitem_LDADD = \ + $(top_srcdir)/tests/mocks/configcache/libconfigcachemock.a \ + $(CACHE_LIBS) @SERVER_LIBS@ +dc_expand_user_macros_in_calcitem_LDFLAGS = @SERVER_LDFLAGS@ endif diff --git a/tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.c b/tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.c new file mode 100644 index 00000000000..f10092a8b06 --- /dev/null +++ b/tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.c @@ -0,0 +1,62 @@ +/* +** Zabbix +** Copyright (C) 2001-2020 Zabbix SIA +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**/ + +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" +#include "zbxmockutil.h" + +#include "zbxserver.h" +#include "common.h" +#include "zbxalgo.h" +#include "dbcache.h" +#include "mutexs.h" + +#define ZBX_DBCONFIG_IMPL +#include "dbconfig.h" + +#include "configcache_mock.h" + +/****************************************************************************** + * * + * Function: zbx_mock_test_entry * + * * + ******************************************************************************/ +void zbx_mock_test_entry(void **state) +{ + char *returned_formula; + const char *formula, *expected_formula; + zbx_uint64_t hostid; + + ZBX_UNUSED(state); + + mock_config_init(); + mock_config_load_user_macros("in.macros"); + mock_config_load_hosts("in.hosts"); + + formula = zbx_mock_get_parameter_string("in.formula"); + hostid = zbx_mock_get_parameter_uint64("in.hostid"); + expected_formula = zbx_mock_get_parameter_string("out.formula"); + returned_formula = dc_expand_user_macros_in_calcitem(formula, hostid); + zbx_mock_assert_str_eq("Expanded parameters", expected_formula, returned_formula); + + zbx_free(returned_formula); + + mock_config_free(); +} diff --git a/tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.yaml b/tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.yaml new file mode 100644 index 00000000000..f286db500cd --- /dev/null +++ b/tests/libs/zbxdbcache/dc_expand_user_macros_in_calcitem.yaml @@ -0,0 +1,174 @@ +--- +test case: Expand 'last("hostA:key[{$A}]",{$A}) + last("hostB:key[{$A}]",{$A})' with hostA:{$A}=#1, hostB:{$A}=#2 +in: + hosts: + - hostid: 1 + host: hostA + - hostid: 2 + host: hostB + macros: + - hostid: 1 + name: '{$A}' + value: '#1' + - hostid: 2 + name: '{$A}' + value: '#2' + hostid: 1 + formula: 'last("hostA:key[{$A}]",{$A}) + last("hostB:key[{$A}]",{$A})' +out: + formula: 'last("hostA:key[{$A}]",#1) + last("hostB:key[{$A}]",#2)' +--- +test case: Expand 'last("hostA:key[{$A}]",{$A}) + last("hostB:key[{$A}]",{$A})' with hostA:{$A}='', hostB:{$A}='' +in: + hosts: + - hostid: 1 + host: hostA + - hostid: 2 + host: hostB + macros: + - hostid: 1 + name: '{$A}' + value: '' + - hostid: 2 + name: '{$A}' + value: '' + hostid: 1 + formula: 'last("hostA:key[{$A}]",{$A}) + last("hostB:key[{$A}]",{$A})' +out: + formula: 'last("hostA:key[{$A}]",) + last("hostB:key[{$A}]",)' +--- +test case: Expand 'last(host:key)' +in: + hosts: [] + macros: [] + hostid: 1 + formula: 'last(host:key)' +out: + formula: 'last(host:key)' +--- +test case: Expand 'last("key[{$A}]",{$A})' on hostA with hostA:{$A}='#1', hostB:{$A}='#2' +in: + hosts: + - hostid: 1 + host: hostA + - hostid: 2 + host: hostB + macros: + - hostid: 1 + name: '{$A}' + value: '#1' + - hostid: 2 + name: '{$A}' + value: '#2' + hostid: 1 + formula: 'last("key[{$A}]",{$A})' +out: + formula: 'last("key[{$A}]",#1)' +--- +test case: Expand 'last("key[{$A}]",{$A})' on hostB with hostA:{$A}='#1', hostB:{$A}='#2' +in: + hosts: + - hostid: 1 + host: hostA + - hostid: 2 + host: hostB + macros: + - hostid: 1 + name: '{$A}' + value: '#1' + - hostid: 2 + name: '{$A}' + value: '#2' + hostid: 2 + formula: 'last("key[{$A}]",{$A})' +out: + formula: 'last("key[{$A}]",#2)' +--- +test case: Expand '{$A}last(host:key){$B}' +in: + hosts: [] + macros: + - hostid: 1 + name: '{$A}' + value: '11' + - hostid: 2 + name: '{$A}' + value: '21' + - hostid: 1 + name: '{$B}' + value: '12' + - hostid: 2 + name: '{$B}' + value: '22' + hostid: 1 + formula: '{$A}last(host:key){$B}' +out: + formula: '11last(host:key)12' +--- +test case: Expand '{$A}last(host:key){$B}' +in: + hosts: [] + macros: + - hostid: 1 + name: '{$A}' + value: '11' + - hostid: 2 + name: '{$A}' + value: '21' + - hostid: 1 + name: '{$B}' + value: '12' + - hostid: 2 + name: '{$B}' + value: '22' + hostid: 2 + formula: '{$A}last(host:key){$B}' +out: + formula: '21last(host:key)22' +--- +test case: Expand '{$X}+last("hostB:key[{$X}]",{$X})+{$Y}+last("key[{$X}]",{$X})+{$Z}' +in: + hosts: + - hostid: 1 + host: hostA + - hostid: 2 + host: hostB + macros: + - hostid: 1 + name: '{$X}' + value: '1' + - hostid: 1 + name: '{$Y}' + value: '2' + - hostid: 1 + name: '{$Z}' + value: '3' + - hostid: 2 + name: '{$X}' + value: '100' + hostid: 1 + formula: '{$X}+last("hostB:key[{$X}]",{$X})+{$Y}+last("key[{$X}]",{$X})+{$Z}' +out: + formula: '1+last("hostB:key[{$X}]",100)+2+last("key[{$X}]",1)+3' +--- +test case: Expand '{$A}{$B}last("hostA:key[{$A}]",{$A}${B}){$A}{$B}' +in: + hosts: + - hostid: 1 + host: hostA + - hostid: 2 + host: hostB + macros: + - hostid: 1 + name: '{$A}' + value: '#' + - hostid: 1 + name: '{$B}' + value: '1' + hostid: 1 + formula: '{$A}{$B}last("hostA:key[{$A}]",{$A}{$B}){$A}{$B}' +out: + formula: '"#"1last("hostA:key[{$A}]",#1)"#"1' +... + + diff --git a/tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.c b/tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.c new file mode 100644 index 00000000000..77d9bee8658 --- /dev/null +++ b/tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.c @@ -0,0 +1,58 @@ +/* +** Zabbix +** Copyright (C) 2001-2020 Zabbix SIA +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**/ + +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" +#include "zbxmockutil.h" + +#include "zbxserver.h" +#include "common.h" +#include "zbxalgo.h" +#include "dbcache.h" +#include "mutexs.h" +#define ZBX_DBCONFIG_IMPL +#include "dbconfig.h" + +#include "configcache_mock.h" + +/****************************************************************************** + * * + * Function: zbx_mock_test_entry * + * * + ******************************************************************************/ +void zbx_mock_test_entry(void **state) +{ + char *returned_expression; + const char *expression, *expected_expression; + + ZBX_UNUSED(state); + + mock_config_init(); + mock_config_load_user_macros("in.macros"); + + expression = zbx_mock_get_parameter_string("in.expression"); + expected_expression = zbx_mock_get_parameter_string("out.expression"); + returned_expression = dc_expand_user_macros_in_expression(expression, NULL, 0); + zbx_mock_assert_str_eq("Expanded expression", expected_expression, returned_expression); + + zbx_free(returned_expression); + + mock_config_free(); +} diff --git a/tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.yaml b/tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.yaml new file mode 100644 index 00000000000..f0d195176fd --- /dev/null +++ b/tests/libs/zbxdbcache/dc_expand_user_macros_in_expression.yaml @@ -0,0 +1,200 @@ +--- +test case: Expand '{1} < {$A}' with {$A}=1 +in: + macros: + - name: '{$A}' + value: 1 + expression: '{1} < {$A}' +out: + expression: '{1} < 1' +--- +test case: Expand '{1} < "{$A}"' with {$A}=1 +in: + macros: + - name: '{$A}' + value: 1 + expression: '{1} < "{$A}"' +out: + expression: '{1} < "1"' +--- +test case: Expand '{1} < {$A}' +in: + macros: [] + expression: '{1} < {$A}' +out: + expression: '{1} < {$A}' +--- +test case: Expand '{1} < {$A}' with {$A}=x +in: + macros: + - name: '{$A}' + value: x + expression: '{1} < {$A}' +out: + expression: '{1} < "x"' +--- +test case: Expand '{1} < {$A}' with {$A}="x" +in: + macros: + - name: '{$A}' + value: '"x"' + expression: '{1} < {$A}' +out: + expression: '{1} < "\"x\""' +--- +test case: Expand '{1} < "{$A}"' with {$A}="x" +in: + macros: + - name: '{$A}' + value: '"x"' + expression: '{1} < "{$A}"' +out: + expression: '{1} < "\"x\""' +--- +test case: Expand '{1} < "{$A}"' +in: + macros: [] + expression: '{1} < "{$A}"' +out: + expression: '{1} < "{$A}"' +--- +test case: Expand '{1} = "x" and {2} < {$A}' with {$A}="x" +in: + macros: + - name: '{$A}' + value: '"x"' + expression: '{1} = "x" and {2} < {$A}' +out: + expression: '{1} = "x" and {2} < "\"x\""' +--- +test case: Expand '{1} < "{$A}{$B}"' with {$A}=1,'{$B}'="x" +in: + macros: + - name: '{$A}' + value: 1 + - name: '{$B}' + value: '"x"' + expression: '{1} < "{$A}{$B}"' +out: + expression: '{1} < "1\"x\""' +--- +test case: Expand '{1} < "{$A}{$B}"' with {$A}=1,'{$B}'=2 +in: + macros: + - name: '{$A}' + value: 1 + - name: '{$B}' + value: 2 + expression: '{1} < "{$A}{$B}"' +out: + expression: '{1} < "12"' +--- +test case: Expand '{1} < "{$A}{$B}"' with {$A}="x",'{$B}'="y" +in: + macros: + - name: '{$A}' + value: '"x"' + - name: '{$B}' + value: '"y"' + expression: '{1} < "{$A}{$B}"' +out: + expression: '{1} < "\"x\"\"y\""' +--- +test case: Expand '{1} < {$A}{$B}' with {$A}="x",'{$B}'="y" +in: + macros: + - name: '{$A}' + value: '"x"' + - name: '{$B}' + value: '"y"' + expression: '{1} < {$A}{$B}' +out: + expression: '{1} < "\"x\"""\"y\""' +--- +test case: Expand '{1} < {$A}{$B}' with {$A}=1,'{$B}'=0 +in: + macros: + - name: '{$A}' + value: 1 + - name: '{$B}' + value: 0 + expression: '{1} < {$A}{$B}' +out: + expression: '{1} < 10' +--- +test case: Expand '{1} < "\"{$A}\"{$B}\""' with {$A}=\",'{$B}'=\ +in: + macros: + - name: '{$A}' + value: '\"' + - name: '{$B}' + value: '\' + expression: '{1} < "\"{$A}\"{$B}\""' +out: + expression: '{1} < "\"\\\"\"\\\""' +--- +test case: Expand '{1} < " \" {$A} \" {$B} \" " and {$A}<>"\"{$B}" and " {$A} " = " {$A} "' with {$A}=",'{$B}'=\\ +in: + macros: + - name: '{$A}' + value: '"' + - name: '{$B}' + value: '\\' + expression: '{1} < " \" {$A} \" {$B} \" " and {$A}<>"\"{$B}" and " {$A} " = " {$A} "' +out: + expression: '{1} < " \" \" \" \\\\ \" " and "\""<>"\"\\\\" and " \" " = " \" "' +--- +test case: Expand '{1} < {$A}' with {$A}="" +in: + macros: + - name: '{$A}' + value: '' + expression: '{1} < {$A}' +out: + expression: '{1} < ""' +--- +test case: Expand '{1} < "{$A}"' with {$A}="" +in: + macros: + - name: '{$A}' + value: '' + expression: '{1} < "{$A}"' +out: + expression: '{1} < ""' +--- +test case: Expand '{1} = {$A:"x:\"1\""} or {2} = {$A:"y"}' with {$A:"x:\"1\""}=ax, {$A}=a +in: + macros: + - name: '{$A:"x:\"1\""}' + value: ax + - name: '{$A}' + value: a + expression: '{1} = {$A:"x:\"1\""} or {2} = {$A:"y"}' +out: + expression: '{1} = "ax" or {2} = "a"' +--- +test case: Expand '{1} < {$A:"x:\"1\""}' +in: + macros: [] + expression: '{1} < {$A:"x:\"1\""}' +out: + expression: '{1} < {$A:"x:\"1\""}' +--- +test case: Expand '{2000} < {$A}' with {$A}=2e+3 +in: + macros: + - name: '{$A}' + value: 2e+3 + expression: '{2000} < {$A}' +out: + expression: '{2000} < 2e+3' +--- +test case: Expand '{1} < "\\\"a\"" and {1} = {$A}' with {$A}="b" +in: + macros: + - name: '{$A}' + value: '"b"' + expression: '{1} < "\\\"a\"" and {1} = {$A}' +out: + expression: '{1} < "\\\"a\"" and {1} = "\"b\""' +... diff --git a/tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.c b/tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.c new file mode 100644 index 00000000000..db4b1fa0c73 --- /dev/null +++ b/tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.c @@ -0,0 +1,59 @@ +/* +** Zabbix +** Copyright (C) 2001-2020 Zabbix SIA +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**/ + +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" +#include "zbxmockutil.h" + +#include "zbxserver.h" +#include "common.h" +#include "zbxalgo.h" +#include "dbcache.h" +#include "mutexs.h" + +#define ZBX_DBCONFIG_IMPL +#include "dbconfig.h" + +#include "configcache_mock.h" + +/****************************************************************************** + * * + * Function: zbx_mock_test_entry * + * * + ******************************************************************************/ +void zbx_mock_test_entry(void **state) +{ + char *returned_params; + const char *params, *expected_params; + + ZBX_UNUSED(state); + + mock_config_init(); + mock_config_load_user_macros("in.macros"); + + params = zbx_mock_get_parameter_string("in.params"); + expected_params = zbx_mock_get_parameter_string("out.params"); + returned_params = dc_expand_user_macros_in_func_params(params, 1); + zbx_mock_assert_str_eq("Expanded parameters", expected_params, returned_params); + + zbx_free(returned_params); + + mock_config_free(); +} diff --git a/tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.yaml b/tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.yaml new file mode 100644 index 00000000000..d234696adbe --- /dev/null +++ b/tests/libs/zbxdbcache/dc_expand_user_macros_in_func_params.yaml @@ -0,0 +1,147 @@ +--- +test case: Expand '{$A}' with {$A}=1 +in: + macros: + - hostid: 1 + name: '{$A}' + value: 1 + params: '{$A}' +out: + params: '1' +--- +test case: Expand '{$A}' with {$A}=(1) +in: + macros: + - hostid: 1 + name: '{$A}' + value: (1) + params: '{$A}' +out: + params: '"(1)"' +--- +test case: Expand '' +in: + macros: [] + params: '' +out: + params: '' +--- +test case: Expand ' ' +in: + macros: [] + params: ' ' +out: + params: '' +--- +test case: Expand '{$A}' +in: + macros: [] + params: '{$A}' +out: + params: '{$A}' +--- +test case: Expand ',' +in: + macros: [] + params: ',' +out: + params: ',' +--- +test case: Expand '{$A}' with {$A}="1" +in: + macros: + - hostid: 1 + name: '{$A}' + value: '"1"' + params: '{$A}' +out: + params: '"\"1\""' +--- +test case: Expand '"{$A}"' with {$A}=1 +in: + macros: + - hostid: 1 + name: '{$A}' + value: 1 + params: '"{$A}"' +out: + params: '"1"' +--- +test case: Expand '1 ,2 ,3 ' +in: + macros: [] + params: '1 ,2 ,3 ' +out: + params: '1 ,2 ,3 ' +--- +test case: Expand '1 , ,3 ' +in: + macros: [] + params: '1 , ,3 ' +out: + params: '1 ,,3 ' +--- +test case: Expand ',2 ,3 ' +in: + macros: [] + params: ',2 ,3 ' +out: + params: ',2 ,3 ' +--- +test case: Expand '{$A} ,{$B} ,{$C} with {$A}=1, {$B}="2", {$C}=(3)' +in: + macros: + - hostid: 1 + name: '{$A}' + value: 1 + - hostid: 1 + name: '{$B}' + value: '"2"' + - hostid: 1 + name: '{$C}' + value: (3) + params: '{$A} ,{$B} ,{$C} ' +out: + params: '1 ,"\"2\" ","(3) "' +--- +test case: Expand '{$A}{$B}{$C} with {$A}=1, {$B}="2", {$C}=(3)' +in: + macros: + - hostid: 1 + name: '{$A}' + value: 1 + - hostid: 1 + name: '{$B}' + value: '"2"' + - hostid: 1 + name: '{$C}' + value: (3) + params: '{$A}{$B}{$C}' +out: + params: '"1\"2\"(3)"' +--- +test case: Expand '{$A}' with {$A}=1,2,3 +in: + macros: + - hostid: 1 + name: '{$A}' + value: 1,2,3 + params: '{$A}' +out: + params: '"1,2,3"' +--- +test case: Expand '{$A},{$B}' with {$A}='', {$B}='' +in: + macros: + - hostid: 1 + name: '{$A}' + value: '' + - hostid: 1 + name: '{$B}' + value: '' + params: '{$A},{$B}' +out: + params: ',' +... + + diff --git a/tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.c b/tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.c deleted file mode 100644 index 162c1055352..00000000000 --- a/tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.c +++ /dev/null @@ -1,187 +0,0 @@ -/* -** Zabbix -** Copyright (C) 2001-2020 Zabbix SIA -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -**/ - -#include "zbxmocktest.h" -#include "zbxmockdata.h" -#include "zbxmockassert.h" -#include "zbxmockutil.h" - -#include "zbxserver.h" -#include "common.h" -#include "zbxalgo.h" -#include "dbcache.h" -#include "mutexs.h" -#define ZBX_DBCONFIG_IMPL -#include "dbconfig.h" - -static zbx_vector_ptr_t macros; - -void *__wrap_zbx_hashset_search(zbx_hashset_t *hs, const void *data) -{ - int i; - const ZBX_DC_GMACRO_M *query = (const ZBX_DC_GMACRO_M *)data; - - if (hs != &config->gmacros_m) - return NULL; - - for (i = 0; i < macros.values_num; i++) - { - ZBX_DC_GMACRO_M *gm = (ZBX_DC_GMACRO_M *)macros.values[i]; - if (0 == strcmp(gm->macro, query->macro)) - return gm; - } - - return NULL; -} - -static int config_gmacro_context_compare(const void *d1, const void *d2) -{ - const ZBX_DC_GMACRO *m1 = *(const ZBX_DC_GMACRO **)d1; - const ZBX_DC_GMACRO *m2 = *(const ZBX_DC_GMACRO **)d2; - - /* macros without context have higher priority than macros with */ - if (NULL == m1->context) - return NULL == m2->context ? 0 : -1; - - if (NULL == m2->context) - return 1; - - /* CONDITION_OPERATOR_EQUAL (0) has higher priority than CONDITION_OPERATOR_REGEXP (8) */ - ZBX_RETURN_IF_NOT_EQUAL(m1->context_op, m2->context_op); - - return strcmp(m1->context, m2->context); -} - - -static void init_macros(const char *path) -{ - zbx_mock_handle_t hmacros, handle; - zbx_mock_error_t err; - const char *macro_name, *macro_value; - int i; - ZBX_DC_GMACRO_M *gm; - ZBX_DC_GMACRO *macro; - - zbx_vector_ptr_create(¯os); - - hmacros = zbx_mock_get_parameter_handle(path); - while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hmacros, &handle)))) - { - char *name = NULL, *context = NULL; - - if (ZBX_MOCK_SUCCESS != err) - { - fail_msg("Cannot read 'macros' element #%d: %s", macros.values_num, - zbx_mock_error_string(err)); - } - - macro_name = zbx_mock_get_object_member_string(handle, "name"); - macro_value = zbx_mock_get_object_member_string(handle, "value"); - - if (SUCCEED != zbx_user_macro_parse_dyn(macro_name, &name, &context, NULL, NULL)) - fail_msg("invalid user macro: %s", macro_name); - - for (i = 0; i < macros.values_num; i++) - { - gm = (ZBX_DC_GMACRO_M *)macros.values[i]; - if (0 == strcmp(gm->macro, name)) - break; - } - - if (i == macros.values_num) - { - gm = zbx_malloc(NULL, sizeof(ZBX_DC_GMACRO_M)); - gm->macro = zbx_strdup(NULL, name); - zbx_vector_ptr_create(&gm->gmacros); - zbx_vector_ptr_append(¯os, gm); - } - - macro = (ZBX_DC_GMACRO *)zbx_malloc(0, sizeof(ZBX_DC_GMACRO)); - memset(macro, 0, sizeof(ZBX_DC_GMACRO)); - macro->macro = name; - macro->context = context; - macro->value = macro_value; - zbx_vector_ptr_append(&gm->gmacros, macro); - } - - for (i = 0; i < macros.values_num; i++) - { - gm = (ZBX_DC_GMACRO_M *)macros.values[i]; - zbx_vector_ptr_sort(&gm->gmacros, config_gmacro_context_compare); - } -} - -static void free_string(const char *str) -{ - char *ptr = (char *)str; - zbx_free(ptr); -} - -static void free_macros() -{ - ZBX_DC_GMACRO_M *gm; - ZBX_DC_GMACRO *macro; - int i, j; - - for (i = 0; i < macros.values_num; i++) - { - gm = (ZBX_DC_GMACRO_M *)macros.values[i]; - for (j = 0; j < gm->gmacros.values_num; j++) - { - macro = (ZBX_DC_GMACRO *)gm->gmacros.values[j]; - free_string(macro->macro); - free_string(macro->context); - zbx_free(macro); - } - zbx_vector_ptr_destroy(&gm->gmacros); - free_string(gm->macro); - zbx_free(gm); - } - - zbx_vector_ptr_destroy(¯os); -} - -/****************************************************************************** - * * - * Function: zbx_mock_test_entry * - * * - ******************************************************************************/ -void zbx_mock_test_entry(void **state) -{ - char *returned_expression; - const char *expression, *expected_expression; - ZBX_DC_CONFIG dc_config = {0}; - - ZBX_UNUSED(state); - - config = &dc_config; - init_macros("in.macros"); - - expression = zbx_mock_get_parameter_string("in.expression"); - expected_expression = zbx_mock_get_parameter_string("out.expression"); - - /* the macro expansion relies on wrapped zbx_hashset_search which returns mocked */ - /* macros when used with global macro index hashset */ - returned_expression = zbx_dc_expand_user_macros_in_expression(expression, NULL, 0); - zbx_mock_assert_str_eq("Expanded expression", expected_expression, returned_expression); - - zbx_free(returned_expression); - - free_macros(); -} diff --git a/tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.yaml b/tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.yaml deleted file mode 100644 index f0d195176fd..00000000000 --- a/tests/libs/zbxdbcache/zbx_dc_expand_user_macros_in_expression.yaml +++ /dev/null @@ -1,200 +0,0 @@ ---- -test case: Expand '{1} < {$A}' with {$A}=1 -in: - macros: - - name: '{$A}' - value: 1 - expression: '{1} < {$A}' -out: - expression: '{1} < 1' ---- -test case: Expand '{1} < "{$A}"' with {$A}=1 -in: - macros: - - name: '{$A}' - value: 1 - expression: '{1} < "{$A}"' -out: - expression: '{1} < "1"' ---- -test case: Expand '{1} < {$A}' -in: - macros: [] - expression: '{1} < {$A}' -out: - expression: '{1} < {$A}' ---- -test case: Expand '{1} < {$A}' with {$A}=x -in: - macros: - - name: '{$A}' - value: x - expression: '{1} < {$A}' -out: - expression: '{1} < "x"' ---- -test case: Expand '{1} < {$A}' with {$A}="x" -in: - macros: - - name: '{$A}' - value: '"x"' - expression: '{1} < {$A}' -out: - expression: '{1} < "\"x\""' ---- -test case: Expand '{1} < "{$A}"' with {$A}="x" -in: - macros: - - name: '{$A}' - value: '"x"' - expression: '{1} < "{$A}"' -out: - expression: '{1} < "\"x\""' ---- -test case: Expand '{1} < "{$A}"' -in: - macros: [] - expression: '{1} < "{$A}"' -out: - expression: '{1} < "{$A}"' ---- -test case: Expand '{1} = "x" and {2} < {$A}' with {$A}="x" -in: - macros: - - name: '{$A}' - value: '"x"' - expression: '{1} = "x" and {2} < {$A}' -out: - expression: '{1} = "x" and {2} < "\"x\""' ---- -test case: Expand '{1} < "{$A}{$B}"' with {$A}=1,'{$B}'="x" -in: - macros: - - name: '{$A}' - value: 1 - - name: '{$B}' - value: '"x"' - expression: '{1} < "{$A}{$B}"' -out: - expression: '{1} < "1\"x\""' ---- -test case: Expand '{1} < "{$A}{$B}"' with {$A}=1,'{$B}'=2 -in: - macros: - - name: '{$A}' - value: 1 - - name: '{$B}' - value: 2 - expression: '{1} < "{$A}{$B}"' -out: - expression: '{1} < "12"' ---- -test case: Expand '{1} < "{$A}{$B}"' with {$A}="x",'{$B}'="y" -in: - macros: - - name: '{$A}' - value: '"x"' - - name: '{$B}' - value: '"y"' - expression: '{1} < "{$A}{$B}"' -out: - expression: '{1} < "\"x\"\"y\""' ---- -test case: Expand '{1} < {$A}{$B}' with {$A}="x",'{$B}'="y" -in: - macros: - - name: '{$A}' - value: '"x"' - - name: '{$B}' - value: '"y"' - expression: '{1} < {$A}{$B}' -out: - expression: '{1} < "\"x\"""\"y\""' ---- -test case: Expand '{1} < {$A}{$B}' with {$A}=1,'{$B}'=0 -in: - macros: - - name: '{$A}' - value: 1 - - name: '{$B}' - value: 0 - expression: '{1} < {$A}{$B}' -out: - expression: '{1} < 10' ---- -test case: Expand '{1} < "\"{$A}\"{$B}\""' with {$A}=\",'{$B}'=\ -in: - macros: - - name: '{$A}' - value: '\"' - - name: '{$B}' - value: '\' - expression: '{1} < "\"{$A}\"{$B}\""' -out: - expression: '{1} < "\"\\\"\"\\\""' ---- -test case: Expand '{1} < " \" {$A} \" {$B} \" " and {$A}<>"\"{$B}" and " {$A} " = " {$A} "' with {$A}=",'{$B}'=\\ -in: - macros: - - name: '{$A}' - value: '"' - - name: '{$B}' - value: '\\' - expression: '{1} < " \" {$A} \" {$B} \" " and {$A}<>"\"{$B}" and " {$A} " = " {$A} "' -out: - expression: '{1} < " \" \" \" \\\\ \" " and "\""<>"\"\\\\" and " \" " = " \" "' ---- -test case: Expand '{1} < {$A}' with {$A}="" -in: - macros: - - name: '{$A}' - value: '' - expression: '{1} < {$A}' -out: - expression: '{1} < ""' ---- -test case: Expand '{1} < "{$A}"' with {$A}="" -in: - macros: - - name: '{$A}' - value: '' - expression: '{1} < "{$A}"' -out: - expression: '{1} < ""' ---- -test case: Expand '{1} = {$A:"x:\"1\""} or {2} = {$A:"y"}' with {$A:"x:\"1\""}=ax, {$A}=a -in: - macros: - - name: '{$A:"x:\"1\""}' - value: ax - - name: '{$A}' - value: a - expression: '{1} = {$A:"x:\"1\""} or {2} = {$A:"y"}' -out: - expression: '{1} = "ax" or {2} = "a"' ---- -test case: Expand '{1} < {$A:"x:\"1\""}' -in: - macros: [] - expression: '{1} < {$A:"x:\"1\""}' -out: - expression: '{1} < {$A:"x:\"1\""}' ---- -test case: Expand '{2000} < {$A}' with {$A}=2e+3 -in: - macros: - - name: '{$A}' - value: 2e+3 - expression: '{2000} < {$A}' -out: - expression: '{2000} < 2e+3' ---- -test case: Expand '{1} < "\\\"a\"" and {1} = {$A}' with {$A}="b" -in: - macros: - - name: '{$A}' - value: '"b"' - expression: '{1} < "\\\"a\"" and {1} = {$A}' -out: - expression: '{1} < "\\\"a\"" and {1} = "\"b\""' -... -- cgit v1.2.3