From 83a338b1a0e5691f966c98d327d1f03c80147fdd Mon Sep 17 00:00:00 2001 From: Andrejs Kozlovs Date: Tue, 9 Apr 2019 13:32:29 +0000 Subject: .......... [DEV-873] converted item_preproc_xpath function cunit test to cmocka framework --- tests/zabbix_server/preprocessor/Makefile.am | 23 ++++- .../zabbix_server/preprocessor/item_preproc_test.c | 23 +++++ .../zabbix_server/preprocessor/item_preproc_test.h | 24 +++++ .../preprocessor/item_preproc_xpath.c | 60 ++++++++++++ .../preprocessor/item_preproc_xpath.yaml | 105 +++++++++++++++++++++ 5 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 tests/zabbix_server/preprocessor/item_preproc_test.c create mode 100644 tests/zabbix_server/preprocessor/item_preproc_test.h create mode 100644 tests/zabbix_server/preprocessor/item_preproc_xpath.c create mode 100644 tests/zabbix_server/preprocessor/item_preproc_xpath.yaml (limited to 'tests/zabbix_server') diff --git a/tests/zabbix_server/preprocessor/Makefile.am b/tests/zabbix_server/preprocessor/Makefile.am index 5cdcf1209ee..6b7719e104c 100644 --- a/tests/zabbix_server/preprocessor/Makefile.am +++ b/tests/zabbix_server/preprocessor/Makefile.am @@ -1,5 +1,14 @@ if SERVER -noinst_PROGRAMS = zbx_item_preproc +SERVER_tests = zbx_item_preproc + +if HAVE_LIBXML2 +SERVER_tests += item_preproc_xpath +endif + +noinst_PROGRAMS = $(SERVER_tests) + +COMMON_SRC_FILES = \ + ../../zbxmocktest.h JSON_LIBS = \ $(top_srcdir)/tests/libzbxmocktest.a \ @@ -32,4 +41,16 @@ zbx_item_preproc_LDADD += @SERVER_LIBS@ zbx_item_preproc_LDFLAGS = @SERVER_LDFLAGS@ zbx_item_preproc_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ + +item_preproc_xpath_SOURCES = \ + ../../../src/zabbix_server/preprocessor/item_preproc.c \ + item_preproc_xpath.c \ + $(COMMON_SRC_FILES) + +item_preproc_xpath_LDADD = $(JSON_LIBS) + +item_preproc_xpath_LDADD += @SERVER_LIBS@ +item_preproc_xpath_LDFLAGS = @SERVER_LDFLAGS@ + +item_preproc_xpath_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ endif diff --git a/tests/zabbix_server/preprocessor/item_preproc_test.c b/tests/zabbix_server/preprocessor/item_preproc_test.c new file mode 100644 index 00000000000..27c58a97701 --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_test.c @@ -0,0 +1,23 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 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. +**/ + +int zbx_item_preproc_xpath(zbx_variant_t *value, const char *params, char **errmsg) +{ + return item_preproc_xpath(value, params, errmsg); +} diff --git a/tests/zabbix_server/preprocessor/item_preproc_test.h b/tests/zabbix_server/preprocessor/item_preproc_test.h new file mode 100644 index 00000000000..203b187878e --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_test.h @@ -0,0 +1,24 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 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. +**/ +#ifndef ITEM_PREPROC_TEST_H +#define ITEM_PREPROC_TEST_H + +int zbx_item_preproc_xpath(zbx_variant_t *value, const char *params, char **errmsg); + +#endif diff --git a/tests/zabbix_server/preprocessor/item_preproc_xpath.c b/tests/zabbix_server/preprocessor/item_preproc_xpath.c new file mode 100644 index 00000000000..7a9ff5b0bf7 --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_xpath.c @@ -0,0 +1,60 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 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 +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockutil.h" +#include "zbxmockassert.h" +#include "common.h" + +#include "item_preproc_test.h" +#include "zbxembed.h" + +zbx_es_t es_engine; + +void zbx_mock_test_entry(void **state) +{ + zbx_variant_t value; + const char *xml; + const char *xpath, *exp_xml; + char *errmsg = NULL; + int act_ret, exp_ret; + + ZBX_UNUSED(state); + + xml = zbx_mock_get_parameter_string("in.xml"); + xpath = zbx_mock_get_parameter_string("in.xpath"); + exp_xml = zbx_mock_get_parameter_string("out.result"); + zbx_variant_set_str(&value, zbx_strdup(NULL, xml)); + + act_ret = zbx_item_preproc_xpath(&value, xpath, &errmsg); + + exp_ret = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return")); + zbx_mock_assert_int_eq("return value", exp_ret, act_ret); + + if (FAIL == act_ret) + { + zbx_mock_assert_ptr_ne("error message", NULL, errmsg); + zbx_free(errmsg); + } + else + zbx_mock_assert_str_eq("result", exp_xml, value.data.str); + + zbx_variant_clear(&value); +} diff --git a/tests/zabbix_server/preprocessor/item_preproc_xpath.yaml b/tests/zabbix_server/preprocessor/item_preproc_xpath.yaml new file mode 100644 index 00000000000..bec529d3e45 --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_xpath.yaml @@ -0,0 +1,105 @@ +--- +test case: 'empty input parameters' +in: + xml: '' + xpath: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'single start tag' +in: + xml: '' + xpath: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'single end tag' +in: + xml: '' + xpath: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'wrong operation format' +in: + xml: '' + xpath: '/a[\' +out: + result: '' + return: 'FAIL' +--- +test case: 'wrong operation expression' +in: + xml: '' + xpath: '1 div 0' +out: + result: '' + return: 'FAIL' +--- +test case: 'wrong operation format 2' +in: + xml: '' + xpath: '-a' +out: + result: '' + return: 'FAIL' +--- +test case: 'empty output' +in: + xml: '' + xpath: '/b' +out: + result: '' + return: 'SUCCEED' +--- +test case: 'successful expression' +in: + xml: '' + xpath: '3 div 2' +out: + result: '1.5' + return: 'SUCCEED' +--- +test case: 'return end tag' +in: + xml: '' + xpath: '/a' +out: + result: '' + return: 'SUCCEED' +--- +test case: 'return text' +in: + xml: '1' + xpath: '/a/text()' +out: + result: '1' + return: 'SUCCEED' +--- +test case: 'return string' +in: + xml: '1' + xpath: 'string(/a)' +out: + result: '1' + return: 'SUCCEED' +--- +test case: 'return attribute' +in: + xml: '1' + xpath: 'string(/a/@b)' +out: + result: '10' + return: 'SUCCEED' +--- +test case: 'return pattern' +in: + xml: '' + xpath: '//*[@x="1"]' +out: + result: '' + return: 'SUCCEED' +... -- cgit v1.2.3