Skip to content

Commit 50e793e

Browse files
committed
selftests/bpf: convert test_bpftool_metadata.sh into test_progs framework
The test_bpftool_metadata.sh script validates that bpftool properly returns in its ouptput any metadata generated by bpf programs through some .rodata sections. Port this test to the test_progs framework so that it can be executed automatically in CI. The new test, similarly to the former script, checks that valid data appears both for textual output and json output, as well as for both data not used at all and used data. For the json check part, the expected json string is hardcoded to avoid bringing a new external dependency (eg: a json deserializer) for test_progs. As the test is now converted into test_progs, remove the former script. The newly converted test brings two new subtests: #37/1 bpftool_metadata/metadata_unused:OK #37/2 bpftool_metadata/metadata_used:OK #37 bpftool_metadata:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Alexis Lothoré (eBPF Foundation) <[email protected]> --- Changes in v2: - move the new test in prog_tests directory - use power of 2 for bpftool output buffer size - check snprintf return when building commands - target program by pin path rather than prog name - remove a few blank lines
1 parent b4ae63c commit 50e793e

File tree

3 files changed

+134
-86
lines changed

3 files changed

+134
-86
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ TEST_PROGS := test_kmod.sh \
109109
test_bpftool_build.sh \
110110
test_bpftool.sh \
111111
test_bpftool_map.sh \
112-
test_bpftool_metadata.sh \
113112
test_doc_build.sh \
114113
test_xsk.sh \
115114
test_xdp_features.sh
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <bpftool_helpers.h>
3+
#include <test_progs.h>
4+
#include <linux/bpf.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
#include <fcntl.h>
8+
#include <sys/stat.h>
9+
#include <stdbool.h>
10+
11+
#define BPFFS_DIR "/sys/fs/bpf/test_metadata"
12+
#define BPFFS_USED BPFFS_DIR "/used"
13+
#define BPFFS_UNUSED BPFFS_DIR "/unused"
14+
15+
#define BPF_FILE_USED "metadata_used.bpf.o"
16+
#define BPF_FILE_UNUSED "metadata_unused.bpf.o"
17+
18+
#define MAX_BPFTOOL_OUTPUT_LEN (64*1024)
19+
20+
#define MAX_TOKENS_TO_CHECK 3
21+
static char output[MAX_BPFTOOL_OUTPUT_LEN];
22+
23+
struct test_desc {
24+
char *name;
25+
char *bpf_prog;
26+
char *bpffs_path;
27+
char *expected_output[MAX_TOKENS_TO_CHECK];
28+
char *expected_output_json[MAX_TOKENS_TO_CHECK];
29+
};
30+
31+
static int setup(struct test_desc *test)
32+
{
33+
return mkdir(BPFFS_DIR, 0700);
34+
}
35+
36+
static void cleanup(struct test_desc *test)
37+
{
38+
unlink(test->bpffs_path);
39+
rmdir(BPFFS_DIR);
40+
}
41+
42+
static int check_metadata(char *buf, char * const *tokens, int count)
43+
{
44+
int i;
45+
46+
for (i = 0; i < count && tokens[i]; i++)
47+
if (!strstr(buf, tokens[i]))
48+
return 1;
49+
50+
return 0;
51+
}
52+
53+
static void run_test(struct test_desc *test)
54+
{
55+
int ret;
56+
char cmd[MAX_BPFTOOL_CMD_LEN];
57+
58+
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "prog load %s %s",
59+
test->bpf_prog, test->bpffs_path);
60+
if (!ASSERT_GT(ret, 0, "format prog insert command"))
61+
return;
62+
ret = run_bpftool_command(cmd);
63+
if (!ASSERT_OK(ret, "load program"))
64+
return;
65+
66+
/* Check output with default format */
67+
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "prog show pinned %s",
68+
test->bpffs_path);
69+
if (!ASSERT_GT(ret, 0, "format pinned prog check command"))
70+
return;
71+
ret = get_bpftool_command_output(cmd, output,
72+
MAX_BPFTOOL_OUTPUT_LEN);
73+
if (ASSERT_OK(ret, "get program info")) {
74+
ret = check_metadata(output, test->expected_output,
75+
ARRAY_SIZE(test->expected_output));
76+
ASSERT_OK(ret, "find metadata");
77+
}
78+
79+
/* Check output with json format */
80+
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "prog -j show pinned %s",
81+
test->bpffs_path);
82+
if (!ASSERT_GT(ret, 0, "format pinned prog check command in json"))
83+
return;
84+
ret = get_bpftool_command_output(cmd, output,
85+
MAX_BPFTOOL_OUTPUT_LEN);
86+
if (ASSERT_OK(ret, "get program info in json")) {
87+
ret = check_metadata(output, test->expected_output_json,
88+
ARRAY_SIZE(test->expected_output_json));
89+
ASSERT_OK(ret, "find metadata in json");
90+
}
91+
92+
}
93+
94+
static struct test_desc tests[] = {
95+
{
96+
.name = "metadata_unused",
97+
.bpf_prog = BPF_FILE_UNUSED,
98+
.bpffs_path = BPFFS_UNUSED,
99+
.expected_output = {
100+
"a = \"foo\"",
101+
"b = 1"
102+
},
103+
.expected_output_json = {
104+
"\"metadata\":{\"a\":\"foo\",\"b\":1}"
105+
}
106+
},
107+
{
108+
.name = "metadata_used",
109+
.bpf_prog = BPF_FILE_USED,
110+
.bpffs_path = BPFFS_USED,
111+
.expected_output = {
112+
"a = \"bar\"",
113+
"b = 2"
114+
},
115+
.expected_output_json = {
116+
"\"metadata\":{\"a\":\"bar\",\"b\":2}"
117+
}
118+
}
119+
};
120+
static const int tests_count = ARRAY_SIZE(tests);
121+
122+
void test_bpftool_metadata(void)
123+
{
124+
int i;
125+
126+
for (i = 0; i < tests_count; i++) {
127+
if (!test__start_subtest(tests[i].name))
128+
continue;
129+
if (ASSERT_OK(setup(&tests[i]), "setup bpffs pin dir")) {
130+
run_test(&tests[i]);
131+
cleanup(&tests[i]);
132+
}
133+
}
134+
}

tools/testing/selftests/bpf/test_bpftool_metadata.sh

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)