2 * Copyright(c) 2012-2018 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
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>
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>
27 #include "print_desc.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"
38 /* Mocks reqired for compilation */
39 int __wrap_ocf_log_raw(const struct ocf_logger
*logger
, ocf_logger_lvl_t lvl
,
44 ocf_ctx_t
__wrap_ocf_cache_get_ctx(ocf_cache_t cache
)
48 char *__wrap_ocf_cache_get_name(ocf_cache_t cache
)
52 int __wrap_ocf_mngt_cache_lock(ocf_cache_t cache
)
57 void __wrap_ocf_mngt_cache_unlock(ocf_cache_t cache
)
61 void __wrap_ocf_metadata_lock(struct ocf_cache
*cache
, int rw
)
65 void __wrap_ocf_metadata_unlock(struct ocf_cache
*cache
, int rw
)
69 /* Functions mocked for testing purposes */
70 bool __wrap_ocf_part_is_added(struct ocf_user_part
*part
)
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
)
83 void __wrap_ocf_part_set_prio(struct ocf_cache
*cache
,
84 struct ocf_user_part
*part
, int16_t prio
)
89 bool __wrap_ocf_part_is_valid(struct ocf_user_part
*part
)
96 void __wrap_ocf_part_set_valid(struct ocf_cache
*cache
, ocf_part_id_t id
,
100 check_expected(valid
);
104 int __wrap__ocf_mngt_io_class_validate_cfg(ocf_cache_t cache
,
105 const struct ocf_mngt_io_class_config
*cfg
)
111 void __wrap_ocf_part_sort(struct ocf_cache
*cache
)
116 int __wrap_ocf_metadata_flush_superblock(struct ocf_cache
*cache
)
120 /* Helper function for test prepration */
121 static inline void setup_valid_config(struct ocf_mngt_io_class_config
*cfg
,
125 for (i
= 0; i
< OCF_IO_CLASS_MAX
; i
++) {
127 cfg
[i
].name
= remove
? NULL
: "test_io_class_name" ;
129 cfg
[i
].cache_mode
= ocf_cache_mode_pt
;
130 cfg
[i
].min_size
= 2*i
;
131 cfg
[i
].max_size
= 20*i
;
135 static void ocf_mngt_io_classes_configure_test03(void **state
)
137 struct ocf_cache cache
= {0};
138 struct ocf_mngt_io_classes_config cfg
= {0};
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
));
147 setup_valid_config(cfg
.config
, true);
149 print_test_description("Remove all io classes");
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);
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);
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);
168 expect_function_call(__wrap_ocf_part_sort
);
170 result
= ocf_mngt_cache_io_classes_configure(&cache
, &cfg
);
172 assert_int_equal(result
, 0);
174 for (i
= 0; i
< OCF_IO_CLASS_MAX
; i
++)
175 test_free(cache
.user_parts
[i
].config
);
178 static void ocf_mngt_io_classes_configure_test02(void **state
)
180 struct ocf_cache cache
= {0};
181 struct ocf_mngt_io_classes_config cfg
= {0};
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
));
190 setup_valid_config(cfg
.config
, false);
192 print_test_description("Configure all possible io classes");
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);
199 /* Configure default io_class */
200 expect_function_call(__wrap_ocf_part_is_added
);
201 will_return(__wrap_ocf_part_is_added
, 1);
203 expect_function_call(__wrap__ocf_mngt_set_partition_size
);
204 will_return(__wrap__ocf_mngt_set_partition_size
, 0);
206 expect_function_call(__wrap_ocf_part_set_prio
);
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);
213 expect_function_call(__wrap__ocf_mngt_set_partition_size
);
214 will_return(__wrap__ocf_mngt_set_partition_size
, 0);
216 expect_function_call(__wrap_ocf_part_is_valid
);
217 will_return(__wrap_ocf_part_is_valid
, 0);
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);
224 expect_function_call(__wrap_ocf_part_set_prio
);
227 expect_function_call(__wrap_ocf_part_sort
);
229 result
= ocf_mngt_cache_io_classes_configure(&cache
, &cfg
);
231 assert_int_equal(result
, 0);
233 for (i
= 0; i
< OCF_IO_CLASS_MAX
; i
++)
234 test_free(cache
.user_parts
[i
].config
);
237 static void ocf_mngt_io_classes_configure_test01(void **state
)
239 struct ocf_cache cache
;
240 struct ocf_mngt_io_classes_config cfg
[OCF_IO_CLASS_MAX
];
241 int error_code
= -OCF_ERR_INVAL
;
244 print_test_description("Invalid config - "
245 "termination with error");
247 expect_function_call(__wrap__ocf_mngt_io_class_validate_cfg
);
248 will_return(__wrap__ocf_mngt_io_class_validate_cfg
, error_code
);
250 result
= ocf_mngt_cache_io_classes_configure(&cache
, &cfg
);
252 assert_int_equal(result
, error_code
);
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
)
263 print_message("Unit test of src/mngt/ocf_mngt_io_class.c");
265 return cmocka_run_group_tests(tests
, NULL
, NULL
);