Skip to content

Commit

Permalink
Merge tag 'linux-kselftest-kunit-6.4-rc1' of git://git.kernel.org/pub…
Browse files Browse the repository at this point in the history
…/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
  • Loading branch information
torvalds committed Apr 24, 2023
2 parents 0f50767 + a42077b commit 1be89fa
Show file tree
Hide file tree
Showing 17 changed files with 491 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ modules.order
!.get_maintainer.ignore
!.gitattributes
!.gitignore
!.kunitconfig
!.mailmap
!.rustfmt.toml

Expand Down
2 changes: 1 addition & 1 deletion include/kunit/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
* params.gfp = gfp;
*
* return kunit_alloc_resource(test, kunit_kmalloc_init,
* kunit_kmalloc_free, &params);
* kunit_kmalloc_free, gfp, &params);
* }
*
* Resources can also be named, with lookup/removal done on a name
Expand Down
4 changes: 2 additions & 2 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ DECLARE_STATIC_KEY_FALSE(kunit_running);
struct kunit;

/* Size of log associated with test. */
#define KUNIT_LOG_SIZE 512
#define KUNIT_LOG_SIZE 2048

/* Maximum size of parameter description string. */
#define KUNIT_PARAM_DESC_SIZE 128
Expand Down Expand Up @@ -420,7 +420,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
#define kunit_log(lvl, test_or_suite, fmt, ...) \
do { \
printk(lvl fmt, ##__VA_ARGS__); \
kunit_log_append((test_or_suite)->log, fmt "\n", \
kunit_log_append((test_or_suite)->log, fmt, \
##__VA_ARGS__); \
} while (0)

Expand Down
14 changes: 12 additions & 2 deletions lib/kunit/debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,24 @@ static int debugfs_print_results(struct seq_file *seq, void *v)
enum kunit_status success = kunit_suite_has_succeeded(suite);
struct kunit_case *test_case;

if (!suite || !suite->log)
if (!suite)
return 0;

seq_printf(seq, "%s", suite->log);
/* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
seq_puts(seq, "KTAP version 1\n");
seq_puts(seq, "1..1\n");

/* Print suite header because it is not stored in the test logs. */
seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));

kunit_suite_for_each_test_case(suite, test_case)
debugfs_print_result(seq, suite, test_case);

if (suite->log)
seq_printf(seq, "%s", suite->log);

seq_printf(seq, "%s %d %s\n",
kunit_status_to_ok_not_ok(success), 1, suite->name);
return 0;
Expand Down
77 changes: 64 additions & 13 deletions lib/kunit/kunit-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Author: Brendan Higgins <[email protected]>
*/
#include <kunit/test.h>
#include <kunit/test-bug.h>

#include "try-catch-impl.h"

Expand Down Expand Up @@ -443,18 +444,6 @@ static struct kunit_suite kunit_resource_test_suite = {
.test_cases = kunit_resource_test_cases,
};

static void kunit_log_test(struct kunit *test);

static struct kunit_case kunit_log_test_cases[] = {
KUNIT_CASE(kunit_log_test),
{}
};

static struct kunit_suite kunit_log_test_suite = {
.name = "kunit-log-test",
.test_cases = kunit_log_test_cases,
};

static void kunit_log_test(struct kunit *test)
{
struct kunit_suite suite;
Expand All @@ -481,6 +470,29 @@ static void kunit_log_test(struct kunit *test)
#endif
}

static void kunit_log_newline_test(struct kunit *test)
{
kunit_info(test, "Add newline\n");
if (test->log) {
KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"),
"Missing log line, full log:\n%s", test->log);
KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n"));
} else {
kunit_skip(test, "only useful when debugfs is enabled");
}
}

static struct kunit_case kunit_log_test_cases[] = {
KUNIT_CASE(kunit_log_test),
KUNIT_CASE(kunit_log_newline_test),
{}
};

static struct kunit_suite kunit_log_test_suite = {
.name = "kunit-log-test",
.test_cases = kunit_log_test_cases,
};

static void kunit_status_set_failure_test(struct kunit *test)
{
struct kunit fake;
Expand Down Expand Up @@ -521,7 +533,46 @@ static struct kunit_suite kunit_status_test_suite = {
.test_cases = kunit_status_test_cases,
};

static void kunit_current_test(struct kunit *test)
{
/* Check results of both current->kunit_test and
* kunit_get_current_test() are equivalent to current test.
*/
KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
}

static void kunit_current_fail_test(struct kunit *test)
{
struct kunit fake;

kunit_init_test(&fake, "fake test", NULL);
KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);

/* Set current->kunit_test to fake test. */
current->kunit_test = &fake;

kunit_fail_current_test("This should make `fake` test fail.");
KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
kunit_cleanup(&fake);

/* Reset current->kunit_test to current test. */
current->kunit_test = test;
}

static struct kunit_case kunit_current_test_cases[] = {
KUNIT_CASE(kunit_current_test),
KUNIT_CASE(kunit_current_fail_test),
{}
};

static struct kunit_suite kunit_current_test_suite = {
.name = "kunit_current",
.test_cases = kunit_current_test_cases,
};

kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
&kunit_log_test_suite, &kunit_status_test_suite);
&kunit_log_test_suite, &kunit_status_test_suite,
&kunit_current_test_suite);

MODULE_LICENSE("GPL v2");
57 changes: 44 additions & 13 deletions lib/kunit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,51 @@ static void kunit_print_test_stats(struct kunit *test,
stats.total);
}

/**
* kunit_log_newline() - Add newline to the end of log if one is not
* already present.
* @log: The log to add the newline to.
*/
static void kunit_log_newline(char *log)
{
int log_len, len_left;

log_len = strlen(log);
len_left = KUNIT_LOG_SIZE - log_len - 1;

if (log_len > 0 && log[log_len - 1] != '\n')
strncat(log, "\n", len_left);
}

/*
* Append formatted message to log, size of which is limited to
* KUNIT_LOG_SIZE bytes (including null terminating byte).
*/
void kunit_log_append(char *log, const char *fmt, ...)
{
char line[KUNIT_LOG_SIZE];
va_list args;
int len_left;
int len, log_len, len_left;

if (!log)
return;

len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
log_len = strlen(log);
len_left = KUNIT_LOG_SIZE - log_len - 1;
if (len_left <= 0)
return;

/* Evaluate length of line to add to log */
va_start(args, fmt);
len = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args);

/* Print formatted line to the log */
va_start(args, fmt);
vsnprintf(line, sizeof(line), fmt, args);
vsnprintf(log + log_len, min(len, len_left), fmt, args);
va_end(args);

strncat(log, line, len_left);
/* Add newline to end of log if not already present. */
kunit_log_newline(log);
}
EXPORT_SYMBOL_GPL(kunit_log_append);

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

static void kunit_print_suite_start(struct kunit_suite *suite)
{
kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
/*
* We do not log the test suite header as doing so would
* mean debugfs display would consist of the test suite
* header prior to individual test results.
* Hence directly printk the suite status, and we will
* separately seq_printf() the suite header for the debugfs
* representation.
*/
pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
suite->name);
kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
kunit_suite_num_test_cases(suite));
}

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

/*
* We do not log the test suite results as doing so would
* mean debugfs display would consist of the test suite
* description and status prior to individual test results.
* Hence directly printk the suite status, and we will
* separately seq_printf() the suite status for the debugfs
* mean debugfs display would consist of an incorrect test
* number. Hence directly printk the suite result, and we will
* separately seq_printf() the suite results for the debugfs
* representation.
*/
if (suite)
Expand Down Expand Up @@ -437,7 +467,6 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
struct kunit_try_catch_context context;
struct kunit_try_catch *try_catch;

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

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

kunit_init_test(&test, test_case->name, test_case->log);

if (!test_case->generate_params) {
/* Non-parameterised test. */
kunit_run_case_catch_errors(suite, test_case, &test);
Expand Down
Loading

0 comments on commit 1be89fa

Please sign in to comment.