diff --git a/test/unit/lib/bdev/part.c/part_ut.c b/test/unit/lib/bdev/part.c/part_ut.c index f20771858..c66ffac96 100644 --- a/test/unit/lib/bdev/part.c/part_ut.c +++ b/test/unit/lib/bdev/part.c/part_ut.c @@ -15,6 +15,18 @@ #include "bdev/bdev.c" #include "bdev/part.c" +struct ut_expected_io { +}; + +struct bdev_ut_channel { + TAILQ_HEAD(, spdk_bdev_io) outstanding_io; + uint32_t outstanding_io_count; + TAILQ_HEAD(, ut_expected_io) expected_io; +}; + +static uint32_t g_part_ut_io_device; +static struct bdev_ut_channel *g_bdev_ut_channel; + DEFINE_STUB(spdk_notify_send, uint64_t, (const char *type, const char *ctx), 0); DEFINE_STUB(spdk_notify_type_register, struct spdk_notify_type *, (const char *type), NULL); DEFINE_STUB(spdk_memory_domain_get_dma_device_id, const char *, (struct spdk_memory_domain *domain), @@ -54,20 +66,82 @@ _part_cleanup(struct spdk_bdev_part *part) free(part->internal.bdev.product_name); } +static struct spdk_io_channel * +part_ut_get_io_channel(void *ctx) +{ + return spdk_get_io_channel(&g_part_ut_io_device); +} + void spdk_scsi_nvme_translate(const struct spdk_bdev_io *bdev_io, int *sc, int *sk, int *asc, int *ascq) { } +static int +bdev_ut_create_ch(void *io_device, void *ctx_buf) +{ + struct bdev_ut_channel *ch = ctx_buf; + + CU_ASSERT(g_bdev_ut_channel == NULL); + g_bdev_ut_channel = ch; + g_part_ut_io_device++; + + TAILQ_INIT(&ch->outstanding_io); + ch->outstanding_io_count = 0; + TAILQ_INIT(&ch->expected_io); + return 0; +} + +static void +bdev_ut_destroy_ch(void *io_device, void *ctx_buf) +{ + CU_ASSERT(g_bdev_ut_channel != NULL); + g_bdev_ut_channel = NULL; + g_part_ut_io_device--; +} + +struct spdk_bdev_module bdev_ut_if; + +static int +bdev_ut_module_init(void) +{ + spdk_io_device_register(&g_part_ut_io_device, bdev_ut_create_ch, bdev_ut_destroy_ch, + sizeof(struct bdev_ut_channel), NULL); + spdk_bdev_module_init_done(&bdev_ut_if); + return 0; +} + +static void +bdev_ut_module_fini(void) +{ + spdk_io_device_unregister(&g_part_ut_io_device, NULL); +} + struct spdk_bdev_module bdev_ut_if = { .name = "bdev_ut", + .module_init = bdev_ut_module_init, + .module_fini = bdev_ut_module_fini, + .async_init = true, }; static void vbdev_ut_examine(struct spdk_bdev *bdev); +static int +vbdev_ut_module_init(void) +{ + return 0; +} + +static void +vbdev_ut_module_fini(void) +{ +} + struct spdk_bdev_module vbdev_ut_if = { .name = "vbdev_ut", + .module_init = vbdev_ut_module_init, + .module_fini = vbdev_ut_module_fini, .examine_config = vbdev_ut_examine, }; @@ -88,11 +162,28 @@ __destruct(void *ctx) static struct spdk_bdev_fn_table base_fn_table = { .destruct = __destruct, + .get_io_channel = part_ut_get_io_channel, }; static struct spdk_bdev_fn_table part_fn_table = { .destruct = __destruct, }; +static void +bdev_init_cb(void *arg, int rc) +{ + CU_ASSERT(rc == 0); +} + +static void +bdev_fini_cb(void *arg) +{ +} + +static void +bdev_ut_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) +{ +} + static void part_test(void) { @@ -172,6 +263,65 @@ part_free_test(void) poll_threads(); } +static void +part_get_io_channel_test(void) +{ + struct spdk_bdev_part_base *base = NULL; + struct spdk_bdev_desc *desc = NULL; + struct spdk_io_channel *io_ch; + struct spdk_bdev_part *part; + struct spdk_bdev bdev_base = {}; + SPDK_BDEV_PART_TAILQ tailq = TAILQ_HEAD_INITIALIZER(tailq); + int rc; + + spdk_bdev_initialize(bdev_init_cb, NULL); + + bdev_base.name = "base"; + bdev_base.blocklen = 512; + bdev_base.blockcnt = 1024; + bdev_base.fn_table = &base_fn_table; + bdev_base.module = &bdev_ut_if; + rc = spdk_bdev_register(&bdev_base); + CU_ASSERT(rc == 0); + + rc = spdk_bdev_part_base_construct_ext("base", NULL, &vbdev_ut_if, + &part_fn_table, &tailq, NULL, + NULL, 100, NULL, NULL, &base); + CU_ASSERT(rc == 0); + CU_ASSERT(TAILQ_EMPTY(&tailq)); + SPDK_CU_ASSERT_FATAL(base != NULL); + + part = calloc(1, sizeof(*part)); + SPDK_CU_ASSERT_FATAL(part != NULL); + rc = spdk_bdev_part_construct(part, base, "test", 0, 100, "test"); + SPDK_CU_ASSERT_FATAL(rc == 0); + CU_ASSERT(!TAILQ_EMPTY(&tailq)); + + rc = spdk_bdev_open_ext("test", true, bdev_ut_event_cb, NULL, &desc); + CU_ASSERT(rc == 0); + SPDK_CU_ASSERT_FATAL(desc != NULL); + CU_ASSERT(&part->internal.bdev == spdk_bdev_desc_get_bdev(desc)); + + io_ch = spdk_bdev_get_io_channel(desc); + CU_ASSERT(io_ch != NULL); + CU_ASSERT(g_part_ut_io_device == 1); + + spdk_put_io_channel(io_ch); + spdk_bdev_close(desc); + spdk_bdev_unregister(&part->internal.bdev, NULL, NULL); + poll_threads(); + CU_ASSERT(g_part_ut_io_device == 0); + + rc = spdk_bdev_part_free(part); + CU_ASSERT(rc == 1); + poll_threads(); + CU_ASSERT(TAILQ_EMPTY(&tailq)); + + spdk_bdev_unregister(&bdev_base, NULL, NULL); + spdk_bdev_finish(bdev_fini_cb, NULL); + poll_threads(); +} + int main(int argc, char **argv) { @@ -185,7 +335,9 @@ main(int argc, char **argv) CU_ADD_TEST(suite, part_test); CU_ADD_TEST(suite, part_free_test); + CU_ADD_TEST(suite, part_get_io_channel_test); + allocate_cores(1); allocate_threads(1); set_thread(0); @@ -195,6 +347,7 @@ main(int argc, char **argv) CU_cleanup_registry(); free_threads(); + free_cores(); return num_failures; }