From: Jakub Kicinski <kuba@kernel.org>
To: jakub@cloudflare.com
Cc: shuah@kernel.org, keescook@chromium.org,
linux-kselftest@vger.kernel.org, netdev@vger.kernel.org,
Jakub Kicinski <kuba@kernel.org>
Subject: [RFC 4/7] selftests: kselftest: add ksft_test_result_code(), handling all exit codes
Date: Thu, 15 Feb 2024 16:41:19 -0800 [thread overview]
Message-ID: <20240216004122.2004689-5-kuba@kernel.org> (raw)
In-Reply-To: <20240216004122.2004689-1-kuba@kernel.org>
For generic test harness code it's more useful to deal with exit
codes directly, rather than having to switch on them and call
the right ksft_test_result_*() helper. Add such function to kselftest.h.
Note that "directive" and "diagnostic" are what ktap docs call
those parts of the message.
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
tools/testing/selftests/kselftest.h | 39 +++++++++++++++++++++
tools/testing/selftests/kselftest_harness.h | 14 ++++----
2 files changed, 47 insertions(+), 6 deletions(-)
diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index a781e6311810..12ad7f8dfe3a 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -25,6 +25,7 @@
* ksft_test_result_skip(fmt, ...);
* ksft_test_result_xfail(fmt, ...);
* ksft_test_result_error(fmt, ...);
+ * ksft_test_result_code(exit_code, test_name, fmt, ...);
*
* When all tests are finished, clean up and exit the program with one of:
*
@@ -254,6 +255,44 @@ static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
va_end(args);
}
+static inline __printf(2, 3)
+void ksft_test_result_code(int exit_code, const char *msg, ...)
+{
+ const char *tap_code = "ok";
+ const char *directive = "";
+ int saved_errno = errno;
+ va_list args;
+
+ switch (exit_code) {
+ case KSFT_PASS:
+ ksft_cnt.ksft_pass++;
+ break;
+ case KSFT_XFAIL:
+ directive = " # XFAIL ";
+ ksft_cnt.ksft_xfail++;
+ break;
+ case KSFT_XPASS:
+ directive = " # XPASS ";
+ ksft_cnt.ksft_xpass++;
+ break;
+ case KSFT_SKIP:
+ directive = " # SKIP ";
+ ksft_cnt.ksft_xskip++;
+ break;
+ case KSFT_FAIL:
+ default:
+ tap_code = "not ok";
+ ksft_cnt.ksft_fail++;
+ break;
+ }
+
+ va_start(args, msg);
+ printf("%s %u%s", tap_code, ksft_test_num(), directive);
+ errno = saved_errno;
+ vprintf(msg, args);
+ va_end(args);
+}
+
static inline int ksft_exit_pass(void)
{
ksft_print_cnts();
diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 6923cd7060fc..79a3fec0cefa 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -1140,6 +1140,7 @@ void __run_test(struct __fixture_metadata *f,
struct __test_metadata *t)
{
char test_name[LINE_MAX];
+ const char *diagnostic;
/* reset test struct */
t->exit_code = KSFT_PASS;
@@ -1171,12 +1172,13 @@ void __run_test(struct __fixture_metadata *f,
ksft_print_msg(" %4s %s\n",
__test_passed(t) ? "OK" : "FAIL", test_name);
- if (t->exit_code == KSFT_SKIP)
- ksft_test_result_skip("%s\n", t->results->reason[0] ?
- t->results->reason : "unknown");
- else if (t->exit_code == KSFT_XFAIL)
- ksft_test_result_xfail("%s\n", t->results->reason[0] ?
- t->results->reason : "unknown");
+ if (t->results->reason[0])
+ diagnostic = t->results->reason;
+ else
+ diagnostic = "unknown";
+
+ if (t->exit_code == KSFT_SKIP || t->exit_code == KSFT_XFAIL)
+ ksft_test_result_code(t->exit_code, "%s\n", diagnostic);
else
ksft_test_result(__test_passed(t), "%s\n", test_name);
}
--
2.43.0
next prev parent reply other threads:[~2024-02-16 0:41 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-16 0:41 [RFC 0/7] selftests: kselftest_harness: use common result printing helper Jakub Kicinski
2024-02-16 0:41 ` [RFC 1/7] selftests: kselftest_harness: generate test name once Jakub Kicinski
2024-02-16 21:24 ` Kees Cook
2024-02-16 0:41 ` [RFC 2/7] selftests: kselftest_harness: save full exit code in metadata Jakub Kicinski
2024-02-16 0:41 ` [RFC 3/7] selftests: kselftest_harness: use exit code to store skip and xfail Jakub Kicinski
2024-02-16 0:41 ` Jakub Kicinski [this message]
2024-02-16 0:41 ` [RFC 5/7] selftests: kselftest_harness: print test name for SKIP and XFAIL Jakub Kicinski
2024-02-16 0:41 ` [RFC 6/7] selftests: kselftest_harness: let ksft_test_result_code() handle line termination Jakub Kicinski
2024-02-16 0:41 ` [RFC 7/7] selftests: kselftest_harness: let PASS / FAIL provide diagnostic Jakub Kicinski
2024-02-16 21:32 ` [RFC 0/7] selftests: kselftest_harness: use common result printing helper Kees Cook
2024-02-17 0:31 ` Jakub Kicinski
2024-02-17 0:33 ` Jakub Kicinski
2024-02-17 1:26 ` Jakub Kicinski
2024-02-17 1:48 ` Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240216004122.2004689-5-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=jakub@cloudflare.com \
--cc=keescook@chromium.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).