Skip to content

Commit

Permalink
test: add generic unlink wrapper
Browse files Browse the repository at this point in the history
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 spdk#1357.

Signed-off-by: Jim Harris <[email protected]>
Change-Id: I1ee4c424ad31fe7d91d7b524ed47aedd279e5b5c
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/1948
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <[email protected]>
Reviewed-by: Ben Walker <[email protected]>
Reviewed-by: Tomasz Zawadzki <[email protected]>
  • Loading branch information
jimharris authored and tomzawadzki committed May 6, 2020
1 parent c4868f3 commit cfb65ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion autopackage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
Expand Down
10 changes: 10 additions & 0 deletions include/spdk_internal/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
21 changes: 21 additions & 0 deletions lib/ut_mock/mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 5 additions & 9 deletions test/unit/lib/reduce/reduce.c/reduce_ut.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cfb65ba

Please sign in to comment.