xref: /spdk/test/unit/lib/bdev/compress.c/compress_ut.c (revision ceb6ef8958c080b7702557bfedac9fd8fbcad0b6)
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 "common/lib/test_env.c"
37 #include "spdk_internal/mock.h"
38 #include "unit/lib/json_mock.c"
39 
40 #include <rte_compressdev.h>
41 
42 /* Those functions are defined as static inline in DPDK, so we can't
43  * mock them straight away. We use defines to redirect them into
44  * our custom functions.
45  */
46 
47 void __rte_experimental mock_rte_compressdev_info_get(uint8_t dev_id,
48 		struct rte_compressdev_info *dev_info);
49 #define rte_compressdev_info_get mock_rte_compressdev_info_get
50 void __rte_experimental
51 mock_rte_compressdev_info_get(uint8_t dev_id, struct rte_compressdev_info *dev_info)
52 {
53 	/* TODO return global struct */
54 }
55 
56 int __rte_experimental mock_rte_compressdev_configure(uint8_t dev_id,
57 		struct rte_compressdev_config *config);
58 #define rte_compressdev_configure mock_rte_compressdev_configure
59 int __rte_experimental
60 mock_rte_compressdev_configure(uint8_t dev_id, struct rte_compressdev_config *config)
61 {
62 	return 0;
63 }
64 
65 int __rte_experimental mock_rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
66 		uint32_t max_inflight_ops, int socket_id);
67 #define rte_compressdev_queue_pair_setup mock_rte_compressdev_queue_pair_setup
68 int __rte_experimental
69 mock_rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
70 				      uint32_t max_inflight_ops, int socket_id)
71 {
72 	return 0;
73 }
74 
75 int __rte_experimental mock_rte_compressdev_start(uint8_t dev_id);
76 #define rte_compressdev_start mock_rte_compressdev_start
77 int __rte_experimental
78 mock_rte_compressdev_start(uint8_t dev_id)
79 {
80 	return 0;
81 }
82 
83 int __rte_experimental mock_rte_compressdev_private_xform_create(uint8_t dev_id,
84 		const struct rte_comp_xform *xform, void **private_xform);
85 #define rte_compressdev_private_xform_create mock_rte_compressdev_private_xform_create
86 int __rte_experimental
87 mock_rte_compressdev_private_xform_create(uint8_t dev_id,
88 		const struct rte_comp_xform *xform, void **private_xform)
89 {
90 	return 0;
91 }
92 
93 uint8_t __rte_experimental mock_rte_compressdev_count(void);
94 #define rte_compressdev_count mock_rte_compressdev_count
95 uint8_t __rte_experimental
96 mock_rte_compressdev_count(void)
97 {
98 	return 0;
99 }
100 
101 struct rte_mempool *__rte_experimental mock_rte_comp_op_pool_create(const char *name,
102 		unsigned int nb_elts, unsigned int cache_size, uint16_t user_size,
103 		int socket_id);
104 #define rte_comp_op_pool_create mock_rte_comp_op_pool_create
105 struct rte_mempool *__rte_experimental
106 mock_rte_comp_op_pool_create(const char *name, unsigned int nb_elts,
107 			     unsigned int cache_size, uint16_t user_size, int socket_id)
108 {
109 	return NULL;
110 }
111 
112 #include "bdev/compress/vbdev_compress.c"
113 
114 /* SPDK stubs */
115 DEFINE_STUB_V(spdk_bdev_module_list_add, (struct spdk_bdev_module *bdev_module));
116 DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *g_bdev_io));
117 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev,
118 		enum spdk_bdev_io_type io_type), 0);
119 DEFINE_STUB_V(spdk_bdev_module_release_bdev, (struct spdk_bdev *bdev));
120 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc));
121 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), 0);
122 DEFINE_STUB(spdk_env_get_current_core, uint32_t, (void), 0);
123 DEFINE_STUB(spdk_bdev_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_desc *desc), 0);
124 DEFINE_STUB_V(spdk_bdev_unregister, (struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn,
125 				     void *cb_arg));
126 DEFINE_STUB(spdk_bdev_open, int, (struct spdk_bdev *bdev, bool write,
127 				  spdk_bdev_remove_cb_t remove_cb,
128 				  void *remove_ctx, struct spdk_bdev_desc **_desc), 0);
129 DEFINE_STUB(spdk_bdev_module_claim_bdev, int, (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
130 		struct spdk_bdev_module *module), 0);
131 DEFINE_STUB_V(spdk_bdev_module_examine_done, (struct spdk_bdev_module *module));
132 DEFINE_STUB(spdk_bdev_register, int, (struct spdk_bdev *bdev), 0);
133 DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL);
134 DEFINE_STUB(spdk_env_get_socket_id, uint32_t, (uint32_t core), 0);
135 DEFINE_STUB_V(spdk_reduce_vol_readv, (struct spdk_reduce_vol *vol,
136 				      struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
137 				      spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
138 DEFINE_STUB_V(spdk_reduce_vol_writev, (struct spdk_reduce_vol *vol,
139 				       struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
140 				       spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
141 DEFINE_STUB(spdk_bdev_io_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_io *bdev_io),
142 	    0);
143 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch,
144 		struct spdk_bdev_io_wait_entry *entry), 0);
145 DEFINE_STUB_V(spdk_reduce_vol_unload, (struct spdk_reduce_vol *vol,
146 				       spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
147 DEFINE_STUB_V(spdk_reduce_vol_load, (struct spdk_reduce_backing_dev *backing_dev,
148 				     spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg));
149 
150 /* DPDK stubs */
151 DEFINE_STUB(rte_socket_id, unsigned, (void), 0);
152 DEFINE_STUB(rte_eal_get_configuration, struct rte_config *, (void), NULL);
153 DEFINE_STUB(rte_vdev_init, int, (const char *name, const char *args), 0);
154 DEFINE_STUB_V(rte_mempool_free, (struct rte_mempool *mp));
155 DEFINE_STUB(rte_compressdev_dequeue_burst, uint16_t,
156 	    (uint8_t dev_id, uint16_t qp_id, struct rte_comp_op **ops, uint16_t nb_ops), 0);
157 DEFINE_STUB(rte_compressdev_enqueue_burst, uint16_t,
158 	    (uint8_t dev_id, uint16_t qp_id, struct rte_comp_op **ops, uint16_t nb_ops), 0);
159 DEFINE_STUB_V(rte_comp_op_free, (struct rte_comp_op *op));
160 DEFINE_STUB(rte_comp_op_alloc, struct rte_comp_op *, (struct rte_mempool *mempool), NULL);
161 
162 struct spdk_bdev_io *g_bdev_io;
163 struct spdk_io_channel *g_io_ch;
164 
165 void
166 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
167 {
168 	cb(g_io_ch, g_bdev_io, true);
169 }
170 
171 /* Mock these functions to call the callback and then return the value we require */
172 int ut_spdk_bdev_readv_blocks = 0;
173 bool ut_spdk_bdev_readv_blocks_mocked = false;
174 int
175 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
176 		       struct iovec *iov, int iovcnt,
177 		       uint64_t offset_blocks, uint64_t num_blocks,
178 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
179 {
180 	cb(g_bdev_io, !ut_spdk_bdev_readv_blocks, cb_arg);
181 	return ut_spdk_bdev_readv_blocks;
182 }
183 
184 int ut_spdk_bdev_writev_blocks = 0;
185 bool ut_spdk_bdev_writev_blocks_mocked = false;
186 int
187 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
188 			struct iovec *iov, int iovcnt,
189 			uint64_t offset_blocks, uint64_t num_blocks,
190 			spdk_bdev_io_completion_cb cb, void *cb_arg)
191 {
192 	cb(g_bdev_io, !ut_spdk_bdev_writev_blocks, cb_arg);
193 	return ut_spdk_bdev_writev_blocks;
194 }
195 
196 int ut_spdk_bdev_unmap_blocks = 0;
197 bool ut_spdk_bdev_unmap_blocks_mocked = false;
198 int
199 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
200 		       uint64_t offset_blocks, uint64_t num_blocks,
201 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
202 {
203 	cb(g_bdev_io, !ut_spdk_bdev_unmap_blocks, cb_arg);
204 	return ut_spdk_bdev_unmap_blocks;
205 }
206 
207 int ut_spdk_bdev_flush_blocks = 0;
208 bool ut_spdk_bdev_flush_blocks_mocked = false;
209 int
210 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
211 		       uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
212 		       void *cb_arg)
213 {
214 	cb(g_bdev_io, !ut_spdk_bdev_flush_blocks, cb_arg);
215 	return ut_spdk_bdev_flush_blocks;
216 }
217 
218 int ut_spdk_bdev_reset = 0;
219 bool ut_spdk_bdev_reset_mocked = false;
220 int
221 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
222 		spdk_bdev_io_completion_cb cb, void *cb_arg)
223 {
224 	cb(g_bdev_io, !ut_spdk_bdev_reset, cb_arg);
225 	return ut_spdk_bdev_reset;
226 }
227 
228 bool g_completion_called = false;
229 void
230 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
231 {
232 	bdev_io->internal.status = status;
233 	g_completion_called = true;
234 }
235 
236 
237 /* Global setup for all tests that share a bunch of preparation... */
238 static int
239 test_setup(void)
240 {
241 
242 	return 0;
243 }
244 
245 /* Global teardown for all tests */
246 static int
247 test_cleanup(void)
248 {
249 
250 	return 0;
251 }
252 
253 static void
254 test_error_paths(void)
255 {
256 
257 }
258 
259 static void
260 test_simple_write(void)
261 {
262 }
263 
264 static void
265 test_simple_read(void)
266 {
267 }
268 
269 static void
270 test_large_rw(void)
271 {
272 }
273 
274 static void
275 test_passthru(void)
276 {
277 
278 }
279 
280 static void
281 test_reset(void)
282 {
283 	/* TODO: There are a few different ways to do this given that
284 	 * the code uses spdk_for_each_channel() to implement reset
285 	 * handling. SUbmitting w/o UT for this function for now and
286 	 * will follow up with something shortly.
287 	 */
288 }
289 
290 static void
291 test_initdrivers(void)
292 {
293 }
294 
295 static void
296 test_supported_io(void)
297 {
298 
299 }
300 
301 int
302 main(int argc, char **argv)
303 {
304 	CU_pSuite	suite = NULL;
305 	unsigned int	num_failures;
306 
307 	if (CU_initialize_registry() != CUE_SUCCESS) {
308 		return CU_get_error();
309 	}
310 
311 	suite = CU_add_suite("compress", test_setup, test_cleanup);
312 	if (suite == NULL) {
313 		CU_cleanup_registry();
314 		return CU_get_error();
315 	}
316 
317 	if (CU_add_test(suite, "test_error_paths",
318 			test_error_paths) == NULL ||
319 	    CU_add_test(suite, "test_simple_write",
320 			test_simple_write) == NULL ||
321 	    CU_add_test(suite, "test_simple_read",
322 			test_simple_read) == NULL ||
323 	    CU_add_test(suite, "test_large_rw",
324 			test_large_rw) == NULL ||
325 	    CU_add_test(suite, "test_passthru",
326 			test_passthru) == NULL ||
327 	    CU_add_test(suite, "test_initdrivers",
328 			test_initdrivers) == NULL ||
329 	    CU_add_test(suite, "test_supported_io",
330 			test_supported_io) == NULL ||
331 	    CU_add_test(suite, "test_reset",
332 			test_reset) == NULL
333 	   ) {
334 		CU_cleanup_registry();
335 		return CU_get_error();
336 	}
337 
338 	CU_basic_set_mode(CU_BRM_VERBOSE);
339 	CU_basic_run_tests();
340 	num_failures = CU_get_number_of_failures();
341 	CU_cleanup_registry();
342 	return num_failures;
343 }
344