From cfb65ba61180ae80899756239e08f4656866beb3 Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Mon, 20 Apr 2020 12:58:34 -0700 Subject: [PATCH] test: add generic unlink wrapper reduce library uses unlink, but the unit tests need to override it in a specific way. But linking unit tests with LTO requires the wrapper definitions be in objects/libraries listed *after* the object/library that refers to it. So we need to make the unlink wrapper somewhat generic. We do this by exporting a string and callback function that the user can set to enable a user-defined function to be called when unlink() is called with a specific file name. Also revert 3ef6d06 as part of this patch, since we no longer require the workaround that it implemented. Fixes issue #1357. Signed-off-by: Jim Harris Change-Id: I1ee4c424ad31fe7d91d7b524ed47aedd279e5b5c Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/1948 Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Tomasz Zawadzki --- autopackage.sh | 2 +- configure | 5 ----- include/spdk_internal/mock.h | 10 ++++++++++ lib/ut_mock/mock.c | 21 +++++++++++++++++++++ test/unit/lib/reduce/reduce.c/reduce_ut.c | 14 +++++--------- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/autopackage.sh b/autopackage.sh index 0bd446a1b..d54d05d55 100755 --- a/autopackage.sh +++ b/autopackage.sh @@ -36,7 +36,7 @@ fi timing_enter build_release if [ $(uname -s) = Linux ]; then - ./configure $(get_config_params) --disable-debug --enable-lto --disable-unit-tests + ./configure $(get_config_params) --disable-debug --enable-lto else # LTO needs a special compiler to work on BSD. ./configure $(get_config_params) --disable-debug diff --git a/configure b/configure index 6655dd53e..b9d1e8177 100755 --- a/configure +++ b/configure @@ -499,11 +499,6 @@ if [[ "${CONFIG[ISAL]}" = "n" ]] && [[ "${CONFIG[REDUCE]}" = "y" ]]; then exit 1 fi -if [[ "${CONFIG[LTO]}" = "y" ]] && [[ "${CONFIG[UNIT_TESTS]}" = "y" ]]; then - echo "ERROR Conflicting options: --enable-lto is not compatible with --enable-unit-tests." - exit 1 -fi - if [ -z "${CONFIG[ENV]}" ]; then CONFIG[ENV]=$rootdir/lib/env_dpdk echo "Using default SPDK env in ${CONFIG[ENV]}" diff --git a/include/spdk_internal/mock.h b/include/spdk_internal/mock.h index 705378fac..8de44ae55 100644 --- a/include/spdk_internal/mock.h +++ b/include/spdk_internal/mock.h @@ -122,4 +122,14 @@ DECLARE_WRAPPER(sendmsg, ssize_t, (int sockfd, const struct msghdr *msg, int fla DECLARE_WRAPPER(writev, ssize_t, (int fd, const struct iovec *iov, int iovcnt)); +/* unlink is done a bit differently. */ +extern char *g_unlink_path; +extern void (*g_unlink_callback)(void); +/* If g_unlink_path is NULL, __wrap_unlink will return ENOENT. + * If the __wrap_unlink() parameter does not match g_unlink_path, it will return ENOENT. + * If g_unlink_path does match, and g_unlink_callback has been set, g_unlink_callback will + * be called before returning 0. + */ +int __wrap_unlink(const char *path); + #endif /* SPDK_INTERNAL_MOCK_H */ diff --git a/lib/ut_mock/mock.c b/lib/ut_mock/mock.c index a7b2a23d3..cfe51c1d5 100644 --- a/lib/ut_mock/mock.c +++ b/lib/ut_mock/mock.c @@ -48,3 +48,24 @@ DEFINE_WRAPPER(sendmsg, ssize_t, (int sockfd, const struct msghdr *msg, int flag flags)) DEFINE_WRAPPER(writev, ssize_t, (int fd, const struct iovec *iov, int iovcnt), (fd, iov, iovcnt)) + +char *g_unlink_path; +void (*g_unlink_callback)(void); + +int +__attribute__((used)) +__wrap_unlink(const char *path) +{ + if (g_unlink_path == NULL) { + return ENOENT; + } + + if (strcmp(g_unlink_path, path) != 0) { + return ENOENT; + } + + if (g_unlink_callback) { + g_unlink_callback(); + } + return 0; +} diff --git a/test/unit/lib/reduce/reduce.c/reduce_ut.c b/test/unit/lib/reduce/reduce.c/reduce_ut.c index 8759721a0..9c94a4ac6 100644 --- a/test/unit/lib/reduce/reduce.c/reduce_ut.c +++ b/test/unit/lib/reduce/reduce.c/reduce_ut.c @@ -176,17 +176,10 @@ persistent_pm_buf_destroy(void) g_persistent_pm_buf_len = 0; } -int __wrap_unlink(const char *path); - -int -__wrap_unlink(const char *path) +static void +unlink_cb(void) { - if (strcmp(g_path, path) != 0) { - return ENOENT; - } - persistent_pm_buf_destroy(); - return 0; } static void @@ -1296,6 +1289,9 @@ main(int argc, char **argv) CU_ADD_TEST(suite, overlapped); CU_ADD_TEST(suite, compress_algorithm); + g_unlink_path = g_path; + g_unlink_callback = unlink_cb; + CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); num_failures = CU_get_number_of_failures();