Skip to content

Commit

Permalink
kunit: add is_init test attribute
Browse files Browse the repository at this point in the history
Add is_init test attribute of type bool. Add to_string, get, and filter
methods to lib/kunit/attributes.c.

Mark each of the tests in the init section with the is_init=true attribute.

Add is_init to the attributes documentation.

Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Rae Moar <rmoar@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
  • Loading branch information
Rae Moar authored and Shuah Khan committed Dec 18, 2023
1 parent 2cf4528 commit 6c4ea2f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Documentation/dev-tools/kunit/running_tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,10 @@ This attribute indicates the name of the module associated with the test.

This attribute is automatically saved as a string and is printed for each suite.
Tests can also be filtered using this attribute.

``is_init``

This attribute indicates whether the test uses init data or functions.

This attribute is automatically saved as a boolean and tests can also be
filtered using this attribute.
1 change: 1 addition & 0 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ struct kunit_suite {
struct dentry *debugfs;
struct string_stream *log;
int suite_init_err;
bool is_init;
};

/* Stores an array of suites, end points one past the end */
Expand Down
60 changes: 60 additions & 0 deletions lib/kunit/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ static const char *attr_enum_to_string(void *attr, const char * const str_list[]
return str_list[val];
}

static const char *attr_bool_to_string(void *attr, bool *to_free)
{
bool val = (bool)attr;

*to_free = false;
if (val)
return "true";
return "false";
}

static const char *attr_speed_to_string(void *attr, bool *to_free)
{
return attr_enum_to_string(attr, speed_str_list, to_free);
Expand Down Expand Up @@ -166,6 +176,37 @@ static int attr_string_filter(void *attr, const char *input, int *err)
return false;
}

static int attr_bool_filter(void *attr, const char *input, int *err)
{
int i, input_int = -1;
long val = (long)attr;
const char *input_str = NULL;

for (i = 0; input[i]; i++) {
if (!strchr(op_list, input[i])) {
input_str = input + i;
break;
}
}

if (!input_str) {
*err = -EINVAL;
pr_err("kunit executor: filter value not found: %s\n", input);
return false;
}

if (!strcmp(input_str, "true"))
input_int = (int)true;
else if (!strcmp(input_str, "false"))
input_int = (int)false;
else {
*err = -EINVAL;
pr_err("kunit executor: invalid filter input: %s\n", input);
return false;
}

return int_filter(val, input, input_int, err);
}

/* Get Attribute Methods */

Expand Down Expand Up @@ -194,6 +235,17 @@ static void *attr_module_get(void *test_or_suite, bool is_test)
return (void *) "";
}

static void *attr_is_init_get(void *test_or_suite, bool is_test)
{
struct kunit_suite *suite = is_test ? NULL : test_or_suite;
struct kunit_case *test = is_test ? test_or_suite : NULL;

if (test)
return ((void *) NULL);
else
return ((void *) suite->is_init);
}

/* List of all Test Attributes */

static struct kunit_attr kunit_attr_list[] = {
Expand All @@ -212,6 +264,14 @@ static struct kunit_attr kunit_attr_list[] = {
.filter = attr_string_filter,
.attr_default = (void *)"",
.print = PRINT_SUITE,
},
{
.name = "is_init",
.get_attr = attr_is_init_get,
.to_string = attr_bool_to_string,
.filter = attr_bool_filter,
.attr_default = (void *)false,
.print = PRINT_SUITE,
}
};

Expand Down
6 changes: 5 additions & 1 deletion lib/kunit/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_
struct kunit_suite_set total_suite_set = {NULL, NULL};
struct kunit_suite **total_suite_start = NULL;
size_t init_num_suites, num_suites, suite_size;
int i = 0;

init_num_suites = init_suite_set.end - init_suite_set.start;
num_suites = suite_set.end - suite_set.start;
Expand All @@ -310,8 +311,11 @@ struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_
if (!total_suite_start)
return total_suite_set;

/* Append init suites and then all other kunit suites */
/* Append and mark init suites and then append all other kunit suites */
memcpy(total_suite_start, init_suite_set.start, init_num_suites * suite_size);
for (i = 0; i < init_num_suites; i++)
total_suite_start[i]->is_init = true;

memcpy(total_suite_start + init_num_suites, suite_set.start, num_suites * suite_size);

/* Set kunit suite set start and end */
Expand Down

0 comments on commit 6c4ea2f

Please sign in to comment.