From be74ecf79a9634d9de8653861369030e1af0b4af Mon Sep 17 00:00:00 2001 From: Paul Luse Date: Mon, 12 Jun 2017 14:31:50 -0700 Subject: [PATCH] test: add one unit test showing example of ut_mock wrap This patch shows how to use the spdk_mock library which now only consists of --wrap capability for a few functions. Next will be a patch that provides a generic mock capability to use either the --wrap feature or a combination of globals and stubs to make adding more UT easier wrt mocking. After that is in place nvme_ut.c will be updated to use the new library for all mocking. Change-Id: I1a6ffb722da043bb70bd4cfe1afa7c5e39a0fccb Signed-off-by: Paul Luse Reviewed-on: https://review.gerrithub.io/365074 Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris Reviewed-by: Daniel Verkamp --- include/spdk_internal/spdk_mock.h | 3 +++ mk/nvme.unittest.mk | 3 ++- test/lib/nvme/unit/nvme_c/nvme_ut.c | 41 +++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/spdk_internal/spdk_mock.h b/include/spdk_internal/spdk_mock.h index 103f16e0f..4666a3bb0 100644 --- a/include/spdk_internal/spdk_mock.h +++ b/include/spdk_internal/spdk_mock.h @@ -40,8 +40,11 @@ ret __wrap_ ## fn args; ret __real_ ## fn args; /* define new wrappers (alphabetically please) here using above helper macro */ +extern int ut_fake_pthread_mutex_init; DECLARE_WRAPPER(pthread_mutex_init, int, (pthread_mutex_t *mtx, const pthread_mutexattr_t *attr)); + +extern int ut_fake_pthread_mutexattr_init; DECLARE_WRAPPER(pthread_mutexattr_init, int, (pthread_mutexattr_t *attr)); diff --git a/mk/nvme.unittest.mk b/mk/nvme.unittest.mk index cc36fb10d..0dfc9d8ca 100644 --- a/mk/nvme.unittest.mk +++ b/mk/nvme.unittest.mk @@ -35,13 +35,14 @@ NVME_DIR := $(SPDK_ROOT_DIR)/lib/nvme include $(SPDK_ROOT_DIR)/mk/spdk.common.mk include $(SPDK_ROOT_DIR)/mk/spdk.app.mk +include $(SPDK_ROOT_DIR)/mk/spdk.mock.unittest.mk C_SRCS = $(TEST_FILE) $(OTHER_FILES) CFLAGS += -I$(SPDK_ROOT_DIR)/lib CFLAGS += -I$(SPDK_ROOT_DIR)/test -SPDK_LIB_LIST = util log +SPDK_LIB_LIST = util log spdk_mock LIBS += -lcunit $(SPDK_LIB_LINKER_ARGS) diff --git a/test/lib/nvme/unit/nvme_c/nvme_ut.c b/test/lib/nvme/unit/nvme_c/nvme_ut.c index 1ca808c99..1b4e9ff75 100644 --- a/test/lib/nvme/unit/nvme_c/nvme_ut.c +++ b/test/lib/nvme/unit/nvme_c/nvme_ut.c @@ -39,6 +39,8 @@ #include "lib/test_env.c" +#include "spdk_internal/spdk_mock.h" + int spdk_pci_nvme_enumerate(spdk_pci_enum_cb enum_cb, void *enum_ctx) { @@ -151,6 +153,37 @@ memset_trid(struct spdk_nvme_transport_id *trid1, struct spdk_nvme_transport_id memset(trid2, 0, sizeof(struct spdk_nvme_transport_id)); } +static void +test_nvme_robust_mutex_init_shared(void) +{ + pthread_mutex_t mtx; + int rc = 0; + + ut_fake_pthread_mutexattr_init = 0; + ut_fake_pthread_mutex_init = 0; + rc = nvme_robust_mutex_init_shared(&mtx); + CU_ASSERT(rc == 0); + + ut_fake_pthread_mutexattr_init = -1; + ut_fake_pthread_mutex_init = 0; + rc = nvme_robust_mutex_init_shared(&mtx); + /* for FreeBSD the only possible return value is 0 */ +#ifndef __FreeBSD__ + CU_ASSERT(rc != 0); +#else + CU_ASSERT(rc == 0); +#endif + + ut_fake_pthread_mutexattr_init = 0; + ut_fake_pthread_mutex_init = -1; + rc = nvme_robust_mutex_init_shared(&mtx); +#ifndef __FreeBSD__ + CU_ASSERT(rc != 0); +#else + CU_ASSERT(rc == 0); +#endif +} + static void test_opc_data_transfer(void) { @@ -364,12 +397,16 @@ int main(int argc, char **argv) } if ( - CU_add_test(suite, "test_opc_data_transfer", test_opc_data_transfer) == NULL || + CU_add_test(suite, "test_opc_data_transfer", + test_opc_data_transfer) == NULL || CU_add_test(suite, "test_spdk_nvme_transport_id_parse_trtype", test_spdk_nvme_transport_id_parse_trtype) == NULL || CU_add_test(suite, "test_spdk_nvme_transport_id_parse_adrfam", test_spdk_nvme_transport_id_parse_adrfam) == NULL || - CU_add_test(suite, "test_trid_parse_and_compare", test_trid_parse_and_compare) == NULL + CU_add_test(suite, "test_trid_parse_and_compare", + test_trid_parse_and_compare) == NULL || + CU_add_test(suite, "test_nvme_robust_mutex_init_shared", + test_nvme_robust_mutex_init_shared) == NULL ) { CU_cleanup_registry(); return CU_get_error();