summaryrefslogtreecommitdiffstats
path: root/src/spdk/test/unit/lib/blobfs/blobfs_bdev.c/blobfs_bdev_ut.c
blob: 425b29882249b04573f3c5fec663c5307e8dab91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*-
 *   BSD LICENSE
 *
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "spdk_cunit.h"
#include "spdk/string.h"
#include "spdk/stdinc.h"

#include "blobfs/bdev/blobfs_bdev.c"

int g_fserrno;

bool g_bdev_open_ext_fail = false;
bool g_bdev_create_bs_dev_from_desc_fail = false;
bool g_fs_load_fail = false;
bool g_fs_unload_fail = false;
bool g_bs_bdev_claim_fail = false;
bool g_blobfs_fuse_start_fail = false;
struct blobfs_bdev_operation_ctx *g_fs_ctx;

const char *g_bdev_name = "ut_bdev";

int
spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
		   void *event_ctx, struct spdk_bdev_desc **_desc)
{
	if (g_bdev_open_ext_fail) {
		return -1;
	}

	return 0;
}

static  void
bs_dev_destroy(struct spdk_bs_dev *dev)
{
}

struct spdk_bs_dev *
spdk_bdev_create_bs_dev_from_desc(struct spdk_bdev_desc *desc)
{
	static struct spdk_bs_dev bs_dev;

	if (g_bdev_create_bs_dev_from_desc_fail) {
		return NULL;
	}

	bs_dev.destroy = bs_dev_destroy;
	return &bs_dev;
}

void
spdk_fs_load(struct spdk_bs_dev *dev, fs_send_request_fn send_request_fn,
	     spdk_fs_op_with_handle_complete cb_fn, void *cb_arg)
{
	int rc = 0;

	if (g_fs_load_fail) {
		rc = -1;
	}

	cb_fn(cb_arg, NULL, rc);

	return;
}

void
spdk_fs_unload(struct spdk_filesystem *fs, spdk_fs_op_complete cb_fn, void *cb_arg)
{
	int rc = 0;

	if (g_fs_unload_fail) {
		rc = -1;
	}

	cb_fn(cb_arg, rc);
	return;
}

void
spdk_fs_init(struct spdk_bs_dev *dev, struct spdk_blobfs_opts *opt,
	     fs_send_request_fn send_request_fn,
	     spdk_fs_op_with_handle_complete cb_fn, void *cb_arg)
{
	int rc = 0;

	if (g_fs_load_fail) {
		rc = -1;
	}

	cb_fn(cb_arg, NULL, rc);
	return;
}

int
spdk_bs_bdev_claim(struct spdk_bs_dev *bs_dev, struct spdk_bdev_module *module)
{
	if (g_bs_bdev_claim_fail) {
		return -1;
	}

	return 0;
}

int
blobfs_fuse_start(const char *bdev_name, const char *mountpoint, struct spdk_filesystem *fs,
		  blobfs_fuse_unmount_cb cb_fn, void *cb_arg, struct spdk_blobfs_fuse **_bfuse)
{
	if (g_blobfs_fuse_start_fail) {
		return -1;
	}

	/* store the ctx for unmount operation */
	g_fs_ctx = cb_arg;

	return 0;
}

void
spdk_bdev_close(struct spdk_bdev_desc *desc)
{
}

int
spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx)
{
	fn(ctx);
	return 0;
}

struct spdk_thread *
spdk_get_thread(void)
{
	struct spdk_thread *thd = (struct spdk_thread *)0x1;

	return thd;
}

const char *
spdk_bdev_get_name(const struct spdk_bdev *bdev)
{
	return g_bdev_name;
}

void
spdk_fs_opts_init(struct spdk_blobfs_opts *opts)
{
}

void
blobfs_fuse_send_request(fs_request_fn fn, void *arg)
{
}

void
blobfs_fuse_stop(struct spdk_blobfs_fuse *bfuse)
{
}

static void
blobfs_bdev_op_complete(void *cb_arg, int fserrno)
{
	g_fserrno = fserrno;
}

static void
spdk_blobfs_bdev_detect_test(void)
{
	/* spdk_bdev_open_ext() fails */
	g_bdev_open_ext_fail = true;
	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bdev_open_ext_fail = false;

	/* spdk_bdev_create_bs_dev_from_desc() fails */
	g_bdev_create_bs_dev_from_desc_fail = true;
	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bdev_create_bs_dev_from_desc_fail = false;

	/* spdk_fs_load() fails */
	g_fs_load_fail = true;
	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_fs_load_fail = false;

	/* spdk_fs_unload() fails */
	g_fs_unload_fail = true;
	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_fs_unload_fail = false;

	/* no fail */
	spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno == 0);
}

static void
spdk_blobfs_bdev_create_test(void)
{
	uint32_t cluster_sz = 1024 * 1024;

	/* spdk_bdev_open_ext() fails */
	g_bdev_open_ext_fail = true;
	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bdev_open_ext_fail = false;

	/* spdk_bdev_create_bs_dev_from_desc() fails */
	g_bdev_create_bs_dev_from_desc_fail = true;
	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bdev_create_bs_dev_from_desc_fail = false;

	/* spdk_bs_bdev_claim() fails */
	g_bs_bdev_claim_fail = true;
	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bs_bdev_claim_fail = false;

	/* spdk_fs_init() fails */
	g_fs_load_fail = true;
	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_fs_load_fail = false;

	/* spdk_fs_unload() fails */
	g_fs_unload_fail = true;
	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_fs_unload_fail = false;

	/* no fail */
	spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno == 0);
}

static void
spdk_blobfs_bdev_mount_test(void)
{
#ifdef SPDK_CONFIG_FUSE
	const char *mountpoint = "/mnt";

	/* spdk_bdev_open_ext() fails */
	g_bdev_open_ext_fail = true;
	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bdev_open_ext_fail = false;

	/* spdk_bdev_create_bs_dev_from_desc() fails */
	g_bdev_create_bs_dev_from_desc_fail = true;
	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bdev_create_bs_dev_from_desc_fail = false;

	/* spdk_bs_bdev_claim() fails */
	g_bs_bdev_claim_fail = true;
	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_bs_bdev_claim_fail = false;

	/* spdk_fs_load() fails */
	g_fs_load_fail = true;
	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_fs_load_fail = false;

	/* blobfs_fuse_start() fails */
	g_blobfs_fuse_start_fail = true;
	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno != 0);

	g_blobfs_fuse_start_fail = false;

	/* no fail */
	spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL);
	CU_ASSERT(g_fserrno == 0);
	CU_ASSERT(g_fs_ctx != NULL);

	/* after mount operation success , we need make sure unmount operation success */
	blobfs_bdev_unmount(g_fs_ctx);
	CU_ASSERT(g_fserrno == 0);
#endif
}

int main(int argc, char **argv)
{
	CU_pSuite	suite = NULL;
	unsigned int	num_failures;

	CU_set_error_action(CUEA_ABORT);
	CU_initialize_registry();

	suite = CU_add_suite("blobfs_bdev_ut", NULL, NULL);

	CU_ADD_TEST(suite, spdk_blobfs_bdev_detect_test);
	CU_ADD_TEST(suite, spdk_blobfs_bdev_create_test);
	CU_ADD_TEST(suite, spdk_blobfs_bdev_mount_test);

	CU_basic_set_mode(CU_BRM_VERBOSE);
	CU_basic_run_tests();
	num_failures = CU_get_number_of_failures();
	CU_cleanup_registry();

	return num_failures;
}