]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/event/subsystem/subsystem_ut.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / spdk / test / lib / event / subsystem / subsystem_ut.c
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 <stdint.h>
35 #include <stdlib.h>
36
37 #include <CUnit/Basic.h>
38
39 #include "subsystem.c"
40
41 static struct spdk_subsystem g_ut_subsystems[8];
42 static struct spdk_subsystem_depend g_ut_subsystem_deps[8];
43
44 static void
45 set_up_subsystem(struct spdk_subsystem *subsystem, const char *name)
46 {
47 subsystem->init = NULL;
48 subsystem->fini = NULL;
49 subsystem->config = NULL;
50 subsystem->name = name;
51 }
52
53 static void
54 set_up_depends(struct spdk_subsystem_depend *depend, const char *subsystem_name,
55 const char *dpends_on_name)
56 {
57 depend->name = subsystem_name;
58 depend->depends_on = dpends_on_name;
59 }
60
61 static void
62 subsystem_clear(void)
63 {
64 struct spdk_subsystem *subsystem, *subsystem_tmp;
65 struct spdk_subsystem_depend *subsystem_dep, *subsystem_dep_tmp;
66
67 TAILQ_FOREACH_SAFE(subsystem, &g_subsystems, tailq, subsystem_tmp) {
68 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
69 }
70
71 TAILQ_FOREACH_SAFE(subsystem_dep, &g_depends, tailq, subsystem_dep_tmp) {
72 TAILQ_REMOVE(&g_depends, subsystem_dep, tailq);
73 }
74 }
75
76 static void
77 subsystem_sort_test_depends_on_single(void)
78 {
79 struct spdk_subsystem *subsystem;
80 int i;
81 char subsystem_name[16];
82
83 spdk_subsystem_init();
84
85 i = 4;
86 TAILQ_FOREACH(subsystem, &g_subsystems, tailq) {
87 snprintf(subsystem_name, sizeof(subsystem_name), "subsystem%d", i);
88 i--;
89 CU_ASSERT(strcmp(subsystem_name, subsystem->name) == 0);
90 }
91 }
92
93 static void
94 subsystem_sort_test_depends_on_multiple(void)
95 {
96 int i;
97 struct spdk_subsystem *subsystem;
98
99 subsystem_clear();
100 set_up_subsystem(&g_ut_subsystems[0], "iscsi");
101 set_up_subsystem(&g_ut_subsystems[1], "nvmf");
102 set_up_subsystem(&g_ut_subsystems[2], "sock");
103 set_up_subsystem(&g_ut_subsystems[3], "bdev");
104 set_up_subsystem(&g_ut_subsystems[4], "rpc");
105 set_up_subsystem(&g_ut_subsystems[5], "scsi");
106 set_up_subsystem(&g_ut_subsystems[6], "interface");
107 set_up_subsystem(&g_ut_subsystems[7], "copy");
108
109 for (i = 0; i < 8; i++) {
110 spdk_add_subsystem(&g_ut_subsystems[i]);
111 }
112
113 set_up_depends(&g_ut_subsystem_deps[0], "bdev", "copy");
114 set_up_depends(&g_ut_subsystem_deps[1], "scsi", "bdev");
115 set_up_depends(&g_ut_subsystem_deps[2], "rpc", "interface");
116 set_up_depends(&g_ut_subsystem_deps[3], "sock", "interface");
117 set_up_depends(&g_ut_subsystem_deps[4], "nvmf", "interface");
118 set_up_depends(&g_ut_subsystem_deps[5], "iscsi", "scsi");
119 set_up_depends(&g_ut_subsystem_deps[6], "iscsi", "sock");
120 set_up_depends(&g_ut_subsystem_deps[7], "iscsi", "rpc");
121
122 for (i = 0; i < 8; i++) {
123 spdk_add_subsystem_depend(&g_ut_subsystem_deps[i]);
124 }
125
126 spdk_subsystem_init();
127
128 subsystem = TAILQ_FIRST(&g_subsystems);
129 CU_ASSERT(strcmp(subsystem->name, "interface") == 0);
130 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
131
132 subsystem = TAILQ_FIRST(&g_subsystems);
133 CU_ASSERT(strcmp(subsystem->name, "copy") == 0);
134 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
135
136 subsystem = TAILQ_FIRST(&g_subsystems);
137 CU_ASSERT(strcmp(subsystem->name, "nvmf") == 0);
138 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
139
140 subsystem = TAILQ_FIRST(&g_subsystems);
141 CU_ASSERT(strcmp(subsystem->name, "sock") == 0);
142 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
143
144 subsystem = TAILQ_FIRST(&g_subsystems);
145 CU_ASSERT(strcmp(subsystem->name, "bdev") == 0);
146 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
147
148 subsystem = TAILQ_FIRST(&g_subsystems);
149 CU_ASSERT(strcmp(subsystem->name, "rpc") == 0);
150 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
151
152 subsystem = TAILQ_FIRST(&g_subsystems);
153 CU_ASSERT(strcmp(subsystem->name, "scsi") == 0);
154 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
155
156 subsystem = TAILQ_FIRST(&g_subsystems);
157 CU_ASSERT(strcmp(subsystem->name, "iscsi") == 0);
158 TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
159 }
160
161 SPDK_SUBSYSTEM_REGISTER(subsystem1, NULL, NULL, NULL)
162 SPDK_SUBSYSTEM_REGISTER(subsystem2, NULL, NULL, NULL)
163 SPDK_SUBSYSTEM_REGISTER(subsystem3, NULL, NULL, NULL)
164 SPDK_SUBSYSTEM_REGISTER(subsystem4, NULL, NULL, NULL)
165 SPDK_SUBSYSTEM_DEPEND(subsystem1, subsystem2)
166 SPDK_SUBSYSTEM_DEPEND(subsystem2, subsystem3)
167 SPDK_SUBSYSTEM_DEPEND(subsystem3, subsystem4)
168
169
170 static void
171 subsystem_sort_test_missing_dependency(void)
172 {
173 /*
174 * A depends on B, but B is missing
175 */
176
177 subsystem_clear();
178 set_up_subsystem(&g_ut_subsystems[0], "A");
179 spdk_add_subsystem(&g_ut_subsystems[0]);
180
181 set_up_depends(&g_ut_subsystem_deps[0], "A", "B");
182 spdk_add_subsystem_depend(&g_ut_subsystem_deps[0]);
183
184 CU_ASSERT(spdk_subsystem_init() != 0);
185
186 /*
187 * Dependency from C to A is defined, but C is missing
188 */
189
190 subsystem_clear();
191 set_up_subsystem(&g_ut_subsystems[0], "A");
192 spdk_add_subsystem(&g_ut_subsystems[0]);
193
194 set_up_depends(&g_ut_subsystem_deps[0], "C", "A");
195 spdk_add_subsystem_depend(&g_ut_subsystem_deps[0]);
196
197 CU_ASSERT(spdk_subsystem_init() != 0);
198
199 }
200
201 int
202 main(int argc, char **argv)
203 {
204 CU_pSuite suite = NULL;
205 unsigned int num_failures;
206
207 if (CU_initialize_registry() != CUE_SUCCESS) {
208 return CU_get_error();
209 }
210
211 suite = CU_add_suite("subsystem_suite", NULL, NULL);
212 if (suite == NULL) {
213 CU_cleanup_registry();
214 return CU_get_error();
215 }
216
217 if (
218 CU_add_test(suite, "subsystem_sort_test_depends_on_single",
219 subsystem_sort_test_depends_on_single) == NULL
220 || CU_add_test(suite, "subsystem_sort_test_depends_on_multiple",
221 subsystem_sort_test_depends_on_multiple) == NULL
222 || CU_add_test(suite, "subsystem_sort_test_missing_dependency",
223 subsystem_sort_test_missing_dependency) == NULL
224 ) {
225 CU_cleanup_registry();
226 return CU_get_error();
227 }
228
229 CU_basic_set_mode(CU_BRM_VERBOSE);
230 CU_basic_run_tests();
231 num_failures = CU_get_number_of_failures();
232 CU_cleanup_registry();
233
234 return num_failures;
235 }