xref: /spdk/test/unit/lib/blobfs/blobfs_bdev.c/blobfs_bdev_ut.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
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 #include "spdk/string.h"
36 #include "spdk/stdinc.h"
37 
38 #include "blobfs/bdev/blobfs_bdev.c"
39 
40 int g_fserrno;
41 
42 bool g_bdev_open_ext_fail = false;
43 bool g_bdev_create_bs_dev_from_desc_fail = false;
44 bool g_fs_load_fail = false;
45 bool g_fs_unload_fail = false;
46 bool g_bs_bdev_claim_fail = false;
47 bool g_blobfs_fuse_start_fail = false;
48 
49 const char *g_bdev_name = "ut_bdev";
50 
51 int
52 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
53 		   void *event_ctx, struct spdk_bdev_desc **_desc)
54 {
55 	if (g_bdev_open_ext_fail) {
56 		return -1;
57 	}
58 
59 	return 0;
60 }
61 
62 static  void
63 bs_dev_destroy(struct spdk_bs_dev *dev)
64 {
65 }
66 
67 struct spdk_bs_dev *
68 spdk_bdev_create_bs_dev_from_desc(struct spdk_bdev_desc *desc)
69 {
70 	static struct spdk_bs_dev bs_dev;
71 
72 	if (g_bdev_create_bs_dev_from_desc_fail) {
73 		return NULL;
74 	}
75 
76 	bs_dev.destroy = bs_dev_destroy;
77 	return &bs_dev;
78 }
79 
80 void
81 spdk_fs_load(struct spdk_bs_dev *dev, fs_send_request_fn send_request_fn,
82 	     spdk_fs_op_with_handle_complete cb_fn, void *cb_arg)
83 {
84 	int rc = 0;
85 
86 	if (g_fs_load_fail) {
87 		rc = -1;
88 	}
89 
90 	cb_fn(cb_arg, NULL, rc);
91 	return;
92 }
93 
94 void
95 spdk_fs_unload(struct spdk_filesystem *fs, spdk_fs_op_complete cb_fn, void *cb_arg)
96 {
97 	int rc = 0;
98 
99 	if (g_fs_unload_fail) {
100 		rc = -1;
101 	}
102 
103 	cb_fn(cb_arg, rc);
104 	return;
105 }
106 
107 void
108 spdk_fs_init(struct spdk_bs_dev *dev, struct spdk_blobfs_opts *opt,
109 	     fs_send_request_fn send_request_fn,
110 	     spdk_fs_op_with_handle_complete cb_fn, void *cb_arg)
111 {
112 	int rc = 0;
113 
114 	if (g_fs_load_fail) {
115 		rc = -1;
116 	}
117 
118 	cb_fn(cb_arg, NULL, rc);
119 	return;
120 }
121 
122 int
123 spdk_bs_bdev_claim(struct spdk_bs_dev *bs_dev, struct spdk_bdev_module *module)
124 {
125 	if (g_bs_bdev_claim_fail == true) {
126 		return -1;
127 	}
128 
129 	return 0;
130 }
131 
132 int
133 spdk_blobfs_fuse_start(const char *bdev_name, const char *mountpoint, struct spdk_filesystem *fs,
134 		       blobfs_fuse_unmount_cb cb_fn, void *cb_arg, struct spdk_blobfs_fuse **_bfuse)
135 {
136 	if (g_blobfs_fuse_start_fail == true) {
137 		return -1;
138 	}
139 
140 	return 0;
141 }
142 
143 void
144 spdk_bdev_close(struct spdk_bdev_desc *desc)
145 {
146 }
147 
148 void
149 spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx)
150 {
151 	fn(ctx);
152 }
153 
154 struct spdk_thread *
155 spdk_get_thread(void)
156 {
157 	struct spdk_thread *thd = (struct spdk_thread *)0x1;
158 
159 	return thd;
160 }
161 
162 const char *
163 spdk_bdev_get_name(const struct spdk_bdev *bdev)
164 {
165 	return g_bdev_name;
166 }
167 
168 void
169 spdk_fs_opts_init(struct spdk_blobfs_opts *opts)
170 {
171 }
172 
173 void
174 spdk_blobfs_fuse_send_request(fs_request_fn fn, void *arg)
175 {
176 }
177 
178 void
179 spdk_blobfs_fuse_stop(struct spdk_blobfs_fuse *bfuse)
180 {
181 }
182 
183 static void
184 blobfs_bdev_op_complete(void *cb_arg, int fserrno)
185 {
186 	g_fserrno = fserrno;
187 }
188 
189 static void
190 spdk_blobfs_bdev_detect_test(void)
191 {
192 	/* spdk_bdev_open_ext() fails */
193 	g_bdev_open_ext_fail = true;
194 	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
195 	CU_ASSERT(g_fserrno != 0);
196 
197 	g_bdev_open_ext_fail = false;
198 
199 	/* spdk_bdev_create_bs_dev_from_desc() fails */
200 	g_bdev_create_bs_dev_from_desc_fail = true;
201 	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
202 	CU_ASSERT(g_fserrno != 0);
203 
204 	g_bdev_create_bs_dev_from_desc_fail = false;
205 
206 	/* spdk_fs_load() fails */
207 	g_fs_load_fail = true;
208 	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
209 	CU_ASSERT(g_fserrno != 0);
210 
211 	g_fs_load_fail = false;
212 
213 	/* spdk_fs_unload() fails */
214 	g_fs_unload_fail = true;
215 	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
216 	CU_ASSERT(g_fserrno != 0);
217 
218 	g_fs_unload_fail = false;
219 
220 	/* no fail */
221 	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
222 	CU_ASSERT(g_fserrno == 0);
223 }
224 
225 static void
226 spdk_blobfs_bdev_create_test(void)
227 {
228 	uint32_t cluster_sz = 1024 * 1024;
229 
230 	/* spdk_bdev_open_ext() fails */
231 	g_bdev_open_ext_fail = true;
232 	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
233 	CU_ASSERT(g_fserrno != 0);
234 
235 	g_bdev_open_ext_fail = false;
236 
237 	/* spdk_bdev_create_bs_dev_from_desc() fails */
238 	g_bdev_create_bs_dev_from_desc_fail = true;
239 	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
240 	CU_ASSERT(g_fserrno != 0);
241 
242 	g_bdev_create_bs_dev_from_desc_fail = false;
243 
244 	/* spdk_bs_bdev_claim() fails */
245 	g_bs_bdev_claim_fail = true;
246 	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
247 	CU_ASSERT(g_fserrno != 0);
248 
249 	g_bs_bdev_claim_fail = false;
250 
251 	/* spdk_fs_init() fails */
252 	g_fs_load_fail = true;
253 	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
254 	CU_ASSERT(g_fserrno != 0);
255 
256 	g_fs_load_fail = false;
257 
258 	/* spdk_fs_unload() fails */
259 	g_fs_unload_fail = true;
260 	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
261 	CU_ASSERT(g_fserrno != 0);
262 
263 	g_fs_unload_fail = false;
264 
265 	/* no fail */
266 	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
267 	CU_ASSERT(g_fserrno == 0);
268 }
269 
270 static void
271 spdk_blobfs_bdev_mount_test(void)
272 {
273 #ifdef SPDK_CONFIG_FUSE
274 	const char *mountpoint = "/mnt";
275 
276 	/* spdk_bdev_open_ext() fails */
277 	g_bdev_open_ext_fail = true;
278 	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
279 	CU_ASSERT(g_fserrno != 0);
280 
281 	g_bdev_open_ext_fail = false;
282 
283 	/* spdk_bdev_create_bs_dev_from_desc() fails */
284 	g_bdev_create_bs_dev_from_desc_fail = true;
285 	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
286 	CU_ASSERT(g_fserrno != 0);
287 
288 	g_bdev_create_bs_dev_from_desc_fail = false;
289 
290 	/* spdk_bs_bdev_claim() fails */
291 	g_bs_bdev_claim_fail = true;
292 	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
293 	CU_ASSERT(g_fserrno != 0);
294 
295 	g_bs_bdev_claim_fail = false;
296 
297 	/* spdk_fs_load() fails */
298 	g_fs_load_fail = true;
299 	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
300 	CU_ASSERT(g_fserrno != 0);
301 
302 	g_fs_load_fail = false;
303 
304 	/* spdk_blobfs_fuse_start() fails */
305 	g_blobfs_fuse_start_fail = true;
306 	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
307 	CU_ASSERT(g_fserrno != 0);
308 
309 	g_blobfs_fuse_start_fail = false;
310 
311 	/* no fail */
312 	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
313 	CU_ASSERT(g_fserrno == 0);
314 #endif
315 }
316 
317 int main(int argc, char **argv)
318 {
319 	CU_pSuite	suite = NULL;
320 	unsigned int	num_failures;
321 
322 	if (CU_initialize_registry() != CUE_SUCCESS) {
323 		return CU_get_error();
324 	}
325 
326 	suite = CU_add_suite("blobfs_bdev_ut", NULL, NULL);
327 	if (suite == NULL) {
328 		CU_cleanup_registry();
329 		return CU_get_error();
330 	}
331 
332 	if (
333 		CU_add_test(suite, "spdk_blobfs_bdev_detect_test", spdk_blobfs_bdev_detect_test) == NULL ||
334 		CU_add_test(suite, "spdk_blobfs_bdev_create_test", spdk_blobfs_bdev_create_test) == NULL ||
335 		CU_add_test(suite, "spdk_blobfs_bdev_mount_test", spdk_blobfs_bdev_mount_test) == NULL
336 	) {
337 		CU_cleanup_registry();
338 		return CU_get_error();
339 	}
340 
341 	CU_basic_set_mode(CU_BRM_VERBOSE);
342 	CU_basic_run_tests();
343 	num_failures = CU_get_number_of_failures();
344 	CU_cleanup_registry();
345 
346 	return num_failures;
347 }
348 
349 SPDK_LOG_REGISTER_COMPONENT("blobfs", SPDK_LOG_BLOBFS)
350