xref: /spdk/test/unit/lib/bdev/compress.c/compress_ut.c (revision c4d9daeb7bf491bc0eb6e8d417b75d44773cb009)
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 void mock_rte_pktmbuf_free(struct rte_mbuf *m);
113 #define rte_pktmbuf_free mock_rte_pktmbuf_free
114 void mock_rte_pktmbuf_free(struct rte_mbuf *m)
115 {
116 }
117 
118 static int ut_rte_pktmbuf_alloc_bulk = 0;
119 int mock_rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs,
120 				unsigned count);
121 #define rte_pktmbuf_alloc_bulk mock_rte_pktmbuf_alloc_bulk
122 int mock_rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs,
123 				unsigned count)
124 {
125 	return ut_rte_pktmbuf_alloc_bulk;
126 }
127 
128 #include "bdev/compress/vbdev_compress.c"
129 
130 /* SPDK stubs */
131 DEFINE_STUB_V(spdk_bdev_module_list_add, (struct spdk_bdev_module *bdev_module));
132 DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *g_bdev_io));
133 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev,
134 		enum spdk_bdev_io_type io_type), 0);
135 DEFINE_STUB_V(spdk_bdev_module_release_bdev, (struct spdk_bdev *bdev));
136 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc));
137 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), 0);
138 DEFINE_STUB(spdk_env_get_current_core, uint32_t, (void), 0);
139 DEFINE_STUB(spdk_bdev_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_desc *desc), 0);
140 DEFINE_STUB_V(spdk_bdev_unregister, (struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn,
141 				     void *cb_arg));
142 DEFINE_STUB(spdk_bdev_open, int, (struct spdk_bdev *bdev, bool write,
143 				  spdk_bdev_remove_cb_t remove_cb,
144 				  void *remove_ctx, struct spdk_bdev_desc **_desc), 0);
145 DEFINE_STUB(spdk_bdev_module_claim_bdev, int, (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
146 		struct spdk_bdev_module *module), 0);
147 DEFINE_STUB_V(spdk_bdev_module_examine_done, (struct spdk_bdev_module *module));
148 DEFINE_STUB(spdk_bdev_register, int, (struct spdk_bdev *bdev), 0);
149 DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL);
150 DEFINE_STUB(spdk_env_get_socket_id, uint32_t, (uint32_t core), 0);
151 DEFINE_STUB_V(spdk_reduce_vol_readv, (struct spdk_reduce_vol *vol,
152 				      struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
153 				      spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
154 DEFINE_STUB_V(spdk_reduce_vol_writev, (struct spdk_reduce_vol *vol,
155 				       struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
156 				       spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
157 DEFINE_STUB(spdk_bdev_io_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_io *bdev_io),
158 	    0);
159 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch,
160 		struct spdk_bdev_io_wait_entry *entry), 0);
161 DEFINE_STUB_V(spdk_reduce_vol_unload, (struct spdk_reduce_vol *vol,
162 				       spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
163 DEFINE_STUB_V(spdk_reduce_vol_load, (struct spdk_reduce_backing_dev *backing_dev,
164 				     spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg));
165 DEFINE_STUB(spdk_reduce_vol_get_params, const struct spdk_reduce_vol_params *,
166 	    (struct spdk_reduce_vol *vol), NULL);
167 
168 /* DPDK stubs */
169 DEFINE_STUB(rte_socket_id, unsigned, (void), 0);
170 DEFINE_STUB(rte_eal_get_configuration, struct rte_config *, (void), NULL);
171 DEFINE_STUB(rte_vdev_init, int, (const char *name, const char *args), 0);
172 DEFINE_STUB_V(rte_mempool_free, (struct rte_mempool *mp));
173 DEFINE_STUB(rte_compressdev_dequeue_burst, uint16_t,
174 	    (uint8_t dev_id, uint16_t qp_id, struct rte_comp_op **ops, uint16_t nb_ops), 0);
175 DEFINE_STUB(rte_compressdev_enqueue_burst, uint16_t,
176 	    (uint8_t dev_id, uint16_t qp_id, struct rte_comp_op **ops, uint16_t nb_ops), 0);
177 DEFINE_STUB_V(rte_comp_op_free, (struct rte_comp_op *op));
178 DEFINE_STUB(rte_comp_op_alloc, struct rte_comp_op *, (struct rte_mempool *mempool), NULL);
179 DEFINE_STUB(rte_pktmbuf_pool_create, struct rte_mempool *, (const char *name, unsigned n,
180 		unsigned cache_size, uint16_t priv_size,
181 		uint16_t data_room_size, int socket_id), NULL);
182 
183 struct spdk_bdev_io *g_bdev_io;
184 struct spdk_io_channel *g_io_ch;
185 
186 void
187 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
188 {
189 	cb(g_io_ch, g_bdev_io, true);
190 }
191 
192 /* Mock these functions to call the callback and then return the value we require */
193 int ut_spdk_bdev_readv_blocks = 0;
194 bool ut_spdk_bdev_readv_blocks_mocked = false;
195 int
196 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
197 		       struct iovec *iov, int iovcnt,
198 		       uint64_t offset_blocks, uint64_t num_blocks,
199 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
200 {
201 	cb(g_bdev_io, !ut_spdk_bdev_readv_blocks, cb_arg);
202 	return ut_spdk_bdev_readv_blocks;
203 }
204 
205 int ut_spdk_bdev_writev_blocks = 0;
206 bool ut_spdk_bdev_writev_blocks_mocked = false;
207 int
208 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
209 			struct iovec *iov, int iovcnt,
210 			uint64_t offset_blocks, uint64_t num_blocks,
211 			spdk_bdev_io_completion_cb cb, void *cb_arg)
212 {
213 	cb(g_bdev_io, !ut_spdk_bdev_writev_blocks, cb_arg);
214 	return ut_spdk_bdev_writev_blocks;
215 }
216 
217 int ut_spdk_bdev_unmap_blocks = 0;
218 bool ut_spdk_bdev_unmap_blocks_mocked = false;
219 int
220 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
221 		       uint64_t offset_blocks, uint64_t num_blocks,
222 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
223 {
224 	cb(g_bdev_io, !ut_spdk_bdev_unmap_blocks, cb_arg);
225 	return ut_spdk_bdev_unmap_blocks;
226 }
227 
228 int ut_spdk_bdev_flush_blocks = 0;
229 bool ut_spdk_bdev_flush_blocks_mocked = false;
230 int
231 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
232 		       uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
233 		       void *cb_arg)
234 {
235 	cb(g_bdev_io, !ut_spdk_bdev_flush_blocks, cb_arg);
236 	return ut_spdk_bdev_flush_blocks;
237 }
238 
239 int ut_spdk_bdev_reset = 0;
240 bool ut_spdk_bdev_reset_mocked = false;
241 int
242 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
243 		spdk_bdev_io_completion_cb cb, void *cb_arg)
244 {
245 	cb(g_bdev_io, !ut_spdk_bdev_reset, cb_arg);
246 	return ut_spdk_bdev_reset;
247 }
248 
249 bool g_completion_called = false;
250 void
251 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
252 {
253 	bdev_io->internal.status = status;
254 	g_completion_called = true;
255 }
256 
257 
258 /* Global setup for all tests that share a bunch of preparation... */
259 static int
260 test_setup(void)
261 {
262 
263 	return 0;
264 }
265 
266 /* Global teardown for all tests */
267 static int
268 test_cleanup(void)
269 {
270 
271 	return 0;
272 }
273 
274 static void
275 test_error_paths(void)
276 {
277 
278 }
279 
280 static void
281 test_simple_write(void)
282 {
283 }
284 
285 static void
286 test_simple_read(void)
287 {
288 }
289 
290 static void
291 test_large_rw(void)
292 {
293 }
294 
295 static void
296 test_passthru(void)
297 {
298 
299 }
300 
301 static void
302 test_reset(void)
303 {
304 	/* TODO: There are a few different ways to do this given that
305 	 * the code uses spdk_for_each_channel() to implement reset
306 	 * handling. SUbmitting w/o UT for this function for now and
307 	 * will follow up with something shortly.
308 	 */
309 }
310 
311 static void
312 test_initdrivers(void)
313 {
314 }
315 
316 static void
317 test_supported_io(void)
318 {
319 
320 }
321 
322 int
323 main(int argc, char **argv)
324 {
325 	CU_pSuite	suite = NULL;
326 	unsigned int	num_failures;
327 
328 	if (CU_initialize_registry() != CUE_SUCCESS) {
329 		return CU_get_error();
330 	}
331 
332 	suite = CU_add_suite("compress", test_setup, test_cleanup);
333 	if (suite == NULL) {
334 		CU_cleanup_registry();
335 		return CU_get_error();
336 	}
337 
338 	if (CU_add_test(suite, "test_error_paths",
339 			test_error_paths) == NULL ||
340 	    CU_add_test(suite, "test_simple_write",
341 			test_simple_write) == NULL ||
342 	    CU_add_test(suite, "test_simple_read",
343 			test_simple_read) == NULL ||
344 	    CU_add_test(suite, "test_large_rw",
345 			test_large_rw) == NULL ||
346 	    CU_add_test(suite, "test_passthru",
347 			test_passthru) == NULL ||
348 	    CU_add_test(suite, "test_initdrivers",
349 			test_initdrivers) == NULL ||
350 	    CU_add_test(suite, "test_supported_io",
351 			test_supported_io) == NULL ||
352 	    CU_add_test(suite, "test_reset",
353 			test_reset) == NULL
354 	   ) {
355 		CU_cleanup_registry();
356 		return CU_get_error();
357 	}
358 
359 	CU_basic_set_mode(CU_BRM_VERBOSE);
360 	CU_basic_run_tests();
361 	num_failures = CU_get_number_of_failures();
362 	CU_cleanup_registry();
363 	return num_failures;
364 }
365