Commit be74ecf7 authored by Paul Luse's avatar Paul Luse Committed by Daniel Verkamp
Browse files

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: default avatarPaul Luse <paul.e.luse@intel.com>
Reviewed-on: https://review.gerrithub.io/365074


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent 9e90fd6e
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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));

+2 −1
Original line number Diff line number Diff line
@@ -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)

+39 −2
Original line number Diff line number Diff line
@@ -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();