Skip to contentSkip to conversation

selftests/bpf: Improve by-name subtest selection logic in prog_tests

PATCH Superseded Mykola Lysenko 2 participants 3 messages Apr 6, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Improve by-name subtest selection logic in prog_tests

Improve subtest selection logic when using -t/-a/-d parameters. In particular, more than one subtest can be specified or a combination of tests / subtests.

-a send_signal -d send_signal/send_signal_nmi* - runs send_signal test without nmi tests

-a send_signal/send_signal_nmi*,find_vma - runs two send_signal subtests and find_vma test

This will allow us to have granular control over which subtests to disable in the CI system instead of disabling whole tests.

Also, add new selftest to avoid possible regression when changing prog_test test name selection logic.

---
 .../selftests/bpf/prog_tests/arg_parsing.c    |  88 ++++++++++
 tools/testing/selftests/bpf/test_progs.c      | 157 +++++++++---------
 tools/testing/selftests/bpf/test_progs.h      |  16 +-
 tools/testing/selftests/bpf/testing_helpers.c |  87 ++++++++++
 tools/testing/selftests/bpf/testing_helpers.h |   6 +
 5 files changed, 266 insertions(+), 88 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arg_parsing.c
tools/testing/selftests/bpf/prog_tests/arg_parsing.c new file +88 -0
new file mode 100644
index 000000000000..f41a532e6194
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+#include "test_progs.h"
+#include "testing_helpers.h"
+
+static int duration;
+
+static void init_test_set(struct test_set *test_set)
+{
+	test_set->cnt = 0;
+	test_set->tests = NULL;
+}
+
+static void free_test_set(struct test_set *test_set)
+{
+	int i, j;
+
+	for (i = 0; i < test_set->cnt; i++) {
+		for (j = 0; j < test_set->tests[i].subtest_cnt; j++)
+			free((void *)test_set->tests[i].subtests[j]);
+		free(test_set->tests[i].subtests);
+		free(test_set->tests[i].name);
+	}
+
+	free(test_set->tests);
+	init_test_set(test_set);
+}
+
+static void test_parse_test_list(void)
+{
+	struct test_set test_set;
+
+	init_test_set(&test_set);
+
+	parse_test_list("arg_parsing", &test_set, true);
+	if (CHECK(test_set.cnt != 1, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+		goto error;
+	if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+		goto error;
+	if (CHECK(!test_set.tests[0].whole_test, "parse_test_list subtest argument", "Expected test 0 to be initialized"))
+		goto error;
+	if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be initialized"))
+		goto error;
+	free_test_set(&test_set);
+
+	parse_test_list("arg_parsing,bpf_cookie", &test_set, true);
+	if (CHECK(test_set.cnt != 2, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+		goto error;
+	if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+		goto error;
+	if (CHECK(!test_set.tests[0].whole_test, "parse_test_list subtest argument", "Expected test 0 to be fully runnable"))
+		goto error;
+	if (CHECK(!test_set.tests[1].whole_test, "parse_test_list subtest argument", "Expected test 1 to be fully runnable"))
+		goto error;
+	if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be arg_parsing"))
+		goto error;
+	if (CHECK(strcmp("bpf_cookie", test_set.tests[1].name), "parse_test_list subtest argument", "Expected test 1 to be bpf_cookie"))
+		goto error;
+	free_test_set(&test_set);
+
+	parse_test_list("arg_parsing/test_parse_test_list,bpf_cookie", &test_set, true);
+	if (CHECK(test_set.cnt != 2, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+		goto error;
+	if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+		goto error;
+	if (CHECK(test_set.tests[0].whole_test, "parse_test_list no subtest argument", "Expected test 0 to be fully runnable"))
+		goto error;
+	if (CHECK(!test_set.tests[1].whole_test, "parse_test_list no subtest argument", "Expected test 0 to be fully runnable"))
+		goto error;
+	if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be arg_parsing"))
+		goto error;
+	if (CHECK(test_set.tests[0].subtest_cnt != 1, "parse_test_list subtest number", "Unexpected number of subtests for arg_parsing %d\n", test_set.tests[0].subtest_cnt))
+		goto error;
+	if (CHECK(strcmp("test_parse_test_list", test_set.tests[0].subtests[0]), "parse_test_list subtest name", "Expected test 0 first subtest to be to be test_parse_test_list"))
+		goto error;
+	if (CHECK(test_set.tests[1].subtest_cnt != 0, "parse_test_list subtest number", "Unexpected number of subtests for bpf_cookie %d\n", test_set.tests[1].subtest_cnt))
+		goto error;
+	if (CHECK(strcmp("bpf_cookie", test_set.tests[1].name), "parse_test_list subtest argument", "Expected test 1 to be bpf_cookie"))
+		goto error;
+error:
+	free_test_set(&test_set);
+}
+
+void test_arg_parsing(void)
+{
+	if (test__start_subtest("test_parse_test_list"))
+		test_parse_test_list();
+}
tools/testing/selftests/bpf/test_progs.c +74 -83

232 lines not on this page · raw message

tools/testing/selftests/bpf/test_progs.h +11 -5
index eec4c7385b14..6a465a98341e 100644
@@ -37,7 +37,6 @@ typedef __u16 __sum16;
 #include <bpf/bpf_endian.h>
 #include "trace_helpers.h"
 #include "testing_helpers.h"
-#include "flow_dissector_load.h"
 
 enum verbosity {
 	VERBOSE_NONE,
@@ -46,14 +45,21 @@ enum verbosity {
 	VERBOSE_SUPER,
 };
 
-struct str_set {
-	const char **strs;
+struct prog_test {
+	char *name;
+	char **subtests;
+	int subtest_cnt;
+	bool whole_test;
+};
+
+struct test_set {
+	struct prog_test *tests;
 	int cnt;
 };
 
 struct test_selector {
-	struct str_set whitelist;
-	struct str_set blacklist;
+	struct test_set whitelist;
+	struct test_set blacklist;
 	bool *num_set;
 	int num_set_len;
 };
tools/testing/selftests/bpf/testing_helpers.c +87 -0

99 lines not on this page · raw message

tools/testing/selftests/bpf/testing_helpers.h +6 -0
index f46ebc476ee8..d2f502184cd1 100644
@@ -12,3 +12,9 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 			  size_t insns_cnt, const char *license,
 			  __u32 kern_version, char *log_buf,
 			  size_t log_buf_sz);
+
+/*
+ * below function is exported for testing in prog_test test
+ */
+struct test_set;
+int parse_test_list(const char *s, struct test_set *test_set, bool is_glob_pattern);
Signature · 1 line
2.30.2
#1 lore raw
Quoted 11 lines · Mykola Lysenko

Improve subtest selection logic when using -t/-a/-d parameters. In particular, more than one subtest can be specified or a combination of tests / subtests.

-a send_signal -d send_signal/send_signal_nmi* - runs send_signal test without nmi tests

-a send_signal/send_signal_nmi*,find_vma - runs two send_signal subtests and find_vma test

Only somewhat related, but while we are at this topic. Can you please check whether equivalent approach works:

-a send_signal/send_signal_nmi* -a find_vma

i.e., multi -a/-d/-t/-b are concatenating their selectors.

Quoted 16 lines

This will allow us to have granular control over which subtests to disable in the CI system instead of disabling whole tests.

Also, add new selftest to avoid possible regression when changing prog_test test name selection logic.

---
 .../selftests/bpf/prog_tests/arg_parsing.c    |  88 ++++++++++
 tools/testing/selftests/bpf/test_progs.c      | 157 +++++++++---------
 tools/testing/selftests/bpf/test_progs.h      |  16 +-
 tools/testing/selftests/bpf/testing_helpers.c |  87 ++++++++++
 tools/testing/selftests/bpf/testing_helpers.h |   6 +
 5 files changed, 266 insertions(+), 88 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arg_parsing.c

[...]

tools/testing/selftests/bpf/prog_tests/arg_parsing.c line 35

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/arg_parsing.c new file +7 -0
+static void test_parse_test_list(void)
+{
+       struct test_set test_set;
+
+       init_test_set(&test_set);
+
+       parse_test_list("arg_parsing", &test_set, true);

ASSER_OK() return value?

Quoted 1 line

+ if (CHECK(test_set.cnt != 1, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))

please don't use CHECK()s in new tests, we only add ASSERT_xxx() now. Also line length should be under 100 characters (except for the case when we have a long string literal, we don't break string literals, no matter how long).

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/arg_parsing.c new file +8 -0
+               goto error;
+       if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+               goto error;
+       if (CHECK(!test_set.tests[0].whole_test, "parse_test_list subtest argument", "Expected test 0 to be initialized"))
+               goto error;
+       if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be initialized"))
+               goto error;
+       free_test_set(&test_set);

tools/testing/selftests/bpf/prog_tests/arg_parsing.c line 54

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/arg_parsing.c new file +10 -0
+
+       parse_test_list("arg_parsing,bpf_cookie", &test_set, true);
+       if (CHECK(test_set.cnt != 2, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+               goto error;
+       if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+               goto error;
+       if (CHECK(!test_set.tests[0].whole_test, "parse_test_list subtest argument", "Expected test 0 to be fully runnable"))
+               goto error;
+       if (CHECK(!test_set.tests[1].whole_test, "parse_test_list subtest argument", "Expected test 1 to be fully runnable"))
+               goto error;

nit: I think there is no need to goto error after each check. We need goto error if subsequent checks can crash due to some invalid state. In this case, validating the value of few independent values is ok to always check without goto error jumps. Makes tests shorter and simpler.

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/arg_parsing.c new file +9 -0
+       if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be arg_parsing"))
+               goto error;
+       if (CHECK(strcmp("bpf_cookie", test_set.tests[1].name), "parse_test_list subtest argument", "Expected test 1 to be bpf_cookie"))
+               goto error;
+       free_test_set(&test_set);
+
+       parse_test_list("arg_parsing/test_parse_test_list,bpf_cookie", &test_set, true);
+       if (CHECK(test_set.cnt != 2, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+               goto error;

[...]

Quoted 13 lines
tools/testing/selftests/bpf/test_progs.c +1 -0
index 0a4b45d7b515..671e37cada4b 100644
@@ -3,6 +3,7 @@
  */
 #define _GNU_SOURCE
 #include "test_progs.h"
+#include "testing_helpers.h"
 #include "cgroup_helpers.h"
 #include <argp.h>
 #include <pthread.h>

tools/testing/selftests/bpf/test_progs.c int usleep(useconds_t usec) · line 85

Quoted 4 lines
tools/testing/selftests/bpf/test_progs.c +0 -1
@@ -82,14 +83,14 @@ int usleep(useconds_t usec)
 static bool should_run(struct test_selector *sel, int num, const char *name)
 {
        int i;
-

keep empty line between variable declaration block and first statement

Quoted 13 lines
tools/testing/selftests/bpf/test_progs.c +3 -2
        for (i = 0; i < sel->blacklist.cnt; i++) {
-               if (glob_match(name, sel->blacklist.strs[i]))
+               if (glob_match(name, sel->blacklist.tests[i].name) &&
+                   sel->blacklist.tests[i].whole_test)
                        return false;
        }
 
        for (i = 0; i < sel->whitelist.cnt; i++) {
-               if (glob_match(name, sel->whitelist.strs[i]))
+               if (glob_match(name, sel->whitelist.tests[i].name))
                        return true;
        }
 

[...]

Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +1 -2
-bool test__start_subtest(const char *name)
+bool test__start_subtest(const char *subtest_name)
 {
        struct prog_test_def *test = env.test;
@@ -205,17 +246,21 @@ bool test__start_subtest(const char *name)
 
        test->subtest_num++;
 
-       if (!name || !name[0]) {

tools/testing/selftests/bpf/test_progs.c bool test__start_subtest(const char *name) · line 257

Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +3 -1
+       if (!subtest_name || !subtest_name[0]) {
                fprintf(env.stderr,
                        "Subtest #%d didn't provide sub-test name!\n",
                        test->subtest_num);
                return false;
        }
 
-       if (!should_run(&env.subtest_selector, test->subtest_num, name))
+       if (!should_run_subtest(&env.test_selector,
+                               &env.subtest_selector,

we don't have subtest_selector anymore, do we?

also, do you think that maybe combining should_run and should_rub_subtest would be a good idea? You can distinguish by having subtest_name NULL when you need to check only test?

Quoted 14 lines
tools/testing/selftests/bpf/test_progs.c +4 -1
+                               test->subtest_num,
+                               test->test_name,
+                               subtest_name))
                return false;
 
-       test->subtest_name = strdup(name);
+       test->subtest_name = strdup(subtest_name);
        if (!test->subtest_name) {
                fprintf(env.stderr,
                        "Subtest #%d: failed to copy subtest name!\n",
@@ -527,63 +572,29 @@ static int libbpf_print_fn(enum libbpf_print_level level,
        return 0;
 }
 

[...]

Quoted 17 lines
tools/testing/selftests/bpf/test_progs.h +0 -2
index eec4c7385b14..6a465a98341e 100644
@@ -37,7 +37,6 @@ typedef __u16 __sum16;
 #include <bpf/bpf_endian.h>
 #include "trace_helpers.h"
 #include "testing_helpers.h"
-#include "flow_dissector_load.h"
 
 enum verbosity {
        VERBOSE_NONE,
@@ -46,14 +45,21 @@ enum verbosity {
        VERBOSE_SUPER,
 };
 
-struct str_set {

tools/testing/selftests/bpf/test_progs.h enum verbosity { · line 56

Quoted 10 lines
tools/testing/selftests/bpf/test_progs.h +9 -1
-       const char **strs;
+struct prog_test {
+       char *name;
+       char **subtests;
+       int subtest_cnt;
+       bool whole_test;
+};
+
+struct test_set {
+       struct prog_test *tests;

prog_test is a bad name, IMO. it's a "test filter", right? Let's call it that?

test_set isn't most accurate as well, maybe test_filter_set or test_set_filter?

Quoted 44 lines
tools/testing/selftests/bpf/test_progs.h +2 -2
        int cnt;
 };
 
 struct test_selector {
-       struct str_set whitelist;
-       struct str_set blacklist;
+       struct test_set whitelist;
+       struct test_set blacklist;
        bool *num_set;
        int num_set_len;
 };
tools/testing/selftests/bpf/testing_helpers.c +18 -0
index 795b6798ccee..d2160d2a1303 100644
@@ -6,6 +6,7 @@
 #include <errno.h>
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
+#include "test_progs.h"
 #include "testing_helpers.h"
 
 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
@@ -69,6 +70,92 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
        return 0;
 }
 
+int parse_test_list(const char *s,
+                   struct test_set *test_set,
+                   bool is_glob_pattern)
+{
+       char *input, *state = NULL, *next;
+       struct prog_test *tmp, *tests = NULL;
+       int i, j, cnt = 0;
+
+       input = strdup(s);
+       if (!input)
+               return -ENOMEM;
+
+       while ((next = strtok_r(state ? NULL : input, ",", &state))) {
+               char *subtest_str = strchr(next, '/');
+               char *pattern = NULL;
+
+               tmp = realloc(tests, sizeof(*tests) * (cnt + 1));

tools/testing/selftests/bpf/testing_helpers.c int parse_num_list(const char *s, bool **num_set, int *num_set_len) · line 99

Quoted 10 lines
tools/testing/selftests/bpf/testing_helpers.c +10 -0
+               if (!tmp)
+                       goto err;
+               tests = tmp;
+
+               tests[cnt].subtest_cnt = 0;
+               tests[cnt].subtests = NULL;
+               tests[cnt].whole_test = false;
+
+               if (subtest_str) {
+                       char **tmp_subtests = NULL;

need an empty line between variable declarations and statements

Quoted 7 lines
tools/testing/selftests/bpf/testing_helpers.c +7 -0
+                       *subtest_str = '\0';
+                       int subtest_cnt = tests[cnt].subtest_cnt;
+
+                       tmp_subtests = realloc(tests[cnt].subtests,
+                                              sizeof(*tmp_subtests) *
+                                              (subtest_cnt + 1));
+                       if (!tmp_subtests)

tools/testing/selftests/bpf/testing_helpers.c int parse_num_list(const char *s, bool **num_set, int *num_set_len) · line 116

Quoted 10 lines
tools/testing/selftests/bpf/testing_helpers.c +10 -0
+                               goto err;
+                       tests[cnt].subtests = tmp_subtests;
+
+                       tests[cnt].subtests[subtest_cnt] = strdup(subtest_str + 1);
+                       if (!tests[cnt].subtests[subtest_cnt])
+                               goto err;
+
+                       tests[cnt].subtest_cnt++;
+               } else {
+                       tests[cnt].whole_test = true;

isn't whole_test equivalent to subtest_cnt > 0? Why keeping extra bool of state then?

Quoted 62 lines
tools/testing/selftests/bpf/testing_helpers.c +42 -0
+               }
+
+               if (is_glob_pattern) {
+                       pattern = "%s";
+                       tests[cnt].name = malloc(strlen(next) + 1);
+               } else {
+                       pattern = "*%s*";
+                       tests[cnt].name = malloc(strlen(next) + 2 + 1);
+               }
+
+               if (!tests[cnt].name)
+                       goto err;
+
+               sprintf(tests[cnt].name, pattern, next);
+
+               cnt++;
+       }
+
+       tmp = realloc(test_set->tests, sizeof(*tests) * (cnt + test_set->cnt));
+       if (!tmp)
+               goto err;
+
+       memcpy(tmp + test_set->cnt, tests, sizeof(*tests) * cnt);
+       test_set->tests = tmp;
+       test_set->cnt += cnt;
+
+       free(tests);
+       free(input);
+       return 0;
+
+err:
+       for (i = 0; i < cnt; i++) {
+               for (j = 0; j < tests[i].subtest_cnt; j++)
+                       free(tests[i].subtests[j]);
+
+               free(tests[i].name);
+       }
+       free(tests);
+       free(input);
+       return -ENOMEM;
+}
+
 __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
 {
        __u32 info_len = sizeof(*info);
tools/testing/selftests/bpf/testing_helpers.h +6 -1
index f46ebc476ee8..d2f502184cd1 100644
@@ -12,3 +12,9 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
                          size_t insns_cnt, const char *license,
                          __u32 kern_version, char *log_buf,
                          size_t log_buf_sz);
+
+/*
+ * below function is exported for testing in prog_test test
+ */
+struct test_set;
+int parse_test_list(const char *s, struct test_set *test_set, bool is_glob_pattern);
--

2.30.2

#2 lore raw

Thanks for the super quick review Andrii!

Quoted 18 lines · Andrii Nakryiko
Quoted 11 lines · Mykola Lysenko

Improve subtest selection logic when using -t/-a/-d parameters. In particular, more than one subtest can be specified or a combination of tests / subtests.

-a send_signal -d send_signal/send_signal_nmi* - runs send_signal test without nmi tests

-a send_signal/send_signal_nmi*,find_vma - runs two send_signal subtests and find_vma test

Only somewhat related, but while we are at this topic. Can you please check whether equivalent approach works:

Quoted 3 lines

-a send_signal/send_signal_nmi* -a find_vma

i.e., multi -a/-d/-t/-b are concatenating their selectors.

Yes, these options can be combined. I will add a test for the sequence of parse_test_list calls in v2.

Quoted 20 lines
Quoted 17 lines

This will allow us to have granular control over which subtests to disable in the CI system instead of disabling whole tests.

Also, add new selftest to avoid possible regression when changing prog_test test name selection logic.

---
.../selftests/bpf/prog_tests/arg_parsing.c | 88 ++++++++++ tools/testing/selftests/bpf/test_progs.c | 157 +++++++++--------- tools/testing/selftests/bpf/test_progs.h | 16 +- tools/testing/selftests/bpf/testing_helpers.c | 87 ++++++++++ tools/testing/selftests/bpf/testing_helpers.h | 6 +

5 files changed, 266 insertions(+), 88 deletions(-)

create mode 100644 tools/testing/selftests/bpf/prog_tests/arg_parsing.c

[...]

Quoted 9 lines

tools/testing/selftests/bpf/prog_tests/arg_parsing.c line 35

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/arg_parsing.c new file +7 -0
+static void test_parse_test_list(void)
+{
+ struct test_set test_set;
+
+ init_test_set(&test_set);
+
+ parse_test_list("arg_parsing", &test_set, true);

ASSER_OK() return value?

Will fix

Quoted 7 lines
Quoted 2 lines

+ if (CHECK(test_set.cnt != 1, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))

please don't use CHECK()s in new tests, we only add ASSERT_xxx() now. Also line length should be under 100 characters (except for the case when we have a long string literal, we don't break string literals, no matter how long).

Will fix

Quoted 20 lines
Quoted 11 lines

+ goto error;
+ if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+ goto error;
+ if (CHECK(!test_set.tests[0].whole_test, "parse_test_list subtest argument", "Expected test 0 to be initialized"))
+ goto error;
+ if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be initialized"))
+ goto error;
+ free_test_set(&test_set);
+
+ parse_test_list("arg_parsing,bpf_cookie", &test_set, true);

Quoted 8 lines

+ if (CHECK(test_set.cnt != 2, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+ goto error;
+ if (!ASSERT_OK_PTR(test_set.tests, "tests__initialized"))
+ goto error;
+ if (CHECK(!test_set.tests[0].whole_test, "parse_test_list subtest argument", "Expected test 0 to be fully runnable"))
+ goto error;
+ if (CHECK(!test_set.tests[1].whole_test, "parse_test_list subtest argument", "Expected test 1 to be fully runnable"))
+ goto error;

Quoted 5 lines

nit: I think there is no need to goto error after each check. We need goto error if subsequent checks can crash due to some invalid state. In this case, validating the value of few independent values is ok to always check without goto error jumps. Makes tests shorter and simpler.

Will fix

Quoted 25 lines
Quoted 11 lines

+ if (CHECK(strcmp("arg_parsing", test_set.tests[0].name), "parse_test_list subtest argument", "Expected test 0 to be arg_parsing"))
+ goto error;
+ if (CHECK(strcmp("bpf_cookie", test_set.tests[1].name), "parse_test_list subtest argument", "Expected test 1 to be bpf_cookie"))
+ goto error;
+ free_test_set(&test_set);
+
+ parse_test_list("arg_parsing/test_parse_test_list,bpf_cookie", &test_set, true);
+ if (CHECK(test_set.cnt != 2, "parse_test_list subtest argument", "Unexpected number of tests in num table %d\n", test_set.cnt))
+ goto error;

[...]

Quoted 11 lines
tools/testing/selftests/bpf/test_progs.c +1 -0
index 0a4b45d7b515..671e37cada4b 100644
@@ -3,6 +3,7 @@
 */
 #define _GNU_SOURCE
 #include "test_progs.h"
+#include "testing_helpers.h"
 #include "cgroup_helpers.h"
 #include <argp.h>
Quoted 8 lines

tools/testing/selftests/bpf/test_progs.c int usleep(useconds_t usec) · line 85

Quoted 6 lines
tools/testing/selftests/bpf/test_progs.c +0 -1
 #include <pthread.h>
@@ -82,14 +83,14 @@ int usleep(useconds_t usec)
 static bool should_run(struct test_selector *sel, int num, const char *name)
 {
 int i;
-

keep empty line between variable declaration block and first statement

Will fix, curious though why it is not caught by the script. Probably because this line is not modified

Quoted 34 lines
Quoted 14 lines
tools/testing/selftests/bpf/test_progs.c +3 -2
 for (i = 0; i < sel->blacklist.cnt; i++) {
- if (glob_match(name, sel->blacklist.strs[i]))
+ if (glob_match(name, sel->blacklist.tests[i].name) &&
+ sel->blacklist.tests[i].whole_test)
 return false;
 }
 
 for (i = 0; i < sel->whitelist.cnt; i++) {
- if (glob_match(name, sel->whitelist.strs[i]))
+ if (glob_match(name, sel->whitelist.tests[i].name))
 return true;
 }
 

[...]

Quoted 17 lines
tools/testing/selftests/bpf/test_progs.c +1 -1
-bool test__start_subtest(const char *name)
+bool test__start_subtest(const char *subtest_name)

{
struct prog_test_def *test = env.test;

tools/testing/selftests/bpf/test_progs.c +1 -1
@@ -205,17 +246,21 @@ bool test__start_subtest(const char *name)
 
 test->subtest_num++;
 
- if (!name || !name[0]) {
+ if (!subtest_name || !subtest_name[0]) {
 fprintf(env.stderr,
 "Subtest #%d didn't provide sub-test name!\n",
 test->subtest_num);
 return false;
 }
 
Quoted 5 lines

tools/testing/selftests/bpf/test_progs.c bool test__start_subtest(const char *name) · line 257

Quoted 3 lines
tools/testing/selftests/bpf/test_progs.c +2 -1
- if (!should_run(&env.subtest_selector, test->subtest_num, name))
+ if (!should_run_subtest(&env.test_selector,
+ &env.subtest_selector,

we don't have subtest_selector anymore, do we?

Unfortunately, we do use subtest selector for -n option. I do have a change for -n option, but I want to review it separately. Otherwise it makes the change way to big

Quoted 4 lines

also, do you think that maybe combining should_run and should_rub_subtest would be a good idea? You can distinguish by having subtest_name NULL when you need to check only test?

Similar to above, because of -n option, combining two makes logic too convoluted. Happy to address it as a separate change once we figure out -n option behavior

Quoted 48 lines
Quoted 15 lines
tools/testing/selftests/bpf/test_progs.c +4 -1
+ test->subtest_num,
+ test->test_name,
+ subtest_name))
 return false;
 
- test->subtest_name = strdup(name);
+ test->subtest_name = strdup(subtest_name);
 if (!test->subtest_name) {
 fprintf(env.stderr,
 "Subtest #%d: failed to copy subtest name!\n",
@@ -527,63 +572,29 @@ static int libbpf_print_fn(enum libbpf_print_level level,
 return 0;
 }
 

[...]

Quoted 17 lines
tools/testing/selftests/bpf/test_progs.h +0 -2
index eec4c7385b14..6a465a98341e 100644
@@ -37,7 +37,6 @@ typedef __u16 __sum16;
 #include <bpf/bpf_endian.h>
 #include "trace_helpers.h"
 #include "testing_helpers.h"
-#include "flow_dissector_load.h"
 
 enum verbosity {
 VERBOSE_NONE,
@@ -46,14 +45,21 @@ enum verbosity {
 VERBOSE_SUPER,
 };
 
-struct str_set {

tools/testing/selftests/bpf/test_progs.h enum verbosity { · line 56

Quoted 10 lines
tools/testing/selftests/bpf/test_progs.h +9 -1
- const char **strs;
+struct prog_test {
+ char *name;
+ char **subtests;
+ int subtest_cnt;
+ bool whole_test;
+};
+
+struct test_set {
+ struct prog_test *tests;

prog_test is a bad name, IMO. it's a "test filter", right? Let's call it that?

Quoted 1 line

test_set isn't most accurate as well, maybe test_filter_set or test_set_filter?

Will change

Quoted 49 lines
Quoted 49 lines
tools/testing/selftests/bpf/test_progs.h +2 -2
 int cnt;
 };
 
 struct test_selector {
- struct str_set whitelist;
- struct str_set blacklist;
+ struct test_set whitelist;
+ struct test_set blacklist;
 bool *num_set;
 int num_set_len;
 };
tools/testing/selftests/bpf/testing_helpers.c +22 -0
index 795b6798ccee..d2160d2a1303 100644
@@ -6,6 +6,7 @@
 #include <errno.h>
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
+#include "test_progs.h"
 #include "testing_helpers.h"
 
 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
@@ -69,6 +70,92 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 return 0;
 }
 
+int parse_test_list(const char *s,
+ struct test_set *test_set,
+ bool is_glob_pattern)
+{
+ char *input, *state = NULL, *next;
+ struct prog_test *tmp, *tests = NULL;
+ int i, j, cnt = 0;
+
+ input = strdup(s);
+ if (!input)
+ return -ENOMEM;
+
+ while ((next = strtok_r(state ? NULL : input, ",", &state))) {
+ char *subtest_str = strchr(next, '/');
+ char *pattern = NULL;
+
+ tmp = realloc(tests, sizeof(*tests) * (cnt + 1));
+ if (!tmp)
+ goto err;
+ tests = tmp;
+
Quoted 8 lines

tools/testing/selftests/bpf/testing_helpers.c int parse_num_list(const char *s, bool **num_set, int *num_set_len) · line 99

Quoted 6 lines
tools/testing/selftests/bpf/testing_helpers.c +6 -0
+ tests[cnt].subtest_cnt = 0;
+ tests[cnt].subtests = NULL;
+ tests[cnt].whole_test = false;
+
+ if (subtest_str) {
+ char **tmp_subtests = NULL;

need an empty line between variable declarations and statements

Will fix

Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/bpf/testing_helpers.c +12 -0
+ *subtest_str = '\0';
+ int subtest_cnt = tests[cnt].subtest_cnt;
+
+ tmp_subtests = realloc(tests[cnt].subtests,
+ sizeof(*tmp_subtests) *
+ (subtest_cnt + 1));
+ if (!tmp_subtests)
+ goto err;
+ tests[cnt].subtests = tmp_subtests;
+
+ tests[cnt].subtests[subtest_cnt] = strdup(subtest_str + 1);
+ if (!tests[cnt].subtests[subtest_cnt])
Quoted 8 lines

tools/testing/selftests/bpf/testing_helpers.c int parse_num_list(const char *s, bool **num_set, int *num_set_len) · line 116

Quoted 5 lines
tools/testing/selftests/bpf/testing_helpers.c +5 -0
+ goto err;
+
+ tests[cnt].subtest_cnt++;
+ } else {
+ tests[cnt].whole_test = true;

isn't whole_test equivalent to subtest_cnt > 0? Why keeping extra bool of state then?

Will fix. Overthought it here a bit

Quoted 62 lines
Quoted 62 lines
tools/testing/selftests/bpf/testing_helpers.c +42 -0
+ }
+
+ if (is_glob_pattern) {
+ pattern = "%s";
+ tests[cnt].name = malloc(strlen(next) + 1);
+ } else {
+ pattern = "*%s*";
+ tests[cnt].name = malloc(strlen(next) + 2 + 1);
+ }
+
+ if (!tests[cnt].name)
+ goto err;
+
+ sprintf(tests[cnt].name, pattern, next);
+
+ cnt++;
+ }
+
+ tmp = realloc(test_set->tests, sizeof(*tests) * (cnt + test_set->cnt));
+ if (!tmp)
+ goto err;
+
+ memcpy(tmp + test_set->cnt, tests, sizeof(*tests) * cnt);
+ test_set->tests = tmp;
+ test_set->cnt += cnt;
+
+ free(tests);
+ free(input);
+ return 0;
+
+err:
+ for (i = 0; i < cnt; i++) {
+ for (j = 0; j < tests[i].subtest_cnt; j++)
+ free(tests[i].subtests[j]);
+
+ free(tests[i].name);
+ }
+ free(tests);
+ free(input);
+ return -ENOMEM;
+}
+
 __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
 {
 __u32 info_len = sizeof(*info);
tools/testing/selftests/bpf/testing_helpers.h +6 -1
index f46ebc476ee8..d2f502184cd1 100644
@@ -12,3 +12,9 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 size_t insns_cnt, const char *license,
 __u32 kern_version, char *log_buf,
 size_t log_buf_sz);
+
+/*
+ * below function is exported for testing in prog_test test
+ */
+struct test_set;
+int parse_test_list(const char *s, struct test_set *test_set, bool is_glob_pattern);
--

2.30.2