diff options
Diffstat (limited to 'include/kunit')
-rw-r--r-- | include/kunit/attributes.h | 50 | ||||
-rw-r--r-- | include/kunit/static_stub.h | 6 | ||||
-rw-r--r-- | include/kunit/test-bug.h | 2 | ||||
-rw-r--r-- | include/kunit/test.h | 91 |
4 files changed, 144 insertions, 5 deletions
diff --git a/include/kunit/attributes.h b/include/kunit/attributes.h new file mode 100644 index 000000000000..bc76a0b786d2 --- /dev/null +++ b/include/kunit/attributes.h @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KUnit API to save and access test attributes + * + * Copyright (C) 2023, Google LLC. + * Author: Rae Moar <rmoar@google.com> + */ + +#ifndef _KUNIT_ATTRIBUTES_H +#define _KUNIT_ATTRIBUTES_H + +/* + * struct kunit_attr_filter - representation of attributes filter with the + * attribute object and string input + */ +struct kunit_attr_filter { + struct kunit_attr *attr; + char *input; +}; + +/* + * Returns the name of the filter's attribute. + */ +const char *kunit_attr_filter_name(struct kunit_attr_filter filter); + +/* + * Print all test attributes for a test case or suite. + * Output format for test cases: "# <test_name>.<attribute>: <value>" + * Output format for test suites: "# <attribute>: <value>" + */ +void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level); + +/* + * Returns the number of fitlers in input. + */ +int kunit_get_filter_count(char *input); + +/* + * Parse attributes filter input and return an objects containing the + * attribute object and the string input of the next filter. + */ +struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err); + +/* + * Returns a copy of the suite containing only tests that pass the filter. + */ +struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suite, + struct kunit_attr_filter filter, char *action, int *err); + +#endif /* _KUNIT_ATTRIBUTES_H */ diff --git a/include/kunit/static_stub.h b/include/kunit/static_stub.h index 9b80150a5d62..85315c80b303 100644 --- a/include/kunit/static_stub.h +++ b/include/kunit/static_stub.h @@ -11,7 +11,7 @@ #if !IS_ENABLED(CONFIG_KUNIT) /* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ -#define KUNIT_TRIGGER_STATIC_STUB(real_fn_name, args...) do {} while (0) +#define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) #else @@ -30,7 +30,7 @@ * This is a function prologue which is used to allow calls to the current * function to be redirected by a KUnit test. KUnit tests can call * kunit_activate_static_stub() to pass a replacement function in. The - * replacement function will be called by KUNIT_TRIGGER_STATIC_STUB(), which + * replacement function will be called by KUNIT_STATIC_STUB_REDIRECT(), which * will then return from the function. If the caller is not in a KUnit context, * the function will continue execution as normal. * @@ -87,7 +87,7 @@ void __kunit_activate_static_stub(struct kunit *test, * When activated, calls to real_fn_addr from within this test (even if called * indirectly) will instead call replacement_addr. The function pointed to by * real_fn_addr must begin with the static stub prologue in - * KUNIT_TRIGGER_STATIC_STUB() for this to work. real_fn_addr and + * KUNIT_STATIC_STUB_REDIRECT() for this to work. real_fn_addr and * replacement_addr must have the same type. * * The redirection can be disabled again with kunit_deactivate_static_stub(). diff --git a/include/kunit/test-bug.h b/include/kunit/test-bug.h index 30ca541b6ff2..47aa8f21ccce 100644 --- a/include/kunit/test-bug.h +++ b/include/kunit/test-bug.h @@ -9,6 +9,8 @@ #ifndef _KUNIT_TEST_BUG_H #define _KUNIT_TEST_BUG_H +#include <linux/stddef.h> /* for NULL */ + #if IS_ENABLED(CONFIG_KUNIT) #include <linux/jump_label.h> /* For static branch */ diff --git a/include/kunit/test.h b/include/kunit/test.h index 107c81431634..68ff01aee244 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -64,12 +64,35 @@ enum kunit_status { KUNIT_SKIPPED, }; +/* Attribute struct/enum definitions */ + +/* + * Speed Attribute is stored as an enum and separated into categories of + * speed: very_slowm, slow, and normal. These speeds are relative to + * other KUnit tests. + * + * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL. + */ +enum kunit_speed { + KUNIT_SPEED_UNSET, + KUNIT_SPEED_VERY_SLOW, + KUNIT_SPEED_SLOW, + KUNIT_SPEED_NORMAL, + KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL, +}; + +/* Holds attributes for each test case and suite */ +struct kunit_attributes { + enum kunit_speed speed; +}; + /** * struct kunit_case - represents an individual test case. * * @run_case: the function representing the actual test case. * @name: the name of the test case. * @generate_params: the generator function for parameterized tests. + * @attr: the attributes associated with the test * * A test case is a function with the signature, * ``void (*)(struct kunit *)`` @@ -105,9 +128,11 @@ struct kunit_case { void (*run_case)(struct kunit *test); const char *name; const void* (*generate_params)(const void *prev, char *desc); + struct kunit_attributes attr; /* private: internal use only. */ enum kunit_status status; + char *module_name; char *log; }; @@ -132,7 +157,32 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) * &struct kunit_case object from it. See the documentation for * &struct kunit_case for an example on how to use it. */ -#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } +#define KUNIT_CASE(test_name) \ + { .run_case = test_name, .name = #test_name, \ + .module_name = KBUILD_MODNAME} + +/** + * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case + * with attributes + * + * @test_name: a reference to a test case function. + * @attributes: a reference to a struct kunit_attributes object containing + * test attributes + */ +#define KUNIT_CASE_ATTR(test_name, attributes) \ + { .run_case = test_name, .name = #test_name, \ + .attr = attributes, .module_name = KBUILD_MODNAME} + +/** + * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case + * with the slow attribute + * + * @test_name: a reference to a test case function. + */ + +#define KUNIT_CASE_SLOW(test_name) \ + { .run_case = test_name, .name = #test_name, \ + .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME} /** * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case @@ -153,7 +203,21 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) */ #define KUNIT_CASE_PARAM(test_name, gen_params) \ { .run_case = test_name, .name = #test_name, \ - .generate_params = gen_params } + .generate_params = gen_params, .module_name = KBUILD_MODNAME} + +/** + * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct + * kunit_case with attributes + * + * @test_name: a reference to a test case function. + * @gen_params: a reference to a parameter generator function. + * @attributes: a reference to a struct kunit_attributes object containing + * test attributes + */ +#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \ + { .run_case = test_name, .name = #test_name, \ + .generate_params = gen_params, \ + .attr = attributes, .module_name = KBUILD_MODNAME} /** * struct kunit_suite - describes a related collection of &struct kunit_case @@ -164,6 +228,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) * @init: called before every test case. * @exit: called after every test case. * @test_cases: a null terminated array of test cases. + * @attr: the attributes associated with the test suite * * A kunit_suite is a collection of related &struct kunit_case s, such that * @init is called before every test case and @exit is called after every @@ -183,6 +248,7 @@ struct kunit_suite { int (*init)(struct kunit *test); void (*exit)(struct kunit *test); struct kunit_case *test_cases; + struct kunit_attributes attr; /* private: internal use only */ char status_comment[KUNIT_STATUS_COMMENT_SIZE]; @@ -191,6 +257,12 @@ struct kunit_suite { int suite_init_err; }; +/* Stores an array of suites, end points one past the end */ +struct kunit_suite_set { + struct kunit_suite * const *start; + struct kunit_suite * const *end; +}; + /** * struct kunit - represents a running instance of a test. * @@ -238,6 +310,10 @@ static inline void kunit_set_failure(struct kunit *test) } bool kunit_enabled(void); +const char *kunit_action(void); +const char *kunit_filter_glob(void); +char *kunit_filter(void); +char *kunit_filter_action(void); void kunit_init_test(struct kunit *test, const char *name, char *log); @@ -248,10 +324,21 @@ size_t kunit_suite_num_test_cases(struct kunit_suite *suite); unsigned int kunit_test_case_num(struct kunit_suite *suite, struct kunit_case *test_case); +struct kunit_suite_set +kunit_filter_suites(const struct kunit_suite_set *suite_set, + const char *filter_glob, + char *filters, + char *filter_action, + int *err); +void kunit_free_suite_set(struct kunit_suite_set suite_set); + int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites); void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites); +void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin); +void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr); + #if IS_BUILTIN(CONFIG_KUNIT) int kunit_run_all_tests(void); #else |