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