diff mbox series

[RFC,bpf-next,3/5] selftests/bpf: generate boilerplate code for test_loader-based tests

Message ID 20230123145148.2791939-4-eddyz87@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series test_verifier tests migration to inline assembly | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 fail Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on s390x with gcc
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 10 maintainers not CCed: linux-kselftest@vger.kernel.org kpsingh@kernel.org haoluo@google.com song@kernel.org martin.lau@linux.dev sdf@google.com john.fastabend@gmail.com shuah@kernel.org jolsa@kernel.org mykolal@fb.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 67 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Eduard Zingerman Jan. 23, 2023, 2:51 p.m. UTC
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(+)

Comments

Andrii Nakryiko Jan. 26, 2023, 1:43 a.m. UTC | #1
On Mon, Jan 23, 2023 at 6:52 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> 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>
> ---

It feels like this is a bit of an overkill, tbh. There are

$ ls verifier/*.c | wc -l
94

files. We can move each migrated set of tests from verifier/xxx.c to
progs/verifier_xxx.c. And then just have just manually maintained
prog_tests/verifier.c file where for each converted test we have one
#include and one void test_some_test_1(void) { RUN_TESTS(some_test_1);
}.

It sometimes would useful to add some extra debugging printfs in such
a file, so having it auto generated would be actually an
inconvenience. And that on top of further Makefile complication.

For initial conversion we can auto-generate this file, of course. And
then for each migrated file adding 2 lines manually doesn't seem like
a big deal?



>  tools/testing/selftests/bpf/Makefile          | 34 +++++++++++++++++++
>  .../selftests/bpf/prog_tests/.gitignore       |  1 +
>  2 files changed, 35 insertions(+)
>

[...]
Eduard Zingerman Jan. 26, 2023, 11:29 p.m. UTC | #2
On Wed, 2023-01-25 at 17:43 -0800, Andrii Nakryiko wrote:
> On Mon, Jan 23, 2023 at 6:52 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
> > 
> > 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>
> > ---
> 
> It feels like this is a bit of an overkill, tbh. There are
> 
> $ ls verifier/*.c | wc -l
> 94
> 
> files. We can move each migrated set of tests from verifier/xxx.c to
> progs/verifier_xxx.c. And then just have just manually maintained
> prog_tests/verifier.c file where for each converted test we have one
> #include and one void test_some_test_1(void) { RUN_TESTS(some_test_1);
> }.
> 
> It sometimes would useful to add some extra debugging printfs in such
> a file, so having it auto generated would be actually an
> inconvenience. And that on top of further Makefile complication.
> 
> For initial conversion we can auto-generate this file, of course. And
> then for each migrated file adding 2 lines manually doesn't seem like
> a big deal?

Ok, I'll remove the makefile changes.

> 
> 
> 
> >  tools/testing/selftests/bpf/Makefile          | 34 +++++++++++++++++++
> >  .../selftests/bpf/prog_tests/.gitignore       |  1 +
> >  2 files changed, 35 insertions(+)
> > 
> 
> [...]
diff mbox series

Patch

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