selftests/bpf: test_progs: add test__join_cgroup helper
authorStanislav Fomichev <sdf@google.com>
Wed, 4 Sep 2019 16:25:04 +0000 (09:25 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 6 Sep 2019 16:59:05 +0000 (09:59 -0700)
test__join_cgroup() combines the following operations that usually
go hand in hand and returns cgroup fd:

  * setup cgroup environment (make sure cgroupfs is mounted)
  * mkdir cgroup
  * join cgroup

It also marks a test as a "cgroup cleanup needed" and removes cgroup
state after the test is done.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/test_progs.c
tools/testing/selftests/bpf/test_progs.h

index 9eef5edf17be5ec8e30ffddb6ab7d1c4fb62831c..ce9650bad453e0bf5f070be5ac550818063c7bc6 100644 (file)
@@ -106,7 +106,7 @@ $(OUTPUT)/test_socket_cookie: cgroup_helpers.c
 $(OUTPUT)/test_sockmap: cgroup_helpers.c
 $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
 $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
-$(OUTPUT)/test_progs: trace_helpers.c
+$(OUTPUT)/test_progs: cgroup_helpers.c trace_helpers.c
 $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
 $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
 $(OUTPUT)/test_netcnt: cgroup_helpers.c
@@ -200,7 +200,7 @@ $(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
                                                | $(ALU32_BUILD_DIR)
        $(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
                -o $(ALU32_BUILD_DIR)/test_progs_32 \
-               test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \
+               test_progs.c test_stub.c cgroup_helpers.c trace_helpers.c prog_tests/*.c \
                $(OUTPUT)/libbpf.a $(LDLIBS)
 
 $(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
index e8616e778cb5070cd6a1685f459499e09677b28f..af75a1c7a4587253d36c68f4bd0bd9f68798593b 100644 (file)
@@ -2,6 +2,7 @@
 /* Copyright (c) 2017 Facebook
  */
 #include "test_progs.h"
+#include "cgroup_helpers.h"
 #include "bpf_rlimit.h"
 #include <argp.h>
 #include <string.h>
@@ -17,6 +18,7 @@ struct prog_test_def {
        int error_cnt;
        int skip_cnt;
        bool tested;
+       bool need_cgroup_cleanup;
 
        const char *subtest_name;
        int subtest_num;
@@ -122,6 +124,39 @@ void test__fail(void)
        env.test->error_cnt++;
 }
 
+int test__join_cgroup(const char *path)
+{
+       int fd;
+
+       if (!env.test->need_cgroup_cleanup) {
+               if (setup_cgroup_environment()) {
+                       fprintf(stderr,
+                               "#%d %s: Failed to setup cgroup environment\n",
+                               env.test->test_num, env.test->test_name);
+                       return -1;
+               }
+
+               env.test->need_cgroup_cleanup = true;
+       }
+
+       fd = create_and_get_cgroup(path);
+       if (fd < 0) {
+               fprintf(stderr,
+                       "#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
+                       env.test->test_num, env.test->test_name, path, errno);
+               return fd;
+       }
+
+       if (join_cgroup(path)) {
+               fprintf(stderr,
+                       "#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
+                       env.test->test_num, env.test->test_name, path, errno);
+               return -1;
+       }
+
+       return fd;
+}
+
 struct ipv4_packet pkt_v4 = {
        .eth.h_proto = __bpf_constant_htons(ETH_P_IP),
        .iph.ihl = 5,
@@ -530,6 +565,9 @@ int main(int argc, char **argv)
                fprintf(env.stdout, "#%d %s:%s\n",
                        test->test_num, test->test_name,
                        test->error_cnt ? "FAIL" : "OK");
+
+               if (test->need_cgroup_cleanup)
+                       cleanup_cgroup_environment();
        }
        stdio_restore();
        printf("Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
index c8edb9464ba637c03412ef2ceadc4bab55eab544..e518bd5da3e29bcd11fd3a1c1d020b805bac1303 100644 (file)
@@ -71,6 +71,7 @@ extern void test__force_log();
 extern bool test__start_subtest(const char *name);
 extern void test__skip(void);
 extern void test__fail(void);
+extern int test__join_cgroup(const char *path);
 
 #define MAGIC_BYTES 123