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