4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "spdk_cunit.h"
36 #include "common/lib/test_env.c"
37 #include "unit/lib/json_mock.c"
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
43 #include "bdev/bdev.c"
44 #include "bdev/part.c"
46 DEFINE_STUB(spdk_conf_find_section
, struct spdk_conf_section
*, (struct spdk_conf
*cp
,
47 const char *name
), NULL
);
48 DEFINE_STUB(spdk_conf_section_get_nmval
, char *,
49 (struct spdk_conf_section
*sp
, const char *key
, int idx1
, int idx2
), NULL
);
50 DEFINE_STUB(spdk_conf_section_get_intval
, int, (struct spdk_conf_section
*sp
, const char *key
), -1);
52 struct spdk_trace_histories
*g_trace_histories
;
53 DEFINE_STUB_V(spdk_trace_add_register_fn
, (struct spdk_trace_register_fn
*reg_fn
));
54 DEFINE_STUB_V(spdk_trace_register_owner
, (uint8_t type
, char id_prefix
));
55 DEFINE_STUB_V(spdk_trace_register_object
, (uint8_t type
, char id_prefix
));
56 DEFINE_STUB_V(spdk_trace_register_description
, (const char *name
, const char *short_name
,
57 uint16_t tpoint_id
, uint8_t owner_type
,
58 uint8_t object_type
, uint8_t new_object
,
59 uint8_t arg1_is_ptr
, const char *arg1_name
));
60 DEFINE_STUB_V(_spdk_trace_record
, (uint64_t tsc
, uint16_t tpoint_id
, uint16_t poller_id
,
61 uint32_t size
, uint64_t object_id
, uint64_t arg1
));
64 _part_send_msg(spdk_thread_fn fn
, void *ctx
, void *thread_ctx
)
70 _part_cleanup(struct spdk_bdev_part
*part
)
72 free(part
->internal
.bdev
.name
);
73 free(part
->internal
.bdev
.product_name
);
77 spdk_scsi_nvme_translate(const struct spdk_bdev_io
*bdev_io
,
78 int *sc
, int *sk
, int *asc
, int *ascq
)
82 struct spdk_bdev_module bdev_ut_if
= {
86 static void vbdev_ut_examine(struct spdk_bdev
*bdev
);
88 struct spdk_bdev_module vbdev_ut_if
= {
90 .examine_config
= vbdev_ut_examine
,
93 SPDK_BDEV_MODULE_REGISTER(&bdev_ut_if
)
94 SPDK_BDEV_MODULE_REGISTER(&vbdev_ut_if
)
97 vbdev_ut_examine(struct spdk_bdev
*bdev
)
99 spdk_bdev_module_examine_done(&vbdev_ut_if
);
103 __destruct(void *ctx
)
108 static struct spdk_bdev_fn_table base_fn_table
= {
109 .destruct
= __destruct
,
111 static struct spdk_bdev_fn_table part_fn_table
= {
112 .destruct
= __destruct
,
118 struct spdk_bdev_part_base
*base
;
119 struct spdk_bdev_part part1
= {};
120 struct spdk_bdev_part part2
= {};
121 struct spdk_bdev bdev_base
= {};
122 SPDK_BDEV_PART_TAILQ tailq
= TAILQ_HEAD_INITIALIZER(tailq
);
125 bdev_base
.name
= "base";
126 bdev_base
.fn_table
= &base_fn_table
;
127 bdev_base
.module
= &bdev_ut_if
;
128 rc
= spdk_bdev_register(&bdev_base
);
130 base
= spdk_bdev_part_base_construct(&bdev_base
, NULL
, &vbdev_ut_if
,
131 &part_fn_table
, &tailq
, NULL
,
132 NULL
, 0, NULL
, NULL
);
134 SPDK_CU_ASSERT_FATAL(base
!= NULL
);
136 rc
= spdk_bdev_part_construct(&part1
, base
, "test1", 0, 100, "test");
137 SPDK_CU_ASSERT_FATAL(rc
== 0);
138 rc
= spdk_bdev_part_construct(&part2
, base
, "test2", 100, 100, "test");
139 SPDK_CU_ASSERT_FATAL(rc
== 0);
141 spdk_bdev_part_base_hotremove(&bdev_base
, &tailq
);
143 spdk_bdev_part_base_free(base
);
144 _part_cleanup(&part1
);
145 _part_cleanup(&part2
);
146 spdk_bdev_unregister(&bdev_base
, NULL
, NULL
);
150 main(int argc
, char **argv
)
152 CU_pSuite suite
= NULL
;
153 unsigned int num_failures
;
155 if (CU_initialize_registry() != CUE_SUCCESS
) {
156 return CU_get_error();
159 suite
= CU_add_suite("bdev_part", NULL
, NULL
);
161 CU_cleanup_registry();
162 return CU_get_error();
166 CU_add_test(suite
, "part", part_test
) == NULL
168 CU_cleanup_registry();
169 return CU_get_error();
172 spdk_allocate_thread(_part_send_msg
, NULL
, NULL
, NULL
, "thread0");
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();