Skip to content

Commit 90f1ddf

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
selftests/bpf: Replace CHECK with ASSERT_* in ns_current_pid_tgid test
Replace CHECK in selftest ns_current_pid_tgid with recommended ASSERT_* style. I also shortened subtest name as the prefix of subtest name is covered by the test name already. This patch does fix a testing issue. Currently even if bss->user_{pid,tgid} is not correct, the test still passed since the clone func returns 0. I fixed it to return a non-zero value if bss->user_{pid,tgid} is incorrect. Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Acked-by: Jiri Olsa <jolsa@kernel.org>
1 parent 3da4f52 commit 90f1ddf

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ static int test_current_pid_tgid(void *args)
2020
{
2121
struct test_ns_current_pid_tgid__bss *bss;
2222
struct test_ns_current_pid_tgid *skel;
23-
int err = -1, duration = 0;
23+
int ret = -1, err;
2424
pid_t tgid, pid;
2525
struct stat st;
2626

2727
skel = test_ns_current_pid_tgid__open_and_load();
28-
if (CHECK(!skel, "skel_open_load", "failed to load skeleton\n"))
29-
goto cleanup;
28+
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open_and_load"))
29+
goto out;
3030

3131
pid = syscall(SYS_gettid);
3232
tgid = getpid();
3333

3434
err = stat("/proc/self/ns/pid", &st);
35-
if (CHECK(err, "stat", "failed /proc/self/ns/pid: %d\n", err))
35+
if (!ASSERT_OK(err, "stat /proc/self/ns/pid"))
3636
goto cleanup;
3737

3838
bss = skel->bss;
@@ -42,24 +42,26 @@ static int test_current_pid_tgid(void *args)
4242
bss->user_tgid = 0;
4343

4444
err = test_ns_current_pid_tgid__attach(skel);
45-
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
45+
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__attach"))
4646
goto cleanup;
4747

4848
/* trigger tracepoint */
4949
usleep(1);
50-
ASSERT_EQ(bss->user_pid, pid, "pid");
51-
ASSERT_EQ(bss->user_tgid, tgid, "tgid");
52-
err = 0;
50+
if (!ASSERT_EQ(bss->user_pid, pid, "pid"))
51+
goto cleanup;
52+
if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid"))
53+
goto cleanup;
54+
ret = 0;
5355

5456
cleanup:
55-
test_ns_current_pid_tgid__destroy(skel);
56-
57-
return err;
57+
test_ns_current_pid_tgid__destroy(skel);
58+
out:
59+
return ret;
5860
}
5961

6062
static void test_ns_current_pid_tgid_new_ns(void)
6163
{
62-
int wstatus, duration = 0;
64+
int wstatus;
6365
pid_t cpid;
6466

6567
/* Create a process in a new namespace, this process
@@ -68,21 +70,21 @@ static void test_ns_current_pid_tgid_new_ns(void)
6870
cpid = clone(test_current_pid_tgid, child_stack + STACK_SIZE,
6971
CLONE_NEWPID | SIGCHLD, NULL);
7072

71-
if (CHECK(cpid == -1, "clone", "%s\n", strerror(errno)))
73+
if (!ASSERT_NEQ(cpid, -1, "clone"))
7274
return;
7375

74-
if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", "%s\n", strerror(errno)))
76+
if (!ASSERT_NEQ(waitpid(cpid, &wstatus, 0), -1, "waitpid"))
7577
return;
7678

77-
if (CHECK(WEXITSTATUS(wstatus) != 0, "newns_pidtgid", "failed"))
79+
if (!ASSERT_OK(WEXITSTATUS(wstatus), "newns_pidtgid"))
7880
return;
7981
}
8082

8183
/* TODO: use a different tracepoint */
8284
void serial_test_ns_current_pid_tgid(void)
8385
{
84-
if (test__start_subtest("ns_current_pid_tgid_root_ns"))
86+
if (test__start_subtest("root_ns_tp"))
8587
test_current_pid_tgid(NULL);
86-
if (test__start_subtest("ns_current_pid_tgid_new_ns"))
88+
if (test__start_subtest("new_ns_tp"))
8789
test_ns_current_pid_tgid_new_ns();
8890
}

0 commit comments

Comments
 (0)