]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/tests/unit/tests/mngt/ocf_mngt_io_class.c/ocf_mngt_io_class.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / ocf / tests / unit / tests / mngt / ocf_mngt_io_class.c / ocf_mngt_io_class.c
1 /*
2 * Copyright(c) 2012-2018 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
4 */
5
6 /*
7 * <tested_file_path>src/mngt/ocf_mngt_io_class.c</tested_file_path>
8 * <tested_function>ocf_mngt_cache_io_classes_configure</tested_function>
9 * <functions_to_leave>
10 * INSERT HERE LIST OF FUNCTIONS YOU WANT TO LEAVE
11 * ONE FUNCTION PER LINE
12 * _ocf_mngt_io_class_edit
13 * _ocf_mngt_io_class_configure
14 * _ocf_mngt_io_class_remove
15 * </functions_to_leave>
16 */
17
18 #undef static
19
20 #undef inline
21
22
23 #include <stdarg.h>
24 #include <stddef.h>
25 #include <setjmp.h>
26 #include <cmocka.h>
27 #include "print_desc.h"
28
29 #include "ocf/ocf.h"
30 #include "ocf_mngt_common.h"
31 #include "../ocf_priv.h"
32 #include "../metadata/metadata.h"
33 #include "../engine/cache_engine.h"
34 #include "../utils/utils_part.h"
35 #include "../eviction/ops.h"
36 #include "ocf_env.h"
37
38 /* Mocks reqired for compilation */
39 int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl,
40 const char *fmt, ...)
41 {
42 }
43
44 ocf_ctx_t __wrap_ocf_cache_get_ctx(ocf_cache_t cache)
45 {
46 }
47
48 char *__wrap_ocf_cache_get_name(ocf_cache_t cache)
49 {
50 }
51
52 int __wrap_ocf_mngt_cache_lock(ocf_cache_t cache)
53 {
54 return 0;
55 }
56
57 void __wrap_ocf_mngt_cache_unlock(ocf_cache_t cache)
58 {
59 }
60
61 void __wrap_ocf_metadata_lock(struct ocf_cache *cache, int rw)
62 {
63 }
64
65 void __wrap_ocf_metadata_unlock(struct ocf_cache *cache, int rw)
66 {
67 }
68
69 /* Functions mocked for testing purposes */
70 bool __wrap_ocf_part_is_added(struct ocf_user_part *part)
71 {
72 function_called();
73 return mock();
74 }
75
76 int __wrap__ocf_mngt_set_partition_size(struct ocf_cache *cache,
77 ocf_part_id_t part_id, uint32_t min, uint32_t max)
78 {
79 function_called();
80 return mock();
81 }
82
83 void __wrap_ocf_part_set_prio(struct ocf_cache *cache,
84 struct ocf_user_part *part, int16_t prio)
85 {
86 function_called();
87 }
88
89 bool __wrap_ocf_part_is_valid(struct ocf_user_part *part)
90 {
91 function_called();
92 return mock();
93 }
94
95
96 void __wrap_ocf_part_set_valid(struct ocf_cache *cache, ocf_part_id_t id,
97 bool valid)
98 {
99 function_called();
100 check_expected(valid);
101 check_expected(id);
102 }
103
104 int __wrap__ocf_mngt_io_class_validate_cfg(ocf_cache_t cache,
105 const struct ocf_mngt_io_class_config *cfg)
106 {
107 function_called();
108 return mock();
109 }
110
111 void __wrap_ocf_part_sort(struct ocf_cache *cache)
112 {
113 function_called();
114 }
115
116 int __wrap_ocf_metadata_flush_superblock(struct ocf_cache *cache)
117 {
118 }
119
120 /* Helper function for test prepration */
121 static inline void setup_valid_config(struct ocf_mngt_io_class_config *cfg,
122 bool remove)
123 {
124 int i;
125 for (i = 0; i < OCF_IO_CLASS_MAX; i++) {
126 cfg[i].class_id = i;
127 cfg[i].name = remove ? NULL : "test_io_class_name" ;
128 cfg[i].prio = i;
129 cfg[i].cache_mode = ocf_cache_mode_pt;
130 cfg[i].min_size = 2*i;
131 cfg[i].max_size = 20*i;
132 }
133 }
134
135 static void ocf_mngt_io_classes_configure_test03(void **state)
136 {
137 struct ocf_cache cache = {0};
138 struct ocf_mngt_io_classes_config cfg = {0};
139 int result, i;
140
141 for (i = 0; i < OCF_IO_CLASS_MAX; i++) {
142 cache.user_parts[i].config =
143 test_malloc(sizeof(struct ocf_user_part_config));
144 }
145 cache.device = 1;
146
147 setup_valid_config(cfg.config, true);
148
149 print_test_description("Remove all io classes");
150
151 for (i = 0; i < OCF_IO_CLASS_MAX; i++) {
152 expect_function_call(__wrap__ocf_mngt_io_class_validate_cfg);
153 will_return(__wrap__ocf_mngt_io_class_validate_cfg, 0);
154 }
155
156 /* Removing default io_class is not allowed */
157 for (i = 1; i < OCF_IO_CLASS_MAX; i++) {
158 expect_function_call(__wrap_ocf_part_is_valid);
159 will_return(__wrap_ocf_part_is_valid, 1);
160
161 expect_function_call(__wrap_ocf_part_set_valid);
162 /* Test assumes default partition has id equal 0 */
163 expect_in_range(__wrap_ocf_part_set_valid, id, OCF_IO_CLASS_ID_MIN + 1,
164 OCF_IO_CLASS_ID_MAX);
165 expect_value(__wrap_ocf_part_set_valid, valid, false);
166 }
167
168 expect_function_call(__wrap_ocf_part_sort);
169
170 result = ocf_mngt_cache_io_classes_configure(&cache, &cfg);
171
172 assert_int_equal(result, 0);
173
174 for (i = 0; i < OCF_IO_CLASS_MAX; i++)
175 test_free(cache.user_parts[i].config);
176 }
177
178 static void ocf_mngt_io_classes_configure_test02(void **state)
179 {
180 struct ocf_cache cache = {0};
181 struct ocf_mngt_io_classes_config cfg = {0};
182 int result, i;
183
184 for (i = 0; i < OCF_IO_CLASS_MAX; i++) {
185 cache.user_parts[i].config =
186 test_malloc(sizeof(struct ocf_user_part_config));
187 }
188 cache.device = 1;
189
190 setup_valid_config(cfg.config, false);
191
192 print_test_description("Configure all possible io classes");
193
194 for (i = 0; i < OCF_IO_CLASS_MAX; i++) {
195 expect_function_call(__wrap__ocf_mngt_io_class_validate_cfg);
196 will_return(__wrap__ocf_mngt_io_class_validate_cfg, 0);
197 }
198
199 /* Configure default io_class */
200 expect_function_call(__wrap_ocf_part_is_added);
201 will_return(__wrap_ocf_part_is_added, 1);
202
203 expect_function_call(__wrap__ocf_mngt_set_partition_size);
204 will_return(__wrap__ocf_mngt_set_partition_size, 0);
205
206 expect_function_call(__wrap_ocf_part_set_prio);
207
208 /* Configure custom io_classes */
209 for (i = 1; i < OCF_IO_CLASS_MAX; i++) {
210 expect_function_call(__wrap_ocf_part_is_added);
211 will_return(__wrap_ocf_part_is_added, 1);
212
213 expect_function_call(__wrap__ocf_mngt_set_partition_size);
214 will_return(__wrap__ocf_mngt_set_partition_size, 0);
215
216 expect_function_call(__wrap_ocf_part_is_valid);
217 will_return(__wrap_ocf_part_is_valid, 0);
218
219 expect_function_call(__wrap_ocf_part_set_valid);
220 expect_in_range(__wrap_ocf_part_set_valid, id, OCF_IO_CLASS_ID_MIN,
221 OCF_IO_CLASS_ID_MAX);
222 expect_value(__wrap_ocf_part_set_valid, valid, true);
223
224 expect_function_call(__wrap_ocf_part_set_prio);
225 }
226
227 expect_function_call(__wrap_ocf_part_sort);
228
229 result = ocf_mngt_cache_io_classes_configure(&cache, &cfg);
230
231 assert_int_equal(result, 0);
232
233 for (i = 0; i < OCF_IO_CLASS_MAX; i++)
234 test_free(cache.user_parts[i].config);
235 }
236
237 static void ocf_mngt_io_classes_configure_test01(void **state)
238 {
239 struct ocf_cache cache;
240 struct ocf_mngt_io_classes_config cfg[OCF_IO_CLASS_MAX];
241 int error_code = -OCF_ERR_INVAL;
242 int result;
243
244 print_test_description("Invalid config - "
245 "termination with error");
246
247 expect_function_call(__wrap__ocf_mngt_io_class_validate_cfg);
248 will_return(__wrap__ocf_mngt_io_class_validate_cfg, error_code);
249
250 result = ocf_mngt_cache_io_classes_configure(&cache, &cfg);
251
252 assert_int_equal(result, error_code);
253 }
254
255 int main(void)
256 {
257 const struct CMUnitTest tests[] = {
258 cmocka_unit_test(ocf_mngt_io_classes_configure_test01),
259 cmocka_unit_test(ocf_mngt_io_classes_configure_test02),
260 cmocka_unit_test(ocf_mngt_io_classes_configure_test03)
261 };
262
263 print_message("Unit test of src/mngt/ocf_mngt_io_class.c");
264
265 return cmocka_run_group_tests(tests, NULL, NULL);
266 }