xref: /spdk/test/unit/lib/bdev/compress.c/compress_ut.c (revision 12ab86e24d916615b84d6eeb6f741600c25c64ab)
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_vbdev_register, int, (struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs,
133 				       int base_bdev_count), 0);
134 DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL);
135 DEFINE_STUB(spdk_env_get_socket_id, uint32_t, (uint32_t core), 0);
136 DEFINE_STUB_V(spdk_reduce_vol_readv, (struct spdk_reduce_vol *vol,
137 				      struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
138 				      spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
139 DEFINE_STUB_V(spdk_reduce_vol_writev, (struct spdk_reduce_vol *vol,
140 				       struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
141 				       spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
142 DEFINE_STUB(spdk_bdev_io_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_io *bdev_io),
143 	    0);
144 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch,
145 		struct spdk_bdev_io_wait_entry *entry), 0);
146 DEFINE_STUB_V(spdk_reduce_vol_unload, (struct spdk_reduce_vol *vol,
147 				       spdk_reduce_vol_op_complete cb_fn, void *cb_arg));
148 DEFINE_STUB_V(spdk_reduce_vol_load, (struct spdk_reduce_backing_dev *backing_dev,
149 				     spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg));
150 
151 /* DPDK stubs */
152 DEFINE_STUB(rte_socket_id, unsigned, (void), 0);
153 DEFINE_STUB(rte_eal_get_configuration, struct rte_config *, (void), NULL);
154 DEFINE_STUB(rte_vdev_init, int, (const char *name, const char *args), 0);
155 DEFINE_STUB_V(rte_mempool_free, (struct rte_mempool *mp));
156 
157 struct spdk_bdev_io *g_bdev_io;
158 struct spdk_io_channel *g_io_ch;
159 
160 void
161 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
162 {
163 	cb(g_io_ch, g_bdev_io, true);
164 }
165 
166 /* Mock these functions to call the callback and then return the value we require */
167 int ut_spdk_bdev_readv_blocks = 0;
168 bool ut_spdk_bdev_readv_blocks_mocked = false;
169 int
170 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
171 		       struct iovec *iov, int iovcnt,
172 		       uint64_t offset_blocks, uint64_t num_blocks,
173 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
174 {
175 	cb(g_bdev_io, !ut_spdk_bdev_readv_blocks, cb_arg);
176 	return ut_spdk_bdev_readv_blocks;
177 }
178 
179 int ut_spdk_bdev_writev_blocks = 0;
180 bool ut_spdk_bdev_writev_blocks_mocked = false;
181 int
182 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
183 			struct iovec *iov, int iovcnt,
184 			uint64_t offset_blocks, uint64_t num_blocks,
185 			spdk_bdev_io_completion_cb cb, void *cb_arg)
186 {
187 	cb(g_bdev_io, !ut_spdk_bdev_writev_blocks, cb_arg);
188 	return ut_spdk_bdev_writev_blocks;
189 }
190 
191 int ut_spdk_bdev_unmap_blocks = 0;
192 bool ut_spdk_bdev_unmap_blocks_mocked = false;
193 int
194 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
195 		       uint64_t offset_blocks, uint64_t num_blocks,
196 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
197 {
198 	cb(g_bdev_io, !ut_spdk_bdev_unmap_blocks, cb_arg);
199 	return ut_spdk_bdev_unmap_blocks;
200 }
201 
202 int ut_spdk_bdev_flush_blocks = 0;
203 bool ut_spdk_bdev_flush_blocks_mocked = false;
204 int
205 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
206 		       uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
207 		       void *cb_arg)
208 {
209 	cb(g_bdev_io, !ut_spdk_bdev_flush_blocks, cb_arg);
210 	return ut_spdk_bdev_flush_blocks;
211 }
212 
213 int ut_spdk_bdev_reset = 0;
214 bool ut_spdk_bdev_reset_mocked = false;
215 int
216 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
217 		spdk_bdev_io_completion_cb cb, void *cb_arg)
218 {
219 	cb(g_bdev_io, !ut_spdk_bdev_reset, cb_arg);
220 	return ut_spdk_bdev_reset;
221 }
222 
223 bool g_completion_called = false;
224 void
225 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
226 {
227 	bdev_io->internal.status = status;
228 	g_completion_called = true;
229 }
230 
231 
232 /* Global setup for all tests that share a bunch of preparation... */
233 static int
234 test_setup(void)
235 {
236 
237 	return 0;
238 }
239 
240 /* Global teardown for all tests */
241 static int
242 test_cleanup(void)
243 {
244 
245 	return 0;
246 }
247 
248 static void
249 test_error_paths(void)
250 {
251 
252 }
253 
254 static void
255 test_simple_write(void)
256 {
257 }
258 
259 static void
260 test_simple_read(void)
261 {
262 }
263 
264 static void
265 test_large_rw(void)
266 {
267 }
268 
269 static void
270 test_passthru(void)
271 {
272 
273 }
274 
275 static void
276 test_reset(void)
277 {
278 	/* TODO: There are a few different ways to do this given that
279 	 * the code uses spdk_for_each_channel() to implement reset
280 	 * handling. SUbmitting w/o UT for this function for now and
281 	 * will follow up with something shortly.
282 	 */
283 }
284 
285 static void
286 test_initdrivers(void)
287 {
288 }
289 
290 static void
291 test_supported_io(void)
292 {
293 
294 }
295 
296 int
297 main(int argc, char **argv)
298 {
299 	CU_pSuite	suite = NULL;
300 	unsigned int	num_failures;
301 
302 	if (CU_initialize_registry() != CUE_SUCCESS) {
303 		return CU_get_error();
304 	}
305 
306 	suite = CU_add_suite("compress", test_setup, test_cleanup);
307 	if (suite == NULL) {
308 		CU_cleanup_registry();
309 		return CU_get_error();
310 	}
311 
312 	if (CU_add_test(suite, "test_error_paths",
313 			test_error_paths) == NULL ||
314 	    CU_add_test(suite, "test_simple_write",
315 			test_simple_write) == NULL ||
316 	    CU_add_test(suite, "test_simple_read",
317 			test_simple_read) == NULL ||
318 	    CU_add_test(suite, "test_large_rw",
319 			test_large_rw) == NULL ||
320 	    CU_add_test(suite, "test_passthru",
321 			test_passthru) == NULL ||
322 	    CU_add_test(suite, "test_initdrivers",
323 			test_initdrivers) == NULL ||
324 	    CU_add_test(suite, "test_supported_io",
325 			test_supported_io) == NULL ||
326 	    CU_add_test(suite, "test_reset",
327 			test_reset) == NULL
328 	   ) {
329 		CU_cleanup_registry();
330 		return CU_get_error();
331 	}
332 
333 	CU_basic_set_mode(CU_BRM_VERBOSE);
334 	CU_basic_run_tests();
335 	num_failures = CU_get_number_of_failures();
336 	CU_cleanup_registry();
337 	return num_failures;
338 }
339