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

unit test: add new mock macro and use macros in nvme_ut.c



No new coverage added, just used the STUB macros in nvme UT
to make sure they cover all the cases and discovered a few small
tweaks needed: (1) added _V variant to declare void stubs (if
someone sees an easy way to make DECLARE_STUB handle this case
that'd be cleaner but I don't think its a big deal) and (2)
updated DECLARE_STUB so that it can set a struct return value
by adding {} to the ut_ variable set statement.

Also ordered the declarations simply for readability, the _V
first, the regular stubs next, the _P next and then the stubs
that don't have a macro to cover them because they do something
other than return a specific value.

Change-Id: Idd8919d2b9e9daa76dd629364ea1d0285657fa81
Signed-off-by: default avatarPaul Luse <paul.e.luse@intel.com>
Reviewed-on: https://review.gerrithub.io/368420


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
parent 4f7210f6
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -39,14 +39,18 @@
/* used to signify pass through */
#define MOCK_PASS_THRU (0xdeadbeef)

/* helper for initializing struct value with mock macros */
#define MOCK_STRUCT_INIT(...) \
	{ __VA_ARGS__ }

/* for controlling mocked function behavior, setting */
/* and getting values from the stub, the _P macros are */
/* for mocking functions that return pointer values */
#define MOCK_SET(fn, ret, val) \
	ut_ ## fn = (ret){val}
	ut_ ## fn = (ret)val

#define MOCK_SET_P(fn, ret, val) \
	ut_p_ ## fn = (ret){val}
	ut_p_ ## fn = (ret)val

#define MOCK_GET(fn) \
	ut_ ## fn
+40 −87
Original line number Diff line number Diff line
@@ -41,102 +41,64 @@

#include "spdk_internal/mock.h"

int
spdk_pci_nvme_enumerate(spdk_pci_enum_cb enum_cb, void *enum_ctx)
{
	return -1;
}
DEFINE_STUB_V(nvme_ctrlr_destruct, (struct spdk_nvme_ctrlr *ctrlr))

struct spdk_pci_id
spdk_pci_device_get_id(struct spdk_pci_device *pci_dev)
{
	struct spdk_pci_id pci_id;
DEFINE_STUB_V(nvme_ctrlr_fail,
	      (struct spdk_nvme_ctrlr *ctrlr, bool hot_remove))

	memset(&pci_id, 0xFF, sizeof(pci_id));
DEFINE_STUB_V(nvme_ctrlr_proc_get_ref, (struct spdk_nvme_ctrlr *ctrlr))

	return pci_id;
}
DEFINE_STUB_V(nvme_ctrlr_proc_put_ref, (struct spdk_nvme_ctrlr *ctrlr))

bool
spdk_nvme_transport_available(enum spdk_nvme_transport_type trtype)
{
	return true;
}
DEFINE_STUB(spdk_pci_nvme_enumerate, int,
	    (spdk_pci_enum_cb enum_cb, void *enum_ctx), -1)

int
nvme_transport_ctrlr_scan(const struct spdk_nvme_transport_id *trid,
			  void *cb_ctx,
			  spdk_nvme_probe_cb probe_cb,
			  spdk_nvme_remove_cb remove_cb)
{
	return 0;
}
DEFINE_STUB(spdk_pci_device_get_id, struct spdk_pci_id,
	    (struct spdk_pci_device *pci_dev),
	    MOCK_STRUCT_INIT(.vendor_id = 0xffff, .device_id = 0xffff,
			     .subvendor_id = 0xffff, .subdevice_id = 0xffff))

void
nvme_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
{
}
DEFINE_STUB(spdk_nvme_transport_available, bool,
	    (enum spdk_nvme_transport_type trtype), true)

int
nvme_ctrlr_add_process(struct spdk_nvme_ctrlr *ctrlr, void *devhandle)
{
	return 0;
}
DEFINE_STUB(nvme_transport_ctrlr_scan, int,
	    (const struct spdk_nvme_transport_id *trid,
	     void *cb_ctx,
	     spdk_nvme_probe_cb probe_cb,
	     spdk_nvme_remove_cb remove_c), 0)

int
nvme_ctrlr_process_init(struct spdk_nvme_ctrlr *ctrlr)
{
	return 0;
}
DEFINE_STUB(nvme_ctrlr_add_process, int,
	    (struct spdk_nvme_ctrlr *ctrlr, void *devhandle), 0)

int
nvme_ctrlr_start(struct spdk_nvme_ctrlr *ctrlr)
{
	return 0;
}
DEFINE_STUB(nvme_ctrlr_process_init, int,
	    (struct spdk_nvme_ctrlr *ctrlr), 0)

void
nvme_ctrlr_fail(struct spdk_nvme_ctrlr *ctrlr, bool hot_remove)
{
}
DEFINE_STUB(nvme_ctrlr_start, int,
	    (struct spdk_nvme_ctrlr *ctrlr), 0)

void
spdk_nvme_ctrlr_opts_set_defaults(struct spdk_nvme_ctrlr_opts *opts)
{
	memset(opts, 0, sizeof(*opts));
}
DEFINE_STUB(spdk_pci_device_get_addr, struct spdk_pci_addr,
	    (struct spdk_pci_device *pci_dev), {0})

struct spdk_pci_addr
spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev)
{
	struct spdk_pci_addr pci_addr;
DEFINE_STUB(spdk_pci_addr_compare, int,
	    (const struct spdk_pci_addr *a1,
	     const struct spdk_pci_addr *a2), 1)

	memset(&pci_addr, 0, sizeof(pci_addr));
	return pci_addr;
}
DEFINE_STUB(nvme_ctrlr_get_ref_count, int,
	    (struct spdk_nvme_ctrlr *ctrlr), 0)

int
spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2)
{
	return true;
}
DEFINE_STUB(dummy_probe_cb, bool,
	    (void *cb_ctx, const struct spdk_nvme_transport_id *trid,
	     struct spdk_nvme_ctrlr_opts *opts), false)

void
nvme_ctrlr_proc_get_ref(struct spdk_nvme_ctrlr *ctrlr)
{
	return;
}
DEFINE_STUB_P(nvme_transport_ctrlr_construct, struct spdk_nvme_ctrlr,
	      (const struct spdk_nvme_transport_id *trid,
	       const struct spdk_nvme_ctrlr_opts *opts,
	       void *devhandle), {0})

void
nvme_ctrlr_proc_put_ref(struct spdk_nvme_ctrlr *ctrlr)
{
	return;
}

int
nvme_ctrlr_get_ref_count(struct spdk_nvme_ctrlr *ctrlr)
spdk_nvme_ctrlr_opts_set_defaults(struct spdk_nvme_ctrlr_opts *opts)
{
	return 0;
	memset(opts, 0, sizeof(*opts));
}

static void
@@ -146,15 +108,6 @@ memset_trid(struct spdk_nvme_transport_id *trid1, struct spdk_nvme_transport_id
	memset(trid2, 0, sizeof(struct spdk_nvme_transport_id));
}

DEFINE_STUB_P(nvme_transport_ctrlr_construct, struct spdk_nvme_ctrlr,
	      (const struct spdk_nvme_transport_id *trid,
	       const struct spdk_nvme_ctrlr_opts *opts,
	       void *devhandle), {0})

DEFINE_STUB(dummy_probe_cb, bool,
	    (void *cb_ctx, const struct spdk_nvme_transport_id *trid,
	     struct spdk_nvme_ctrlr_opts *opts), false)

static void
test_nvme_ctrlr_probe(void)
{