linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
To: "Shuah Khan" <shuah@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Willy Tarreau" <w@1wt.eu>,
	"Thomas Weißschuh" <linux@weissschuh.net>
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Subject: [PATCH v2 01/32] selftests: harness: Add harness selftest
Date: Mon, 07 Apr 2025 08:52:24 +0200	[thread overview]
Message-ID: <20250407-nolibc-kselftest-harness-v2-1-f8812f76e930@linutronix.de> (raw)
In-Reply-To: <20250407-nolibc-kselftest-harness-v2-0-f8812f76e930@linutronix.de>

Add a selftest for the harness itself so any changes can be validated.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
 tools/testing/selftests/Makefile                   |   1 +
 tools/testing/selftests/kselftest/.gitignore       |   1 +
 tools/testing/selftests/kselftest/Makefile         |   6 +
 .../testing/selftests/kselftest/harness-selftest.c | 129 +++++++++++++++++++++
 .../selftests/kselftest/harness-selftest.expected  |  62 ++++++++++
 .../selftests/kselftest/harness-selftest.sh        |  14 +++
 6 files changed, 213 insertions(+)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index c77c8c8e3d9bdd8047c9cb7722c3830447e504e5..3ce071eefa86af59aadd1df7972fdf3bf6e01d52 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -48,6 +48,7 @@ TARGETS += ipc
 TARGETS += ir
 TARGETS += kcmp
 TARGETS += kexec
+TARGETS += kselftest
 TARGETS += kvm
 TARGETS += landlock
 TARGETS += lib
diff --git a/tools/testing/selftests/kselftest/.gitignore b/tools/testing/selftests/kselftest/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..9aab194e8ea25a2c5ccc116f9f728b8afd0c51c4
--- /dev/null
+++ b/tools/testing/selftests/kselftest/.gitignore
@@ -0,0 +1 @@
+/harness-selftest
diff --git a/tools/testing/selftests/kselftest/Makefile b/tools/testing/selftests/kselftest/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..2f2933553dbc47014d9dfe0b4bd8ad3bc36e38c2
--- /dev/null
+++ b/tools/testing/selftests/kselftest/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := harness-selftest
+TEST_PROGS := harness-selftest.sh
+
+include ../lib.mk
diff --git a/tools/testing/selftests/kselftest/harness-selftest.c b/tools/testing/selftests/kselftest/harness-selftest.c
new file mode 100644
index 0000000000000000000000000000000000000000..8d39e7a0b99c41a5d33edfe2dbf875cac04c098d
--- /dev/null
+++ b/tools/testing/selftests/kselftest/harness-selftest.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+
+#ifndef NOLIBC
+#include <sys/resource.h>
+#include <sys/prctl.h>
+#endif
+
+/* Avoid any inconsistencies */
+#define TH_LOG_STREAM stdout
+
+#include "../kselftest_harness.h"
+
+TEST(standalone_pass) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 0);
+	EXPECT_EQ(0, 0);
+	TH_LOG("after");
+}
+
+TEST(standalone_fail) {
+	TH_LOG("before");
+	EXPECT_EQ(0, 0);
+	EXPECT_EQ(0, 1);
+	ASSERT_EQ(0, 1);
+	TH_LOG("after");
+}
+
+TEST_SIGNAL(signal_pass, SIGUSR1) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 0);
+	TH_LOG("after");
+	kill(getpid(), SIGUSR1);
+}
+
+TEST_SIGNAL(signal_fail, SIGUSR1) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 1);
+	TH_LOG("after");
+	kill(getpid(), SIGUSR1);
+}
+
+FIXTURE(fixture) {
+	pid_t testpid;
+};
+
+FIXTURE_SETUP(fixture) {
+	TH_LOG("setup");
+	self->testpid = getpid();
+}
+
+FIXTURE_TEARDOWN(fixture) {
+	TH_LOG("teardown same-process=%d", self->testpid == getpid());
+}
+
+TEST_F(fixture, pass) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 0);
+	TH_LOG("after");
+}
+
+TEST_F(fixture, fail) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 1);
+	TH_LOG("after");
+}
+
+TEST_F_TIMEOUT(fixture, timeout, 1) {
+	TH_LOG("before");
+	sleep(2);
+	TH_LOG("after");
+}
+
+FIXTURE(fixture_parent) {
+	pid_t testpid;
+};
+
+FIXTURE_SETUP(fixture_parent) {
+	TH_LOG("setup");
+	self->testpid = getpid();
+}
+
+FIXTURE_TEARDOWN_PARENT(fixture_parent) {
+	TH_LOG("teardown same-process=%d", self->testpid == getpid());
+}
+
+TEST_F(fixture_parent, pass) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 0);
+	TH_LOG("after");
+}
+
+FIXTURE(fixture_setup_failure) {
+	pid_t testpid;
+};
+
+FIXTURE_SETUP(fixture_setup_failure) {
+	TH_LOG("setup");
+	self->testpid = getpid();
+	ASSERT_EQ(0, 1);
+}
+
+FIXTURE_TEARDOWN(fixture_setup_failure) {
+	TH_LOG("teardown same-process=%d", self->testpid == getpid());
+}
+
+TEST_F(fixture_setup_failure, pass) {
+	TH_LOG("before");
+	ASSERT_EQ(0, 0);
+	TH_LOG("after");
+}
+
+int main(int argc, char **argv)
+{
+	/*
+	 * The harness uses abort() to signal assertion failures, which triggers coredumps.
+	 * This may be useful to debug real failures but not for this selftest, disable them.
+	 */
+	struct rlimit rlimit = {
+		.rlim_cur = 0,
+		.rlim_max = 0,
+	};
+
+	prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
+	setrlimit(RLIMIT_CORE, &rlimit);
+
+	return test_harness_run(argc, argv);
+}
diff --git a/tools/testing/selftests/kselftest/harness-selftest.expected b/tools/testing/selftests/kselftest/harness-selftest.expected
new file mode 100644
index 0000000000000000000000000000000000000000..1aa6461db90d4e7cc0679f19b69aadf4032875ec
--- /dev/null
+++ b/tools/testing/selftests/kselftest/harness-selftest.expected
@@ -0,0 +1,62 @@
+TAP version 13
+1..9
+# Starting 9 tests from 4 test cases.
+#  RUN           global.standalone_pass ...
+# harness-selftest.c:16:standalone_pass:before
+# harness-selftest.c:19:standalone_pass:after
+#            OK  global.standalone_pass
+ok 1 global.standalone_pass
+#  RUN           global.standalone_fail ...
+# harness-selftest.c:23:standalone_fail:before
+# harness-selftest.c:25:standalone_fail:Expected 0 (0) == 1 (1)
+# harness-selftest.c:26:standalone_fail:Expected 0 (0) == 1 (1)
+# standalone_fail: Test terminated by assertion
+#          FAIL  global.standalone_fail
+not ok 2 global.standalone_fail
+#  RUN           global.signal_pass ...
+# harness-selftest.c:31:signal_pass:before
+# harness-selftest.c:33:signal_pass:after
+#            OK  global.signal_pass
+ok 3 global.signal_pass
+#  RUN           global.signal_fail ...
+# harness-selftest.c:38:signal_fail:before
+# harness-selftest.c:39:signal_fail:Expected 0 (0) == 1 (1)
+# signal_fail: Test terminated by assertion
+#          FAIL  global.signal_fail
+not ok 4 global.signal_fail
+#  RUN           fixture.pass ...
+# harness-selftest.c:49:pass:setup
+# harness-selftest.c:58:pass:before
+# harness-selftest.c:60:pass:after
+# harness-selftest.c:54:pass:teardown same-process=1
+#            OK  fixture.pass
+ok 5 fixture.pass
+#  RUN           fixture.fail ...
+# harness-selftest.c:49:fail:setup
+# harness-selftest.c:64:fail:before
+# harness-selftest.c:65:fail:Expected 0 (0) == 1 (1)
+# harness-selftest.c:54:fail:teardown same-process=1
+# fail: Test terminated by assertion
+#          FAIL  fixture.fail
+not ok 6 fixture.fail
+#  RUN           fixture.timeout ...
+# harness-selftest.c:49:timeout:setup
+# harness-selftest.c:70:timeout:before
+# timeout: Test terminated by timeout
+#          FAIL  fixture.timeout
+not ok 7 fixture.timeout
+#  RUN           fixture_parent.pass ...
+# harness-selftest.c:80:pass:setup
+# harness-selftest.c:89:pass:before
+# harness-selftest.c:91:pass:after
+# harness-selftest.c:85:pass:teardown same-process=0
+#            OK  fixture_parent.pass
+ok 8 fixture_parent.pass
+#  RUN           fixture_setup_failure.pass ...
+# harness-selftest.c:99:pass:setup
+# harness-selftest.c:101:pass:Expected 0 (0) == 1 (1)
+# pass: Test terminated by assertion
+#          FAIL  fixture_setup_failure.pass
+not ok 9 fixture_setup_failure.pass
+# FAILED: 4 / 9 tests passed.
+# Totals: pass:4 fail:5 xfail:0 xpass:0 skip:0 error:0
diff --git a/tools/testing/selftests/kselftest/harness-selftest.sh b/tools/testing/selftests/kselftest/harness-selftest.sh
new file mode 100755
index 0000000000000000000000000000000000000000..bf691adf0b41aaedb2fbc5f06add9fd1d5689044
--- /dev/null
+++ b/tools/testing/selftests/kselftest/harness-selftest.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Selftest for kselftest_harness.h
+#
+
+DIR="$(dirname $(readlink -f "$0"))"
+
+TMPFILE="$(mktemp)"
+trap 'rm "$TMPFILE"' EXIT
+
+$DIR/harness-selftest > "$TMPFILE"
+
+diff -u "$DIR"/harness-selftest.expected "$TMPFILE"

-- 
2.49.0


  reply	other threads:[~2025-04-07  6:53 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-07  6:52 [PATCH v2 00/32] kselftest harness and nolibc compatibility Thomas Weißschuh
2025-04-07  6:52 ` Thomas Weißschuh [this message]
2025-04-10 15:19   ` [PATCH v2 01/32] selftests: harness: Add harness selftest Shuah Khan
2025-04-07  6:52 ` [PATCH v2 02/32] selftests: harness: Use C89 comment style Thomas Weißschuh
2025-04-10 15:33   ` Shuah Khan
2025-04-07  6:52 ` [PATCH v2 03/32] selftests: harness: Ignore unused variant argument warning Thomas Weißschuh
2025-04-10 15:33   ` Shuah Khan
2025-04-07  6:52 ` [PATCH v2 04/32] selftests: harness: Mark functions without prototypes static Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 05/32] selftests: harness: Remove inline qualifier for wrappers Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 06/32] selftests: harness: Remove dependency on libatomic Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 07/32] selftests: harness: Implement test timeouts through pidfd Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 08/32] selftests: harness: Don't set setup_completed for fixtureless tests Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 09/32] selftests: harness: Always provide "self" and "variant" Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 10/32] selftests: harness: Move teardown conditional into test metadata Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 11/32] selftests: harness: Add teardown callback to " Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 12/32] selftests: harness: Stop using setjmp()/longjmp() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 13/32] selftests: harness: Guard includes on nolibc Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 14/32] tools/nolibc: handle intmax_t/uintmax_t in printf Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 15/32] tools/nolibc: use intmax definitions from compiler Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 16/32] tools/nolibc: use pselect6_time64 if available Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 17/32] tools/nolibc: use ppoll_time64 " Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 18/32] tools/nolibc: add tolower() and toupper() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 19/32] tools/nolibc: add _exit() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 20/32] tools/nolibc: add setpgrp() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 21/32] tools/nolibc: implement waitpid() in terms of waitid() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 22/32] Revert "selftests/nolibc: use waitid() over waitpid()" Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 23/32] tools/nolibc: add dprintf() and vdprintf() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 24/32] tools/nolibc: add getopt() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 25/32] tools/nolibc: allow different write callbacks in printf Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 26/32] tools/nolibc: allow limiting of printf destination size Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 27/32] tools/nolibc: add snprintf() and friends Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 28/32] selftests/nolibc: use snprintf() for printf tests Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 29/32] selftests/nolibc: rename vfprintf test suite Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 30/32] selftests/nolibc: add test for snprintf() truncation Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 31/32] tools/nolibc: implement width padding in printf() Thomas Weißschuh
2025-04-07  6:52 ` [PATCH v2 32/32] HACK: selftests/nolibc: demonstrate usage of the kselftest harness Thomas Weißschuh
2025-04-10 15:19 ` [PATCH v2 00/32] kselftest harness and nolibc compatibility Shuah Khan
2025-04-10 20:01   ` Shuah Khan

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=20250407-nolibc-kselftest-harness-v2-1-f8812f76e930@linutronix.de \
    --to=thomas.weissschuh@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=w@1wt.eu \
    /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).