Skip to content

Commit 766f2a5

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
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: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 61098e8 commit 766f2a5

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,8 @@ $(OUTPUT)/test_progs: test_progs.c $(PROG_TESTS_FILES) | $(PROG_TESTS_H)
239239
$(PROG_TESTS_H): $(PROG_TESTS_FILES) | $(PROG_TESTS_DIR)
240240
$(shell ( cd prog_tests/; \
241241
echo '/* Generated header, do not edit */'; \
242-
echo '#ifdef DECLARE'; \
243242
ls *.c 2> /dev/null | \
244-
sed -e 's@\([^\.]*\)\.c@extern void test_\1(void);@'; \
245-
echo '#endif'; \
246-
echo '#ifdef CALL'; \
247-
ls *.c 2> /dev/null | \
248-
sed -e 's@\([^\.]*\)\.c@test_\1();@'; \
249-
echo '#endif' \
243+
sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \
250244
) > $(PROG_TESTS_H))
251245

252246
MAP_TESTS_DIR = $(OUTPUT)/map_tests

tools/testing/selftests/bpf/test_progs.c

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
#include "test_progs.h"
55
#include "bpf_rlimit.h"
6+
#include <argp.h>
67

78
int error_cnt, pass_cnt;
89
bool jit_enabled;
@@ -156,22 +157,89 @@ void *spin_lock_thread(void *arg)
156157
pthread_exit(arg);
157158
}
158159

159-
#define DECLARE
160+
/* extern declarations for test funcs */
161+
#define DEFINE_TEST(name) extern void test_##name();
160162
#include <prog_tests/tests.h>
161-
#undef DECLARE
163+
#undef DEFINE_TEST
162164

163-
int main(int ac, char **av)
165+
struct prog_test_def {
166+
const char *test_name;
167+
void (*run_test)(void);
168+
};
169+
170+
static struct prog_test_def prog_test_defs[] = {
171+
#define DEFINE_TEST(name) { \
172+
.test_name = #name, \
173+
.run_test = &test_##name, \
174+
},
175+
#include <prog_tests/tests.h>
176+
#undef DEFINE_TEST
177+
};
178+
179+
const char *argp_program_version = "test_progs 0.1";
180+
const char *argp_program_bug_address = "<[email protected]>";
181+
const char argp_program_doc[] = "BPF selftests test runner";
182+
183+
enum ARG_KEYS {
184+
ARG_VERIFIER_STATS = 's',
185+
};
186+
187+
static const struct argp_option opts[] = {
188+
{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
189+
"Output verifier statistics", },
190+
{},
191+
};
192+
193+
struct test_env {
194+
bool verifier_stats;
195+
};
196+
197+
static struct test_env env = {};
198+
199+
static error_t parse_arg(int key, char *arg, struct argp_state *state)
164200
{
201+
struct test_env *env = state->input;
202+
203+
switch (key) {
204+
case ARG_VERIFIER_STATS:
205+
env->verifier_stats = true;
206+
break;
207+
case ARGP_KEY_ARG:
208+
argp_usage(state);
209+
break;
210+
case ARGP_KEY_END:
211+
break;
212+
default:
213+
return ARGP_ERR_UNKNOWN;
214+
}
215+
return 0;
216+
}
217+
218+
219+
int main(int argc, char **argv)
220+
{
221+
static const struct argp argp = {
222+
.options = opts,
223+
.parser = parse_arg,
224+
.doc = argp_program_doc,
225+
};
226+
const struct prog_test_def *def;
227+
int err, i;
228+
229+
err = argp_parse(&argp, argc, argv, 0, NULL, &env);
230+
if (err)
231+
return err;
232+
165233
srand(time(NULL));
166234

167235
jit_enabled = is_jit_enabled();
168236

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

172-
#define CALL
173-
#include <prog_tests/tests.h>
174-
#undef CALL
239+
for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
240+
def = &prog_test_defs[i];
241+
def->run_test();
242+
}
175243

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

0 commit comments

Comments
 (0)