@cryptotaxi247 / netdata-1 / commits / 16d40fbb1

Add CMocka unit tests (#6985)

* Add str2ld test * Build test with Autotools * Add storage_number test * Configure tests in CMake

Vladimir Kobal committed Oct 15, 2019 at 15:15 UTC 16d40fbb14f29e51902434a5da69932c4b0353a0
12 files changed +218 -1
.gitignore
+3
@@ -173,6 +173,9 @@ tests/alarm_repetition/alarm.sh
173 tests/template_dimension/template_dim.sh
174
175 # tests and temp files
176 +test-driver
177 +**/tests/*_testdriver
178 +**/tests/*_testdriver.trs
179 python.d/python-modules-installer.sh
180
181 # documentation generated files
CMakeLists.txt
+22
@@ -876,3 +876,25 @@ IF(ENABLE_PLUGIN_CGROUP_NETWORK)
876 ELSE()
877 message(STATUS "cgroup-network: disabled (requires Linux)")
878 ENDIF()
879 +
880 +
881 +# -----------------------------------------------------------------------------
882 +# Unit tests
883 +
884 +if(UNIT_TESTING)
885 + message(STATUS "Looking for CMocka which is required for unit testing")
886 + find_package(CMocka REQUIRED)
887 + include(CTest)
888 +
889 +if(BUILD_TESTING)
890 + add_executable(str2ld_testdriver libnetdata/tests/test_str2ld.c)
891 + target_link_libraries(str2ld_testdriver libnetdata ${NETDATA_COMMON_LIBRARIES} ${CMOCKA_LIBRARIES})
892 + add_test(NAME test_str2ld COMMAND str2ld_testdriver)
893 +
894 + add_executable(storage_number_testdriver libnetdata/storage_number/tests/test_storage_number.c)
895 + target_link_libraries(storage_number_testdriver libnetdata ${NETDATA_COMMON_LIBRARIES} ${CMOCKA_LIBRARIES})
896 + add_test(NAME test_storage_number COMMAND storage_number_testdriver)
897 +
898 + set_target_properties(str2ld_testdriver storage_number_testdriver PROPERTIES RUNTIME_OUTPUT_DIRECTORY tests)
899 +endif()
900 +endif()
Makefile.am
+19
@@ -637,3 +637,22 @@ if ENABLE_BACKEND_MONGODB
637 netdata_SOURCES += $(MONGODB_BACKEND_FILES)
638 netdata_LDADD += $(OPTIONAL_MONGOC_LIBS)
639 endif
640 +
641 +check_PROGRAMS = \
642 + libnetdata/tests/str2ld_testdriver \
643 + libnetdata/storage_number/tests/storage_number_testdriver \
644 + $(NULL)
645 +
646 +TESTS = $(check_PROGRAMS)
647 +
648 +libnetdata_tests_str2ld_testdriver_SOURCES = \
649 + libnetdata/tests/test_str2ld.c \
650 + $(LIBNETDATA_FILES) \
651 + $(NULL)
652 +libnetdata_tests_str2ld_testdriver_LDADD = $(NETDATA_COMMON_LIBS) $(TEST_LIBS)
653 +
654 +libnetdata_storage_number_tests_storage_number_testdriver_SOURCES = \
655 + libnetdata/storage_number/tests/test_storage_number.c \
656 + $(LIBNETDATA_FILES) \
657 + $(NULL)
658 +libnetdata_storage_number_tests_storage_number_testdriver_LDADD = $(NETDATA_COMMON_LIBS) $(TEST_LIBS)
configure.ac
+26
@@ -1130,6 +1130,30 @@ AC_SUBST([OPTIONAL_PROMETHEUS_REMOTE_WRITE_LIBS])
1130 AC_SUBST([OPTIONAL_MONGOC_CFLAGS])
1131 AC_SUBST([OPTIONAL_MONGOC_LIBS])
1132
1133 +# -----------------------------------------------------------------------------
1134 +# Check if cmocka is available - needed for unit testing
1135 +
1136 +AC_ARG_ENABLE(
1137 + [unit-tests],
1138 + [AS_HELP_STRING([--disable-unit-tests],
1139 + [Disables building and running the unit tests suite])],
1140 + [],
1141 + [enable_unit_tests="yes"]
1142 +)
1143 +
1144 +PKG_CHECK_MODULES(
1145 + [CMOCKA], [cmocka],
1146 + [have_cmocka="yes"],
1147 + [AC_MSG_WARN([cmocka.pc not found on the system. Unit tests disabled])]
1148 +)
1149 +AM_CONDITIONAL([ENABLE_UNITTESTS], [test "${enable_unit_tests}" = "yes" -a "${have_cmocka}" = "yes" ])
1150 +AC_SUBST([ENABLE_UNITTESTS])
1151 +
1152 +TEST_CFLAGS="${CFLAGS} ${CMOCKA_CFLAGS}"
1153 +TEST_LIBS="${CMOCKA_LIBS}"
1154 +
1155 +AC_SUBST([TEST_CFLAGS])
1156 +AC_SUBST([TEST_LIBS])
1157
1158 AC_CONFIG_FILES([
1159 Makefile
@@ -1172,6 +1196,7 @@ AC_CONFIG_FILES([
1196 health/Makefile
1197 health/notifications/Makefile
1198 libnetdata/Makefile
1199 + libnetdata/tests/Makefile
1200 libnetdata/adaptive_resortable_list/Makefile
1201 libnetdata/avl/Makefile
1202 libnetdata/buffer/Makefile
@@ -1187,6 +1212,7 @@ AC_CONFIG_FILES([
1212 libnetdata/socket/Makefile
1213 libnetdata/statistical/Makefile
1214 libnetdata/storage_number/Makefile
1215 + libnetdata/storage_number/tests/Makefile
1216 libnetdata/threads/Makefile
1217 libnetdata/url/Makefile
1218 libnetdata/json/Makefile
daemon/Makefile.am
+1 -1
@@ -1,7 +1,7 @@
1 # SPDX-License-Identifier: GPL-3.0-or-later
2
3 AUTOMAKE_OPTIONS = subdir-objects
4 -MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
4 +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
5 CLEANFILES = \
6 anonymous-statistics.sh \
7 $(NULL)
libnetdata/Makefile.am
+1
@@ -23,6 +23,7 @@ SUBDIRS = \
23 storage_number \
24 threads \
25 url \
26 + tests \
27 $(NULL)
28
29 dist_noinst_DATA = \
libnetdata/required_dummies.h new
+38
@@ -0,0 +1,38 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +#ifndef NETDATA_LIB_DUMMIES_H
4 +#define NETDATA_LIB_DUMMIES_H 1
5 +
6 +// callback required by fatal()
7 +void netdata_cleanup_and_exit(int ret)
8 +{
9 + exit(ret);
10 +}
11 +
12 +void send_statistics(const char *action, const char *action_result, const char *action_data)
13 +{
14 + (void)action;
15 + (void)action_result;
16 + (void)action_data;
17 + return;
18 +}
19 +
20 +// callbacks required by popen()
21 +void signals_block(void){};
22 +void signals_unblock(void){};
23 +void signals_reset(void){};
24 +
25 +// callback required by eval()
26 +int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result)
27 +{
28 + (void)variable;
29 + (void)hash;
30 + (void)rc;
31 + (void)result;
32 + return 0;
33 +};
34 +
35 +// required by get_system_cpus()
36 +char *netdata_configured_host_prefix = "";
37 +
38 +#endif // NETDATA_LIB_DUMMIES_H
libnetdata/storage_number/Makefile.am
+3
@@ -3,6 +3,9 @@
3 AUTOMAKE_OPTIONS = subdir-objects
4 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
5
6 +SUBDIRS = \
7 + tests \
8 + $(NULL)
9
10 dist_noinst_DATA = \
11 README.md \
libnetdata/storage_number/tests/Makefile.am new
+4
@@ -0,0 +1,4 @@
1 +# SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +AUTOMAKE_OPTIONS = subdir-objects
4 +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
libnetdata/storage_number/tests/test_storage_number.c new
+49
@@ -0,0 +1,49 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +#include "../../libnetdata.h"
4 +#include "../../required_dummies.h"
5 +#include <setjmp.h>
6 +#include <cmocka.h>
7 +
8 +static void test_number_pinting(void **state)
9 +{
10 + (void)state;
11 +
12 + char value[50];
13 +
14 + print_calculated_number(value, 0);
15 + assert_string_equal(value, "0");
16 +
17 + print_calculated_number(value, 0.0000001);
18 + assert_string_equal(value, "0.0000001");
19 +
20 + print_calculated_number(value, 0.00000009);
21 + assert_string_equal(value, "0.0000001");
22 +
23 + print_calculated_number(value, 0.000000001);
24 + assert_string_equal(value, "0");
25 +
26 + print_calculated_number(value, 99.99999999999999999);
27 + assert_string_equal(value, "100");
28 +
29 + print_calculated_number(value, -99.99999999999999999);
30 + assert_string_equal(value, "-100");
31 +
32 + print_calculated_number(value, 123.4567890123456789);
33 + assert_string_equal(value, "123.456789");
34 +
35 + print_calculated_number(value, 9999.9999999);
36 + assert_string_equal(value, "9999.9999999");
37 +
38 + print_calculated_number(value, -9999.9999999);
39 + assert_string_equal(value, "-9999.9999999");
40 +}
41 +
42 +int main(void)
43 +{
44 + const struct CMUnitTest tests[] = {
45 + cmocka_unit_test(test_number_pinting)
46 + };
47 +
48 + return cmocka_run_group_tests_name("storage_number", tests, NULL, NULL);
49 +}
libnetdata/tests/Makefile.am new
+4
@@ -0,0 +1,4 @@
1 +# SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +AUTOMAKE_OPTIONS = subdir-objects
4 +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
libnetdata/tests/test_str2ld.c new
+48
@@ -0,0 +1,48 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +#include "../libnetdata.h"
4 +#include "../required_dummies.h"
5 +#include <setjmp.h>
6 +#include <cmocka.h>
7 +
8 +static void test_str2ld(void **state)
9 +{
10 + (void)state;
11 + char *values[] = {
12 + "1.2345678",
13 + "-35.6",
14 + "0.00123",
15 + "23842384234234.2",
16 + ".1",
17 + "1.2e-10",
18 + "hello",
19 + "1wrong",
20 + "nan",
21 + "inf",
22 + NULL
23 + };
24 +
25 + for (int i = 0; values[i]; i++) {
26 + char *e_mine = "hello", *e_sys = "world";
27 + LONG_DOUBLE mine = str2ld(values[i], &e_mine);
28 + LONG_DOUBLE sys = strtold(values[i], &e_sys);
29 +
30 + if (isnan(mine))
31 + assert_true(isnan(sys));
32 + else if (isinf(mine))
33 + assert_true(isinf(sys));
34 + else if (mine != sys)
35 + assert_false(abs(mine - sys) > 0.000001);
36 +
37 + assert_ptr_equal(e_mine, e_sys);
38 + }
39 +}
40 +
41 +int main(void)
42 +{
43 + const struct CMUnitTest tests[] = {
44 + cmocka_unit_test(test_str2ld)
45 + };
46 +
47 + return cmocka_run_group_tests_name("str2ld", tests, NULL, NULL);
48 +}