xref: /spdk/test/unit/lib/bdev/part.c/part_ut.c (revision 5fd9561f54daa8eff7f3bcb56c789655bca846b1)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk_cunit.h"
35 
36 #include "common/lib/ut_multithread.c"
37 #include "unit/lib/json_mock.c"
38 
39 #include "spdk/config.h"
40 /* HACK: disable VTune integration so the unit test doesn't need VTune headers and libs to build */
41 #undef SPDK_CONFIG_VTUNE
42 
43 #include "bdev/bdev.c"
44 #include "bdev/part.c"
45 
46 DEFINE_STUB(spdk_notify_send, uint64_t, (const char *type, const char *ctx), 0);
47 DEFINE_STUB(spdk_notify_type_register, struct spdk_notify_type *, (const char *type), NULL);
48 DEFINE_STUB(spdk_memory_domain_get_dma_device_id, const char *, (struct spdk_memory_domain *domain),
49 	    "test_domain");
50 DEFINE_STUB(spdk_memory_domain_get_dma_device_type, enum spdk_dma_device_type,
51 	    (struct spdk_memory_domain *domain), 0);
52 
53 DEFINE_RETURN_MOCK(spdk_memory_domain_pull_data, int);
54 int
55 spdk_memory_domain_pull_data(struct spdk_memory_domain *src_domain, void *src_domain_ctx,
56 			     struct iovec *src_iov, uint32_t src_iov_cnt, struct iovec *dst_iov, uint32_t dst_iov_cnt,
57 			     spdk_memory_domain_data_cpl_cb cpl_cb, void *cpl_cb_arg)
58 {
59 	HANDLE_RETURN_MOCK(spdk_memory_domain_pull_data);
60 
61 	cpl_cb(cpl_cb_arg, 0);
62 	return 0;
63 }
64 
65 DEFINE_RETURN_MOCK(spdk_memory_domain_push_data, int);
66 int
67 spdk_memory_domain_push_data(struct spdk_memory_domain *dst_domain, void *dst_domain_ctx,
68 			     struct iovec *dst_iov, uint32_t dst_iovcnt, struct iovec *src_iov, uint32_t src_iovcnt,
69 			     spdk_memory_domain_data_cpl_cb cpl_cb, void *cpl_cb_arg)
70 {
71 	HANDLE_RETURN_MOCK(spdk_memory_domain_push_data);
72 
73 	cpl_cb(cpl_cb_arg, 0);
74 	return 0;
75 }
76 
77 static void
78 _part_cleanup(struct spdk_bdev_part *part)
79 {
80 	free(part->internal.bdev.name);
81 	free(part->internal.bdev.product_name);
82 }
83 
84 void
85 spdk_scsi_nvme_translate(const struct spdk_bdev_io *bdev_io,
86 			 int *sc, int *sk, int *asc, int *ascq)
87 {
88 }
89 
90 struct spdk_bdev_module bdev_ut_if = {
91 	.name = "bdev_ut",
92 };
93 
94 static void vbdev_ut_examine(struct spdk_bdev *bdev);
95 
96 struct spdk_bdev_module vbdev_ut_if = {
97 	.name = "vbdev_ut",
98 	.examine_config = vbdev_ut_examine,
99 };
100 
101 SPDK_BDEV_MODULE_REGISTER(bdev_ut, &bdev_ut_if)
102 SPDK_BDEV_MODULE_REGISTER(vbdev_ut, &vbdev_ut_if)
103 
104 static void
105 vbdev_ut_examine(struct spdk_bdev *bdev)
106 {
107 	spdk_bdev_module_examine_done(&vbdev_ut_if);
108 }
109 
110 static int
111 __destruct(void *ctx)
112 {
113 	return 0;
114 }
115 
116 static struct spdk_bdev_fn_table base_fn_table = {
117 	.destruct		= __destruct,
118 };
119 static struct spdk_bdev_fn_table part_fn_table = {
120 	.destruct		= __destruct,
121 };
122 
123 static void
124 part_test(void)
125 {
126 	struct spdk_bdev_part_base	*base;
127 	struct spdk_bdev_part		part1 = {};
128 	struct spdk_bdev_part		part2 = {};
129 	struct spdk_bdev		bdev_base = {};
130 	SPDK_BDEV_PART_TAILQ		tailq = TAILQ_HEAD_INITIALIZER(tailq);
131 	int rc;
132 
133 	bdev_base.name = "base";
134 	bdev_base.fn_table = &base_fn_table;
135 	bdev_base.module = &bdev_ut_if;
136 	rc = spdk_bdev_register(&bdev_base);
137 	CU_ASSERT(rc == 0);
138 	rc = spdk_bdev_part_base_construct_ext("base", NULL, &vbdev_ut_if,
139 					       &part_fn_table, &tailq, NULL,
140 					       NULL, 0, NULL, NULL, &base);
141 
142 	CU_ASSERT(rc == 0);
143 	SPDK_CU_ASSERT_FATAL(base != NULL);
144 
145 	rc = spdk_bdev_part_construct(&part1, base, "test1", 0, 100, "test");
146 	SPDK_CU_ASSERT_FATAL(rc == 0);
147 	rc = spdk_bdev_part_construct(&part2, base, "test2", 100, 100, "test");
148 	SPDK_CU_ASSERT_FATAL(rc == 0);
149 
150 	spdk_bdev_part_base_hotremove(base, &tailq);
151 
152 	spdk_bdev_part_base_free(base);
153 	_part_cleanup(&part1);
154 	_part_cleanup(&part2);
155 	spdk_bdev_unregister(&bdev_base, NULL, NULL);
156 
157 	poll_threads();
158 }
159 
160 int
161 main(int argc, char **argv)
162 {
163 	CU_pSuite		suite = NULL;
164 	unsigned int		num_failures;
165 
166 	CU_set_error_action(CUEA_ABORT);
167 	CU_initialize_registry();
168 
169 	suite = CU_add_suite("bdev_part", NULL, NULL);
170 
171 	CU_ADD_TEST(suite, part_test);
172 
173 	allocate_threads(1);
174 	set_thread(0);
175 
176 	CU_basic_set_mode(CU_BRM_VERBOSE);
177 	CU_basic_run_tests();
178 	num_failures = CU_get_number_of_failures();
179 	CU_cleanup_registry();
180 
181 	free_threads();
182 
183 	return num_failures;
184 }
185