Skip to content

Commit 0ff97e5

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: abstract away test log output
This patch changes how test output is printed out. By default, if test had no errors, the only output will be a single line with test number, name, and verdict at the end, e.g.: #31 xdp:OK If test had any errors, all log output captured during test execution will be output after test completes. It's possible to force output of log with `-v` (`--verbose`) option, in which case output won't be buffered and will be output immediately. To support this, individual tests are required to use helper methods for logging: `test__printf()` and `test__vprintf()`. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 329e38f commit 0ff97e5

File tree

12 files changed

+183
-73
lines changed

12 files changed

+183
-73
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ void test_bpf_obj_id(void)
106106
if (CHECK(err ||
107107
prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER ||
108108
info_len != sizeof(struct bpf_prog_info) ||
109-
(jit_enabled && !prog_infos[i].jited_prog_len) ||
110-
(jit_enabled &&
109+
(env.jit_enabled && !prog_infos[i].jited_prog_len) ||
110+
(env.jit_enabled &&
111111
!memcmp(jited_insns, zeros, sizeof(zeros))) ||
112112
!prog_infos[i].xlated_prog_len ||
113113
!memcmp(xlated_insns, zeros, sizeof(zeros)) ||
@@ -121,7 +121,7 @@ void test_bpf_obj_id(void)
121121
err, errno, i,
122122
prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
123123
info_len, sizeof(struct bpf_prog_info),
124-
jit_enabled,
124+
env.jit_enabled,
125125
prog_infos[i].jited_prog_len,
126126
prog_infos[i].xlated_prog_len,
127127
!!memcmp(jited_insns, zeros, sizeof(zeros)),

tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
static int libbpf_debug_print(enum libbpf_print_level level,
55
const char *format, va_list args)
66
{
7-
if (level != LIBBPF_DEBUG)
8-
return vfprintf(stderr, format, args);
7+
if (level != LIBBPF_DEBUG) {
8+
test__vprintf(format, args);
9+
return 0;
10+
}
911

1012
if (!strstr(format, "verifier log"))
1113
return 0;
12-
return vfprintf(stderr, "%s", args);
14+
test__vprintf("%s", args);
15+
return 0;
1316
}
1417

1518
static int check_load(const char *file, enum bpf_prog_type type)
@@ -73,32 +76,38 @@ void test_bpf_verif_scale(void)
7376
libbpf_print_fn_t old_print_fn = NULL;
7477
int err, i;
7578

76-
if (verifier_stats)
79+
if (env.verifier_stats) {
80+
test__force_log();
7781
old_print_fn = libbpf_set_print(libbpf_debug_print);
82+
}
7883

7984
err = check_load("./loop3.o", BPF_PROG_TYPE_RAW_TRACEPOINT);
80-
printf("test_scale:loop3:%s\n", err ? (error_cnt--, "OK") : "FAIL");
85+
test__printf("test_scale:loop3:%s\n",
86+
err ? (error_cnt--, "OK") : "FAIL");
8187

8288
for (i = 0; i < ARRAY_SIZE(sched_cls); i++) {
8389
err = check_load(sched_cls[i], BPF_PROG_TYPE_SCHED_CLS);
84-
printf("test_scale:%s:%s\n", sched_cls[i], err ? "FAIL" : "OK");
90+
test__printf("test_scale:%s:%s\n", sched_cls[i],
91+
err ? "FAIL" : "OK");
8592
}
8693

8794
for (i = 0; i < ARRAY_SIZE(raw_tp); i++) {
8895
err = check_load(raw_tp[i], BPF_PROG_TYPE_RAW_TRACEPOINT);
89-
printf("test_scale:%s:%s\n", raw_tp[i], err ? "FAIL" : "OK");
96+
test__printf("test_scale:%s:%s\n", raw_tp[i],
97+
err ? "FAIL" : "OK");
9098
}
9199

92100
for (i = 0; i < ARRAY_SIZE(cg_sysctl); i++) {
93101
err = check_load(cg_sysctl[i], BPF_PROG_TYPE_CGROUP_SYSCTL);
94-
printf("test_scale:%s:%s\n", cg_sysctl[i], err ? "FAIL" : "OK");
102+
test__printf("test_scale:%s:%s\n", cg_sysctl[i],
103+
err ? "FAIL" : "OK");
95104
}
96105
err = check_load("./test_xdp_loop.o", BPF_PROG_TYPE_XDP);
97-
printf("test_scale:test_xdp_loop:%s\n", err ? "FAIL" : "OK");
106+
test__printf("test_scale:test_xdp_loop:%s\n", err ? "FAIL" : "OK");
98107

99108
err = check_load("./test_seg6_loop.o", BPF_PROG_TYPE_LWT_SEG6LOCAL);
100-
printf("test_scale:test_seg6_loop:%s\n", err ? "FAIL" : "OK");
109+
test__printf("test_scale:test_seg6_loop:%s\n", err ? "FAIL" : "OK");
101110

102-
if (verifier_stats)
111+
if (env.verifier_stats)
103112
libbpf_set_print(old_print_fn);
104113
}

tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void get_stack_print_output(void *ctx, int cpu, void *data, __u32 size)
4141
* just assume it is good if the stack is not empty.
4242
* This could be improved in the future.
4343
*/
44-
if (jit_enabled) {
44+
if (env.jit_enabled) {
4545
found = num_stack > 0;
4646
} else {
4747
for (i = 0; i < num_stack; i++) {
@@ -58,7 +58,7 @@ static void get_stack_print_output(void *ctx, int cpu, void *data, __u32 size)
5858
}
5959
} else {
6060
num_stack = e->kern_stack_size / sizeof(__u64);
61-
if (jit_enabled) {
61+
if (env.jit_enabled) {
6262
good_kern_stack = num_stack > 0;
6363
} else {
6464
for (i = 0; i < num_stack; i++) {

tools/testing/selftests/bpf/prog_tests/l4lb_all.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void test_l4lb(const char *file)
7474
}
7575
if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) {
7676
error_cnt++;
77-
printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts);
77+
test__printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts);
7878
}
7979
out:
8080
bpf_object__close(obj);

tools/testing/selftests/bpf/prog_tests/map_lock.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ static void *parallel_map_access(void *arg)
99
for (i = 0; i < 10000; i++) {
1010
err = bpf_map_lookup_elem_flags(map_fd, &key, vars, BPF_F_LOCK);
1111
if (err) {
12-
printf("lookup failed\n");
12+
test__printf("lookup failed\n");
1313
error_cnt++;
1414
goto out;
1515
}
1616
if (vars[0] != 0) {
17-
printf("lookup #%d var[0]=%d\n", i, vars[0]);
17+
test__printf("lookup #%d var[0]=%d\n", i, vars[0]);
1818
error_cnt++;
1919
goto out;
2020
}
2121
rnd = vars[1];
2222
for (j = 2; j < 17; j++) {
2323
if (vars[j] == rnd)
2424
continue;
25-
printf("lookup #%d var[1]=%d var[%d]=%d\n",
26-
i, rnd, j, vars[j]);
25+
test__printf("lookup #%d var[1]=%d var[%d]=%d\n",
26+
i, rnd, j, vars[j]);
2727
error_cnt++;
2828
goto out;
2929
}
@@ -43,7 +43,7 @@ void test_map_lock(void)
4343

4444
err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
4545
if (err) {
46-
printf("test_map_lock:bpf_prog_load errno %d\n", errno);
46+
test__printf("test_map_lock:bpf_prog_load errno %d\n", errno);
4747
goto close_prog;
4848
}
4949
map_fd[0] = bpf_find_map(__func__, obj, "hash_map");

tools/testing/selftests/bpf/prog_tests/send_signal.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ static int test_send_signal_nmi(void)
202202
-1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
203203
if (pmu_fd == -1) {
204204
if (errno == ENOENT) {
205-
printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
206-
__func__);
205+
test__printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
206+
__func__);
207207
return 0;
208208
}
209209
/* Let the test fail with a more informative message */
@@ -222,8 +222,4 @@ void test_send_signal(void)
222222
ret |= test_send_signal_tracepoint();
223223
ret |= test_send_signal_perf();
224224
ret |= test_send_signal_nmi();
225-
if (!ret)
226-
printf("test_send_signal:OK\n");
227-
else
228-
printf("test_send_signal:FAIL\n");
229225
}

tools/testing/selftests/bpf/prog_tests/spinlock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void test_spinlock(void)
1212

1313
err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
1414
if (err) {
15-
printf("test_spin_lock:bpf_prog_load errno %d\n", errno);
15+
test__printf("test_spin_lock:bpf_prog_load errno %d\n", errno);
1616
goto close_prog;
1717
}
1818
for (i = 0; i < 4; i++)

tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ void test_stacktrace_build_id(void)
109109
if (build_id_matches < 1 && retry--) {
110110
bpf_link__destroy(link);
111111
bpf_object__close(obj);
112-
printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
113-
__func__);
112+
test__printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
113+
__func__);
114114
goto retry;
115115
}
116116

tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ void test_stacktrace_build_id_nmi(void)
140140
if (build_id_matches < 1 && retry--) {
141141
bpf_link__destroy(link);
142142
bpf_object__close(obj);
143-
printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
144-
__func__);
143+
test__printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
144+
__func__);
145145
goto retry;
146146
}
147147

tools/testing/selftests/bpf/prog_tests/xdp_noinline.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ void test_xdp_noinline(void)
7575
}
7676
if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) {
7777
error_cnt++;
78-
printf("test_xdp_noinline:FAIL:stats %lld %lld\n", bytes, pkts);
78+
test__printf("test_xdp_noinline:FAIL:stats %lld %lld\n",
79+
bytes, pkts);
7980
}
8081
out:
8182
bpf_object__close(obj);

0 commit comments

Comments
 (0)