All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com,
	yhs@fb.com, Eduard Zingerman <eddyz87@gmail.com>
Subject: [RFC bpf-next 3/5] selftests/bpf: generate boilerplate code for test_loader-based tests
Date: Mon, 23 Jan 2023 16:51:46 +0200	[thread overview]
Message-ID: <20230123145148.2791939-4-eddyz87@gmail.com> (raw)
In-Reply-To: <20230123145148.2791939-1-eddyz87@gmail.com>

Automatically generate boilerplate code necessary to run tests that
use test_loader.c.

Adds a target 'prog_tests/test_loader_auto_wrappers.c' as part of
rulesets for 'test_progs' and 'test_progs-no_alu32'. The content of
this C file is generated by make and has the following structure:

  #include <test_progs.h>

  #include "some_test_1.skel.h"
  #include "some_test_2.skel.h"
  ...

  void test_some_test_1(void) { RUN_TESTS(some_test_1); }
  void test_some_test_2(void) { RUN_TESTS(some_test_2); }
  ...

Here RUN_TESTS is a macro defined in test_progs.h, it expands to a
code that uses test_loader.c:test_loader__run_subtests() function to
load tests specified by appropriate skel.h.

In order to get the list of tests included in
'test_loader_auto_wrappers.c' the generation script looks for
'progs/*.c' files that contain a special comment:

  /* Use test_loader marker */

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 tools/testing/selftests/bpf/Makefile          | 34 +++++++++++++++++++
 .../selftests/bpf/prog_tests/.gitignore       |  1 +
 2 files changed, 35 insertions(+)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 26e66f9a0977..66ba2941677c 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -398,6 +398,11 @@ TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
 TRUNNER_BINARY := $1$(if $2,-)$2
 TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
 				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
+ifneq ($(TRUNNER_TEST_LOADER_AUTO_WRAPPERS),)
+TRUNNER_TEST_OBJS := $$(filter-out $$(TRUNNER_OUTPUT)/test_loader_auto_wrappers.test.o, \
+				   $$(TRUNNER_TEST_OBJS))
+TRUNNER_TEST_OBJS += $$(TRUNNER_OUTPUT)/test_loader_auto_wrappers.test.o
+endif
 TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
 				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
 TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
@@ -482,6 +487,32 @@ $(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
 		 ) > $$@)
 endif
 
+ifneq ($(TRUNNER_TEST_LOADER_AUTO_WRAPPERS),)
+ifeq ($($(TRUNNER_TESTS_DIR)-test-loader-auto-wrappers-c),)
+$(TRUNNER_TESTS_DIR)-test-loader-auto-wrappers-c := y
+$(TRUNNER_TESTS_DIR)/test_loader_auto_wrappers.c: $(TRUNNER_BPF_PROGS_DIR)/*.c
+	$$(call msg,GEN-TEST,$(TRUNNER_BINARY),$$@)
+	$$(shell (echo '/* Generated source, do not edit */';			\
+		  tests=$$$$(grep --null -lF '/* Use test_loader marker */'	\
+				$$(TRUNNER_BPF_PROGS_DIR)/*.c			\
+				| xargs -0 -I {} basename {} .c);		\
+		  echo "#include <test_progs.h>";				\
+		  echo "";							\
+		  for case in $$$$tests;					\
+		  do								\
+			echo "#include \"$$$$case.skel.h\"";			\
+		  done;								\
+		  echo "";							\
+		  for case in $$$$tests;					\
+		  do								\
+			printf	"void %-50s { %-50s }\n"			\
+				"test_$$$$case(void)"				\
+				"RUN_TESTS($$$$case);";				\
+		  done) > $$@)
+$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/test_loader_auto_wrappers.c
+endif
+endif # TRUNNER_TEST_LOADER_AUTO_WRAPPERS
+
 # compile individual test files
 # Note: we cd into output directory to ensure embedded BPF object is found
 $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
@@ -537,6 +568,7 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
 		       verify_sig_setup.sh				\
 		       $(wildcard progs/btf_dump_test_case_*.c)		\
 		       $(wildcard progs/*.bpf.o)
+TRUNNER_TEST_LOADER_AUTO_WRAPPERS := t
 TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
 $(eval $(call DEFINE_TEST_RUNNER,test_progs))
@@ -560,6 +592,7 @@ TRUNNER_EXTRA_SOURCES := test_maps.c
 TRUNNER_EXTRA_FILES :=
 TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
 TRUNNER_BPF_CFLAGS :=
+TRUNNER_TEST_LOADER_AUTO_WRAPPERS :=
 $(eval $(call DEFINE_TEST_RUNNER,test_maps))
 
 # Define test_verifier test runner.
@@ -625,6 +658,7 @@ $(OUTPUT)/veristat: $(OUTPUT)/veristat.o
 
 EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
 	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
+	prog_tests/test_loader_auto_wrappers.c				\
 	feature bpftool							\
 	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h	\
 			       no_alu32 bpf_gcc bpf_testmod.ko		\
diff --git a/tools/testing/selftests/bpf/prog_tests/.gitignore b/tools/testing/selftests/bpf/prog_tests/.gitignore
index 89c4a3d37544..6a937c1cd78d 100644
--- a/tools/testing/selftests/bpf/prog_tests/.gitignore
+++ b/tools/testing/selftests/bpf/prog_tests/.gitignore
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 tests.h
+test_loader_auto_wrappers.c
-- 
2.39.0


  parent reply	other threads:[~2023-01-23 14:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-23 14:51 [RFC bpf-next 0/5] test_verifier tests migration to inline assembly Eduard Zingerman
2023-01-23 14:51 ` [RFC bpf-next 1/5] selftests/bpf: support custom per-test flags and multiple expected messages Eduard Zingerman
2023-02-28 18:53   ` Andrii Nakryiko
2023-02-28 22:30     ` Eduard Zingerman
2023-03-01 17:12       ` Andrii Nakryiko
2023-03-01 17:58         ` Eduard Zingerman
2023-01-23 14:51 ` [RFC bpf-next 2/5] selftests/bpf: unprivileged tests for test_loader.c Eduard Zingerman
2023-01-23 14:51 ` Eduard Zingerman [this message]
2023-01-26  1:43   ` [RFC bpf-next 3/5] selftests/bpf: generate boilerplate code for test_loader-based tests Andrii Nakryiko
2023-01-26 23:29     ` Eduard Zingerman
2023-01-23 14:51 ` [RFC bpf-next 4/5] selftests/bpf: __imm_insn macro to embed raw insns in inline asm Eduard Zingerman
2023-01-26  2:48   ` Andrii Nakryiko
2023-01-23 14:51 ` [RFC bpf-next 5/5] selftests/bpf: convert jeq_infer_not_null tests to inline assembly Eduard Zingerman
2023-01-26  1:33 ` [RFC bpf-next 0/5] test_verifier tests migration " Andrii Nakryiko
2023-01-26  3:25   ` Alexei Starovoitov
2023-01-27  0:43     ` Eduard Zingerman
2023-01-27  0:30   ` Eduard Zingerman
2023-01-27 17:41     ` Andrii Nakryiko

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=20230123145148.2791939-4-eddyz87@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=yhs@fb.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.