[PATCH] kunit: prevent log overwrite in param_tests

0 views
Skip to first unread message

Carlos Llamas

unread,
Oct 24, 2025, 3:01:13 PM (12 days ago) Oct 24
to Brendan Higgins, David Gow, Rae Moar, Shuah Khan, Marie Zhussupova, kerne...@android.com, linux-...@vger.kernel.org, Tiffany Yang, Carlos Llamas, open list:KERNEL UNIT TESTING FRAMEWORK (KUnit), open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)
When running parameterized tests, each test case is initialized with
kunit_init_test(). This function takes the test_case->log as a parameter
but it clears it via string_stream_clear() on each iteration.

This results in only the log from the last parameter being preserved in
the test_case->log and the results from the previous parameters are lost
from the debugfs entry.

Fix this by manually setting the param_test.log to the test_case->log
after it has been initialized. This prevents kunit_init_test() from
clearing the log on each iteration.

Fixes: 4b59300ba4d2 ("kunit: Add parent kunit for parameterized test context")
Signed-off-by: Carlos Llamas <cmll...@google.com>
---
lib/kunit/test.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index bb66ea1a3eac..62eb529824c6 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -745,7 +745,8 @@ int kunit_run_tests(struct kunit_suite *suite)
.param_index = ++test.param_index,
.parent = &test,
};
- kunit_init_test(&param_test, test_case->name, test_case->log);
+ kunit_init_test(&param_test, test_case->name, NULL);
+ param_test.log = test_case->log;
kunit_run_case_catch_errors(suite, test_case, &param_test);

if (param_desc[0] == '\0') {
--
2.51.1.821.gb6fe4d2222-goog

David Gow

unread,
Oct 24, 2025, 10:01:57 PM (12 days ago) Oct 24
to Carlos Llamas, Brendan Higgins, Rae Moar, Shuah Khan, Marie Zhussupova, kerne...@android.com, linux-...@vger.kernel.org, Tiffany Yang, open list:KERNEL UNIT TESTING FRAMEWORK (KUnit), open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)
On Sat, 25 Oct 2025 at 03:01, Carlos Llamas <cmll...@google.com> wrote:
>
> When running parameterized tests, each test case is initialized with
> kunit_init_test(). This function takes the test_case->log as a parameter
> but it clears it via string_stream_clear() on each iteration.
>
> This results in only the log from the last parameter being preserved in
> the test_case->log and the results from the previous parameters are lost
> from the debugfs entry.
>
> Fix this by manually setting the param_test.log to the test_case->log
> after it has been initialized. This prevents kunit_init_test() from
> clearing the log on each iteration.
>
> Fixes: 4b59300ba4d2 ("kunit: Add parent kunit for parameterized test context")
> Signed-off-by: Carlos Llamas <cmll...@google.com>
> ---

Thanks, Carlos: this is a nice catch!

There was some thought about keeping a separate struct kunit_case (or
equivalent) for individual test parameter (so we could provide more
detail in debugfs), which would negate the need for this, but there
are other problems with that, so just re-using the test_case->log is
definitely the way to go for now.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David
Reply all
Reply to author
Forward
0 new messages