From: Michael Wallner Date: Fri, 31 Jan 2020 17:53:41 +0000 (+0100) Subject: cmake: build&run tests (sortof) X-Git-Tag: post_cmake~24 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=acb156e60a8d705a56806ca51212bab22ec1e79d;p=m6w6%2Flibmemcached cmake: build&run tests (sortof) --- diff --git a/.gitignore b/.gitignore index aa09fc4d..1786a72d 100644 --- a/.gitignore +++ b/.gitignore @@ -171,3 +171,4 @@ tests/testudp tests/var/ tmp_chroot unittests/unittests +venv/ diff --git a/CMakeLists.txt b/CMakeLists.txt index ff0a93a2..4993d05d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ include(Config.cmake) include(Include.cmake) find_package(Threads REQUIRED) +set(THREADS_PREFER_PTHREAD_FLAG ON) + find_library(HAVE_LIBSASL NAMES sasl sasl2) check_header(sasl/sasl.h) @@ -36,6 +38,7 @@ check_header(sys/socket.h) check_header(sys/time.h) check_header(sys/types.h) check_header(sys/un.h) +check_header(sys/wait.h) check_header(time.h) check_header(umem.h) check_header(unistd.h) @@ -62,6 +65,23 @@ check_header(cinttypes) check_debug() include_directories(${CMAKE_BINARY_DIR}) +set(AUTOHEADER_FILE mem_config.h) + +set(CLIENTS + memcapable + memcat + memcp + memdump + memerror + memexist + memflush + memparse + memping + memrm + memslap + memstat + memtouch + ) add_subdirectory(clients) add_subdirectory(libhashkit) @@ -70,7 +90,13 @@ add_subdirectory(libmemcached) add_subdirectory(libmemcached-1.0) add_subdirectory(libmemcachedutil) + +include(CTest) +enable_testing() +add_subdirectory(libtest) +add_subdirectory(tests) + add_subdirectory(docs) # keep last -configure_file(mem_config.h.in ${CMAKE_BINARY_DIR}/mem_config.h @ONLY) +configure_file(mem_config.h.in ${CMAKE_BINARY_DIR}/${AUTOHEADER_FILE} @ONLY) diff --git a/Include.cmake b/Include.cmake index 5821e6de..919fc630 100644 --- a/Include.cmake +++ b/Include.cmake @@ -1,6 +1,12 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake) +function(set_test_target TARGET2) + foreach(TEST IN LISTS TESTS) + add_custom_command(TARGET ${TARGET2} POST_BUILD COMMAND ${TEST}) + endforeach() +endfunction() + include(CheckTypeSize) include(CheckCSourceRuns) include(CheckIncludeFileCXX) diff --git a/clients/CMakeLists.txt b/clients/CMakeLists.txt index 2c8dcac4..9f753f20 100644 --- a/clients/CMakeLists.txt +++ b/clients/CMakeLists.txt @@ -1,29 +1,9 @@ -set(CLIENTS - memcapable - memcat - memcp - memdump - memerror - memexist - memflush - memparse - memping - memrm - memslap - memstat - memtouch) - -add_library(libclient_utilities STATIC utilities.cc) +add_library(libclient_utilities STATIC utilities.cc generator.cc execute.cc) add_library(client_utilities ALIAS libclient_utilities) target_include_directories(libclient_utilities PRIVATE ..) target_link_libraries(libclient_utilities libmemcachedinternal) -add_library(libclient_slap STATIC generator.cc execute.cc) -add_library(client_slap ALIAS libclient_slap) -target_include_directories(libclient_slap PRIVATE ..) -target_link_libraries(libclient_slap libmemcachedinternal) - foreach(CLIENT IN LISTS CLIENTS) add_executable(${CLIENT} ${CLIENT}.cc) target_include_directories(${CLIENT} PRIVATE ..) @@ -36,9 +16,9 @@ target_sources(memcapable PRIVATE ../libmemcached/byteorder.cc) # extra libs -target_link_libraries(memcapable ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(memcapable Threads::Threads) target_link_libraries(memping libmemcachedutil) -target_link_libraries(memslap libclient_slap ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(memslap Threads::Threads) # memaslap is special @@ -50,5 +30,5 @@ if(LIBEVENT AND HAVE_C_STDATOMIC) add_executable(memaslap memaslap.c ms_conn.c ms_setting.c ms_sigsegv.c ms_stats.c ms_task.c ms_thread.c) target_include_directories(memaslap PRIVATE ..) - target_link_libraries(memaslap libclient_slap libclient_utilities ${LIBEVENT} ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(memaslap libclient_utilities ${LIBEVENT} Threads::Threads) endif() diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 31f30ca0..ace24785 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -34,7 +34,7 @@ if(SPHINX_EXECUTABLE) "${SPHINX_BUILD_DIR}/conf.py" @ONLY) - add_custom_target("docs" + add_custom_target(html ${SPHINX_EXECUTABLE} -q -a -b html -c "${SPHINX_BUILD_DIR}" @@ -44,7 +44,6 @@ if(SPHINX_EXECUTABLE) "${SPHINX_HTML_DIR}" BYPRODUCTS ${SPHINX_HTML_DIR} COMMENT "Build HTML documentation with Sphinx") - #list(APPEND ADDITIONAL_CLEAN_FILES ${SPHINX_CACHE_DIR} ${SPHINX_HTML_DIR}) add_custom_target(man ${SPHINX_EXECUTABLE} -q -a -b man @@ -55,5 +54,4 @@ if(SPHINX_EXECUTABLE) "${SPHINX_MAN_DIR}" BYPRODUCTS ${SPHINX_MAN_DIR} COMMENT "Build manpage documentation with Sphinx") - #list(APPEND ADDITIONAL_CLEAN_FILES ${SPHINX_CACHE_DIR} ${SPHINX_MAN_DIR}) endif() diff --git a/libhashkit/CMakeLists.txt b/libhashkit/CMakeLists.txt index dbb76dcf..fa871600 100644 --- a/libhashkit/CMakeLists.txt +++ b/libhashkit/CMakeLists.txt @@ -35,5 +35,4 @@ set_target_properties(libhashkit PROPERTIES LIBRARY_OUTPUT_NAME hashkit) target_include_directories(libhashkit PRIVATE ..) target_compile_options(libhashkit PRIVATE -DBUILDING_HASHKIT) -set(AUTOHEADER_FILE mem_config.h) configure_file(hashkitcon.h.in hashkitcon.h @ONLY) diff --git a/libmemcached/CMakeLists.txt b/libmemcached/CMakeLists.txt index eca34f5d..708b637d 100644 --- a/libmemcached/CMakeLists.txt +++ b/libmemcached/CMakeLists.txt @@ -66,7 +66,7 @@ add_library(libmemcached SHARED ${LIBMEMCACHED_SOURCES}) add_library(memcached ALIAS libmemcached) set_target_properties(libmemcached PROPERTIES LIBRARY_OUTPUT_NAME memcached) -target_link_libraries(libmemcached libhashkit ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS}) +target_link_libraries(libmemcached libhashkit Threads::Threads ${CMAKE_DL_LIBS}) target_include_directories(libmemcached PRIVATE ..) target_compile_definitions(libmemcached PRIVATE -DBUILDING_LIBMEMCACHED) @@ -74,6 +74,6 @@ add_library(libmemcachedinternal STATIC ${LIBMEMCACHED_SOURCES}) add_library(memcachedinternal ALIAS libmemcachedinternal) set_target_properties(libmemcachedinternal PROPERTIES LIBRARY_OUTPUT_NAME memcachedinternal) -target_link_libraries(libmemcachedinternal libhashkit ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS}) +target_link_libraries(libmemcachedinternal libhashkit Threads::Threads ${CMAKE_DL_LIBS}) target_include_directories(libmemcachedinternal PRIVATE ..) target_compile_definitions(libmemcachedinternal PRIVATE -DBUILDING_LIBMEMCACHEDINTERNAL) diff --git a/libmemcachedutil/CMakeLists.txt b/libmemcachedutil/CMakeLists.txt index 2bf1aa98..694a6cfd 100644 --- a/libmemcachedutil/CMakeLists.txt +++ b/libmemcachedutil/CMakeLists.txt @@ -1,6 +1,4 @@ -find_package(Threads) - add_library(libmemcachedutil SHARED ../libmemcached/backtrace.cc flush.cc @@ -13,4 +11,4 @@ add_library(memcachedutil ALIAS libmemcachedutil) set_target_properties(libmemcachedutil PROPERTIES LIBRARY_OUTPUT_NAME memcachedutil) target_include_directories(libmemcachedutil PRIVATE ..) target_compile_definitions(libmemcachedutil PRIVATE -DBUILDING_LIBMEMCACHED) -target_link_libraries(libmemcachedutil libmemcached ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(libmemcachedutil libmemcached Threads::Threads) diff --git a/libtest/CMakeLists.txt b/libtest/CMakeLists.txt new file mode 100644 index 00000000..24686a3c --- /dev/null +++ b/libtest/CMakeLists.txt @@ -0,0 +1,73 @@ + +file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/tmp_chroot) +file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/libtool CONTENT +"#!/bin/bash +shift +exec $@ +") +if(UNIX) + execute_process(COMMAND chmod +x ${CMAKE_BINARY_DIR}/libtool) +endif() + +add_library(libtest STATIC + alarm.cc + binaries.cc + client.cc + cmdline.cc + collection.cc + comparison.cc + core.cc + cpu.cc + dns.cc + dream.cc + drizzled.cc + exception.cc + exception/fatal.cc + formatter.cc + framework.cc + gearmand.cc + has.cc + http.cc + is_local.cc + killpid.cc + libtool.cc + main.cc + memcached.cc + port.cc + result.cc + runner.cc + server.cc + server_container.cc + signal.cc + socket.cc + strerror.cc + timer.cc + tmpfile.cc + vchar.cc + ) +set_target_properties(libtest PROPERTIES LIBRARY_OUTPUT_NAME test) +target_compile_definitions(libtest PRIVATE + BUILDING_LIBTEST=1 + LIBTEST_TEMP=\"${CMAKE_BINARY_DIR}/tmp_chroot\" + + DRIZZLED_BINARY=\"drizzled\" + GEARMAND_BINARY=\"gearmand\" + MEMCACHED_BINARY=\"/usr/bin/memcached\" + HAVE_MEMCACHED_BINARY=1 + ) +target_link_libraries(libtest PRIVATE Threads::Threads ${CMAKE_DL_LIBS}) +target_include_directories(libtest PRIVATE ..) + +add_executable(wait wait.cc dream.cc) +target_include_directories(wait PRIVATE ..) + +add_executable(core_count core_count.cc cpu.cc) +target_include_directories(core_count PRIVATE ..) + +add_executable(abort abort.cc) +add_executable(backtrace backtrace_test.cc ../libmemcached/backtrace.cc) +target_link_libraries(backtrace PRIVATE ${CMAKE_DL_LIBS}) +target_include_directories(backtrace PRIVATE ..) + +configure_file(yatlcon.h.in yatlcon.h @ONLY) +configure_file(version.h.in version.h @ONLY) diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index cbe8ff9a..55e6b5b6 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -142,7 +142,7 @@ Application::Application(const std::string& arg, const bool _use_libtool_arg) : { if (libtool() == NULL) { - FATAL("libtool requested, but know libtool was found"); + FATAL("libtool requested, but no libtool was found"); } } @@ -280,7 +280,16 @@ Application::error_t Application::run(const char *args[]) { if (_will_fail == false) { - Error << strerror(spawn_ret) << "(" << spawn_ret << ")"; + std::string sb; + char buf[1024]; + + std::for_each(built_argv.begin(), built_argv.end()-1, [&sb](const char *a) { + if (sb.size()) { + sb.append(" "); + } + sb.append(a); + }); + Error << strerror(spawn_ret) << "(" << spawn_ret << "): " << sb << " cwd:" << getcwd(buf, sizeof(buf)-1); } _pid= -1; return Application::INVALID_POSIX_SPAWN; @@ -410,6 +419,22 @@ bool Application::slurp() return data_was_read; } +std::pair Application::output() +{ + slurp(); + return { + std::string { + stdout_result().data(), + stdout_result().size() + }, + std::string { + stderr_result().data(), + stderr_result().size() + } + }; + +} + Application::error_t Application::join() { pid_t waited_pid= waitpid(_pid, &_status, WUNTRACED); @@ -826,12 +851,18 @@ int exec_cmdline(const std::string& command, const char *args[], bool use_libtoo Application::error_t ret= app.run(args); + if (Application::SUCCESS == ret) { + ret = app.join(); + } + if (ret != Application::SUCCESS) { - return int(ret); + auto out = app.output(); + Error << command << " stdout: " << out.first; + Error << command << " stderr: " << out.second; } - return int(app.join()); + return int(ret); } } // namespace exec_cmdline diff --git a/libtest/cmdline.h b/libtest/cmdline.h index 52ced4bf..244c6464 100644 --- a/libtest/cmdline.h +++ b/libtest/cmdline.h @@ -180,6 +180,7 @@ public: bool check() const; bool slurp(); + std::pair output(); void murder(); void clear() diff --git a/libtest/has.cc b/libtest/has.cc index 0983f070..6048bacf 100644 --- a/libtest/has.cc +++ b/libtest/has.cc @@ -75,13 +75,15 @@ bool has_libdrizzle(void) bool has_postgres_support(void) { char *getenv_ptr; - if (bool((getenv_ptr= getenv("POSTGES_IS_RUNNING_AND_SETUP")))) + if (bool((getenv_ptr= getenv("POSTGRES_IS_RUNNING_AND_SETUP")))) { (void)(getenv_ptr); +#if defined(HAVE_LIBPQ) && HAVE_LIBPQ if (HAVE_LIBPQ) { return true; } +#endif } return false; diff --git a/libtest/main.cc b/libtest/main.cc index b93f7570..f20cb955 100644 --- a/libtest/main.cc +++ b/libtest/main.cc @@ -263,12 +263,6 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (libtest::libtool() == NULL) - { - Error << "Failed to locate libtool"; - return EXIT_FAILURE; - } - if (getenv("YATL_COLLECTION_TO_RUN")) { if (strlen(getenv("YATL_COLLECTION_TO_RUN"))) diff --git a/libtest/memcached.cc b/libtest/memcached.cc index 7af26092..7d43ec0e 100644 --- a/libtest/memcached.cc +++ b/libtest/memcached.cc @@ -214,7 +214,7 @@ bool Memcached::build() add_option(sasl()); } - add_option("-vv"); + //add_option("-vv"); return true; } diff --git a/libtest/server.cc b/libtest/server.cc index 401218ba..bfb265cd 100644 --- a/libtest/server.cc +++ b/libtest/server.cc @@ -529,20 +529,4 @@ bool Server::kill() return false; } -std::pair Server::output() -{ - _app.slurp(); - return { - std::string { - _app.stdout_result().data(), - _app.stdout_result().size() - }, - std::string { - _app.stderr_result().data(), - _app.stderr_result().size() - } - }; -} - - } // namespace libtest diff --git a/libtest/server.h b/libtest/server.h index 10c1d374..73ad3bf8 100644 --- a/libtest/server.h +++ b/libtest/server.h @@ -205,7 +205,10 @@ public: _log_file.clear(); } - std::pair output(); + std::pair output() + { + return _app.output(); + } pid_t pid() const; diff --git a/libtest/yatlcon.h.in b/libtest/yatlcon.h.in index 17cb0aeb..e5adefb5 100644 --- a/libtest/yatlcon.h.in +++ b/libtest/yatlcon.h.in @@ -39,3 +39,9 @@ #pragma once #include "@AUTOHEADER_FILE@" + +#ifndef LIBTEST_TEMP +# define LIBTEST_TEMP "/tmp" +#endif + +#define HAVE_LIBMEMCACHED 1 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..f983db9a --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,55 @@ + +add_subdirectory(libmemcached-1.0) + +add_executable(cycle cycle.cc) +target_link_libraries(cycle PRIVATE libtest Threads::Threads) +target_include_directories(cycle PRIVATE ..) +add_test(cycle cycle) + +add_executable(parser parser.cc) +target_link_libraries(parser PRIVATE libtest libmemcached) +target_include_directories(parser PRIVATE ..) +add_test(parser parser) + +add_executable(failure failure.cc) +add_executable(testudp mem_udp.cc) + +foreach(TEST IN ITEMS failure testudp) + target_sources(${TEST} PRIVATE + libmemcached-1.0/callback_counter.cc + libmemcached-1.0/fetch_all_results.cc + libmemcached-1.0/generate.cc + libmemcached-1.0/print.cc + ) + target_link_libraries(${TEST} PRIVATE + libclient_utilities + libmemcached + libmemcachedutil + libtest + ) + target_include_directories(${TEST} PRIVATE ..) + add_test(${TEST} ${TEST}) +endforeach() + +add_executable(testhashkit hashkit_functions.cc) +target_link_libraries(testhashkit PRIVATE libtest libhashkit) +target_include_directories(testhashkit PRIVATE ..) +add_test(hashkit testhashkit) + +add_executable(hash_plus hash_plus.cc) +target_link_libraries(hash_plus PRIVATE libtest libhashkit) +target_include_directories(hash_plus PRIVATE ..) +add_test(hashplus hash_plus) + +foreach(CLIENT IN LISTS CLIENTS) + if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT}.cc) + add_executable(test${CLIENT} ${CLIENT}.cc) + target_link_libraries(test${CLIENT} PRIVATE + libmemcached + libmemcachedutil + libtest + ) + target_include_directories(test${CLIENT} PRIVATE ..) + add_test(test${CLIENT} test${CLIENT}) + endif() +endforeach() diff --git a/tests/libmemcached-1.0/CMakeLists.txt b/tests/libmemcached-1.0/CMakeLists.txt new file mode 100644 index 00000000..72bee1c0 --- /dev/null +++ b/tests/libmemcached-1.0/CMakeLists.txt @@ -0,0 +1,102 @@ + +add_executable(internals internals.cc string.cc) +target_link_libraries(internals PRIVATE libtest libmemcachedinternal Threads::Threads) +target_include_directories(internals PRIVATE ../..) +add_test(internals internals) + +add_executable(sasl + basic.cc + debug.cc + deprecated.cc + error_conditions.cc + ketama.cc + namespace.cc + parser.cc + pool.cc + print.cc + replication.cc + sasl.cc + virtual_buckets.cc + callback_counter.cc + fetch_all_results.cc + generate.cc + ) +target_link_libraries(sasl PRIVATE + libclient_utilities + libhashkit + libmemcached + libmemcachedutil + libtest + Threads::Threads + ) +target_include_directories(sasl PRIVATE ../..) +add_test(sasl sasl) + +add_executable(atomsmasher atomsmasher.cc) +add_executable(testplus plus.cpp) + +foreach(TEST IN ITEMS atomsmasher testplus) + target_sources(${TEST} PRIVATE + callback_counter.cc + debug.cc + fetch_all_results.cc + generate.cc + print.cc + ) + target_link_libraries(${TEST} PRIVATE + libclient_utilities + libhashkit + libmemcached + libmemcachedutil + libtest + ) + target_include_directories(${TEST} PRIVATE ../..) + add_test(${TEST} ${TEST}) +endforeach() + + +find_library(LIBUUID uuid REQUIRED) + +add_executable(testapp all_tests.cc) +add_executable(testsocket all_tests_socket.cc) + +foreach(TEST IN ITEMS testapp testsocket) + target_sources(${TEST} PRIVATE + basic.cc + callback_counter.cc + callbacks.cc + debug.cc + deprecated.cc + dump.cc + encoding_key.cc + error_conditions.cc + exist.cc + fetch_all_results.cc + generate.cc + haldenbrand.cc + ketama.cc + mem_functions.cc + memcached_get.cc + namespace.cc + parser.cc + pool.cc + print.cc + replication.cc + server_add.cc + setup_and_teardowns.cc + stat.cc + touch.cc + virtual_buckets.cc + ) + target_link_libraries(${TEST} PRIVATE + libclient_utilities + libhashkit + libmemcached + libmemcachedutil + libtest + Threads::Threads + ${LIBUUID} + ) + target_include_directories(${TEST} PRIVATE ../..) + add_test(${TEST} ${TEST}) +endforeach() diff --git a/tests/libmemcached-1.0/all_tests.cc b/tests/libmemcached-1.0/all_tests.cc index a471d89a..37471773 100644 --- a/tests/libmemcached-1.0/all_tests.cc +++ b/tests/libmemcached-1.0/all_tests.cc @@ -37,7 +37,7 @@ #include #include - +#include "libmemcached-1.0/memcached.h" #include "tests/basic.h" #include "tests/debug.h" #include "tests/deprecated.h" diff --git a/tests/libmemcached-1.0/all_tests_socket.cc b/tests/libmemcached-1.0/all_tests_socket.cc index 8321754d..fc3aa666 100644 --- a/tests/libmemcached-1.0/all_tests_socket.cc +++ b/tests/libmemcached-1.0/all_tests_socket.cc @@ -37,7 +37,7 @@ #include #include - +#include "libmemcached-1.0/memcached.h" #include "tests/basic.h" #include "tests/debug.h" #include "tests/deprecated.h" diff --git a/tests/libmemcached-1.0/callback_counter.cc b/tests/libmemcached-1.0/callback_counter.cc index 468ae846..a3f43de2 100644 --- a/tests/libmemcached-1.0/callback_counter.cc +++ b/tests/libmemcached-1.0/callback_counter.cc @@ -37,7 +37,7 @@ #include #include - +#include #include "tests/libmemcached-1.0/callback_counter.h" memcached_return_t callback_counter(const memcached_st*, memcached_result_st*, void *context) diff --git a/tests/libmemcached-1.0/callbacks.cc b/tests/libmemcached-1.0/callbacks.cc index 13d1b468..89eb75f7 100644 --- a/tests/libmemcached-1.0/callbacks.cc +++ b/tests/libmemcached-1.0/callbacks.cc @@ -37,7 +37,7 @@ #include #include - +#include "libmemcached-1.0/memcached.h" #include using namespace libtest; diff --git a/tests/libmemcached-1.0/exist.cc b/tests/libmemcached-1.0/exist.cc index 37495b0a..912785d8 100644 --- a/tests/libmemcached-1.0/exist.cc +++ b/tests/libmemcached-1.0/exist.cc @@ -36,7 +36,7 @@ #include #include - +#include "libmemcached-1.0/memcached.h" #include using namespace libtest; diff --git a/tests/libmemcached-1.0/fetch_all_results.cc b/tests/libmemcached-1.0/fetch_all_results.cc index aaad9edd..b7532134 100644 --- a/tests/libmemcached-1.0/fetch_all_results.cc +++ b/tests/libmemcached-1.0/fetch_all_results.cc @@ -37,7 +37,7 @@ #include #include - +#include #include test_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned, memcached_return_t& rc) diff --git a/tests/libmemcached-1.0/haldenbrand.cc b/tests/libmemcached-1.0/haldenbrand.cc index cdc476ab..8e9f6cc4 100644 --- a/tests/libmemcached-1.0/haldenbrand.cc +++ b/tests/libmemcached-1.0/haldenbrand.cc @@ -37,7 +37,7 @@ #include #include - +#include "libmemcached-1.0/memcached.h" #include "tests/libmemcached-1.0/haldenbrand.h" #include "tests/libmemcached-1.0/fetch_all_results.h"