diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 3 | ||||
-rw-r--r-- | tests/zabbix_server/Makefile.am | 2 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/Makefile.am | 32 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/zbx_item_preproc.c | 178 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/zbx_item_preproc.yaml | 1132 | ||||
-rw-r--r-- | tests/zbxmockdata.c | 9 | ||||
-rw-r--r-- | tests/zbxmockdata.h | 1 |
7 files changed, 1355 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 93040649a2a..62aba8f6dc4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,7 @@ SUBDIRS = \ . \ - libs + libs \ + zabbix_server noinst_LIBRARIES = \ libzbxmocktest.a \ diff --git a/tests/zabbix_server/Makefile.am b/tests/zabbix_server/Makefile.am new file mode 100644 index 00000000000..48defec4f66 --- /dev/null +++ b/tests/zabbix_server/Makefile.am @@ -0,0 +1,2 @@ +SUBDIRS = \ + preprocessor diff --git a/tests/zabbix_server/preprocessor/Makefile.am b/tests/zabbix_server/preprocessor/Makefile.am new file mode 100644 index 00000000000..372ec85b33b --- /dev/null +++ b/tests/zabbix_server/preprocessor/Makefile.am @@ -0,0 +1,32 @@ +noinst_PROGRAMS = zbx_item_preproc + +JSON_LIBS = \ + $(top_srcdir)/tests/libzbxmocktest.a \ + $(top_srcdir)/tests/libzbxmockdata.a \ + $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ + $(top_srcdir)/src/libs/zbxalgo/libzbxalgo.a \ + $(top_srcdir)/src/libs/zbxcommon/libzbxcommon.a \ + $(top_srcdir)/src/libs/zbxcomms/libzbxcomms.a \ + $(top_srcdir)/src/libs/zbxcompress/libzbxcompress.a \ + $(top_srcdir)/src/libs/zbxcommon/libzbxcommon.a \ + $(top_srcdir)/src/libs/zbxregexp/libzbxregexp.a \ + $(top_srcdir)/src/libs/zbxnix/libzbxnix.a \ + $(top_srcdir)/src/libs/zbxcrypto/libzbxcrypto.a \ + $(top_srcdir)/src/libs/zbxsys/libzbxsys.a \ + $(top_srcdir)/src/libs/zbxlog/libzbxlog.a \ + $(top_srcdir)/src/libs/zbxsys/libzbxsys.a \ + $(top_srcdir)/src/libs/zbxconf/libzbxconf.a \ + $(top_srcdir)/tests/libzbxmockdata.a + +zbx_item_preproc_SOURCES = \ + ../../../src/zabbix_server/preprocessor/item_preproc.c \ + zbx_item_preproc.c + +zbx_item_preproc_LDADD = $(JSON_LIBS) + +if SERVER +zbx_item_preproc_LDADD += @SERVER_LIBS@ +zbx_item_preproc_LDFLAGS = @SERVER_LDFLAGS@ +endif + +zbx_item_preproc_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ diff --git a/tests/zabbix_server/preprocessor/zbx_item_preproc.c b/tests/zabbix_server/preprocessor/zbx_item_preproc.c new file mode 100644 index 00000000000..42eef3902e2 --- /dev/null +++ b/tests/zabbix_server/preprocessor/zbx_item_preproc.c @@ -0,0 +1,178 @@ +/* +** Zabbix +** Copyright (C) 2001-2018 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 "common.h" +#include "zbxjson.h" +#include "dbcache.h" + +#include "../../../src/zabbix_server/preprocessor/item_preproc.h" + +static int str_to_preproc_type(const char *str) +{ + if (0 == strcmp(str, "ZBX_PREPROC_MULTIPLIER")) + return ZBX_PREPROC_MULTIPLIER; + if (0 == strcmp(str, "ZBX_PREPROC_RTRIM")) + return ZBX_PREPROC_RTRIM; + if (0 == strcmp(str, "ZBX_PREPROC_LTRIM")) + return ZBX_PREPROC_LTRIM; + if (0 == strcmp(str, "ZBX_PREPROC_TRIM")) + return ZBX_PREPROC_TRIM; + if (0 == strcmp(str, "ZBX_PREPROC_REGSUB")) + return ZBX_PREPROC_REGSUB; + if (0 == strcmp(str, "ZBX_PREPROC_BOOL2DEC")) + return ZBX_PREPROC_BOOL2DEC; + if (0 == strcmp(str, "ZBX_PREPROC_OCT2DEC")) + return ZBX_PREPROC_OCT2DEC; + if (0 == strcmp(str, "ZBX_PREPROC_HEX2DEC")) + return ZBX_PREPROC_HEX2DEC; + if (0 == strcmp(str, "ZBX_PREPROC_DELTA_VALUE")) + return ZBX_PREPROC_DELTA_VALUE; + if (0 == strcmp(str, "ZBX_PREPROC_DELTA_SPEED")) + return ZBX_PREPROC_DELTA_SPEED; + if (0 == strcmp(str, "ZBX_PREPROC_XPATH")) + return ZBX_PREPROC_XPATH; + if (0 == strcmp(str, "ZBX_PREPROC_JSONPATH")) + return ZBX_PREPROC_JSONPATH; + if (0 == strcmp(str, "ZBX_PREPROC_VALIDATE_RANGE")) + return ZBX_PREPROC_VALIDATE_RANGE; + if (0 == strcmp(str, "ZBX_PREPROC_VALIDATE_REGEX")) + return ZBX_PREPROC_VALIDATE_REGEX; + if (0 == strcmp(str, "ZBX_PREPROC_VALIDATE_NOT_REGEX")) + return ZBX_PREPROC_VALIDATE_NOT_REGEX; + if (0 == strcmp(str, "ZBX_PREPROC_ERROR_FIELD_JSON")) + return ZBX_PREPROC_ERROR_FIELD_JSON; + if (0 == strcmp(str, "ZBX_PREPROC_ERROR_FIELD_XML")) + return ZBX_PREPROC_ERROR_FIELD_XML; + if (0 == strcmp(str, "ZBX_PREPROC_ERROR_FIELD_REGEX")) + return ZBX_PREPROC_ERROR_FIELD_REGEX; + if (0 == strcmp(str, "ZBX_PREPROC_THROTTLE_VALUE")) + return ZBX_PREPROC_THROTTLE_VALUE; + if (0 == strcmp(str, "ZBX_PREPROC_THROTTLE_TIMED_VALUE")) + return ZBX_PREPROC_THROTTLE_TIMED_VALUE; + + fail_msg("unknow preprocessing step type: %s", str); + return FAIL; +} + +static int str_to_preproc_error_handler(const char *str) +{ + if (0 == strcmp(str, "ZBX_PREPROC_FAIL_DEFAULT")) + return ZBX_PREPROC_FAIL_DEFAULT; + if (0 == strcmp(str, "ZBX_PREPROC_FAIL_DISCARD_VALUE")) + return ZBX_PREPROC_FAIL_DISCARD_VALUE; + if (0 == strcmp(str, "ZBX_PREPROC_FAIL_SET_VALUE")) + return ZBX_PREPROC_FAIL_SET_VALUE; + if (0 == strcmp(str, "ZBX_PREPROC_FAIL_SET_ERROR")) + return ZBX_PREPROC_FAIL_SET_ERROR; + + fail_msg("unknow preprocessing error handler: %s", str); + return FAIL; +} + +static void read_value(const char *path, unsigned char *value_type, zbx_timespec_t *ts, + zbx_variant_t *value) +{ + zbx_mock_handle_t handle; + + handle = zbx_mock_get_parameter_handle(path); + *value_type = zbx_mock_str_to_value_type(zbx_mock_get_object_member_string(handle, "value_type")); + zbx_strtime_to_timespec(zbx_mock_get_object_member_string(handle, "time"), ts); + zbx_variant_set_str(value, zbx_strdup(NULL, zbx_mock_get_object_member_string(handle, "data"))); +} + +static void read_step(const char *path, zbx_preproc_op_t *op) +{ + zbx_mock_handle_t hop, hop_params, herror, herror_params; + + hop = zbx_mock_get_parameter_handle(path); + op->type = str_to_preproc_type(zbx_mock_get_object_member_string(hop, "type")); + + if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hop, "params", &hop_params)) + op->params = (char *)zbx_mock_get_object_member_string(hop, "params"); + else + op->params = ""; + + if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hop, "error_handler", &herror)) + op->error_handler = str_to_preproc_error_handler(zbx_mock_get_object_member_string(hop, "error_handler")); + else + op->error_handler = ZBX_PREPROC_FAIL_DEFAULT; + + if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hop, "error_handler_params", &herror_params)) + op->error_handler_params = (char *)zbx_mock_get_object_member_string(hop, "error_handler_params"); + else + op->error_handler_params = ""; +} + +void zbx_mock_test_entry(void **state) +{ + zbx_variant_t value; + unsigned char value_type; + zbx_timespec_t ts; + zbx_item_history_value_t history, *phistory = NULL; + zbx_preproc_op_t op; + int returned_ret, expected_ret; + char *error = NULL; + + ZBX_UNUSED(state); + + read_value("in.value", &value_type, &ts, &value); + read_step("in.step", &op); + + if (ZBX_MOCK_SUCCESS == zbx_mock_parameter_exists("in.history")) + { + history.itemid = 0; + read_value("in.history", &history.value_type, &history.timestamp, &history.value); + phistory = &history; + } + + returned_ret = zbx_item_preproc(0, value_type, &value, &ts, &op, phistory, &error); + expected_ret = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return")); + zbx_mock_assert_result_eq("zbx_item_preproc() return", expected_ret, returned_ret); + + if (SUCCEED == returned_ret) + { + if (ZBX_MOCK_SUCCESS == zbx_mock_parameter_exists("out.value")) + { + if (ZBX_VARIANT_NONE == value.type) + fail_msg("preprocessing result was empty value"); + + zbx_variant_convert(&value, ZBX_VARIANT_STR); + del_zeros(value.data.str); + zbx_mock_assert_str_eq("processed value", zbx_mock_get_parameter_string("out.value"), + value.data.str); + } + else + { + if (ZBX_VARIANT_NONE != value.type) + fail_msg("expected empty value, but got %s", zbx_variant_value_desc(&value)); + } + } + + if (FAIL == returned_ret && ZBX_MOCK_SUCCESS == zbx_mock_parameter_exists("out.error")) + zbx_mock_assert_str_eq("error message", zbx_mock_get_parameter_string("out.error"), error); + + zbx_variant_clear(&value); + zbx_free(error); + +} diff --git a/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml b/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml new file mode 100644 index 00000000000..8ed7a0f0680 --- /dev/null +++ b/tests/zabbix_server/preprocessor/zbx_item_preproc.yaml @@ -0,0 +1,1132 @@ +--- +test case: string(10) * 10 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_MULTIPLIER + params: 10 +out: + return: SUCCEED + value: 100 +--- +test case: string(10x) * 10 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10x + step: + type: ZBX_PREPROC_MULTIPLIER + params: 10 +out: + return: FAIL +--- +test case: string(10) * abc +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_MULTIPLIER + params: abc +out: + return: FAIL +--- +test case: string(1.5) * 3 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 1.5 + step: + type: ZBX_PREPROC_MULTIPLIER + params: 3 +out: + return: SUCCEED + value: 4.5 +--- +test case: uint64(1.5) * 3 +in: + value: + value_type: ITEM_VALUE_TYPE_UINT64 + time: 2017-10-29 03:15:00 +03:00 + data: 1.5 + step: + type: ZBX_PREPROC_MULTIPLIER + params: 3 +out: + return: SUCCEED + value: 3 +--- +test case: uint64(3) * 1.5 +in: + value: + value_type: ITEM_VALUE_TYPE_UINT64 + time: 2017-10-29 03:15:00 +03:00 + data: 3 + step: + type: ZBX_PREPROC_MULTIPLIER + params: 1.5 +out: + return: SUCCEED + value: 4 +--- +test case: float(1.5) * 3 +in: + value: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:15:00 +03:00 + data: 1.5 + step: + type: ZBX_PREPROC_MULTIPLIER + params: 3 +out: + return: SUCCEED + value: 4.5 +--- +test case: float(3) * 1.5 +in: + value: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:15:00 +03:00 + data: 3 + step: + type: ZBX_PREPROC_MULTIPLIER + params: 1.5 +out: + return: SUCCEED + value: 4.5 +--- +test case: rtrim(01abc01, 01) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 01abc01 + step: + type: ZBX_PREPROC_RTRIM + params: 01 +out: + return: SUCCEED + value: 01abc +--- +test case: rtrim(01abc01, abc) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 01abc01 + step: + type: ZBX_PREPROC_RTRIM + params: abc +out: + return: SUCCEED + value: 01abc01 +--- +test case: ltrim(01abc01, 01) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 01abc01 + step: + type: ZBX_PREPROC_LTRIM + params: 01 +out: + return: SUCCEED + value: abc01 +--- +test case: ltrim(01abc01, abc) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 01abc01 + step: + type: ZBX_PREPROC_LTRIM + params: abc +out: + return: SUCCEED + value: 01abc01 +--- +test case: trim(01abc01, 01) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 01abc01 + step: + type: ZBX_PREPROC_TRIM + params: 01 +out: + return: SUCCEED + value: abc +--- +test case: trim(01abc01, abc) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 01abc01 + step: + type: ZBX_PREPROC_TRIM + params: abc +out: + return: SUCCEED + value: 01abc01 +--- +test case: regsub("test 123 number", "([0-9]+", 1) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: test 123 number + step: + type: ZBX_PREPROC_REGSUB + params: "([0-9]+\n\\1" +out: + return: FAIL +--- +test case: regsub("test 123 number", "([0-9]+)", \1) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: test 123 number + step: + type: ZBX_PREPROC_REGSUB + params: "([0-9]+)\n\\1" +out: + return: SUCCEED + value: 123 +--- +test case: regsub("test 123 number", "([0-9]+)", x\1y) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: test 123 number + step: + type: ZBX_PREPROC_REGSUB + params: "([0-9]+)\nx\\1y" +out: + return: SUCCEED + value: x123y +--- +test case: regsub("test 123 number", "([0-9]+)", ) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: test 123 number + step: + type: ZBX_PREPROC_REGSUB + params: "([0-9]+)" +out: + return: FAIL +--- +test case: regsub("test abc number", "([0-9]+)", \1) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: test abc number + step: + type: ZBX_PREPROC_REGSUB + params: "([0-9]+)\n\\1" +out: + return: FAIL +--- +test case: regsub("", "([0-9]+)", \1) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: + step: + type: ZBX_PREPROC_REGSUB + params: "([0-9]+)\n\\1" +out: + return: FAIL +--- +test case: regsub("", "^$", ok) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "" + step: + type: ZBX_PREPROC_REGSUB + params: "^$\nok" +out: + return: SUCCEED + value: ok +--- +test case: bool2dec() +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: FAIL +--- +test case: bool2dec(abc) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "abc" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: FAIL +--- +test case: bool2dec(1) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "1" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(0) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "0" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: bool2dec(true) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "true" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(t) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "t" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(yes) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "yes" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(y) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "y" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(ok) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "ok" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(on) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "on" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 1 +--- +test case: bool2dec(false) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "false" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: bool2dec(f) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "f" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: bool2dec(no) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "no" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: bool2dec(n) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "n" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: bool2dec(err) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "err" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: bool2dec(off) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "off" + step: + type: ZBX_PREPROC_BOOL2DEC +out: + return: SUCCEED + value: 0 +--- +test case: oct2dec(0) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 0 + step: + type: ZBX_PREPROC_OCT2DEC +out: + return: SUCCEED + value: 0 +--- +test case: oct2dec(7) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 7 + step: + type: ZBX_PREPROC_OCT2DEC +out: + return: SUCCEED + value: 7 +--- +test case: oct2dec(10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_OCT2DEC +out: + return: SUCCEED + value: 8 +--- +test case: oct2dec(8) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 8 + step: + type: ZBX_PREPROC_OCT2DEC +out: + return: FAIL +--- +test case: hex2dec(F) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: F + step: + type: ZBX_PREPROC_HEX2DEC +out: + return: SUCCEED + value: 15 +--- +test case: hex2dec(a) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: a + step: + type: ZBX_PREPROC_HEX2DEC +out: + return: SUCCEED + value: 10 +--- +test case: hex2dec(10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_HEX2DEC +out: + return: SUCCEED + value: 16 +--- +test case: hex2dec(g) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: g + step: + type: ZBX_PREPROC_HEX2DEC +out: + return: FAIL +--- +test case: hex2dec(ff ff) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: ff ff + step: + type: ZBX_PREPROC_HEX2DEC +out: + return: SUCCEED + value: 65535 +--- +test case: deltavalue(5, 10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:00 +03:00 + data: 5 + step: + type: ZBX_PREPROC_DELTA_VALUE +out: + return: SUCCEED + value: 5 +--- +test case: deltavalue(-4.5, 5.5) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 5.5 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:00 +03:00 + data: -4.5 + step: + type: ZBX_PREPROC_DELTA_VALUE +out: + return: SUCCEED + value: 10 +--- +test case: deltavalue(10, 10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_DELTA_VALUE +out: + return: SUCCEED + value: 0 +--- +test case: deltavalue(10, 9) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 0 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_DELTA_VALUE +out: + return: SUCCEED +--- +test case: deltavalue(0, 1.5) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 1.5 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:00 +03:00 + data: 0 + step: + type: ZBX_PREPROC_DELTA_VALUE +out: + return: SUCCEED + value: 1.5 +--- +test case: deltaspeed(2, 1, 10s) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 1 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:50 +03:00 + data: 2 + step: + type: ZBX_PREPROC_DELTA_SPEED +out: + return: SUCCEED +--- +test case: deltaspeed(1, 2, -10s) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 2 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:15:10 +03:00 + data: 1 + step: + type: ZBX_PREPROC_DELTA_SPEED +out: + return: SUCCEED +--- +test case: deltaspeed(1, 2, 10s) +in: + value: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:15:00 +03:00 + data: 2 + history: + value_type: ITEM_VALUE_TYPE_FLOAT + time: 2017-10-29 03:14:50 +03:00 + data: 1 + step: + type: ZBX_PREPROC_DELTA_SPEED +out: + return: SUCCEED + value: 0.1 +--- +test case: deltaspeed(2, 3, 10s) +in: + value: + value_type: ITEM_VALUE_TYPE_UINT64 + time: 2017-10-29 03:15:00 +03:00 + data: 3 + history: + value_type: ITEM_VALUE_TYPE_UINT64 + time: 2017-10-29 03:14:50 +03:00 + data: 2 + step: + type: ZBX_PREPROC_DELTA_SPEED +out: + return: SUCCEED + value: 0 +--- +test case: deltaspeed(2, 3, 1s) +in: + value: + value_type: ITEM_VALUE_TYPE_UINT64 + time: 2017-10-29 03:15:00 +03:00 + data: 3 + history: + value_type: ITEM_VALUE_TYPE_UINT64 + time: 2017-10-29 03:14:59 +03:00 + data: 2 + step: + type: ZBX_PREPROC_DELTA_SPEED +out: + return: SUCCEED + value: 1 +--- +test case: xpath1 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: "" + step: + type: ZBX_PREPROC_XPATH + params: "" +out: + return: FAIL +--- +test case: xpath2 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: "" +out: + return: FAIL +--- +test case: xpath3 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: |- + /a["] +out: + return: FAIL +--- +test case: xpath4 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: 1 div 0 +out: + return: FAIL +--- +test case: xpath5 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: -a +out: + return: FAIL +--- +test case: xpath6 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: /b +out: + return: SUCCEED + value: "" +--- +test case: xpath7 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: 3 div 2 +out: + return: SUCCEED + value: 1.5 +--- +test case: xpath8 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a/> + step: + type: ZBX_PREPROC_XPATH + params: /a +out: + return: SUCCEED + value: <a/> +--- +test case: xpath9 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a>1</a> + step: + type: ZBX_PREPROC_XPATH + params: /a/text() +out: + return: SUCCEED + value: 1 +--- +test case: xpath10 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a>1</a> + step: + type: ZBX_PREPROC_XPATH + params: string(/a) +out: + return: SUCCEED + value: 1 +--- +test case: xpath11 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a b="10">1</a> + step: + type: ZBX_PREPROC_XPATH + params: string(/a/@b) +out: + return: SUCCEED + value: 10 +--- +test case: xpath12 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: <a><b x="1"/><c x="2"/><d x="1"/></a> + step: + type: ZBX_PREPROC_XPATH + params: //*[@x="1"] +out: + return: SUCCEED + value: <b x="1"/><d x="1"/> +--- +test case: jsonpath1 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc + step: + type: ZBX_PREPROC_JSONPATH + params: +out: + return: FAIL +--- +test case: jsonpath2 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: |- + {"a":{"b":[1, 2, 3]}} + step: + type: ZBX_PREPROC_JSONPATH + params: $abc +out: + return: FAIL +--- +test case: jsonpath3 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: |- + {"a":{"b":[1, 2, 3]}} + step: + type: ZBX_PREPROC_JSONPATH + params: $.abc +out: + return: FAIL +--- +test case: jsonpath4 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: |- + {"a":{"b":[1, 2, 3]}} + step: + type: ZBX_PREPROC_JSONPATH + params: $.a +out: + return: SUCCEED + value: |- + {"b":[1, 2, 3]} +--- +test case: jsonpath5 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: |- + {"a":{"b":[1, 2, 3]}} + step: + type: ZBX_PREPROC_JSONPATH + params: $.a['b'] +out: + return: SUCCEED + value: |- + [1, 2, 3] +--- +test case: jsonpath6 +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: |- + {"a":{"b":[1, 2, 3]}} + step: + type: ZBX_PREPROC_JSONPATH + params: $.a['b'][1] +out: + return: SUCCEED + value: 2 +--- +test case: validate_range(1, 5, 10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 1 + step: + type: ZBX_PREPROC_VALIDATE_RANGE + params: |- + 5 + 10 +out: + return: FAIL +--- +test case: validate_range(5, 5, 10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 5 + step: + type: ZBX_PREPROC_VALIDATE_RANGE + params: |- + 5 + 10 +out: + return: SUCCEED + value: 5 +--- +test case: validate_range(10, 5, 10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10 + step: + type: ZBX_PREPROC_VALIDATE_RANGE + params: |- + 5 + 10 +out: + return: SUCCEED + value: 10 +--- +test case: validate_range(10.1, 5, 10) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: 10.1 + step: + type: ZBX_PREPROC_VALIDATE_RANGE + params: |- + 5 + 10 +out: + return: FAIL +--- +test case: validate_regex(abc 123 xyz, ([0-9+)) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc 123 xyz + step: + type: ZBX_PREPROC_VALIDATE_REGEX + params: ([0-9+) +out: + return: FAIL +--- +test case: validate_regex(abc opq xyz, ([0-9]+)) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc opq xyz + step: + type: ZBX_PREPROC_VALIDATE_REGEX + params: ([0-9]+) +out: + return: FAIL +--- +test case: validate_regex(abc 123 xyz, ([0-9]+)) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc 123 xyz + step: + type: ZBX_PREPROC_VALIDATE_REGEX + params: ([0-9]+) +out: + return: SUCCEED + value: abc 123 xyz +--- +test case: validate_regex(abc 123 xyz, ([0-9+)) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc 123 xyz + step: + type: ZBX_PREPROC_VALIDATE_NOT_REGEX + params: ([0-9+) +out: + return: FAIL +--- +test case: validate_regex(abc opq xyz, ([0-9]+)) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc opq xyz + step: + type: ZBX_PREPROC_VALIDATE_NOT_REGEX + params: ([0-9]+) +out: + return: SUCCEED + value: abc opq xyz +--- +test case: validate_regex(abc 123 xyz, ([0-9]+)) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: abc 123 xyz + step: + type: ZBX_PREPROC_VALIDATE_NOT_REGEX + params: ([0-9]+) +out: + return: FAIL +--- +test case: string(10) * 10 (discard) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: x + step: + type: ZBX_PREPROC_MULTIPLIER + params: 10 + error_handler: ZBX_PREPROC_FAIL_DISCARD_VALUE +out: + return: SUCCEED +--- +test case: string(10) * 10 (set value) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: x + step: + type: ZBX_PREPROC_MULTIPLIER + params: 10 + error_handler: ZBX_PREPROC_FAIL_SET_VALUE + error_handler_params: invalid +out: + return: SUCCEED + value: invalid +--- +test case: string(10) * 10 (set error) +in: + value: + value_type: ITEM_VALUE_TYPE_STR + time: 2017-10-29 03:15:00 +03:00 + data: x + step: + type: ZBX_PREPROC_MULTIPLIER + params: 10 + error_handler: ZBX_PREPROC_FAIL_SET_ERROR + error_handler_params: custom error +out: + return: FAIL + error: custom error +... + + diff --git a/tests/zbxmockdata.c b/tests/zbxmockdata.c index 2129c918c1a..ffbca1a843c 100644 --- a/tests/zbxmockdata.c +++ b/tests/zbxmockdata.c @@ -612,7 +612,9 @@ static zbx_mock_error_t zbx_mock_parameter_rec(const yaml_node_t *node, const ch /* end of the path, return whatever has been found */ if ('\0' == *pnext) { - *parameter = zbx_mock_handle_alloc(node); + if (NULL != parameter) + *parameter = zbx_mock_handle_alloc(node); + return ZBX_MOCK_SUCCESS; } @@ -662,6 +664,11 @@ zbx_mock_error_t zbx_mock_parameter(const char *path, zbx_mock_handle_t *paramet return zbx_mock_parameter_rec(root, path, parameter); } +zbx_mock_error_t zbx_mock_parameter_exists(const char *path) +{ + return zbx_mock_parameter_rec(root, path, NULL); +} + zbx_mock_error_t zbx_mock_uint64(zbx_mock_handle_t object, zbx_uint64_t *value) { const zbx_mock_pool_handle_t *handle; diff --git a/tests/zbxmockdata.h b/tests/zbxmockdata.h index af8f5e22bb9..7bd92315cf4 100644 --- a/tests/zbxmockdata.h +++ b/tests/zbxmockdata.h @@ -59,6 +59,7 @@ zbx_mock_error_t zbx_mock_vector_element(zbx_mock_handle_t vector, zbx_mock_hand zbx_mock_error_t zbx_mock_string(zbx_mock_handle_t string, const char **value); zbx_mock_error_t zbx_mock_binary(zbx_mock_handle_t binary, const char **value, size_t *length); zbx_mock_error_t zbx_mock_parameter(const char *path, zbx_mock_handle_t *parameter); +zbx_mock_error_t zbx_mock_parameter_exists(const char *path); zbx_mock_error_t zbx_mock_uint64(zbx_mock_handle_t object, zbx_uint64_t *value); /* date/time support */ |