From 74af1d5bd188e484309021792b27522f32c30f8c Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 7 Sep 2023 17:03:56 +0900 Subject: [PATCH] tests: Restructure test Instead of building testsuite into a shared object and the main() dlopens it, the macro * #define TESTSUITE() has been introduced for testsuite. It runs the testsuite and report to the main() how many testcases have failed. Change-Id: I6a9330601b96f7795442a85631dc666a97fe6d47 Signed-off-by: Youngjae Cho --- tests/CMakeLists.txt | 25 +++++++++++++------ tests/libcommon/CMakeLists.txt | 16 ------------ tests/libcommon/test-common.c | 56 ++++++++++++++++++++---------------------- tests/test-main.c | 54 +++++++++++----------------------------- tests/test-main.h | 8 ++++++ tests/test-mock.c | 1 - 6 files changed, 65 insertions(+), 95 deletions(-) delete mode 100644 tests/libcommon/CMakeLists.txt diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bfd44ea..132c796 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,20 +1,29 @@ SET(TEST_DRIVER "test-driver") -FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) -SET(TEST_SUITE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) -ADD_DEFINITIONS("-DTEST_SUITE_DIRECTORY=\"${TEST_SUITE_DIRECTORY}\"") +FILE(GLOB TEST_DRIVER_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") +FILE(GLOB LIBCOMMON_SRCS + "${CMAKE_SOURCE_DIR}/tests/libcommon/*.c" + "${CMAKE_SOURCE_DIR}/src/libcommon/*.c") + +SET(SRCS + ${TEST_DRIVER_SRCS} + ${LIBCOMMON_SRCS} + ) + +SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open") +SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED + glib-2.0 + capi-system-info cmocka) ADD_EXECUTABLE(${TEST_DRIVER} ${SRCS}) -TARGET_LINK_LIBRARIES(${TEST_DRIVER} cmocka dl) -SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "-Wl,--export-dynamic") +TARGET_LINK_LIBRARIES(${TEST_DRIVER} ${REQUIRED_PKGS_LDFLAGS}) +SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "${WRAP_FLAGS} -Wl,--export-dynamic") ADD_CUSTOM_TARGET(run-test ALL "./${TEST_DRIVER}") ADD_DEPENDENCIES(run-test ${TEST_DRIVER}) - -# add test suites -ADD_SUBDIRECTORY(libcommon) diff --git a/tests/libcommon/CMakeLists.txt b/tests/libcommon/CMakeLists.txt deleted file mode 100644 index 9fe27d3..0000000 --- a/tests/libcommon/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -SET(TEST_SUITE "test-driver-libcommon") - -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) - -FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") -FILE(GLOB ORIG_SRCS "${CMAKE_SOURCE_DIR}/src/libcommon/*.c") - -ADD_LIBRARY(${TEST_SUITE} MODULE ${SRCS} ${ORIG_SRCS}) -SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${TEST_SUITE_DIRECTORY}) -ADD_DEPENDENCIES(${TEST_DRIVER} ${TEST_SUITE}) - -SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open") -SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") -SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LINK_FLAGS ${WRAP_FLAGS}) - -TARGET_LINK_LIBRARIES(${TEST_SUITE} cmocka glib-2.0) diff --git a/tests/libcommon/test-common.c b/tests/libcommon/test-common.c index f34ce27..9f97bd2 100644 --- a/tests/libcommon/test-common.c +++ b/tests/libcommon/test-common.c @@ -6,11 +6,11 @@ #include #include -#include "libsyscommon/file.h" -#include "libsyscommon/list.h" -#include "libsyscommon/ini-parser.h" -#include "libsyscommon/bitmap.h" -#include "libsyscommon/notifier.h" +#include +#include +#include +#include +#include #include "../test-main.h" #include "../test-mock.h" @@ -501,28 +501,24 @@ static void test_syscommon_notifier_emit_notify_priority_p1(void **state) syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_8, (void *)(intptr_t)0x1234); } -int run_test_suite(void) -{ - const struct CMUnitTest testsuite[] = { - cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown), - cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown), - cmocka_unit_test(test_sys_get_str_n2), - cmocka_unit_test(test_list_append_p), - cmocka_unit_test(test_list_prepend_p), - cmocka_unit_test(test_config_parse_p), - cmocka_unit_test(test_init_bitmap_p), - cmocka_unit_test(test_init_bitmap_n), - cmocka_unit_test(test_test_bit), - cmocka_unit_test(test_all_bit), - cmocka_unit_test(test_syscommon_notifier_emit_notify_p1), - cmocka_unit_test(test_syscommon_notifier_emit_notify_p2), - cmocka_unit_test(test_syscommon_notifier_emit_notify_p3), - cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p), - cmocka_unit_test(test_destroy_callback_p1), - cmocka_unit_test(test_destroy_callback_p2), - cmocka_unit_test(test_destroy_callback_p3), - cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1), - }; - - return cmocka_run_group_tests(testsuite, NULL, NULL); -} +static const struct CMUnitTest testsuite_libcommon[] = { + cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown), + cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown), + cmocka_unit_test(test_sys_get_str_n2), + cmocka_unit_test(test_list_append_p), + cmocka_unit_test(test_list_prepend_p), + cmocka_unit_test(test_config_parse_p), + cmocka_unit_test(test_init_bitmap_p), + cmocka_unit_test(test_init_bitmap_n), + cmocka_unit_test(test_test_bit), + cmocka_unit_test(test_all_bit), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p1), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p2), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p3), + cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p), + cmocka_unit_test(test_destroy_callback_p1), + cmocka_unit_test(test_destroy_callback_p2), + cmocka_unit_test(test_destroy_callback_p3), + cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1), +}; +TESTSUITE(testsuite_libcommon) diff --git a/tests/test-main.c b/tests/test-main.c index 5af3e94..aa899b4 100644 --- a/tests/test-main.c +++ b/tests/test-main.c @@ -6,46 +6,20 @@ #include "test-main.h" -int main(int argc, char *argv[]) -{ - int fail = 0; - DIR *dir; - struct dirent *ent; - - dir = opendir(TEST_SUITE_DIRECTORY); - if (!dir) { - printf("Failed to opendir, %m\n"); - return 1; - } - - while ((ent = readdir(dir))) { - void *handle; - char sopath[512] = {0, }; - int (*run_test_suite) (void); - - if (!strstr(ent->d_name, ".so")) - continue; - - snprintf(sopath, sizeof(sopath), "%s/%s", TEST_SUITE_DIRECTORY, ent->d_name); - - handle = dlopen(sopath, RTLD_LAZY); - if (!handle) { - printf("Failed to dlopen, %s\n", dlerror()); - continue; - } +static int totalfail = 0; - run_test_suite = dlsym(handle, "run_test_suite"); - if (!run_test_suite) { - printf("Failed to dlsym, %s\n", dlerror()); - dlclose(handle); - continue; - } - - fail += run_test_suite(); - dlclose(handle); - } - - closedir(dir); +void testsuite_report_failcount(int failcount) +{ + totalfail += failcount; +} - return fail; +int main(int argc, char *argv[]) +{ + /** + * All test suites will run by constructor before the main() and the totalfail + * will be updated when each testsuite calls testsuite_record_failcount(), + * notifying about how many testcases they have failed. Therefore, the main() + * just need to return totalfail. + */ + return totalfail; } diff --git a/tests/test-main.h b/tests/test-main.h index 4b04a85..0430e4d 100644 --- a/tests/test-main.h +++ b/tests/test-main.h @@ -6,5 +6,13 @@ #include #include +void testsuite_report_failcount(int failcount); + +#define TESTSUITE(cmocka_unittest) \ +static void __attribute__((constructor)) run(void) \ +{ \ + testsuite_report_failcount(cmocka_run_group_tests(cmocka_unittest, NULL, NULL)); \ +} + #endif //__TEST_MAIN_H__ diff --git a/tests/test-mock.c b/tests/test-mock.c index 4e7f260..58eb80b 100644 --- a/tests/test-mock.c +++ b/tests/test-mock.c @@ -29,7 +29,6 @@ int __wrap_open64(const char *pathname, int flags) return __wrap_open(pathname, flags); } - void mock_open(const char *pathname, int flags) { will_return(__wrap_open, pathname); -- 2.7.4