Skip to content

Commit 16d40fb

Browse files
authored
Add CMocka unit tests (netdata#6985)
* Add str2ld test * Build test with Autotools * Add storage_number test * Configure tests in CMake
1 parent 8a2f992 commit 16d40fb

12 files changed

+218
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ tests/alarm_repetition/alarm.sh
173173
tests/template_dimension/template_dim.sh
174174

175175
# tests and temp files
176+
test-driver
177+
**/tests/*_testdriver
178+
**/tests/*_testdriver.trs
176179
python.d/python-modules-installer.sh
177180

178181
# documentation generated files

CMakeLists.txt

+22
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,25 @@ IF(ENABLE_PLUGIN_CGROUP_NETWORK)
876876
ELSE()
877877
message(STATUS "cgroup-network: disabled (requires Linux)")
878878
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
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,22 @@ if ENABLE_BACKEND_MONGODB
637637
netdata_SOURCES += $(MONGODB_BACKEND_FILES)
638638
netdata_LDADD += $(OPTIONAL_MONGOC_LIBS)
639639
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
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,30 @@ AC_SUBST([OPTIONAL_PROMETHEUS_REMOTE_WRITE_LIBS])
11301130
AC_SUBST([OPTIONAL_MONGOC_CFLAGS])
11311131
AC_SUBST([OPTIONAL_MONGOC_LIBS])
11321132

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])
11331157

11341158
AC_CONFIG_FILES([
11351159
Makefile
@@ -1172,6 +1196,7 @@ AC_CONFIG_FILES([
11721196
health/Makefile
11731197
health/notifications/Makefile
11741198
libnetdata/Makefile
1199+
libnetdata/tests/Makefile
11751200
libnetdata/adaptive_resortable_list/Makefile
11761201
libnetdata/avl/Makefile
11771202
libnetdata/buffer/Makefile
@@ -1187,6 +1212,7 @@ AC_CONFIG_FILES([
11871212
libnetdata/socket/Makefile
11881213
libnetdata/statistical/Makefile
11891214
libnetdata/storage_number/Makefile
1215+
libnetdata/storage_number/tests/Makefile
11901216
libnetdata/threads/Makefile
11911217
libnetdata/url/Makefile
11921218
libnetdata/json/Makefile

daemon/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
22

33
AUTOMAKE_OPTIONS = subdir-objects
4-
MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
4+
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
55
CLEANFILES = \
66
anonymous-statistics.sh \
77
$(NULL)

libnetdata/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SUBDIRS = \
2323
storage_number \
2424
threads \
2525
url \
26+
tests \
2627
$(NULL)
2728

2829
dist_noinst_DATA = \

libnetdata/required_dummies.h

+38
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
AUTOMAKE_OPTIONS = subdir-objects
44
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
55

6+
SUBDIRS = \
7+
tests \
8+
$(NULL)
69

710
dist_noinst_DATA = \
811
README.md \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
AUTOMAKE_OPTIONS = subdir-objects
4+
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
Original file line numberDiff line numberDiff line change
@@ -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

+4
Original file line numberDiff line numberDiff line change
@@ -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

+48
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)