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 <paul.e.luse@intel.com>
Reviewed-on: https://review.gerrithub.io/365074
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Paul Luse 2017-06-12 14:31:50 -07:00 committed by Daniel Verkamp
parent 9e90fd6e93
commit be74ecf79a
3 changed files with 44 additions and 3 deletions

View File

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

View File

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

View File

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