From 42b6254197ef12b05f5fd254f66a11b91d9e4c4c Mon Sep 17 00:00:00 2001 From: Konrad Sztyber Date: Wed, 8 Sep 2021 14:48:33 +0200 Subject: [PATCH] ut/rpc: mock out system calls Mock out open, close, unlink, and flock system calls. Flock isn't supported under nfs, so if the repo is mounted through nfs, the test will fail. And a unit test shouldn't be doing these calls aynway. Additionally, changed listen_addr from an IP address to a file path, as the RPC listens on a UNIX socket, so an IP address doesn't make much sense. Signed-off-by: Konrad Sztyber Change-Id: Icc759a74e6db4d1b9e766313a1e4672820e1c272 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/9446 Tested-by: SPDK CI Jenkins Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Reviewed-by: Paul Luse Reviewed-by: Tomasz Zawadzki Reviewed-by: Shuhei Matsumoto --- test/unit/lib/rpc/rpc.c/Makefile | 2 ++ test/unit/lib/rpc/rpc.c/rpc_ut.c | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/test/unit/lib/rpc/rpc.c/Makefile b/test/unit/lib/rpc/rpc.c/Makefile index c224b90c4..bbf86539a 100644 --- a/test/unit/lib/rpc/rpc.c/Makefile +++ b/test/unit/lib/rpc/rpc.c/Makefile @@ -33,6 +33,8 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + TEST_FILE = rpc_ut.c +LDFLAGS +=-Wl,--wrap,open -Wl,--wrap,close -Wl,--wrap,unlink -Wl,--wrap,flock include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/test/unit/lib/rpc/rpc.c/rpc_ut.c b/test/unit/lib/rpc/rpc.c/rpc_ut.c index fafbd37df..70fab84b2 100644 --- a/test/unit/lib/rpc/rpc.c/rpc_ut.c +++ b/test/unit/lib/rpc/rpc.c/rpc_ut.c @@ -34,6 +34,7 @@ #include "spdk/stdinc.h" #include "spdk_cunit.h" #include "spdk/jsonrpc.h" +#include "spdk_internal/mock.h" #include "common/lib/test_env.c" #include "spdk/log.h" @@ -65,6 +66,13 @@ DEFINE_STUB(spdk_jsonrpc_server_listen, struct spdk_jsonrpc_server *, (int domai DEFINE_STUB(spdk_jsonrpc_server_poll, int, (struct spdk_jsonrpc_server *server), 0); DEFINE_STUB_V(spdk_jsonrpc_server_shutdown, (struct spdk_jsonrpc_server *server)); +DECLARE_WRAPPER(open, int, (const char *pathname, int flags, mode_t mode)); +DECLARE_WRAPPER(close, int, (int fd)); +DECLARE_WRAPPER(flock, int, (int fd, int operation)); +DEFINE_WRAPPER(open, int, (const char *pathname, int flags, mode_t mode), (pathname, flags, mode)); +DEFINE_WRAPPER(close, int, (int fd), (fd)); +DEFINE_WRAPPER(flock, int, (int fd, int operation), (fd, operation)); + int spdk_json_decode_object(const struct spdk_json_val *values, const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) { @@ -235,9 +243,13 @@ test_rpc_spdk_get_version(void) static void test_spdk_rpc_listen_close(void) { - const char listen_addr[128] = "10.67.12.34"; + const char listen_addr[128] = "/var/tmp/spdk-rpc-ut.sock"; char rpc_lock_path[128] = {}; + MOCK_SET(open, 1); + MOCK_SET(close, 0); + MOCK_SET(flock, 0); + spdk_rpc_listen(listen_addr); snprintf(rpc_lock_path, sizeof(g_rpc_lock_path), "%s.lock", g_rpc_listen_addr_unix.sun_path); @@ -253,6 +265,10 @@ test_spdk_rpc_listen_close(void) CU_ASSERT(g_jsonrpc_server == NULL); CU_ASSERT(g_rpc_lock_fd == -1); CU_ASSERT(g_rpc_lock_path[0] == '\0'); + + MOCK_CLEAR(open); + MOCK_CLEAR(close); + MOCK_CLEAR(flock); } int main(int argc, char **argv)