Commit 766f2a59 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: revamp test_progs to allow more control

Refactor test_progs to allow better control on what's being run.
Also use argp to do argument parsing, so that it's easier to keep adding
more options.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 61098e89
...@@ -239,14 +239,8 @@ $(OUTPUT)/test_progs: test_progs.c $(PROG_TESTS_FILES) | $(PROG_TESTS_H) ...@@ -239,14 +239,8 @@ $(OUTPUT)/test_progs: test_progs.c $(PROG_TESTS_FILES) | $(PROG_TESTS_H)
$(PROG_TESTS_H): $(PROG_TESTS_FILES) | $(PROG_TESTS_DIR) $(PROG_TESTS_H): $(PROG_TESTS_FILES) | $(PROG_TESTS_DIR)
$(shell ( cd prog_tests/; \ $(shell ( cd prog_tests/; \
echo '/* Generated header, do not edit */'; \ echo '/* Generated header, do not edit */'; \
echo '#ifdef DECLARE'; \
ls *.c 2> /dev/null | \ ls *.c 2> /dev/null | \
sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \ sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
echo '#endif'; \
echo '#ifdef CALL'; \
ls *.c 2> /dev/null | \
sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
echo '#endif' \
) > $(PROG_TESTS_H)) ) > $(PROG_TESTS_H))
MAP_TESTS_DIR = $(OUTPUT)/map_tests MAP_TESTS_DIR = $(OUTPUT)/map_tests
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
*/ */
#include "test_progs.h" #include "test_progs.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include <argp.h>
int error_cnt, pass_cnt; int error_cnt, pass_cnt;
bool jit_enabled; bool jit_enabled;
...@@ -156,22 +157,89 @@ void *spin_lock_thread(void *arg) ...@@ -156,22 +157,89 @@ void *spin_lock_thread(void *arg)
pthread_exit(arg); pthread_exit(arg);
} }
#define DECLARE /* extern declarations for test funcs */
#define DEFINE_TEST(name) extern void test_##name();
#include <prog_tests/tests.h> #include <prog_tests/tests.h>
#undef DECLARE #undef DEFINE_TEST
int main(int ac, char **av) struct prog_test_def {
const char *test_name;
void (*run_test)(void);
};
static struct prog_test_def prog_test_defs[] = {
#define DEFINE_TEST(name) { \
.test_name = #name, \
.run_test = &test_##name, \
},
#include <prog_tests/tests.h>
#undef DEFINE_TEST
};
const char *argp_program_version = "test_progs 0.1";
const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
const char argp_program_doc[] = "BPF selftests test runner";
enum ARG_KEYS {
ARG_VERIFIER_STATS = 's',
};
static const struct argp_option opts[] = {
{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
"Output verifier statistics", },
{},
};
struct test_env {
bool verifier_stats;
};
static struct test_env env = {};
static error_t parse_arg(int key, char *arg, struct argp_state *state)
{ {
struct test_env *env = state->input;
switch (key) {
case ARG_VERIFIER_STATS:
env->verifier_stats = true;
break;
case ARGP_KEY_ARG:
argp_usage(state);
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
int main(int argc, char **argv)
{
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
.doc = argp_program_doc,
};
const struct prog_test_def *def;
int err, i;
err = argp_parse(&argp, argc, argv, 0, NULL, &env);
if (err)
return err;
srand(time(NULL)); srand(time(NULL));
jit_enabled = is_jit_enabled(); jit_enabled = is_jit_enabled();
if (ac == 2 && strcmp(av[1], "-s") == 0) verifier_stats = env.verifier_stats;
verifier_stats = true;
#define CALL for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
#include <prog_tests/tests.h> def = &prog_test_defs[i];
#undef CALL def->run_test();
}
printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS; return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment