Add CMocka unit tests (#6985)

* Add str2ld test

* Build test with Autotools

* Add storage_number test

* Configure tests in CMake
This commit is contained in:
Vladimir Kobal 2019-10-15 15:15:40 +03:00 committed by GitHub
parent 8a2f99238a
commit 16d40fbb14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 218 additions and 1 deletions

3
.gitignore vendored
View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -23,6 +23,7 @@ SUBDIRS = \
storage_number \
threads \
url \
tests \
$(NULL)
dist_noinst_DATA = \

View File

@ -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

View File

@ -3,6 +3,9 @@
AUTOMAKE_OPTIONS = subdir-objects
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
SUBDIRS = \
tests \
$(NULL)
dist_noinst_DATA = \
README.md \

View File

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-3.0-or-later
AUTOMAKE_OPTIONS = subdir-objects
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in

View File

@ -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);
}

View File

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-3.0-or-later
AUTOMAKE_OPTIONS = subdir-objects
MAINTAINERCLEANFILES = $(srcdir)/Makefile.in

View File

@ -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);
}