Skip to content

Commit

Permalink
selftests/bpf: revamp test_progs to allow more control
Browse files Browse the repository at this point in the history
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: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Jul 28, 2019
1 parent 61098e8 commit 766f2a5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 15 deletions.
8 changes: 1 addition & 7 deletions tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
$(shell ( cd prog_tests/; \
echo '/* Generated header, do not edit */'; \
echo '#ifdef DECLARE'; \
ls *.c 2> /dev/null | \
sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
echo '#endif'; \
echo '#ifdef CALL'; \
ls *.c 2> /dev/null | \
sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
echo '#endif' \
sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
) > $(PROG_TESTS_H))

MAP_TESTS_DIR = $(OUTPUT)/map_tests
Expand Down
84 changes: 76 additions & 8 deletions tools/testing/selftests/bpf/test_progs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
#include "test_progs.h"
#include "bpf_rlimit.h"
#include <argp.h>

int error_cnt, pass_cnt;
bool jit_enabled;
Expand Down Expand Up @@ -156,22 +157,89 @@ void *spin_lock_thread(void *arg)
pthread_exit(arg);
}

#define DECLARE
/* extern declarations for test funcs */
#define DEFINE_TEST(name) extern void test_##name();
#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));

jit_enabled = is_jit_enabled();

if (ac == 2 && strcmp(av[1], "-s") == 0)
verifier_stats = true;
verifier_stats = env.verifier_stats;

#define CALL
#include <prog_tests/tests.h>
#undef CALL
for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
def = &prog_test_defs[i];
def->run_test();
}

printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
Expand Down

0 comments on commit 766f2a5

Please sign in to comment.