Skip to content

Commit 3a516a0

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: add sub-tests support for test_progs
Allow tests to have their own set of sub-tests. Also add ability to do test/subtest selection using `-t <test-name>/<subtest-name>` and `-n <test-nums-set>/<subtest-nums-set>`, as an extension of existing -t/-n selector options. For the <test-num-set> format: it's a comma-separated list of either individual test numbers (1-based), or range of test numbers. E.g., all of the following are valid sets of test numbers: - 10 - 1,2,3 - 1-3 - 5-10,1,3-4 '/<subtest' part is optional, but has the same format. E.g., to select test #3 and its sub-tests #10 through #15, use: -t 3/10-15. Similarly, to select tests by name, use `-t verif/strobe`: $ sudo ./test_progs -t verif/strobe #3/12 strobemeta.o:OK #3/13 strobemeta_nounroll1.o:OK #3/14 strobemeta_nounroll2.o:OK #3 bpf_verif_scale:OK Summary: 1/3 PASSED, 0 FAILED Example of using subtest API is in the next patch, converting bpf_verif_scale.c tests to use sub-tests. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 0ff97e5 commit 3a516a0

File tree

2 files changed

+185
-29
lines changed

2 files changed

+185
-29
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 173 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
#include <string.h>
88

99
/* defined in test_progs.h */
10-
struct test_env env = {
11-
.test_num_selector = -1,
12-
};
10+
struct test_env env;
1311
int error_cnt, pass_cnt;
1412

1513
struct prog_test_def {
@@ -20,8 +18,82 @@ struct prog_test_def {
2018
int pass_cnt;
2119
int error_cnt;
2220
bool tested;
21+
22+
const char *subtest_name;
23+
int subtest_num;
24+
25+
/* store counts before subtest started */
26+
int old_pass_cnt;
27+
int old_error_cnt;
2328
};
2429

30+
static bool should_run(struct test_selector *sel, int num, const char *name)
31+
{
32+
if (sel->name && sel->name[0] && !strstr(name, sel->name))
33+
return false;
34+
35+
if (!sel->num_set)
36+
return true;
37+
38+
return num < sel->num_set_len && sel->num_set[num];
39+
}
40+
41+
static void dump_test_log(const struct prog_test_def *test, bool failed)
42+
{
43+
if (env.verbose || test->force_log || failed) {
44+
if (env.log_cnt) {
45+
fprintf(stdout, "%s", env.log_buf);
46+
if (env.log_buf[env.log_cnt - 1] != '\n')
47+
fprintf(stdout, "\n");
48+
}
49+
env.log_cnt = 0;
50+
}
51+
}
52+
53+
void test__end_subtest()
54+
{
55+
struct prog_test_def *test = env.test;
56+
int sub_error_cnt = error_cnt - test->old_error_cnt;
57+
58+
if (sub_error_cnt)
59+
env.fail_cnt++;
60+
else
61+
env.sub_succ_cnt++;
62+
63+
dump_test_log(test, sub_error_cnt);
64+
65+
printf("#%d/%d %s:%s\n",
66+
test->test_num, test->subtest_num,
67+
test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
68+
}
69+
70+
bool test__start_subtest(const char *name)
71+
{
72+
struct prog_test_def *test = env.test;
73+
74+
if (test->subtest_name) {
75+
test__end_subtest();
76+
test->subtest_name = NULL;
77+
}
78+
79+
test->subtest_num++;
80+
81+
if (!name || !name[0]) {
82+
fprintf(stderr, "Subtest #%d didn't provide sub-test name!\n",
83+
test->subtest_num);
84+
return false;
85+
}
86+
87+
if (!should_run(&env.subtest_selector, test->subtest_num, name))
88+
return false;
89+
90+
test->subtest_name = name;
91+
env.test->old_pass_cnt = pass_cnt;
92+
env.test->old_error_cnt = error_cnt;
93+
94+
return true;
95+
}
96+
2597
void test__force_log() {
2698
env.test->force_log = true;
2799
}
@@ -281,24 +353,103 @@ static int libbpf_print_fn(enum libbpf_print_level level,
281353
return 0;
282354
}
283355

356+
int parse_num_list(const char *s, struct test_selector *sel)
357+
{
358+
int i, set_len = 0, num, start = 0, end = -1;
359+
bool *set = NULL, *tmp, parsing_end = false;
360+
char *next;
361+
362+
while (s[0]) {
363+
errno = 0;
364+
num = strtol(s, &next, 10);
365+
if (errno)
366+
return -errno;
367+
368+
if (parsing_end)
369+
end = num;
370+
else
371+
start = num;
372+
373+
if (!parsing_end && *next == '-') {
374+
s = next + 1;
375+
parsing_end = true;
376+
continue;
377+
} else if (*next == ',') {
378+
parsing_end = false;
379+
s = next + 1;
380+
end = num;
381+
} else if (*next == '\0') {
382+
parsing_end = false;
383+
s = next;
384+
end = num;
385+
} else {
386+
return -EINVAL;
387+
}
388+
389+
if (start > end)
390+
return -EINVAL;
391+
392+
if (end + 1 > set_len) {
393+
set_len = end + 1;
394+
tmp = realloc(set, set_len);
395+
if (!tmp) {
396+
free(set);
397+
return -ENOMEM;
398+
}
399+
set = tmp;
400+
}
401+
for (i = start; i <= end; i++) {
402+
set[i] = true;
403+
}
404+
405+
}
406+
407+
if (!set)
408+
return -EINVAL;
409+
410+
sel->num_set = set;
411+
sel->num_set_len = set_len;
412+
413+
return 0;
414+
}
415+
284416
static error_t parse_arg(int key, char *arg, struct argp_state *state)
285417
{
286418
struct test_env *env = state->input;
287419

288420
switch (key) {
289421
case ARG_TEST_NUM: {
290-
int test_num;
422+
char *subtest_str = strchr(arg, '/');
291423

292-
errno = 0;
293-
test_num = strtol(arg, NULL, 10);
294-
if (errno)
295-
return -errno;
296-
env->test_num_selector = test_num;
424+
if (subtest_str) {
425+
*subtest_str = '\0';
426+
if (parse_num_list(subtest_str + 1,
427+
&env->subtest_selector)) {
428+
fprintf(stderr,
429+
"Failed to parse subtest numbers.\n");
430+
return -EINVAL;
431+
}
432+
}
433+
if (parse_num_list(arg, &env->test_selector)) {
434+
fprintf(stderr, "Failed to parse test numbers.\n");
435+
return -EINVAL;
436+
}
297437
break;
298438
}
299-
case ARG_TEST_NAME:
300-
env->test_name_selector = arg;
439+
case ARG_TEST_NAME: {
440+
char *subtest_str = strchr(arg, '/');
441+
442+
if (subtest_str) {
443+
*subtest_str = '\0';
444+
env->subtest_selector.name = strdup(subtest_str + 1);
445+
if (!env->subtest_selector.name)
446+
return -ENOMEM;
447+
}
448+
env->test_selector.name = strdup(arg);
449+
if (!env->test_selector.name)
450+
return -ENOMEM;
301451
break;
452+
}
302453
case ARG_VERIFIER_STATS:
303454
env->verifier_stats = true;
304455
break;
@@ -353,14 +504,15 @@ int main(int argc, char **argv)
353504
env.test = test;
354505
test->test_num = i + 1;
355506

356-
if (env.test_num_selector >= 0 &&
357-
test->test_num != env.test_num_selector)
358-
continue;
359-
if (env.test_name_selector &&
360-
!strstr(test->test_name, env.test_name_selector))
507+
if (!should_run(&env.test_selector,
508+
test->test_num, test->test_name))
361509
continue;
362510

363511
test->run_test();
512+
/* ensure last sub-test is finalized properly */
513+
if (test->subtest_name)
514+
test__end_subtest();
515+
364516
test->tested = true;
365517
test->pass_cnt = pass_cnt - old_pass_cnt;
366518
test->error_cnt = error_cnt - old_error_cnt;
@@ -369,21 +521,17 @@ int main(int argc, char **argv)
369521
else
370522
env.succ_cnt++;
371523

372-
if (env.verbose || test->force_log || test->error_cnt) {
373-
if (env.log_cnt) {
374-
fprintf(stdout, "%s", env.log_buf);
375-
if (env.log_buf[env.log_cnt - 1] != '\n')
376-
fprintf(stdout, "\n");
377-
}
378-
}
379-
env.log_cnt = 0;
524+
dump_test_log(test, test->error_cnt);
380525

381526
printf("#%d %s:%s\n", test->test_num, test->test_name,
382527
test->error_cnt ? "FAIL" : "OK");
383528
}
384-
printf("Summary: %d PASSED, %d FAILED\n", env.succ_cnt, env.fail_cnt);
529+
printf("Summary: %d/%d PASSED, %d FAILED\n",
530+
env.succ_cnt, env.sub_succ_cnt, env.fail_cnt);
385531

386532
free(env.log_buf);
533+
free(env.test_selector.num_set);
534+
free(env.subtest_selector.num_set);
387535

388536
return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
389537
}

tools/testing/selftests/bpf/test_progs.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ typedef __u16 __sum16;
4040

4141
struct prog_test_def;
4242

43+
struct test_selector {
44+
const char *name;
45+
bool *num_set;
46+
int num_set_len;
47+
};
48+
4349
struct test_env {
44-
int test_num_selector;
45-
const char *test_name_selector;
50+
struct test_selector test_selector;
51+
struct test_selector subtest_selector;
4652
bool verifier_stats;
4753
bool verbose;
4854
bool very_verbose;
@@ -54,8 +60,9 @@ struct test_env {
5460
size_t log_cnt;
5561
size_t log_cap;
5662

57-
int succ_cnt;
58-
int fail_cnt;
63+
int succ_cnt; /* successful tests */
64+
int sub_succ_cnt; /* successful sub-tests */
65+
int fail_cnt; /* total failed tests + sub-tests */
5966
};
6067

6168
extern int error_cnt;
@@ -65,6 +72,7 @@ extern struct test_env env;
6572
extern void test__printf(const char *fmt, ...);
6673
extern void test__vprintf(const char *fmt, va_list args);
6774
extern void test__force_log();
75+
extern bool test__start_subtest(const char *name);
6876

6977
#define MAGIC_BYTES 123
7078

0 commit comments

Comments
 (0)