xref: /spdk/test/unit/lib/bdev/part.c/part_ut.c (revision d1165a653907c85812beba21bf9cb6c657cf3bf1)
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 "lib/test_env.c"
37 
38 #include "bdev/bdev.c"
39 #include "bdev/part.c"
40 
41 void
42 spdk_scsi_nvme_translate(const struct spdk_bdev_io *bdev_io,
43 			 int *sc, int *sk, int *asc, int *ascq)
44 {
45 }
46 
47 SPDK_DECLARE_BDEV_MODULE(vbdev_ut);
48 
49 static void
50 vbdev_ut_examine(struct spdk_bdev *bdev)
51 {
52 	spdk_bdev_module_examine_done(SPDK_GET_BDEV_MODULE(vbdev_ut));
53 }
54 
55 SPDK_BDEV_MODULE_REGISTER(bdev_ut, NULL, NULL, NULL, NULL, NULL)
56 SPDK_BDEV_MODULE_REGISTER(vbdev_ut, NULL, NULL, NULL, NULL, vbdev_ut_examine)
57 
58 static int
59 __destruct(void *ctx)
60 {
61 	return 0;
62 }
63 
64 static struct spdk_bdev_fn_table base_fn_table = {
65 	.destruct		= __destruct,
66 };
67 static struct spdk_bdev_fn_table part_fn_table = {
68 	.destruct		= __destruct,
69 };
70 
71 static void
72 __base_free(struct spdk_bdev_part_base *base)
73 {
74 	free(base);
75 }
76 
77 static void
78 part_test(void)
79 {
80 	struct spdk_bdev_part_base	*base;
81 	struct spdk_bdev_part		part1, part2;
82 	struct spdk_bdev		bdev_base = {};
83 	SPDK_BDEV_PART_TAILQ		tailq = TAILQ_HEAD_INITIALIZER(tailq);
84 	int rc;
85 
86 	base = calloc(1, sizeof(*base));
87 	SPDK_CU_ASSERT_FATAL(base != NULL);
88 
89 	bdev_base.name = "base";
90 	bdev_base.fn_table = &base_fn_table;
91 	bdev_base.module = SPDK_GET_BDEV_MODULE(bdev_ut);
92 	rc = spdk_bdev_register(&bdev_base);
93 	CU_ASSERT(rc == 0);
94 	spdk_bdev_part_base_construct(base, &bdev_base, NULL, SPDK_GET_BDEV_MODULE(vbdev_ut),
95 				      &part_fn_table, &tailq, __base_free, 0, NULL, NULL);
96 
97 	spdk_bdev_part_construct(&part1, base, "test1", 0, 100, "test");
98 	spdk_bdev_part_construct(&part2, base, "test2", 100, 100, "test");
99 
100 	spdk_bdev_part_base_hotremove(&bdev_base, &tailq);
101 
102 	/*
103 	 * The base device was removed - ensure that the partition vbdevs were
104 	 *  removed from the base's vbdev list.
105 	 */
106 	CU_ASSERT(TAILQ_EMPTY(&bdev_base.vbdevs));
107 
108 	spdk_bdev_part_base_free(base);
109 	spdk_bdev_unregister(&bdev_base, NULL, NULL);
110 }
111 
112 int
113 main(int argc, char **argv)
114 {
115 	CU_pSuite	suite = NULL;
116 	unsigned int	num_failures;
117 
118 	if (CU_initialize_registry() != CUE_SUCCESS) {
119 		return CU_get_error();
120 	}
121 
122 	suite = CU_add_suite("bdev_part", NULL, NULL);
123 	if (suite == NULL) {
124 		CU_cleanup_registry();
125 		return CU_get_error();
126 	}
127 
128 	if (
129 		CU_add_test(suite, "part", part_test) == NULL
130 	) {
131 		CU_cleanup_registry();
132 		return CU_get_error();
133 	}
134 
135 	CU_basic_set_mode(CU_BRM_VERBOSE);
136 	CU_basic_run_tests();
137 	num_failures = CU_get_number_of_failures();
138 	CU_cleanup_registry();
139 	return num_failures;
140 }
141