Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kunit: Add ability to filter attributes

Add filtering of test attributes. Users can filter tests using the
module_param called "filter".

Filters are imputed in the format: <attribute_name><operation><value>

Example: kunit.filter="speed>slow"

Operations include: >, <, >=, <=, !=, and =. These operations will act the
same for attributes of the same type but may not between types.

Note multiple filters can be inputted by separating them with a comma.
Example: kunit.filter="speed=slow, module!=example"

Since both suites and test cases can have attributes, there may be
conflicts. The process of filtering follows these rules:
- Filtering always operates at a per-test level.
- If a test has an attribute set, then the test's value is filtered on.
- Otherwise, the value falls back to the suite's value.
- If neither are set, the attribute has a global "default" value, which
is used.

Filtered tests will not be run or show in output. The tests can instead be
skipped using the configurable option "kunit.filter_action=skip".

Note the default settings for running tests remains unfiltered.

Finally, add "filter" methods for the speed and module attributes to parse
and compare attribute values.

Note this filtering functionality will be added to kunit.py in the next
patch.

Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Rae Moar <rmoar@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Rae Moar and committed by
Shuah Khan
529534e8 a00a7270

+389 -28
+31
include/kunit/attributes.h
··· 10 10 #define _KUNIT_ATTRIBUTES_H 11 11 12 12 /* 13 + * struct kunit_attr_filter - representation of attributes filter with the 14 + * attribute object and string input 15 + */ 16 + struct kunit_attr_filter { 17 + struct kunit_attr *attr; 18 + char *input; 19 + }; 20 + 21 + /* 22 + * Returns the name of the filter's attribute. 23 + */ 24 + const char *kunit_attr_filter_name(struct kunit_attr_filter filter); 25 + 26 + /* 13 27 * Print all test attributes for a test case or suite. 14 28 * Output format for test cases: "# <test_name>.<attribute>: <value>" 15 29 * Output format for test suites: "# <attribute>: <value>" 16 30 */ 17 31 void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level); 32 + 33 + /* 34 + * Returns the number of fitlers in input. 35 + */ 36 + int kunit_get_filter_count(char *input); 37 + 38 + /* 39 + * Parse attributes filter input and return an objects containing the 40 + * attribute object and the string input of the next filter. 41 + */ 42 + struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err); 43 + 44 + /* 45 + * Returns a copy of the suite containing only tests that pass the filter. 46 + */ 47 + struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suite, 48 + struct kunit_attr_filter filter, char *action, int *err); 18 49 19 50 #endif /* _KUNIT_ATTRIBUTES_H */
+271
lib/kunit/attributes.c
··· 67 67 return (char *) attr; 68 68 } 69 69 70 + /* Filter Methods */ 71 + 72 + static const char op_list[] = "<>!="; 73 + 74 + /* 75 + * Returns whether the inputted integer value matches the filter given 76 + * by the operation string and inputted integer. 77 + */ 78 + static int int_filter(long val, const char *op, int input, int *err) 79 + { 80 + if (!strncmp(op, "<=", 2)) 81 + return (val <= input); 82 + else if (!strncmp(op, ">=", 2)) 83 + return (val >= input); 84 + else if (!strncmp(op, "!=", 2)) 85 + return (val != input); 86 + else if (!strncmp(op, ">", 1)) 87 + return (val > input); 88 + else if (!strncmp(op, "<", 1)) 89 + return (val < input); 90 + else if (!strncmp(op, "=", 1)) 91 + return (val == input); 92 + *err = -EINVAL; 93 + pr_err("kunit executor: invalid filter operation: %s\n", op); 94 + return false; 95 + } 96 + 97 + /* 98 + * Returns whether the inputted enum value "attr" matches the filter given 99 + * by the input string. Note: the str_list includes the corresponding string 100 + * list to the enum values. 101 + */ 102 + static int attr_enum_filter(void *attr, const char *input, int *err, 103 + const char * const str_list[], int max) 104 + { 105 + int i, j, input_int; 106 + long test_val = (long)attr; 107 + const char *input_val = NULL; 108 + 109 + for (i = 0; input[i]; i++) { 110 + if (!strchr(op_list, input[i])) { 111 + input_val = input + i; 112 + break; 113 + } 114 + } 115 + 116 + if (!input_val) { 117 + *err = -EINVAL; 118 + pr_err("kunit executor: filter value not found: %s\n", input); 119 + return false; 120 + } 121 + 122 + for (j = 0; j <= max; j++) { 123 + if (!strcmp(input_val, str_list[j])) 124 + input_int = j; 125 + } 126 + 127 + if (!input_int) { 128 + *err = -EINVAL; 129 + pr_err("kunit executor: invalid filter input: %s\n", input); 130 + return false; 131 + } 132 + 133 + return int_filter(test_val, input, input_int, err); 134 + } 135 + 136 + static int attr_speed_filter(void *attr, const char *input, int *err) 137 + { 138 + return attr_enum_filter(attr, input, err, speed_str_list, KUNIT_SPEED_MAX); 139 + } 140 + 141 + /* 142 + * Returns whether the inputted string value (attr) matches the filter given 143 + * by the input string. 144 + */ 145 + static int attr_string_filter(void *attr, const char *input, int *err) 146 + { 147 + char *str = attr; 148 + 149 + if (!strncmp(input, "<", 1)) { 150 + *err = -EINVAL; 151 + pr_err("kunit executor: invalid filter input: %s\n", input); 152 + return false; 153 + } else if (!strncmp(input, ">", 1)) { 154 + *err = -EINVAL; 155 + pr_err("kunit executor: invalid filter input: %s\n", input); 156 + return false; 157 + } else if (!strncmp(input, "!=", 2)) { 158 + return (strcmp(input + 2, str) != 0); 159 + } else if (!strncmp(input, "=", 1)) { 160 + return (strcmp(input + 1, str) == 0); 161 + } 162 + *err = -EINVAL; 163 + pr_err("kunit executor: invalid filter operation: %s\n", input); 164 + return false; 165 + } 166 + 167 + 70 168 /* Get Attribute Methods */ 71 169 72 170 static void *attr_speed_get(void *test_or_suite, bool is_test) ··· 197 99 .name = "speed", 198 100 .get_attr = attr_speed_get, 199 101 .to_string = attr_speed_to_string, 102 + .filter = attr_speed_filter, 200 103 .attr_default = (void *)KUNIT_SPEED_NORMAL, 201 104 .print = PRINT_ALWAYS, 202 105 }, ··· 205 106 .name = "module", 206 107 .get_attr = attr_module_get, 207 108 .to_string = attr_string_to_string, 109 + .filter = attr_string_filter, 208 110 .attr_default = (void *)"", 209 111 .print = PRINT_SUITE, 210 112 } 211 113 }; 212 114 213 115 /* Helper Functions to Access Attributes */ 116 + 117 + const char *kunit_attr_filter_name(struct kunit_attr_filter filter) 118 + { 119 + return filter.attr->name; 120 + } 214 121 215 122 void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level) 216 123 { ··· 249 144 kfree(attr_str); 250 145 } 251 146 } 147 + } 148 + 149 + /* Helper Functions to Filter Attributes */ 150 + 151 + int kunit_get_filter_count(char *input) 152 + { 153 + int i, comma_index, count = 0; 154 + 155 + for (i = 0; input[i]; i++) { 156 + if (input[i] == ',') { 157 + if ((i - comma_index) > 1) 158 + count++; 159 + comma_index = i; 160 + } 161 + } 162 + if ((i - comma_index) > 0) 163 + count++; 164 + return count; 165 + } 166 + 167 + struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err) 168 + { 169 + struct kunit_attr_filter filter = {}; 170 + int i, j, comma_index, new_start_index; 171 + int op_index = -1, attr_index = -1; 172 + char op; 173 + char *input = *filters; 174 + 175 + /* Parse input until operation */ 176 + for (i = 0; input[i]; i++) { 177 + if (op_index < 0 && strchr(op_list, input[i])) { 178 + op_index = i; 179 + } else if (!comma_index && input[i] == ',') { 180 + comma_index = i; 181 + } else if (comma_index && input[i] != ' ') { 182 + new_start_index = i; 183 + break; 184 + } 185 + } 186 + 187 + if (op_index <= 0) { 188 + *err = -EINVAL; 189 + pr_err("kunit executor: filter operation not found: %s\n", input); 190 + return filter; 191 + } 192 + 193 + /* Temporarily set operator to \0 character. */ 194 + op = input[op_index]; 195 + input[op_index] = '\0'; 196 + 197 + /* Find associated kunit_attr object */ 198 + for (j = 0; j < ARRAY_SIZE(kunit_attr_list); j++) { 199 + if (!strcmp(input, kunit_attr_list[j].name)) { 200 + attr_index = j; 201 + break; 202 + } 203 + } 204 + 205 + input[op_index] = op; 206 + 207 + if (attr_index < 0) { 208 + *err = -EINVAL; 209 + pr_err("kunit executor: attribute not found: %s\n", input); 210 + } else { 211 + filter.attr = &kunit_attr_list[attr_index]; 212 + } 213 + 214 + if (comma_index) { 215 + input[comma_index] = '\0'; 216 + filter.input = input + op_index; 217 + input = input + new_start_index; 218 + } else { 219 + filter.input = input + op_index; 220 + input = NULL; 221 + } 222 + 223 + *filters = input; 224 + 225 + return filter; 226 + } 227 + 228 + struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suite, 229 + struct kunit_attr_filter filter, char *action, int *err) 230 + { 231 + int n = 0; 232 + struct kunit_case *filtered, *test_case; 233 + struct kunit_suite *copy; 234 + void *suite_val, *test_val; 235 + bool suite_result, test_result, default_result, result; 236 + 237 + /* Allocate memory for new copy of suite and list of test cases */ 238 + copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); 239 + if (!copy) 240 + return ERR_PTR(-ENOMEM); 241 + 242 + kunit_suite_for_each_test_case(suite, test_case) { n++; } 243 + 244 + filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); 245 + if (!filtered) { 246 + kfree(copy); 247 + return ERR_PTR(-ENOMEM); 248 + } 249 + 250 + n = 0; 251 + 252 + /* Save filtering result on default value */ 253 + default_result = filter.attr->filter(filter.attr->attr_default, filter.input, err); 254 + if (*err) { 255 + kfree(copy); 256 + kfree(filtered); 257 + return NULL; 258 + } 259 + 260 + /* Save suite attribute value and filtering result on that value */ 261 + suite_val = filter.attr->get_attr((void *)suite, false); 262 + suite_result = filter.attr->filter(suite_val, filter.input, err); 263 + if (*err) { 264 + kfree(copy); 265 + kfree(filtered); 266 + return NULL; 267 + } 268 + 269 + /* For each test case, save test case if passes filtering. */ 270 + kunit_suite_for_each_test_case(suite, test_case) { 271 + test_val = filter.attr->get_attr((void *) test_case, true); 272 + test_result = filter.attr->filter(filter.attr->get_attr(test_case, true), 273 + filter.input, err); 274 + if (*err) { 275 + kfree(copy); 276 + kfree(filtered); 277 + return NULL; 278 + } 279 + 280 + /* 281 + * If attribute value of test case is set, filter on that value. 282 + * If not, filter on suite value if set. If not, filter on 283 + * default value. 284 + */ 285 + result = false; 286 + if (test_val) { 287 + if (test_result) 288 + result = true; 289 + } else if (suite_val) { 290 + if (suite_result) 291 + result = true; 292 + } else if (default_result) { 293 + result = true; 294 + } 295 + 296 + if (result) { 297 + filtered[n++] = *test_case; 298 + } else if (action && strcmp(action, "skip") == 0) { 299 + test_case->status = KUNIT_SKIPPED; 300 + filtered[n++] = *test_case; 301 + } 302 + } 303 + 304 + if (n == 0) { 305 + kfree(copy); 306 + kfree(filtered); 307 + return NULL; 308 + } 309 + 310 + copy->test_cases = filtered; 311 + 312 + return copy; 252 313 }
+74 -19
lib/kunit/executor.c
··· 17 17 18 18 static char *filter_glob_param; 19 19 static char *action_param; 20 + static char *filter_param; 21 + static char *filter_action_param; 20 22 21 23 module_param_named(filter_glob, filter_glob_param, charp, 0); 22 24 MODULE_PARM_DESC(filter_glob, ··· 29 27 "<none>: run the tests like normal\n" 30 28 "'list' to list test names instead of running them.\n" 31 29 "'list_attr' to list test names and attributes instead of running them.\n"); 30 + module_param_named(filter, filter_param, charp, 0); 31 + MODULE_PARM_DESC(filter, 32 + "Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow"); 33 + module_param_named(filter_action, filter_action_param, charp, 0); 34 + MODULE_PARM_DESC(filter_action, 35 + "Changes behavior of filtered tests using attributes, valid values are:\n" 36 + "<none>: do not run filtered tests as normal\n" 37 + "'skip': skip all filtered tests instead so tests will appear in output\n"); 32 38 33 39 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */ 34 - struct kunit_test_filter { 40 + struct kunit_glob_filter { 35 41 char *suite_glob; 36 42 char *test_glob; 37 43 }; 38 44 39 45 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */ 40 - static void kunit_parse_filter_glob(struct kunit_test_filter *parsed, 46 + static void kunit_parse_glob_filter(struct kunit_glob_filter *parsed, 41 47 const char *filter_glob) 42 48 { 43 49 const int len = strlen(filter_glob); ··· 67 57 68 58 /* Create a copy of suite with only tests that match test_glob. */ 69 59 static struct kunit_suite * 70 - kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob) 60 + kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob) 71 61 { 72 62 int n = 0; 73 63 struct kunit_case *filtered, *test_case; ··· 121 111 122 112 static struct suite_set kunit_filter_suites(const struct suite_set *suite_set, 123 113 const char *filter_glob, 114 + char *filters, 115 + char *filter_action, 124 116 int *err) 125 117 { 126 - int i; 127 - struct kunit_suite **copy, *filtered_suite; 118 + int i, j, k, filter_count; 119 + struct kunit_suite **copy, *filtered_suite, *new_filtered_suite; 128 120 struct suite_set filtered; 129 - struct kunit_test_filter filter; 121 + struct kunit_glob_filter parsed_glob; 122 + struct kunit_attr_filter *parsed_filters; 130 123 131 124 const size_t max = suite_set->end - suite_set->start; 132 125 ··· 140 127 return filtered; 141 128 } 142 129 143 - kunit_parse_filter_glob(&filter, filter_glob); 130 + if (filter_glob) 131 + kunit_parse_glob_filter(&parsed_glob, filter_glob); 132 + 133 + /* Parse attribute filters */ 134 + if (filters) { 135 + filter_count = kunit_get_filter_count(filters); 136 + parsed_filters = kcalloc(filter_count + 1, sizeof(*parsed_filters), GFP_KERNEL); 137 + for (j = 0; j < filter_count; j++) 138 + parsed_filters[j] = kunit_next_attr_filter(&filters, err); 139 + if (*err) 140 + return filtered; 141 + } 144 142 145 143 for (i = 0; &suite_set->start[i] != suite_set->end; i++) { 146 - if (!glob_match(filter.suite_glob, suite_set->start[i]->name)) 147 - continue; 148 - 149 - filtered_suite = kunit_filter_tests(suite_set->start[i], filter.test_glob); 150 - if (IS_ERR(filtered_suite)) { 151 - *err = PTR_ERR(filtered_suite); 152 - return filtered; 144 + filtered_suite = suite_set->start[i]; 145 + if (filter_glob) { 146 + if (!glob_match(parsed_glob.suite_glob, filtered_suite->name)) 147 + continue; 148 + filtered_suite = kunit_filter_glob_tests(filtered_suite, 149 + parsed_glob.test_glob); 150 + if (IS_ERR(filtered_suite)) { 151 + *err = PTR_ERR(filtered_suite); 152 + return filtered; 153 + } 153 154 } 155 + if (filter_count) { 156 + for (k = 0; k < filter_count; k++) { 157 + new_filtered_suite = kunit_filter_attr_tests(filtered_suite, 158 + parsed_filters[k], filter_action, err); 159 + 160 + /* Free previous copy of suite */ 161 + if (k > 0 || filter_glob) 162 + kfree(filtered_suite); 163 + filtered_suite = new_filtered_suite; 164 + 165 + if (*err) 166 + return filtered; 167 + if (IS_ERR(filtered_suite)) { 168 + *err = PTR_ERR(filtered_suite); 169 + return filtered; 170 + } 171 + if (!filtered_suite) 172 + break; 173 + } 174 + } 175 + 154 176 if (!filtered_suite) 155 177 continue; 156 178 ··· 193 145 } 194 146 filtered.end = copy; 195 147 196 - kfree(filter.suite_glob); 197 - kfree(filter.test_glob); 148 + if (filter_glob) { 149 + kfree(parsed_glob.suite_glob); 150 + kfree(parsed_glob.test_glob); 151 + } 152 + 153 + if (filter_count) 154 + kfree(parsed_filters); 155 + 198 156 return filtered; 199 157 } 200 158 ··· 260 206 goto out; 261 207 } 262 208 263 - if (filter_glob_param) { 264 - suite_set = kunit_filter_suites(&suite_set, filter_glob_param, &err); 209 + if (filter_glob_param || filter_param) { 210 + suite_set = kunit_filter_suites(&suite_set, filter_glob_param, 211 + filter_param, filter_action_param, &err); 265 212 if (err) { 266 213 pr_err("kunit executor: error filtering suites: %d\n", err); 267 214 goto out; ··· 278 223 else 279 224 pr_err("kunit executor: unknown action '%s'\n", action_param); 280 225 281 - if (filter_glob_param) { /* a copy was made of each suite */ 226 + if (filter_glob_param || filter_param) { /* a copy was made of each suite */ 282 227 kunit_free_suite_set(suite_set); 283 228 } 284 229
+6 -6
lib/kunit/executor_test.c
··· 24 24 25 25 static void parse_filter_test(struct kunit *test) 26 26 { 27 - struct kunit_test_filter filter = {NULL, NULL}; 27 + struct kunit_glob_filter filter = {NULL, NULL}; 28 28 29 - kunit_parse_filter_glob(&filter, "suite"); 29 + kunit_parse_glob_filter(&filter, "suite"); 30 30 KUNIT_EXPECT_STREQ(test, filter.suite_glob, "suite"); 31 31 KUNIT_EXPECT_FALSE(test, filter.test_glob); 32 32 kfree(filter.suite_glob); 33 33 kfree(filter.test_glob); 34 34 35 - kunit_parse_filter_glob(&filter, "suite.test"); 35 + kunit_parse_glob_filter(&filter, "suite.test"); 36 36 KUNIT_EXPECT_STREQ(test, filter.suite_glob, "suite"); 37 37 KUNIT_EXPECT_STREQ(test, filter.test_glob, "test"); 38 38 kfree(filter.suite_glob); ··· 50 50 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases); 51 51 52 52 /* Want: suite1, suite2, NULL -> suite2, NULL */ 53 - got = kunit_filter_suites(&suite_set, "suite2", &err); 53 + got = kunit_filter_suites(&suite_set, "suite2", NULL, NULL, &err); 54 54 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start); 55 55 KUNIT_ASSERT_EQ(test, err, 0); 56 56 kfree_at_end(test, got.start); ··· 74 74 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases); 75 75 76 76 /* Want: suite1, suite2, NULL -> suite2 (just test1), NULL */ 77 - got = kunit_filter_suites(&suite_set, "suite2.test2", &err); 77 + got = kunit_filter_suites(&suite_set, "suite2.test2", NULL, NULL, &err); 78 78 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start); 79 79 KUNIT_ASSERT_EQ(test, err, 0); 80 80 kfree_at_end(test, got.start); ··· 100 100 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases); 101 101 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases); 102 102 103 - got = kunit_filter_suites(&suite_set, "not_found", &err); 103 + got = kunit_filter_suites(&suite_set, "not_found", NULL, NULL, &err); 104 104 KUNIT_ASSERT_EQ(test, err, 0); 105 105 kfree_at_end(test, got.start); /* just in case */ 106 106
+7 -3
lib/kunit/test.c
··· 613 613 kunit_suite_for_each_test_case(suite, test_case) { 614 614 struct kunit test = { .param_value = NULL, .param_index = 0 }; 615 615 struct kunit_result_stats param_stats = { 0 }; 616 - test_case->status = KUNIT_SKIPPED; 617 616 618 617 kunit_init_test(&test, test_case->name, test_case->log); 619 - 620 - if (!test_case->generate_params) { 618 + if (test_case->status == KUNIT_SKIPPED) { 619 + /* Test marked as skip */ 620 + test.status = KUNIT_SKIPPED; 621 + kunit_update_stats(&param_stats, test.status); 622 + } else if (!test_case->generate_params) { 621 623 /* Non-parameterised test. */ 624 + test_case->status = KUNIT_SKIPPED; 622 625 kunit_run_case_catch_errors(suite, test_case, &test); 623 626 kunit_update_stats(&param_stats, test.status); 624 627 } else { 625 628 /* Get initial param. */ 626 629 param_desc[0] = '\0'; 627 630 test.param_value = test_case->generate_params(NULL, param_desc); 631 + test_case->status = KUNIT_SKIPPED; 628 632 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT 629 633 "KTAP version 1\n"); 630 634 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT