diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 22 | ||||
-rw-r--r-- | Makefile.am | 19 | ||||
-rw-r--r-- | configure.ac | 26 | ||||
-rw-r--r-- | daemon/Makefile.am | 2 | ||||
-rw-r--r-- | libnetdata/Makefile.am | 1 | ||||
-rw-r--r-- | libnetdata/required_dummies.h | 38 | ||||
-rw-r--r-- | libnetdata/storage_number/Makefile.am | 3 | ||||
-rw-r--r-- | libnetdata/storage_number/tests/Makefile.am | 4 | ||||
-rw-r--r-- | libnetdata/storage_number/tests/test_storage_number.c | 49 | ||||
-rw-r--r-- | libnetdata/tests/Makefile.am | 4 | ||||
-rw-r--r-- | libnetdata/tests/test_str2ld.c | 48 |
12 files changed, 218 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore index afb0c67bd8..0d8ba70f0f 100644 --- a/.gitignore +++ b/.gitignore @@ -173,6 +173,9 @@ tests/alarm_repetition/alarm.sh tests/template_dimension/template_dim.sh # tests and temp files +test-driver +**/tests/*_testdriver +**/tests/*_testdriver.trs python.d/python-modules-installer.sh # documentation generated files diff --git a/CMakeLists.txt b/CMakeLists.txt index 75a5427a33..2039acf40a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -876,3 +876,25 @@ IF(ENABLE_PLUGIN_CGROUP_NETWORK) ELSE() message(STATUS "cgroup-network: disabled (requires Linux)") ENDIF() + + +# ----------------------------------------------------------------------------- +# Unit tests + +if(UNIT_TESTING) + message(STATUS "Looking for CMocka which is required for unit testing") + find_package(CMocka REQUIRED) + include(CTest) + +if(BUILD_TESTING) + add_executable(str2ld_testdriver libnetdata/tests/test_str2ld.c) + target_link_libraries(str2ld_testdriver libnetdata ${NETDATA_COMMON_LIBRARIES} ${CMOCKA_LIBRARIES}) + add_test(NAME test_str2ld COMMAND str2ld_testdriver) + + add_executable(storage_number_testdriver libnetdata/storage_number/tests/test_storage_number.c) + target_link_libraries(storage_number_testdriver libnetdata ${NETDATA_COMMON_LIBRARIES} ${CMOCKA_LIBRARIES}) + add_test(NAME test_storage_number COMMAND storage_number_testdriver) + + set_target_properties(str2ld_testdriver storage_number_testdriver PROPERTIES RUNTIME_OUTPUT_DIRECTORY tests) +endif() +endif() diff --git a/Makefile.am b/Makefile.am index 35d9712cb0..a754bd26aa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -637,3 +637,22 @@ if ENABLE_BACKEND_MONGODB netdata_SOURCES += $(MONGODB_BACKEND_FILES) netdata_LDADD += $(OPTIONAL_MONGOC_LIBS) endif + +check_PROGRAMS = \ + libnetdata/tests/str2ld_testdriver \ + libnetdata/storage_number/tests/storage_number_testdriver \ + $(NULL) + +TESTS = $(check_PROGRAMS) + +libnetdata_tests_str2ld_testdriver_SOURCES = \ + libnetdata/tests/test_str2ld.c \ + $(LIBNETDATA_FILES) \ + $(NULL) +libnetdata_tests_str2ld_testdriver_LDADD = $(NETDATA_COMMON_LIBS) $(TEST_LIBS) + +libnetdata_storage_number_tests_storage_number_testdriver_SOURCES = \ + libnetdata/storage_number/tests/test_storage_number.c \ + $(LIBNETDATA_FILES) \ + $(NULL) +libnetdata_storage_number_tests_storage_number_testdriver_LDADD = $(NETDATA_COMMON_LIBS) $(TEST_LIBS) diff --git a/configure.ac b/configure.ac index a966f0ea62..c35639adc2 100644 --- a/configure.ac +++ b/configure.ac @@ -1130,6 +1130,30 @@ AC_SUBST([OPTIONAL_PROMETHEUS_REMOTE_WRITE_LIBS]) AC_SUBST([OPTIONAL_MONGOC_CFLAGS]) AC_SUBST([OPTIONAL_MONGOC_LIBS]) +# ----------------------------------------------------------------------------- +# Check if cmocka is available - needed for unit testing + +AC_ARG_ENABLE( + [unit-tests], + [AS_HELP_STRING([--disable-unit-tests], + [Disables building and running the unit tests suite])], + [], + [enable_unit_tests="yes"] +) + +PKG_CHECK_MODULES( + [CMOCKA], [cmocka], + [have_cmocka="yes"], + [AC_MSG_WARN([cmocka.pc not found on the system. Unit tests disabled])] +) +AM_CONDITIONAL([ENABLE_UNITTESTS], [test "${enable_unit_tests}" = "yes" -a "${have_cmocka}" = "yes" ]) +AC_SUBST([ENABLE_UNITTESTS]) + +TEST_CFLAGS="${CFLAGS} ${CMOCKA_CFLAGS}" +TEST_LIBS="${CMOCKA_LIBS}" + +AC_SUBST([TEST_CFLAGS]) +AC_SUBST([TEST_LIBS]) AC_CONFIG_FILES([ Makefile @@ -1172,6 +1196,7 @@ AC_CONFIG_FILES([ health/Makefile health/notifications/Makefile libnetdata/Makefile + libnetdata/tests/Makefile libnetdata/adaptive_resortable_list/Makefile libnetdata/avl/Makefile libnetdata/buffer/Makefile @@ -1187,6 +1212,7 @@ AC_CONFIG_FILES([ libnetdata/socket/Makefile libnetdata/statistical/Makefile libnetdata/storage_number/Makefile + libnetdata/storage_number/tests/Makefile libnetdata/threads/Makefile libnetdata/url/Makefile libnetdata/json/Makefile diff --git a/daemon/Makefile.am b/daemon/Makefile.am index e020e517b1..ee1b53d095 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later AUTOMAKE_OPTIONS = subdir-objects -MAINTAINERCLEANFILES= $(srcdir)/Makefile.in +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in CLEANFILES = \ anonymous-statistics.sh \ $(NULL) diff --git a/libnetdata/Makefile.am b/libnetdata/Makefile.am index 87f12b32cb..7dc808fab1 100644 --- a/libnetdata/Makefile.am +++ b/libnetdata/Makefile.am @@ -23,6 +23,7 @@ SUBDIRS = \ storage_number \ threads \ url \ + tests \ $(NULL) dist_noinst_DATA = \ diff --git a/libnetdata/required_dummies.h b/libnetdata/required_dummies.h new file mode 100644 index 0000000000..aa87e3964c --- /dev/null +++ b/libnetdata/required_dummies.h @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef NETDATA_LIB_DUMMIES_H +#define NETDATA_LIB_DUMMIES_H 1 + +// callback required by fatal() +void netdata_cleanup_and_exit(int ret) +{ + exit(ret); +} + +void send_statistics(const char *action, const char *action_result, const char *action_data) +{ + (void)action; + (void)action_result; + (void)action_data; + return; +} + +// callbacks required by popen() +void signals_block(void){}; +void signals_unblock(void){}; +void signals_reset(void){}; + +// callback required by eval() +int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result) +{ + (void)variable; + (void)hash; + (void)rc; + (void)result; + return 0; +}; + +// required by get_system_cpus() +char *netdata_configured_host_prefix = ""; + +#endif // NETDATA_LIB_DUMMIES_H diff --git a/libnetdata/storage_number/Makefile.am b/libnetdata/storage_number/Makefile.am index 1cb69ed99a..349dd71f1c 100644 --- a/libnetdata/storage_number/Makefile.am +++ b/libnetdata/storage_number/Makefile.am @@ -3,6 +3,9 @@ AUTOMAKE_OPTIONS = subdir-objects MAINTAINERCLEANFILES = $(srcdir)/Makefile.in +SUBDIRS = \ + tests \ + $(NULL) dist_noinst_DATA = \ README.md \ diff --git a/libnetdata/storage_number/tests/Makefile.am b/libnetdata/storage_number/tests/Makefile.am new file mode 100644 index 0000000000..babdcf0df3 --- /dev/null +++ b/libnetdata/storage_number/tests/Makefile.am @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +AUTOMAKE_OPTIONS = subdir-objects +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in diff --git a/libnetdata/storage_number/tests/test_storage_number.c b/libnetdata/storage_number/tests/test_storage_number.c new file mode 100644 index 0000000000..61a0c18805 --- /dev/null +++ b/libnetdata/storage_number/tests/test_storage_number.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "../../libnetdata.h" +#include "../../required_dummies.h" +#include <setjmp.h> +#include <cmocka.h> + +static void test_number_pinting(void **state) +{ + (void)state; + + char value[50]; + + print_calculated_number(value, 0); + assert_string_equal(value, "0"); + + print_calculated_number(value, 0.0000001); + assert_string_equal(value, "0.0000001"); + + print_calculated_number(value, 0.00000009); + assert_string_equal(value, "0.0000001"); + + print_calculated_number(value, 0.000000001); + assert_string_equal(value, "0"); + + print_calculated_number(value, 99.99999999999999999); + assert_string_equal(value, "100"); + + print_calculated_number(value, -99.99999999999999999); + assert_string_equal(value, "-100"); + + print_calculated_number(value, 123.4567890123456789); + assert_string_equal(value, "123.456789"); + + print_calculated_number(value, 9999.9999999); + assert_string_equal(value, "9999.9999999"); + + print_calculated_number(value, -9999.9999999); + assert_string_equal(value, "-9999.9999999"); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_number_pinting) + }; + + return cmocka_run_group_tests_name("storage_number", tests, NULL, NULL); +} diff --git a/libnetdata/tests/Makefile.am b/libnetdata/tests/Makefile.am new file mode 100644 index 0000000000..babdcf0df3 --- /dev/null +++ b/libnetdata/tests/Makefile.am @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +AUTOMAKE_OPTIONS = subdir-objects +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in diff --git a/libnetdata/tests/test_str2ld.c b/libnetdata/tests/test_str2ld.c new file mode 100644 index 0000000000..9d59f6c0e2 --- /dev/null +++ b/libnetdata/tests/test_str2ld.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "../libnetdata.h" +#include "../required_dummies.h" +#include <setjmp.h> +#include <cmocka.h> + +static void test_str2ld(void **state) +{ + (void)state; + char *values[] = { + "1.2345678", + "-35.6", + "0.00123", + "23842384234234.2", + ".1", + "1.2e-10", + "hello", + "1wrong", + "nan", + "inf", + NULL + }; + + for (int i = 0; values[i]; i++) { + char *e_mine = "hello", *e_sys = "world"; + LONG_DOUBLE mine = str2ld(values[i], &e_mine); + LONG_DOUBLE sys = strtold(values[i], &e_sys); + + if (isnan(mine)) + assert_true(isnan(sys)); + else if (isinf(mine)) + assert_true(isinf(sys)); + else if (mine != sys) + assert_false(abs(mine - sys) > 0.000001); + + assert_ptr_equal(e_mine, e_sys); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_str2ld) + }; + + return cmocka_run_group_tests_name("str2ld", tests, NULL, NULL); +} |