xref: /spdk/test/unit/lib/bdev/part.c/part_ut.c (revision 13a58c41ad5c01eefaf68f049b6c7da2b9d19426)
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 "spdk_internal/thread.h"
44 
45 #include "bdev/bdev.c"
46 #include "bdev/part.c"
47 
48 DEFINE_STUB(spdk_conf_find_section, struct spdk_conf_section *, (struct spdk_conf *cp,
49 		const char *name), NULL);
50 DEFINE_STUB(spdk_conf_section_get_nmval, char *,
51 	    (struct spdk_conf_section *sp, const char *key, int idx1, int idx2), NULL);
52 DEFINE_STUB(spdk_conf_section_get_intval, int, (struct spdk_conf_section *sp, const char *key), -1);
53 
54 struct spdk_trace_histories *g_trace_histories;
55 DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn));
56 DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix));
57 DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix));
58 DEFINE_STUB_V(spdk_trace_register_description, (const char *name, const char *short_name,
59 		uint16_t tpoint_id, uint8_t owner_type,
60 		uint8_t object_type, uint8_t new_object,
61 		uint8_t arg1_is_ptr, const char *arg1_name));
62 DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
63 				   uint32_t size, uint64_t object_id, uint64_t arg1));
64 
65 static void
66 _part_cleanup(struct spdk_bdev_part *part)
67 {
68 	free(part->internal.bdev.name);
69 	free(part->internal.bdev.product_name);
70 }
71 
72 void
73 spdk_scsi_nvme_translate(const struct spdk_bdev_io *bdev_io,
74 			 int *sc, int *sk, int *asc, int *ascq)
75 {
76 }
77 
78 struct spdk_bdev_module bdev_ut_if = {
79 	.name = "bdev_ut",
80 };
81 
82 static void vbdev_ut_examine(struct spdk_bdev *bdev);
83 
84 struct spdk_bdev_module vbdev_ut_if = {
85 	.name = "vbdev_ut",
86 	.examine_config = vbdev_ut_examine,
87 };
88 
89 SPDK_BDEV_MODULE_REGISTER(&bdev_ut_if)
90 SPDK_BDEV_MODULE_REGISTER(&vbdev_ut_if)
91 
92 static void
93 vbdev_ut_examine(struct spdk_bdev *bdev)
94 {
95 	spdk_bdev_module_examine_done(&vbdev_ut_if);
96 }
97 
98 static int
99 __destruct(void *ctx)
100 {
101 	return 0;
102 }
103 
104 static struct spdk_bdev_fn_table base_fn_table = {
105 	.destruct		= __destruct,
106 };
107 static struct spdk_bdev_fn_table part_fn_table = {
108 	.destruct		= __destruct,
109 };
110 
111 static void
112 part_test(void)
113 {
114 	struct spdk_bdev_part_base	*base;
115 	struct spdk_bdev_part		part1 = {};
116 	struct spdk_bdev_part		part2 = {};
117 	struct spdk_bdev		bdev_base = {};
118 	SPDK_BDEV_PART_TAILQ		tailq = TAILQ_HEAD_INITIALIZER(tailq);
119 	int rc;
120 
121 	bdev_base.name = "base";
122 	bdev_base.fn_table = &base_fn_table;
123 	bdev_base.module = &bdev_ut_if;
124 	rc = spdk_bdev_register(&bdev_base);
125 	CU_ASSERT(rc == 0);
126 	base = spdk_bdev_part_base_construct(&bdev_base, NULL, &vbdev_ut_if,
127 					     &part_fn_table, &tailq, NULL,
128 					     NULL, 0, NULL, NULL);
129 
130 	SPDK_CU_ASSERT_FATAL(base != NULL);
131 
132 	rc = spdk_bdev_part_construct(&part1, base, "test1", 0, 100, "test");
133 	SPDK_CU_ASSERT_FATAL(rc == 0);
134 	rc = spdk_bdev_part_construct(&part2, base, "test2", 100, 100, "test");
135 	SPDK_CU_ASSERT_FATAL(rc == 0);
136 
137 	spdk_bdev_part_base_hotremove(base, &tailq);
138 
139 	spdk_bdev_part_base_free(base);
140 	_part_cleanup(&part1);
141 	_part_cleanup(&part2);
142 	spdk_bdev_unregister(&bdev_base, NULL, NULL);
143 
144 	poll_threads();
145 }
146 
147 int
148 main(int argc, char **argv)
149 {
150 	CU_pSuite		suite = NULL;
151 	unsigned int		num_failures;
152 
153 	if (CU_initialize_registry() != CUE_SUCCESS) {
154 		return CU_get_error();
155 	}
156 
157 	suite = CU_add_suite("bdev_part", NULL, NULL);
158 	if (suite == NULL) {
159 		CU_cleanup_registry();
160 		return CU_get_error();
161 	}
162 
163 	if (
164 		CU_add_test(suite, "part", part_test) == NULL
165 	) {
166 		CU_cleanup_registry();
167 		return CU_get_error();
168 	}
169 
170 	allocate_threads(1);
171 	set_thread(0);
172 
173 	CU_basic_set_mode(CU_BRM_VERBOSE);
174 	CU_basic_run_tests();
175 	num_failures = CU_get_number_of_failures();
176 	CU_cleanup_registry();
177 
178 	free_threads();
179 
180 	return num_failures;
181 }
182