diff mbox series

[3/3] kunit: Update reporting function to support results from subtests

Message ID 20230411160056.1586-4-michal.wajdeczko@intel.com
State New
Headers show
Series None | expand

Commit Message

Michal Wajdeczko April 11, 2023, 4 p.m. UTC
There is function to report status of either suite or test, but it
doesn't support parameterized subtests that have to log report on
its own. Extend it to also accept subtest level results to avoid
code duplication.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: David Gow <davidgow@google.com>
---
 lib/kunit/test.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

Comments

David Gow April 13, 2023, 6:31 a.m. UTC | #1
On Thu, 13 Apr 2023 at 04:28, Rae Moar <rmoar@google.com> wrote:
>
> On Tue, Apr 11, 2023 at 12:01 PM Michal Wajdeczko
> <michal.wajdeczko@intel.com> wrote:
> >
> > There is function to report status of either suite or test, but it
> > doesn't support parameterized subtests that have to log report on
> > its own. Extend it to also accept subtest level results to avoid
> > code duplication.
> >
> > Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> > Cc: David Gow <davidgow@google.com>
> > ---
> >  lib/kunit/test.c | 28 +++++++++++++++++-----------
> >  1 file changed, 17 insertions(+), 11 deletions(-)
> >
> > diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> > index 5679197b5f8a..692fce258c5b 100644
> > --- a/lib/kunit/test.c
> > +++ b/lib/kunit/test.c
> > @@ -154,8 +154,14 @@ static void kunit_print_suite_start(struct kunit_suite *suite)
> >                   kunit_suite_num_test_cases(suite));
> >  }
> >
> > +enum kunit_test_or_suite {
> > +       KUNIT_SUITE = 0,
> > +       KUNIT_TEST,
> > +       KUNIT_SUBTEST,
> > +};
> > +
>
> Hi Michal!
>
> Since KUnit's goal is to progress toward supporting arbitrary levels
> of testing, I like the idea of starting to adjust these helper
> functions to allow for greater levels of testing.
>
> However, I'm not sure about this kunit_test_or_suite enum. If our goal
> is to support an arbitrary number of levels of tests then this enum
> still limits us to a finite number of levels. However, if we only want
> to focus on supporting parameterized tests (which is our direct goal),
> this could be the right solution.
>
> Maybe instead we could use an integer denoting the test level instead?
> This would remove the limit but would also remove the nice names of
> the levels.
>
> I'm curious what others opinions are on these ideas?
>
> A bit of a nit: if we do use this enum I wonder if we could clarify
> the name to be kunit_test_level as the current name of
> kunit_test_or_suite seems to indicate to me a binary of two options
> rather than three.
>
> >  static void kunit_print_ok_not_ok(void *test_or_suite,
> > -                                 bool is_test,
> > +                                 enum kunit_test_or_suite is_test,
>
> Similar to above, I think the name of is_test could be clarified. It
> is currently a bit confusing to me as they are all tests. Maybe
> test_level?

I agree with Rae that this is not the ideal long-term solution.

We're basically encoding two things here:
- Are we dealing with a 'struct kunit_suite' or a 'struct kunit'?
- How nested the test is.

Given KUnit originally only had a 2-level nesting (suite->test), and
now has 3-level nesting (always suite->test[->param]), this works, but
the KTAP format permits arbitrary nesting of tests, and we'll want to
have something like that in KUnit going forward. We don't have a
design for that yet, but it could conceivably allow nested suites,
thus breaking the rule that nesting level 0 is always a suite, and the
rest are all tests.

So there's definitely a part of me that would prefer to pass those two
pieces of information in separate arguments, rather than relying on an
enum like this.

That being said, this is all very fuzzy future plans, rather than
anything concrete, and this will all likely need reworking if we do
anything drastic anyway, so I'm not worried if we go with this for
now, and change it when we need to. I do think it's an improvement
over what we're doing currently.
(For example, another possible implementation for nested tests could
be to get rid of the distinction between tests and suites completely:
or at least have them share 'struct kunit', so this wouldn't need
passing in separately.)

Cheers,
-- David


>
> >                                   enum kunit_status status,
> >                                   size_t test_number,
> >                                   const char *description,
> > @@ -180,7 +186,9 @@ static void kunit_print_ok_not_ok(void *test_or_suite,
> >                         (status == KUNIT_SKIPPED) ? directive : "");
> >         else
> >                 kunit_log(KERN_INFO, test,
> > -                         KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
> > +                         "%.*s%s %zd %s%s%s",
> > +                         (int) strlen(KUNIT_SUBTEST_INDENT) * is_test,
>
> I would consider saving the length of KUNIT_SUBTEST_INDENT as a macro.
> Maybe KUNIT_INDENT_LEN?
>
> > +                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT,
> >                           kunit_status_to_ok_not_ok(status),
> >                           test_number, description, directive_header,
> >                           (status == KUNIT_SKIPPED) ? directive : "");
> > @@ -209,7 +217,7 @@ static size_t kunit_suite_counter = 1;
> >
> >  static void kunit_print_suite_end(struct kunit_suite *suite)
> >  {
> > -       kunit_print_ok_not_ok((void *)suite, false,
> > +       kunit_print_ok_not_ok((void *)suite, KUNIT_SUITE,
> >                               kunit_suite_has_succeeded(suite),
> >                               kunit_suite_counter++,
> >                               suite->name,
> > @@ -554,13 +562,11 @@ int kunit_run_tests(struct kunit_suite *suite)
> >                                                  "param-%d", test.param_index);
> >                                 }
> >
> > -                               kunit_log(KERN_INFO, &test,
> > -                                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
> > -                                         "%s %d %s%s%s",
> > -                                         kunit_status_to_ok_not_ok(test.status),
> > -                                         test.param_index + 1, param_desc,
> > -                                         test.status == KUNIT_SKIPPED ? " # SKIP " : "",
> > -                                         test.status == KUNIT_SKIPPED ? test.status_comment : "");
> > +                               kunit_print_ok_not_ok(&test, KUNIT_SUBTEST,
> > +                                                     test.status,
> > +                                                     test.param_index + 1,
> > +                                                     param_desc,
> > +                                                     test.status_comment);
> >
> >                                 /* Get next param. */
> >                                 param_desc[0] = '\0';
> > @@ -574,7 +580,7 @@ int kunit_run_tests(struct kunit_suite *suite)
> >
> >                 kunit_print_test_stats(&test, param_stats);
> >
> > -               kunit_print_ok_not_ok(&test, true, test_case->status,
> > +               kunit_print_ok_not_ok(&test, KUNIT_TEST, test_case->status,
> >                                       kunit_test_case_num(suite, test_case),
> >                                       test_case->name,
> >                                       test.status_comment);
> > --
> > 2.25.1
> >
> > --
> > You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230411160056.1586-4-michal.wajdeczko%40intel.com.
David Gow April 13, 2023, 6:38 a.m. UTC | #2
On Wed, 12 Apr 2023 at 00:01, Michal Wajdeczko
<michal.wajdeczko@intel.com> wrote:
>
> There is function to report status of either suite or test, but it
> doesn't support parameterized subtests that have to log report on
> its own. Extend it to also accept subtest level results to avoid
> code duplication.
>
> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> Cc: David Gow <davidgow@google.com>
> ---

Thanks: this is definitely an improvement on how we handle this.

There's definitely more we can do, particularly looking forward to
supporting more complex test hierarchies in the future, but getting
everything under kunit_print_ok_not_ok is an improvement regardless of
when happens down the line.

My only real concern is that the way the indent is printed is a bit
subtle and difficult to understand fully on first glance. I've added
some notes below.

>  lib/kunit/test.c | 28 +++++++++++++++++-----------
>  1 file changed, 17 insertions(+), 11 deletions(-)
>
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index 5679197b5f8a..692fce258c5b 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -154,8 +154,14 @@ static void kunit_print_suite_start(struct kunit_suite *suite)
>                   kunit_suite_num_test_cases(suite));
>  }
>
> +enum kunit_test_or_suite {
> +       KUNIT_SUITE = 0,
> +       KUNIT_TEST,
> +       KUNIT_SUBTEST,
> +};
> +

As Rae notes, this probably won't be how this code eventually evolves.
I don't think it's a problem to have it now, though.

>  static void kunit_print_ok_not_ok(void *test_or_suite,
> -                                 bool is_test,
> +                                 enum kunit_test_or_suite is_test,
>                                   enum kunit_status status,
>                                   size_t test_number,
>                                   const char *description,
> @@ -180,7 +186,9 @@ static void kunit_print_ok_not_ok(void *test_or_suite,
>                         (status == KUNIT_SKIPPED) ? directive : "");
>         else
>                 kunit_log(KERN_INFO, test,
> -                         KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
> +                         "%.*s%s %zd %s%s%s",
> +                         (int) strlen(KUNIT_SUBTEST_INDENT) * is_test,
> +                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT,

This feels a little bit _too_ clever here: I feel it at the very least
needs a comment, and maybe it'd make more sense to either:
- Make is_test explicitly a "nesting depth" integer, and calculate the
indent based on that.
- Have is_test as an enum, and then just explicitly handle each value
separately. (Like we do with suite vs test).

I think that the former is probably the right long-term solution (it's
much more extensible to more levels of nesting), but the latter is
definitely easier given the differences between suites and tests at
the moment.

If we do continue to share a codepath between tests and subtests, I'd
prefer it if we either didn't use strlen(), or went to some greater
effort to document how that works (hopefully we can guarantee that the
compiler will treat this as a constant). Equally, a comment or
something noting that this will read invalid memory if is_test > 2,
due to the hardcoded two KUNIT_SUBTEST_INDENT, would be nice.


>                           kunit_status_to_ok_not_ok(status),
>                           test_number, description, directive_header,
>                           (status == KUNIT_SKIPPED) ? directive : "");
> @@ -209,7 +217,7 @@ static size_t kunit_suite_counter = 1;
>
>  static void kunit_print_suite_end(struct kunit_suite *suite)
>  {
> -       kunit_print_ok_not_ok((void *)suite, false,
> +       kunit_print_ok_not_ok((void *)suite, KUNIT_SUITE,
>                               kunit_suite_has_succeeded(suite),
>                               kunit_suite_counter++,
>                               suite->name,
> @@ -554,13 +562,11 @@ int kunit_run_tests(struct kunit_suite *suite)
>                                                  "param-%d", test.param_index);
>                                 }
>
> -                               kunit_log(KERN_INFO, &test,
> -                                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
> -                                         "%s %d %s%s%s",
> -                                         kunit_status_to_ok_not_ok(test.status),
> -                                         test.param_index + 1, param_desc,
> -                                         test.status == KUNIT_SKIPPED ? " # SKIP " : "",
> -                                         test.status == KUNIT_SKIPPED ? test.status_comment : "");
> +                               kunit_print_ok_not_ok(&test, KUNIT_SUBTEST,
> +                                                     test.status,
> +                                                     test.param_index + 1,
> +                                                     param_desc,
> +                                                     test.status_comment);
>
>                                 /* Get next param. */
>                                 param_desc[0] = '\0';
> @@ -574,7 +580,7 @@ int kunit_run_tests(struct kunit_suite *suite)
>
>                 kunit_print_test_stats(&test, param_stats);
>
> -               kunit_print_ok_not_ok(&test, true, test_case->status,
> +               kunit_print_ok_not_ok(&test, KUNIT_TEST, test_case->status,
>                                       kunit_test_case_num(suite, test_case),
>                                       test_case->name,
>                                       test.status_comment);
> --
> 2.25.1
>

Otherwise, this all looks good to me. Thanks very much!

Cheers,
-- David
Michal Wajdeczko April 13, 2023, 11:25 a.m. UTC | #3
On 13.04.2023 08:38, David Gow wrote:
> On Wed, 12 Apr 2023 at 00:01, Michal Wajdeczko
> <michal.wajdeczko@intel.com> wrote:
>>
>> There is function to report status of either suite or test, but it
>> doesn't support parameterized subtests that have to log report on
>> its own. Extend it to also accept subtest level results to avoid
>> code duplication.
>>
>> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
>> Cc: David Gow <davidgow@google.com>
>> ---
> 
> Thanks: this is definitely an improvement on how we handle this.
> 
> There's definitely more we can do, particularly looking forward to
> supporting more complex test hierarchies in the future, but getting
> everything under kunit_print_ok_not_ok is an improvement regardless of
> when happens down the line.
> 
> My only real concern is that the way the indent is printed is a bit
> subtle and difficult to understand fully on first glance. I've added
> some notes below.
> 
>>  lib/kunit/test.c | 28 +++++++++++++++++-----------
>>  1 file changed, 17 insertions(+), 11 deletions(-)
>>
>> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
>> index 5679197b5f8a..692fce258c5b 100644
>> --- a/lib/kunit/test.c
>> +++ b/lib/kunit/test.c
>> @@ -154,8 +154,14 @@ static void kunit_print_suite_start(struct kunit_suite *suite)
>>                   kunit_suite_num_test_cases(suite));
>>  }
>>
>> +enum kunit_test_or_suite {
>> +       KUNIT_SUITE = 0,
>> +       KUNIT_TEST,
>> +       KUNIT_SUBTEST,
>> +};
>> +
> 
> As Rae notes, this probably won't be how this code eventually evolves.
> I don't think it's a problem to have it now, though.
> 
>>  static void kunit_print_ok_not_ok(void *test_or_suite,
>> -                                 bool is_test,
>> +                                 enum kunit_test_or_suite is_test,
>>                                   enum kunit_status status,
>>                                   size_t test_number,
>>                                   const char *description,
>> @@ -180,7 +186,9 @@ static void kunit_print_ok_not_ok(void *test_or_suite,
>>                         (status == KUNIT_SKIPPED) ? directive : "");
>>         else
>>                 kunit_log(KERN_INFO, test,
>> -                         KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
>> +                         "%.*s%s %zd %s%s%s",
>> +                         (int) strlen(KUNIT_SUBTEST_INDENT) * is_test,
>> +                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT,
> 
> This feels a little bit _too_ clever here: I feel it at the very least
> needs a comment, and maybe it'd make more sense to either:
> - Make is_test explicitly a "nesting depth" integer, and calculate the
> indent based on that.
> - Have is_test as an enum, and then just explicitly handle each value
> separately. (Like we do with suite vs test).
> 
> I think that the former is probably the right long-term solution (it's
> much more extensible to more levels of nesting), but the latter is
> definitely easier given the differences between suites and tests at
> the moment.
> 
> If we do continue to share a codepath between tests and subtests, I'd
> prefer it if we either didn't use strlen(), or went to some greater

Rae suggested to define KUNIT_INDENT_LEN 4 and I will put it in test.h

> effort to document how that works (hopefully we can guarantee that the
> compiler will treat this as a constant). Equally, a comment or
> something noting that this will read invalid memory if is_test > 2,
> due to the hardcoded two KUNIT_SUBTEST_INDENT, would be nice.

that shouldn't happen as %.*s specifies precision and it's used here to
clamp string with more than needed indents, it will not try to read
beyond terminating \0

but since you plan for arbitrary level of testing I could change that to
a little simpler variant %*s which should always with any level:

		kunit_log(KERN_INFO, test,
			  "%*s%s %zd %s%s%s",
			  KUNIT_INDENT_LEN * test_level, "",

will that be _simple_ enough ?

Thanks,
Michal

> 
> 
>>                           kunit_status_to_ok_not_ok(status),
>>                           test_number, description, directive_header,
>>                           (status == KUNIT_SKIPPED) ? directive : "");
>> @@ -209,7 +217,7 @@ static size_t kunit_suite_counter = 1;
>>
>>  static void kunit_print_suite_end(struct kunit_suite *suite)
>>  {
>> -       kunit_print_ok_not_ok((void *)suite, false,
>> +       kunit_print_ok_not_ok((void *)suite, KUNIT_SUITE,
>>                               kunit_suite_has_succeeded(suite),
>>                               kunit_suite_counter++,
>>                               suite->name,
>> @@ -554,13 +562,11 @@ int kunit_run_tests(struct kunit_suite *suite)
>>                                                  "param-%d", test.param_index);
>>                                 }
>>
>> -                               kunit_log(KERN_INFO, &test,
>> -                                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
>> -                                         "%s %d %s%s%s",
>> -                                         kunit_status_to_ok_not_ok(test.status),
>> -                                         test.param_index + 1, param_desc,
>> -                                         test.status == KUNIT_SKIPPED ? " # SKIP " : "",
>> -                                         test.status == KUNIT_SKIPPED ? test.status_comment : "");
>> +                               kunit_print_ok_not_ok(&test, KUNIT_SUBTEST,
>> +                                                     test.status,
>> +                                                     test.param_index + 1,
>> +                                                     param_desc,
>> +                                                     test.status_comment);
>>
>>                                 /* Get next param. */
>>                                 param_desc[0] = '\0';
>> @@ -574,7 +580,7 @@ int kunit_run_tests(struct kunit_suite *suite)
>>
>>                 kunit_print_test_stats(&test, param_stats);
>>
>> -               kunit_print_ok_not_ok(&test, true, test_case->status,
>> +               kunit_print_ok_not_ok(&test, KUNIT_TEST, test_case->status,
>>                                       kunit_test_case_num(suite, test_case),
>>                                       test_case->name,
>>                                       test.status_comment);
>> --
>> 2.25.1
>>
> 
> Otherwise, this all looks good to me. Thanks very much!
> 
> Cheers,
> -- David
Michal Wajdeczko April 13, 2023, 1:15 p.m. UTC | #4
On 13.04.2023 08:31, David Gow wrote:
> On Thu, 13 Apr 2023 at 04:28, Rae Moar <rmoar@google.com> wrote:
>>
>> On Tue, Apr 11, 2023 at 12:01 PM Michal Wajdeczko
>> <michal.wajdeczko@intel.com> wrote:
>>>
>>> There is function to report status of either suite or test, but it
>>> doesn't support parameterized subtests that have to log report on
>>> its own. Extend it to also accept subtest level results to avoid
>>> code duplication.
>>>
>>> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
>>> Cc: David Gow <davidgow@google.com>
>>> ---
>>>  lib/kunit/test.c | 28 +++++++++++++++++-----------
>>>  1 file changed, 17 insertions(+), 11 deletions(-)
>>>
>>> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
>>> index 5679197b5f8a..692fce258c5b 100644
>>> --- a/lib/kunit/test.c
>>> +++ b/lib/kunit/test.c
>>> @@ -154,8 +154,14 @@ static void kunit_print_suite_start(struct kunit_suite *suite)
>>>                   kunit_suite_num_test_cases(suite));
>>>  }
>>>
>>> +enum kunit_test_or_suite {
>>> +       KUNIT_SUITE = 0,
>>> +       KUNIT_TEST,
>>> +       KUNIT_SUBTEST,
>>> +};
>>> +
>>
>> Hi Michal!
>>
>> Since KUnit's goal is to progress toward supporting arbitrary levels
>> of testing, I like the idea of starting to adjust these helper
>> functions to allow for greater levels of testing.
>>
>> However, I'm not sure about this kunit_test_or_suite enum. If our goal
>> is to support an arbitrary number of levels of tests then this enum
>> still limits us to a finite number of levels. However, if we only want
>> to focus on supporting parameterized tests (which is our direct goal),
>> this could be the right solution.
>>
>> Maybe instead we could use an integer denoting the test level instead?
>> This would remove the limit but would also remove the nice names of
>> the levels.

we can use integer as param but at the same time we can also have define
or anonymous enum as nice aliases to currently known/used levels:

/* Currently supported test levels */
enum {
	KUNIT_LEVEL_SUITE = 0,
	KUNIT_LEVEL_TEST,
	KUNIT_LEVEL_PARAMTEST,
};

/* Future levels are TBD */
#define KUNIT_LEVEL_SUBTEST(n)	(KUNIT_LEVEL_TEST + (n))

>>
>> I'm curious what others opinions are on these ideas?
>>
>> A bit of a nit: if we do use this enum I wonder if we could clarify
>> the name to be kunit_test_level as the current name of
>> kunit_test_or_suite seems to indicate to me a binary of two options
>> rather than three.
>>
>>>  static void kunit_print_ok_not_ok(void *test_or_suite,
>>> -                                 bool is_test,
>>> +                                 enum kunit_test_or_suite is_test,
>>
>> Similar to above, I think the name of is_test could be clarified. It
>> is currently a bit confusing to me as they are all tests. Maybe
>> test_level?

ok

> 
> I agree with Rae that this is not the ideal long-term solution.
> 
> We're basically encoding two things here:
> - Are we dealing with a 'struct kunit_suite' or a 'struct kunit'?
> - How nested the test is.
> 
> Given KUnit originally only had a 2-level nesting (suite->test), and
> now has 3-level nesting (always suite->test[->param]), this works, but
> the KTAP format permits arbitrary nesting of tests, and we'll want to
> have something like that in KUnit going forward. We don't have a
> design for that yet, but it could conceivably allow nested suites,
> thus breaking the rule that nesting level 0 is always a suite, and the
> rest are all tests.

I guess "We don't have a design for that yet" is a key here

> 
> So there's definitely a part of me that would prefer to pass those two
> pieces of information in separate arguments, rather than relying on an
> enum like this.
> 
> That being said, this is all very fuzzy future plans, rather than
> anything concrete, and this will all likely need reworking if we do
> anything drastic anyway, so I'm not worried if we go with this for
> now, and change it when we need to. I do think it's an improvement
> over what we're doing currently.
> (For example, another possible implementation for nested tests could
> be to get rid of the distinction between tests and suites completely:
> or at least have them share 'struct kunit', so this wouldn't need
> passing in separately.)

maybe the problem will go away once we just replace this untyped param:

	kunit_print_ok_not_ok(void *test_or_suite, ...

with proper type:

	kunit_print_ok_not_ok(struct kunit *test, ...

and treat NULL as indication that we just want to print raw results (as
it looks function is not using any suite attributes directly, all input
is passed explicitly and kunit_log() expects test only)

Thanks,
Michal

> 
> Cheers,
> -- David
> 
> 
>>
>>>                                   enum kunit_status status,
>>>                                   size_t test_number,
>>>                                   const char *description,
>>> @@ -180,7 +186,9 @@ static void kunit_print_ok_not_ok(void *test_or_suite,
>>>                         (status == KUNIT_SKIPPED) ? directive : "");
>>>         else
>>>                 kunit_log(KERN_INFO, test,
>>> -                         KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
>>> +                         "%.*s%s %zd %s%s%s",
>>> +                         (int) strlen(KUNIT_SUBTEST_INDENT) * is_test,
>>
>> I would consider saving the length of KUNIT_SUBTEST_INDENT as a macro.
>> Maybe KUNIT_INDENT_LEN?
>>
>>> +                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT,
>>>                           kunit_status_to_ok_not_ok(status),
>>>                           test_number, description, directive_header,
>>>                           (status == KUNIT_SKIPPED) ? directive : "");
>>> @@ -209,7 +217,7 @@ static size_t kunit_suite_counter = 1;
>>>
>>>  static void kunit_print_suite_end(struct kunit_suite *suite)
>>>  {
>>> -       kunit_print_ok_not_ok((void *)suite, false,
>>> +       kunit_print_ok_not_ok((void *)suite, KUNIT_SUITE,
>>>                               kunit_suite_has_succeeded(suite),
>>>                               kunit_suite_counter++,
>>>                               suite->name,
>>> @@ -554,13 +562,11 @@ int kunit_run_tests(struct kunit_suite *suite)
>>>                                                  "param-%d", test.param_index);
>>>                                 }
>>>
>>> -                               kunit_log(KERN_INFO, &test,
>>> -                                         KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
>>> -                                         "%s %d %s%s%s",
>>> -                                         kunit_status_to_ok_not_ok(test.status),
>>> -                                         test.param_index + 1, param_desc,
>>> -                                         test.status == KUNIT_SKIPPED ? " # SKIP " : "",
>>> -                                         test.status == KUNIT_SKIPPED ? test.status_comment : "");
>>> +                               kunit_print_ok_not_ok(&test, KUNIT_SUBTEST,
>>> +                                                     test.status,
>>> +                                                     test.param_index + 1,
>>> +                                                     param_desc,
>>> +                                                     test.status_comment);
>>>
>>>                                 /* Get next param. */
>>>                                 param_desc[0] = '\0';
>>> @@ -574,7 +580,7 @@ int kunit_run_tests(struct kunit_suite *suite)
>>>
>>>                 kunit_print_test_stats(&test, param_stats);
>>>
>>> -               kunit_print_ok_not_ok(&test, true, test_case->status,
>>> +               kunit_print_ok_not_ok(&test, KUNIT_TEST, test_case->status,
>>>                                       kunit_test_case_num(suite, test_case),
>>>                                       test_case->name,
>>>                                       test.status_comment);
>>> --
>>> 2.25.1
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230411160056.1586-4-michal.wajdeczko%40intel.com.
David Gow April 14, 2023, 6:35 a.m. UTC | #5
On Thu, 13 Apr 2023 at 21:15, Michal Wajdeczko
<michal.wajdeczko@intel.com> wrote:
>
>
>
> On 13.04.2023 08:31, David Gow wrote:
> > On Thu, 13 Apr 2023 at 04:28, Rae Moar <rmoar@google.com> wrote:
> >>
> >> On Tue, Apr 11, 2023 at 12:01 PM Michal Wajdeczko
> >> <michal.wajdeczko@intel.com> wrote:
> >>>
> >>> There is function to report status of either suite or test, but it
> >>> doesn't support parameterized subtests that have to log report on
> >>> its own. Extend it to also accept subtest level results to avoid
> >>> code duplication.
> >>>
> >>> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> >>> Cc: David Gow <davidgow@google.com>
> >>> ---
> >>>  lib/kunit/test.c | 28 +++++++++++++++++-----------
> >>>  1 file changed, 17 insertions(+), 11 deletions(-)
> >>>
> >>> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> >>> index 5679197b5f8a..692fce258c5b 100644
> >>> --- a/lib/kunit/test.c
> >>> +++ b/lib/kunit/test.c
> >>> @@ -154,8 +154,14 @@ static void kunit_print_suite_start(struct kunit_suite *suite)
> >>>                   kunit_suite_num_test_cases(suite));
> >>>  }
> >>>
> >>> +enum kunit_test_or_suite {
> >>> +       KUNIT_SUITE = 0,
> >>> +       KUNIT_TEST,
> >>> +       KUNIT_SUBTEST,
> >>> +};
> >>> +
> >>
> >> Hi Michal!
> >>
> >> Since KUnit's goal is to progress toward supporting arbitrary levels
> >> of testing, I like the idea of starting to adjust these helper
> >> functions to allow for greater levels of testing.
> >>
> >> However, I'm not sure about this kunit_test_or_suite enum. If our goal
> >> is to support an arbitrary number of levels of tests then this enum
> >> still limits us to a finite number of levels. However, if we only want
> >> to focus on supporting parameterized tests (which is our direct goal),
> >> this could be the right solution.
> >>
> >> Maybe instead we could use an integer denoting the test level instead?
> >> This would remove the limit but would also remove the nice names of
> >> the levels.
>
> we can use integer as param but at the same time we can also have define
> or anonymous enum as nice aliases to currently known/used levels:
>
> /* Currently supported test levels */
> enum {
>         KUNIT_LEVEL_SUITE = 0,
>         KUNIT_LEVEL_TEST,
>         KUNIT_LEVEL_PARAMTEST,
> };
>
> /* Future levels are TBD */
> #define KUNIT_LEVEL_SUBTEST(n)  (KUNIT_LEVEL_TEST + (n))
>

This sounds good to me!

> >>
> >> I'm curious what others opinions are on these ideas?
> >>
> >> A bit of a nit: if we do use this enum I wonder if we could clarify
> >> the name to be kunit_test_level as the current name of
> >> kunit_test_or_suite seems to indicate to me a binary of two options
> >> rather than three.
> >>
> >>>  static void kunit_print_ok_not_ok(void *test_or_suite,
> >>> -                                 bool is_test,
> >>> +                                 enum kunit_test_or_suite is_test,
> >>
> >> Similar to above, I think the name of is_test could be clarified. It
> >> is currently a bit confusing to me as they are all tests. Maybe
> >> test_level?
>
> ok
>
> >
> > I agree with Rae that this is not the ideal long-term solution.
> >
> > We're basically encoding two things here:
> > - Are we dealing with a 'struct kunit_suite' or a 'struct kunit'?
> > - How nested the test is.
> >
> > Given KUnit originally only had a 2-level nesting (suite->test), and
> > now has 3-level nesting (always suite->test[->param]), this works, but
> > the KTAP format permits arbitrary nesting of tests, and we'll want to
> > have something like that in KUnit going forward. We don't have a
> > design for that yet, but it could conceivably allow nested suites,
> > thus breaking the rule that nesting level 0 is always a suite, and the
> > rest are all tests.
>
> I guess "We don't have a design for that yet" is a key here

Yeah: I wouldn't worry too much about what the future design would
bring, tbh. As long as we don't totally paint ourselves into a corner
(and I don't think anything in this patch is at risk of doing that),
then doing whatever is sensible for the way things are now works.

>
> >
> > So there's definitely a part of me that would prefer to pass those two
> > pieces of information in separate arguments, rather than relying on an
> > enum like this.
> >
> > That being said, this is all very fuzzy future plans, rather than
> > anything concrete, and this will all likely need reworking if we do
> > anything drastic anyway, so I'm not worried if we go with this for
> > now, and change it when we need to. I do think it's an improvement
> > over what we're doing currently.
> > (For example, another possible implementation for nested tests could
> > be to get rid of the distinction between tests and suites completely:
> > or at least have them share 'struct kunit', so this wouldn't need
> > passing in separately.)
>
> maybe the problem will go away once we just replace this untyped param:
>
>         kunit_print_ok_not_ok(void *test_or_suite, ...
>
> with proper type:
>
>         kunit_print_ok_not_ok(struct kunit *test, ...
>
> and treat NULL as indication that we just want to print raw results (as
> it looks function is not using any suite attributes directly, all input
> is passed explicitly and kunit_log() expects test only)
>

Yeah: I think that makes sense. Suite results are still handled
separately in debugfs as well.

Cheers,
-- David
diff mbox series

Patch

diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 5679197b5f8a..692fce258c5b 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -154,8 +154,14 @@  static void kunit_print_suite_start(struct kunit_suite *suite)
 		  kunit_suite_num_test_cases(suite));
 }
 
+enum kunit_test_or_suite {
+	KUNIT_SUITE = 0,
+	KUNIT_TEST,
+	KUNIT_SUBTEST,
+};
+
 static void kunit_print_ok_not_ok(void *test_or_suite,
-				  bool is_test,
+				  enum kunit_test_or_suite is_test,
 				  enum kunit_status status,
 				  size_t test_number,
 				  const char *description,
@@ -180,7 +186,9 @@  static void kunit_print_ok_not_ok(void *test_or_suite,
 			(status == KUNIT_SKIPPED) ? directive : "");
 	else
 		kunit_log(KERN_INFO, test,
-			  KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
+			  "%.*s%s %zd %s%s%s",
+			  (int) strlen(KUNIT_SUBTEST_INDENT) * is_test,
+			  KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT,
 			  kunit_status_to_ok_not_ok(status),
 			  test_number, description, directive_header,
 			  (status == KUNIT_SKIPPED) ? directive : "");
@@ -209,7 +217,7 @@  static size_t kunit_suite_counter = 1;
 
 static void kunit_print_suite_end(struct kunit_suite *suite)
 {
-	kunit_print_ok_not_ok((void *)suite, false,
+	kunit_print_ok_not_ok((void *)suite, KUNIT_SUITE,
 			      kunit_suite_has_succeeded(suite),
 			      kunit_suite_counter++,
 			      suite->name,
@@ -554,13 +562,11 @@  int kunit_run_tests(struct kunit_suite *suite)
 						 "param-%d", test.param_index);
 				}
 
-				kunit_log(KERN_INFO, &test,
-					  KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
-					  "%s %d %s%s%s",
-					  kunit_status_to_ok_not_ok(test.status),
-					  test.param_index + 1, param_desc,
-					  test.status == KUNIT_SKIPPED ? " # SKIP " : "",
-					  test.status == KUNIT_SKIPPED ? test.status_comment : "");
+				kunit_print_ok_not_ok(&test, KUNIT_SUBTEST,
+						      test.status,
+						      test.param_index + 1,
+						      param_desc,
+						      test.status_comment);
 
 				/* Get next param. */
 				param_desc[0] = '\0';
@@ -574,7 +580,7 @@  int kunit_run_tests(struct kunit_suite *suite)
 
 		kunit_print_test_stats(&test, param_stats);
 
-		kunit_print_ok_not_ok(&test, true, test_case->status,
+		kunit_print_ok_not_ok(&test, KUNIT_TEST, test_case->status,
 				      kunit_test_case_num(suite, test_case),
 				      test_case->name,
 				      test.status_comment);