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 <konrad.sztyber@intel.com>
Change-Id: Icc759a74e6db4d1b9e766313a1e4672820e1c272
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/9446
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Paul Luse <paul.e.luse@intel.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Konrad Sztyber 2021-09-08 14:48:33 +02:00 committed by Tomasz Zawadzki
parent 93db90f7f3
commit 42b6254197
2 changed files with 19 additions and 1 deletions

View File

@ -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

View File

@ -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)