diff options
author | Aleksejs Sestakovs <aleksejs.sestakovs@zabbix.com> | 2019-10-02 15:40:51 +0300 |
---|---|---|
committer | Aleksejs Sestakovs <aleksejs.sestakovs@zabbix.com> | 2019-10-02 15:42:49 +0300 |
commit | b9a751c9b33bd0e569671d466d0cf52814f54969 (patch) | |
tree | ab09f472e98769cd4c504a7620226ecc92c0db75 /tests/zabbix_server/preprocessor | |
parent | 1c83315a77f2f47ea1dbc700b6a6f14feca22218 (diff) |
.......... [ZBXNEXT-3970] added test cases
Diffstat (limited to 'tests/zabbix_server/preprocessor')
7 files changed, 537 insertions, 0 deletions
diff --git a/tests/zabbix_server/preprocessor/Makefile.am b/tests/zabbix_server/preprocessor/Makefile.am index b1fc815ae02..c5d7512cf40 100644 --- a/tests/zabbix_server/preprocessor/Makefile.am +++ b/tests/zabbix_server/preprocessor/Makefile.am @@ -1,5 +1,6 @@ if SERVER SERVER_tests = zbx_item_preproc +SERVER_tests += item_preproc_csv_to_json if HAVE_LIBXML2 SERVER_tests += item_preproc_xpath @@ -54,4 +55,16 @@ item_preproc_xpath_LDFLAGS = @SERVER_LDFLAGS@ item_preproc_xpath_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ +item_preproc_csv_to_json_SOURCES = \ + ../../../src/zabbix_server/preprocessor/item_preproc.c \ + item_preproc_csv_to_json.c \ + $(COMMON_SRC_FILES) + +item_preproc_csv_to_json_LDADD = $(JSON_LIBS) + +item_preproc_csv_to_json_LDADD += @SERVER_LIBS@ +item_preproc_csv_to_json_LDFLAGS = @SERVER_LDFLAGS@ + +item_preproc_csv_to_json_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ + endif diff --git a/tests/zabbix_server/preprocessor/item_preproc_csv_to_json.c b/tests/zabbix_server/preprocessor/item_preproc_csv_to_json.c new file mode 100644 index 00000000000..c991b49d36d --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_csv_to_json.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 <stdio.h> +#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 *csv; + const char *xpath, *exp_json; + char *errmsg = NULL; + int act_ret, exp_ret; + + ZBX_UNUSED(state); + + csv = zbx_mock_get_parameter_string("in.csv"); + xpath = zbx_mock_get_parameter_string("in.params"); + exp_json = zbx_mock_get_parameter_string("out.result"); + zbx_variant_set_str(&value, zbx_strdup(NULL, csv)); + + act_ret = zbx_item_preproc_csv_to_json(&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_json, value.data.str); + + zbx_variant_clear(&value); +} diff --git a/tests/zabbix_server/preprocessor/item_preproc_csv_to_json.yaml b/tests/zabbix_server/preprocessor/item_preproc_csv_to_json.yaml new file mode 100644 index 00000000000..f0512de77bc --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_csv_to_json.yaml @@ -0,0 +1,394 @@ +--- +test case: 'empty parameters' +in: + csv: '' + params: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'empty input #1' +in: + csv: '' + params: ",\n\n1" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'empty input #2' +in: + csv: '' + params: ",\n\n0" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'single char input #1' +in: + csv: 'A' + params: ",\n\n1" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'single char input #2' +in: + csv: 'A' + params: ",\n\n0" +out: + result: '[{"1":"A"}]' + return: 'SUCCEED' +--- +test case: 'empty line input #1' +in: + csv: "\n" + params: ",\n\n1" +out: + result: '[{"":""}]' + return: 'SUCCEED' +--- +test case: 'empty line input #2' +in: + csv: "\n" + params: ",\n\n0" +out: + result: '[{},{}]' + return: 'SUCCEED' +--- +test case: 'multiple empty lines input #1' +in: + csv: "\n\n" + params: ",\n\n1" +out: + result: '[{"":""},{"":""}]' + return: 'SUCCEED' +--- +test case: 'multiple empty lines input #2' +in: + csv: "\n\n" + params: ",\n\n0" +out: + result: '[{},{},{}]' + return: 'SUCCEED' +--- +test case: 'csv input with header line' +in: + csv: | + nr,name,addr + 1,abc,xyz + 2,cba,zyx + params: "\n\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":"zyx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'csv input without header line' +in: + csv: | + nr,name,addr + 1,abc,xyz + 2,cba,zyx + params: "\n\n0" +out: + result: '[{"1":"nr","2":"name","3":"addr"},{"1":"1","2":"abc","3":"xyz"},{"1":"2","2":"cba","3":"zyx"},{}]' + return: 'SUCCEED' +--- +test case: 'CSV wihtout trailing new line' +in: + csv: "nr,\"name\",addr\n\"1\",abc,xyz\n\"2\",\"cba\",\"zyx\"\n" + params: "\n\"\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":"zyx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'CSV with header line only' +in: + csv: 'nr,name,addr' + params: "\n\n1" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'one line CSV' +in: + csv: 'nr,name,addr' + params: "\n\n0" +out: + result: '[{"1":"nr","2":"name","3":"addr"}]' + return: 'SUCCEED' +--- +test case: 'multi-byte UTF-8 delimiter' +in: + csv: | + nrыnameыaddr + 1ыabcыxyz + 2ыcbaыzyx + params: "ы\n\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":"zyx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'multi-byte UTF-8 quotation character' +in: + csv: | + nr,ыnameы,addr + 1,abc,ыxyzы + ы2ы,ыcbaы,ыzyxы + params: "\nы\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":"zyx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'multi-byte UTF-8 delimiter and quotation characters' +in: + csv: | + nrфыnameыфaddr + 1фabcфыxyzы + ы2ыфыфcbaфыфыzyфxы + params: "ф\nы\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"фcbaф","addr":"zyфx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'single column CSV input #1' +in: + csv: | + nr + 1 + 2 + params: "\n\n1" +out: + result: '[{"nr":"1"},{"nr":"2"},{"nr":""}]' + return: 'SUCCEED' +--- +test case: 'single column CSV input #2' +in: + csv: | + nr + 1 + 2 + params: "\n\n0" +out: + result: '[{"1":"nr"},{"1":"1"},{"1":"2"},{}]' + return: 'SUCCEED' +--- +test case: 'less fields in one row #1' +in: + csv: | + nr,name,addr + 1,abc,xyz + 2,cba + 3,def,fed + params: "\n\n0" +out: + result: '[{"1":"nr","2":"name","3":"addr"},{"1":"1","2":"abc","3":"xyz"},{"1":"2","2":"cba"},{"1":"3","2":"def","3":"fed"},{}]' + return: 'SUCCEED' +--- +test case: 'less fields in one row #2' +in: + csv: | + nr,name,addr + 1,abc,xyz + 2,cba + 3,def,fed + params: "\n\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":""},{"nr":"3","name":"def","addr":"fed"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'more fields in one row #1' +in: + csv: | + nr,name,addr + 1,abc,xyz + 2,cba,zyx,extra + 3,def,fed + params: "\n\n0" +out: + result: '[{"1":"nr","2":"name","3":"addr"},{"1":"1","2":"abc","3":"xyz"},{"1":"2","2":"cba","3":"zyx","4":"extra"},{"1":"3","2":"def","3":"fed"},{}]' + return: 'SUCCEED' +--- +test case: 'more fields in one row #2' +in: + csv: | + nr,name,addr + 1,abc,xyz + 2,cba,zyx,extra + 3,def,fed + params: "\n\n1" +out: + result: '' + return: 'FAIL' +--- +test case: 'quoted fields' +in: + csv: | + nr,|name, surname|,addr + |1|,ab|c|,|x||y,z| + 2,|||cba|||,|||zyx||| + params: "\n|\n1" +out: + result: '[{"nr":"1","name, surname":"ab|c|","addr":"x|y,z"},{"nr":"2","name, surname":"|cba|","addr":"|zyx|"},{"nr":"","name, surname":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'empty fields #1' +in: + csv: | + ,, + ,, + params: "\n\n1" +out: + result: '' + return: 'FAIL' +--- +test case: 'empty fields #2' +in: + csv: | + ,, + ,, + params: "\n\n0" +out: + result: '[{"1":"","2":"","3":""},{"1":"","2":"","3":""},{}]' + return: 'SUCCEED' +--- +test case: 'empty fields #3' +in: + csv: | + ||,||,|| + ||,||,|| + params: "\n|\n0" +out: + result: '[{"1":"","2":"","3":""},{"1":"","2":"","3":""},{}]' + return: 'SUCCEED' +--- +test case: 'duplicated column names' +in: + csv: | + nr,name,name + 1,abc,xyz + 2,cba,zyx + params: "\n\n1" +out: + result: '' + return: 'FAIL' +--- +test case: 'unclosed quoted field #1' +in: + csv: | + nr,name,addr + 1,|abc,xyz + 2,cba,zyx + params: "\n|\n1" +out: + result: '' + return: 'FAIL' +--- +test case: 'unclosed quoted field #2' +in: + csv: | + nr,name,addr + 1,|abc| ,xyz + 2,cba,zyx + params: "\n|\n1" +out: + result: '' + return: 'FAIL' +--- +test case: 'CSV with sep line #1' +in: + csv: | + Sep=. + nr.name.addr + 1.abc.xyz + 2.cba.zyx + params: "\n\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":"zyx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'CSV with sep line #2' +in: + csv: | + Sep=. + nr.name.addr + 1.abc.xyz + 2.cba.zyx + params: "\n\n0" +out: + result: '[{"1":"nr","2":"name","3":"addr"},{"1":"1","2":"abc","3":"xyz"},{"1":"2","2":"cba","3":"zyx"},{}]' + return: 'SUCCEED' +--- +test case: 'CSV with unsupported sep line #1' +in: + csv: | + Sep=.. + nr.name.addr + 1.abc.xyz + 2.cba.zyx + params: "\n\n1" +out: + result: '[{"Sep=..":"nr.name.addr"},{"Sep=..":"1.abc.xyz"},{"Sep=..":"2.cba.zyx"},{"Sep=..":""}]' + return: 'SUCCEED' +--- +test case: 'CSV with unsupported sep line #2' +in: + csv: | + sep=символы + 1,2,3,4,5,6 + params: "\n\n0" +out: + result: '[{"1":"sep=символы"},{"1":"1","2":"2","3":"3","4":"4","5":"5","6":"6"},{}]' + return: 'SUCCEED' +--- +test case: 'CSV with sep line #3' +in: + csv: | + Sep=! + nr.name.addr + 1.abc.xyz + 2.cba.zyx + params: ".\n\n1" +out: + result: '[{"nr":"1","name":"abc","addr":"xyz"},{"nr":"2","name":"cba","addr":"zyx"},{"nr":"","name":"","addr":""}]' + return: 'SUCCEED' +--- +test case: 'CSV with sep line only #1' +in: + csv: 'Sep=.' + params: "\n\n1" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'CSV with sep line only #2' +in: + csv: 'Sep=.' + params: "\n\n0" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'CSV with unsupported sep line only #1' +in: + csv: 'Sep=..' + params: "\n\n1" +out: + result: '[]' + return: 'SUCCEED' +--- +test case: 'CSV with unsupported sep line only #2' +in: + csv: 'Sep=..' + params: "\n\n0" +out: + result: '[{"1":"Sep=.."}]' + return: 'SUCCEED' +--- +test case: 'cr/nl line endings' +in: + csv: "Sep=.\r\nname.addr\r\nabc.xyz\r\n" + params: "\n\n1" +out: + result: '[{"name":"abc","addr":"xyz"},{"name":"","addr":""}]' + return: 'SUCCEED' +... diff --git a/tests/zabbix_server/preprocessor/item_preproc_test.c b/tests/zabbix_server/preprocessor/item_preproc_test.c index c2bd74d3a7f..a5ff20ff678 100644 --- a/tests/zabbix_server/preprocessor/item_preproc_test.c +++ b/tests/zabbix_server/preprocessor/item_preproc_test.c @@ -23,3 +23,8 @@ int zbx_item_preproc_xpath(zbx_variant_t *value, const char *params, char **errm { return item_preproc_xpath(value, params, errmsg); } + +int zbx_item_preproc_csv_to_json(zbx_variant_t *value, const char *params, char **errmsg) +{ + return item_preproc_csv_to_json(value, params, errmsg); +} diff --git a/tests/zabbix_server/preprocessor/item_preproc_test.h b/tests/zabbix_server/preprocessor/item_preproc_test.h index 203b187878e..08be69a9ea7 100644 --- a/tests/zabbix_server/preprocessor/item_preproc_test.h +++ b/tests/zabbix_server/preprocessor/item_preproc_test.h @@ -20,5 +20,6 @@ #define ITEM_PREPROC_TEST_H int zbx_item_preproc_xpath(zbx_variant_t *value, const char *params, char **errmsg); +int zbx_item_preproc_csv_to_json(zbx_variant_t *value, const char *params, char **errmsg); #endif diff --git a/tests/zabbix_server/preprocessor/zbx_item_preproc.c b/tests/zabbix_server/preprocessor/zbx_item_preproc.c index 7ee73e14670..7c52f585d8c 100644 --- a/tests/zabbix_server/preprocessor/zbx_item_preproc.c +++ b/tests/zabbix_server/preprocessor/zbx_item_preproc.c @@ -78,6 +78,8 @@ static int str_to_preproc_type(const char *str) return ZBX_PREPROC_PROMETHEUS_PATTERN; if (0 == strcmp(str, "ZBX_PREPROC_PROMETHEUS_TO_JSON")) return ZBX_PREPROC_PROMETHEUS_TO_JSON; + if (0 == strcmp(str, "ZBX_PREPROC_CSV_TO_JSON")) + return ZBX_PREPROC_CSV_TO_JSON; fail_msg("unknow preprocessing step type: %s", str); return FAIL; diff --git a/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml b/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml index 4374a05ba06..8f26a3e2b5c 100644 --- a/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml +++ b/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml @@ -1959,4 +1959,66 @@ in: out: return: FAIL error: custom error +--- +test case: csv_to_json1 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: | + sep=. + A.B.C + 1.2.3 + step: + type: ZBX_PREPROC_CSV_TO_JSON + params: "\n\n1" +out: + return: SUCCEED + value: '[{"A":"1","B":"2","C":"3"},{"A":"","B":"","C":""}]' +--- +test case: csv_to_json2 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: | + sep=. + A.`B.``B`.C + 1.`2`.`3.` + step: + type: ZBX_PREPROC_CSV_TO_JSON + params: "\n`\n1" +out: + return: SUCCEED + value: '[{"A":"1","B.`B":"2","C":"3."},{"A":"","B.`B":"","C":""}]' +--- +test case: csv_to_json3 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 1,2,3 + step: + type: ZBX_PREPROC_CSV_TO_JSON + params: "\n\n0" +out: + return: SUCCEED + value: '[{"1":"1","2":"2","3":"3"}]' +--- +test case: csv_to_json4 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: | + A,B,C + 1,2,3,4 + step: + type: ZBX_PREPROC_PROMETHEUS_TO_JSON + params: "\n\n1" + error_handler: ZBX_PREPROC_FAIL_SET_ERROR + error_handler_params: custom error +out: + return: FAIL + error: custom error ... |