selftests/bpf: ensure proper root namespace cleanup when test fail
authorAlexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Thu, 28 Nov 2024 14:38:43 +0000 (15:38 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 2 Dec 2024 19:04:35 +0000 (11:04 -0800)
serial_test_flow_dissector_namespace manipulates both the root net
namespace and a dedicated non-root net namespace. If for some reason a
program attach on root namespace succeeds while it was expected to
fail, the unexpected program will remain attached to the root namespace,
possibly affecting other runs or even other tests in the same run.

Fix undesired test failure side effect by explicitly detaching programs
on failing tests expecting attach to fail. As a side effect of this
change, do not test errno value if the tested operation do not fail.

Fixes: 284ed00a59dd ("selftests/bpf: migrate flow_dissector namespace exclusivity test")
Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://lore.kernel.org/r/20241128-small_flow_test_fix-v1-1-c12d45c98c59@bootlin.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/flow_dissector.c

index 8e6e483fead3f71f21e2223c707c6d4fb548a61e..08bae13248c4a8ab0bfa356a34b2738964d97f4c 100644 (file)
@@ -525,11 +525,14 @@ void serial_test_flow_dissector_namespace(void)
        ns = open_netns(TEST_NS);
        if (!ASSERT_OK_PTR(ns, "enter non-root net namespace"))
                goto out_clean_ns;
-
        err = bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0);
+       if (!ASSERT_ERR(err,
+                       "refuse new flow dissector in non-root net namespace"))
+               bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR);
+       else
+               ASSERT_EQ(errno, EEXIST,
+                         "refused because of already attached prog");
        close_netns(ns);
-       ASSERT_ERR(err, "refuse new flow dissector in non-root net namespace");
-       ASSERT_EQ(errno, EEXIST, "refused because of already attached prog");
 
        /* If no flow dissector is attached to the root namespace, we must
         * be able to attach one to a non-root net namespace
@@ -545,8 +548,11 @@ void serial_test_flow_dissector_namespace(void)
         * a flow dissector to root namespace must fail
         */
        err = bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0);
-       ASSERT_ERR(err, "refuse new flow dissector on root namespace");
-       ASSERT_EQ(errno, EEXIST, "refused because of already attached prog");
+       if (!ASSERT_ERR(err, "refuse new flow dissector on root namespace"))
+               bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR);
+       else
+               ASSERT_EQ(errno, EEXIST,
+                         "refused because of already attached prog");
 
        ns = open_netns(TEST_NS);
        bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR);