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

kunit: Add speed attribute

Add speed attribute to the test attribute API. This attribute will allow
users to mark tests with a category of speed.

Currently the categories of speed proposed are: normal, slow, and very_slow
(outlined in enum kunit_speed). These are outlined in the enum kunit_speed.

The assumed default speed for tests is "normal". This indicates that the
test takes a relatively trivial amount of time (less than 1 second),
regardless of the machine it is running on. Any test slower than this could
be marked as "slow" or "very_slow".

Add the macro KUNIT_CASE_SLOW to set a test as slow, as this is likely a
common use of the attributes API.

Add an example of marking a slow test to kunit-example-test.c.

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
02c2d0c2 39e92cb1

+83 -2
+31 -1
include/kunit/test.h
··· 63 63 KUNIT_SKIPPED, 64 64 }; 65 65 66 + /* Attribute struct/enum definitions */ 67 + 68 + /* 69 + * Speed Attribute is stored as an enum and separated into categories of 70 + * speed: very_slowm, slow, and normal. These speeds are relative to 71 + * other KUnit tests. 72 + * 73 + * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL. 74 + */ 75 + enum kunit_speed { 76 + KUNIT_SPEED_UNSET, 77 + KUNIT_SPEED_VERY_SLOW, 78 + KUNIT_SPEED_SLOW, 79 + KUNIT_SPEED_NORMAL, 80 + KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL, 81 + }; 82 + 66 83 /* Holds attributes for each test case and suite */ 67 - struct kunit_attributes {}; 84 + struct kunit_attributes { 85 + enum kunit_speed speed; 86 + }; 68 87 69 88 /** 70 89 * struct kunit_case - represents an individual test case. ··· 168 149 #define KUNIT_CASE_ATTR(test_name, attributes) \ 169 150 { .run_case = test_name, .name = #test_name, \ 170 151 .attr = attributes } 152 + 153 + /** 154 + * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case 155 + * with the slow attribute 156 + * 157 + * @test_name: a reference to a test case function. 158 + */ 159 + 160 + #define KUNIT_CASE_SLOW(test_name) \ 161 + { .run_case = test_name, .name = #test_name, \ 162 + .attr.speed = KUNIT_SPEED_SLOW } 171 163 172 164 /** 173 165 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
+43 -1
lib/kunit/attributes.c
··· 40 40 enum print_ops print; 41 41 }; 42 42 43 + /* String Lists for enum Attributes */ 44 + 45 + static const char * const speed_str_list[] = {"unset", "very_slow", "slow", "normal"}; 46 + 47 + /* To String Methods */ 48 + 49 + static const char *attr_enum_to_string(void *attr, const char * const str_list[], bool *to_free) 50 + { 51 + long val = (long)attr; 52 + 53 + *to_free = false; 54 + if (!val) 55 + return NULL; 56 + return str_list[val]; 57 + } 58 + 59 + static const char *attr_speed_to_string(void *attr, bool *to_free) 60 + { 61 + return attr_enum_to_string(attr, speed_str_list, to_free); 62 + } 63 + 64 + /* Get Attribute Methods */ 65 + 66 + static void *attr_speed_get(void *test_or_suite, bool is_test) 67 + { 68 + struct kunit_suite *suite = is_test ? NULL : test_or_suite; 69 + struct kunit_case *test = is_test ? test_or_suite : NULL; 70 + 71 + if (test) 72 + return ((void *) test->attr.speed); 73 + else 74 + return ((void *) suite->attr.speed); 75 + } 76 + 43 77 /* List of all Test Attributes */ 44 78 45 - static struct kunit_attr kunit_attr_list[] = {}; 79 + static struct kunit_attr kunit_attr_list[] = { 80 + { 81 + .name = "speed", 82 + .get_attr = attr_speed_get, 83 + .to_string = attr_speed_to_string, 84 + .attr_default = (void *)KUNIT_SPEED_NORMAL, 85 + .print = PRINT_ALWAYS, 86 + }, 87 + }; 46 88 47 89 /* Helper Functions to Access Attributes */ 48 90
+9
lib/kunit/kunit-example-test.c
··· 221 221 } 222 222 223 223 /* 224 + * This test should always pass. Can be used to practice filtering attributes. 225 + */ 226 + static void example_slow_test(struct kunit *test) 227 + { 228 + KUNIT_EXPECT_EQ(test, 1 + 1, 2); 229 + } 230 + 231 + /* 224 232 * Here we make a list of all the test cases we want to add to the test suite 225 233 * below. 226 234 */ ··· 245 237 KUNIT_CASE(example_all_expect_macros_test), 246 238 KUNIT_CASE(example_static_stub_test), 247 239 KUNIT_CASE_PARAM(example_params_test, example_gen_params), 240 + KUNIT_CASE_SLOW(example_slow_test), 248 241 {} 249 242 }; 250 243