Skip to content

Commit 1be89fa

Browse files
committed
Merge tag 'linux-kselftest-kunit-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull KUnit updates from Shuah Khan: - several fixes to kunit tool - new klist structure test - support for m68k under QEMU - support for overriding the QEMU serial port - support for SH under QEMU * tag 'linux-kselftest-kunit-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: add tests for using current KUnit test field kunit: tool: Add support for SH under QEMU kunit: tool: Add support for overriding the QEMU serial port .gitignore: Unignore .kunitconfig list: test: Test the klist structure kunit: increase KUNIT_LOG_SIZE to 2048 bytes kunit: Use gfp in kunit_alloc_resource() kernel-doc kunit: tool: fix pre-existing `mypy --strict` errors and update run_checks.py kunit: tool: remove unused imports and variables kunit: tool: add subscripts for type annotations where appropriate kunit: fix bug of extra newline characters in debugfs logs kunit: fix bug in the order of lines in debugfs logs kunit: fix bug in debugfs logs of parameterized tests kunit: tool: Add support for m68k under QEMU
2 parents 0f50767 + a42077b commit 1be89fa

17 files changed

+491
-72
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ modules.order
103103
!.get_maintainer.ignore
104104
!.gitattributes
105105
!.gitignore
106+
!.kunitconfig
106107
!.mailmap
107108
!.rustfmt.toml
108109

include/kunit/resource.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
7272
* params.gfp = gfp;
7373
*
7474
* return kunit_alloc_resource(test, kunit_kmalloc_init,
75-
* kunit_kmalloc_free, &params);
75+
* kunit_kmalloc_free, gfp, &params);
7676
* }
7777
*
7878
* Resources can also be named, with lookup/removal done on a name

include/kunit/test.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ DECLARE_STATIC_KEY_FALSE(kunit_running);
3434
struct kunit;
3535

3636
/* Size of log associated with test. */
37-
#define KUNIT_LOG_SIZE 512
37+
#define KUNIT_LOG_SIZE 2048
3838

3939
/* Maximum size of parameter description string. */
4040
#define KUNIT_PARAM_DESC_SIZE 128
@@ -420,7 +420,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
420420
#define kunit_log(lvl, test_or_suite, fmt, ...) \
421421
do { \
422422
printk(lvl fmt, ##__VA_ARGS__); \
423-
kunit_log_append((test_or_suite)->log, fmt "\n", \
423+
kunit_log_append((test_or_suite)->log, fmt, \
424424
##__VA_ARGS__); \
425425
} while (0)
426426

lib/kunit/debugfs.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,24 @@ static int debugfs_print_results(struct seq_file *seq, void *v)
5555
enum kunit_status success = kunit_suite_has_succeeded(suite);
5656
struct kunit_case *test_case;
5757

58-
if (!suite || !suite->log)
58+
if (!suite)
5959
return 0;
6060

61-
seq_printf(seq, "%s", suite->log);
61+
/* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
62+
seq_puts(seq, "KTAP version 1\n");
63+
seq_puts(seq, "1..1\n");
64+
65+
/* Print suite header because it is not stored in the test logs. */
66+
seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
67+
seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
68+
seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
6269

6370
kunit_suite_for_each_test_case(suite, test_case)
6471
debugfs_print_result(seq, suite, test_case);
6572

73+
if (suite->log)
74+
seq_printf(seq, "%s", suite->log);
75+
6676
seq_printf(seq, "%s %d %s\n",
6777
kunit_status_to_ok_not_ok(success), 1, suite->name);
6878
return 0;

lib/kunit/kunit-test.c

+64-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Author: Brendan Higgins <[email protected]>
77
*/
88
#include <kunit/test.h>
9+
#include <kunit/test-bug.h>
910

1011
#include "try-catch-impl.h"
1112

@@ -443,18 +444,6 @@ static struct kunit_suite kunit_resource_test_suite = {
443444
.test_cases = kunit_resource_test_cases,
444445
};
445446

446-
static void kunit_log_test(struct kunit *test);
447-
448-
static struct kunit_case kunit_log_test_cases[] = {
449-
KUNIT_CASE(kunit_log_test),
450-
{}
451-
};
452-
453-
static struct kunit_suite kunit_log_test_suite = {
454-
.name = "kunit-log-test",
455-
.test_cases = kunit_log_test_cases,
456-
};
457-
458447
static void kunit_log_test(struct kunit *test)
459448
{
460449
struct kunit_suite suite;
@@ -481,6 +470,29 @@ static void kunit_log_test(struct kunit *test)
481470
#endif
482471
}
483472

473+
static void kunit_log_newline_test(struct kunit *test)
474+
{
475+
kunit_info(test, "Add newline\n");
476+
if (test->log) {
477+
KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"),
478+
"Missing log line, full log:\n%s", test->log);
479+
KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n"));
480+
} else {
481+
kunit_skip(test, "only useful when debugfs is enabled");
482+
}
483+
}
484+
485+
static struct kunit_case kunit_log_test_cases[] = {
486+
KUNIT_CASE(kunit_log_test),
487+
KUNIT_CASE(kunit_log_newline_test),
488+
{}
489+
};
490+
491+
static struct kunit_suite kunit_log_test_suite = {
492+
.name = "kunit-log-test",
493+
.test_cases = kunit_log_test_cases,
494+
};
495+
484496
static void kunit_status_set_failure_test(struct kunit *test)
485497
{
486498
struct kunit fake;
@@ -521,7 +533,46 @@ static struct kunit_suite kunit_status_test_suite = {
521533
.test_cases = kunit_status_test_cases,
522534
};
523535

536+
static void kunit_current_test(struct kunit *test)
537+
{
538+
/* Check results of both current->kunit_test and
539+
* kunit_get_current_test() are equivalent to current test.
540+
*/
541+
KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
542+
KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
543+
}
544+
545+
static void kunit_current_fail_test(struct kunit *test)
546+
{
547+
struct kunit fake;
548+
549+
kunit_init_test(&fake, "fake test", NULL);
550+
KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
551+
552+
/* Set current->kunit_test to fake test. */
553+
current->kunit_test = &fake;
554+
555+
kunit_fail_current_test("This should make `fake` test fail.");
556+
KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
557+
kunit_cleanup(&fake);
558+
559+
/* Reset current->kunit_test to current test. */
560+
current->kunit_test = test;
561+
}
562+
563+
static struct kunit_case kunit_current_test_cases[] = {
564+
KUNIT_CASE(kunit_current_test),
565+
KUNIT_CASE(kunit_current_fail_test),
566+
{}
567+
};
568+
569+
static struct kunit_suite kunit_current_test_suite = {
570+
.name = "kunit_current",
571+
.test_cases = kunit_current_test_cases,
572+
};
573+
524574
kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
525-
&kunit_log_test_suite, &kunit_status_test_suite);
575+
&kunit_log_test_suite, &kunit_status_test_suite,
576+
&kunit_current_test_suite);
526577

527578
MODULE_LICENSE("GPL v2");

lib/kunit/test.c

+44-13
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,51 @@ static void kunit_print_test_stats(struct kunit *test,
108108
stats.total);
109109
}
110110

111+
/**
112+
* kunit_log_newline() - Add newline to the end of log if one is not
113+
* already present.
114+
* @log: The log to add the newline to.
115+
*/
116+
static void kunit_log_newline(char *log)
117+
{
118+
int log_len, len_left;
119+
120+
log_len = strlen(log);
121+
len_left = KUNIT_LOG_SIZE - log_len - 1;
122+
123+
if (log_len > 0 && log[log_len - 1] != '\n')
124+
strncat(log, "\n", len_left);
125+
}
126+
111127
/*
112128
* Append formatted message to log, size of which is limited to
113129
* KUNIT_LOG_SIZE bytes (including null terminating byte).
114130
*/
115131
void kunit_log_append(char *log, const char *fmt, ...)
116132
{
117-
char line[KUNIT_LOG_SIZE];
118133
va_list args;
119-
int len_left;
134+
int len, log_len, len_left;
120135

121136
if (!log)
122137
return;
123138

124-
len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
139+
log_len = strlen(log);
140+
len_left = KUNIT_LOG_SIZE - log_len - 1;
125141
if (len_left <= 0)
126142
return;
127143

144+
/* Evaluate length of line to add to log */
145+
va_start(args, fmt);
146+
len = vsnprintf(NULL, 0, fmt, args) + 1;
147+
va_end(args);
148+
149+
/* Print formatted line to the log */
128150
va_start(args, fmt);
129-
vsnprintf(line, sizeof(line), fmt, args);
151+
vsnprintf(log + log_len, min(len, len_left), fmt, args);
130152
va_end(args);
131153

132-
strncat(log, line, len_left);
154+
/* Add newline to end of log if not already present. */
155+
kunit_log_newline(log);
133156
}
134157
EXPORT_SYMBOL_GPL(kunit_log_append);
135158

@@ -147,10 +170,18 @@ EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
147170

148171
static void kunit_print_suite_start(struct kunit_suite *suite)
149172
{
150-
kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
151-
kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
173+
/*
174+
* We do not log the test suite header as doing so would
175+
* mean debugfs display would consist of the test suite
176+
* header prior to individual test results.
177+
* Hence directly printk the suite status, and we will
178+
* separately seq_printf() the suite header for the debugfs
179+
* representation.
180+
*/
181+
pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
182+
pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
152183
suite->name);
153-
kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
184+
pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
154185
kunit_suite_num_test_cases(suite));
155186
}
156187

@@ -167,10 +198,9 @@ static void kunit_print_ok_not_ok(void *test_or_suite,
167198

168199
/*
169200
* We do not log the test suite results as doing so would
170-
* mean debugfs display would consist of the test suite
171-
* description and status prior to individual test results.
172-
* Hence directly printk the suite status, and we will
173-
* separately seq_printf() the suite status for the debugfs
201+
* mean debugfs display would consist of an incorrect test
202+
* number. Hence directly printk the suite result, and we will
203+
* separately seq_printf() the suite results for the debugfs
174204
* representation.
175205
*/
176206
if (suite)
@@ -437,7 +467,6 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
437467
struct kunit_try_catch_context context;
438468
struct kunit_try_catch *try_catch;
439469

440-
kunit_init_test(test, test_case->name, test_case->log);
441470
try_catch = &test->try_catch;
442471

443472
kunit_try_catch_init(try_catch,
@@ -533,6 +562,8 @@ int kunit_run_tests(struct kunit_suite *suite)
533562
struct kunit_result_stats param_stats = { 0 };
534563
test_case->status = KUNIT_SKIPPED;
535564

565+
kunit_init_test(&test, test_case->name, test_case->log);
566+
536567
if (!test_case->generate_params) {
537568
/* Non-parameterised test. */
538569
kunit_run_case_catch_errors(suite, test_case, &test);

0 commit comments

Comments
 (0)