Commit 61ddff37 authored by Mykola Lysenko's avatar Mykola Lysenko Committed by Andrii Nakryiko

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

-a 'send_signal*' -a find_vma -d send_signal/send_signal_nmi* -
runs 2 send_signal test and find_vma test. Disables two send_signal
nmi subtests

-t send_signal -t find_vma - runs two *send_signal* tests and one
*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.
Signed-off-by: default avatarMykola Lysenko <mykolal@fb.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220409001750.529930-1-mykolal@fb.com
parent 07385998
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
#include "test_progs.h"
#include "testing_helpers.h"
static void init_test_filter_set(struct test_filter_set *set)
{
set->cnt = 0;
set->tests = NULL;
}
static void free_test_filter_set(struct test_filter_set *set)
{
int i, j;
for (i = 0; i < set->cnt; i++) {
for (j = 0; j < set->tests[i].subtest_cnt; j++)
free((void *)set->tests[i].subtests[j]);
free(set->tests[i].subtests);
free(set->tests[i].name);
}
free(set->tests);
init_test_filter_set(set);
}
static void test_parse_test_list(void)
{
struct test_filter_set set;
init_test_filter_set(&set);
ASSERT_OK(parse_test_list("arg_parsing", &set, true), "parsing");
if (!ASSERT_EQ(set.cnt, 1, "test filters count"))
goto error;
if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
goto error;
ASSERT_EQ(set.tests[0].subtest_cnt, 0, "subtest filters count");
ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "subtest name");
free_test_filter_set(&set);
ASSERT_OK(parse_test_list("arg_parsing,bpf_cookie", &set, true),
"parsing");
if (!ASSERT_EQ(set.cnt, 2, "count of test filters"))
goto error;
if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
goto error;
ASSERT_EQ(set.tests[0].subtest_cnt, 0, "subtest filters count");
ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
free_test_filter_set(&set);
ASSERT_OK(parse_test_list("arg_parsing/arg_parsing,bpf_cookie",
&set,
true),
"parsing");
if (!ASSERT_EQ(set.cnt, 2, "count of test filters"))
goto error;
if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
goto error;
if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
goto error;
ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
ASSERT_OK(strcmp("arg_parsing", set.tests[0].subtests[0]),
"subtest name");
ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
free_test_filter_set(&set);
ASSERT_OK(parse_test_list("arg_parsing/arg_parsing", &set, true),
"parsing");
ASSERT_OK(parse_test_list("bpf_cookie", &set, true), "parsing");
ASSERT_OK(parse_test_list("send_signal", &set, true), "parsing");
if (!ASSERT_EQ(set.cnt, 3, "count of test filters"))
goto error;
if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
goto error;
if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
goto error;
ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
ASSERT_EQ(set.tests[2].subtest_cnt, 0, "subtest filters count");
ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
ASSERT_OK(strcmp("arg_parsing", set.tests[0].subtests[0]),
"subtest name");
ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
ASSERT_OK(strcmp("send_signal", set.tests[2].name), "test name");
free_test_filter_set(&set);
ASSERT_OK(parse_test_list("bpf_cookie/trace", &set, false), "parsing");
if (!ASSERT_EQ(set.cnt, 1, "count of test filters"))
goto error;
if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
goto error;
if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
goto error;
ASSERT_OK(strcmp("*bpf_cookie*", set.tests[0].name), "test name");
ASSERT_OK(strcmp("*trace*", set.tests[0].subtests[0]), "subtest name");
error:
free_test_filter_set(&set);
}
void test_arg_parsing(void)
{
if (test__start_subtest("test_parse_test_list"))
test_parse_test_list();
}
......@@ -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>
......@@ -84,12 +85,13 @@ static bool should_run(struct test_selector *sel, int num, const char *name)
int i;
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].subtest_cnt)
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;
}
......@@ -99,6 +101,46 @@ static bool should_run(struct test_selector *sel, int num, const char *name)
return num < sel->num_set_len && sel->num_set[num];
}
static bool should_run_subtest(struct test_selector *sel,
struct test_selector *subtest_sel,
int subtest_num,
const char *test_name,
const char *subtest_name)
{
int i, j;
for (i = 0; i < sel->blacklist.cnt; i++) {
if (glob_match(test_name, sel->blacklist.tests[i].name)) {
if (!sel->blacklist.tests[i].subtest_cnt)
return false;
for (j = 0; j < sel->blacklist.tests[i].subtest_cnt; j++) {
if (glob_match(subtest_name,
sel->blacklist.tests[i].subtests[j]))
return false;
}
}
}
for (i = 0; i < sel->whitelist.cnt; i++) {
if (glob_match(test_name, sel->whitelist.tests[i].name)) {
if (!sel->whitelist.tests[i].subtest_cnt)
return true;
for (j = 0; j < sel->whitelist.tests[i].subtest_cnt; j++) {
if (glob_match(subtest_name,
sel->whitelist.tests[i].subtests[j]))
return true;
}
}
}
if (!sel->whitelist.cnt && !subtest_sel->num_set)
return true;
return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num];
}
static void dump_test_log(const struct prog_test_def *test, bool failed)
{
if (stdout == env.stdout)
......@@ -135,7 +177,6 @@ static void stdio_restore(void);
*/
static void reset_affinity(void)
{
cpu_set_t cpuset;
int i, err;
......@@ -196,7 +237,7 @@ void test__end_subtest(void)
test->subtest_name = NULL;
}
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]) {
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,
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;
}
static void free_str_set(const struct str_set *set)
static void free_test_filter_set(const struct test_filter_set *set)
{
int i;
int i, j;
if (!set)
return;
for (i = 0; i < set->cnt; i++)
free((void *)set->strs[i]);
free(set->strs);
}
static int parse_str_list(const char *s, struct str_set *set, bool is_glob_pattern)
{
char *input, *state = NULL, *next, **tmp, **strs = NULL;
int i, cnt = 0;
for (i = 0; i < set->cnt; i++) {
free((void *)set->tests[i].name);
for (j = 0; j < set->tests[i].subtest_cnt; j++)
free((void *)set->tests[i].subtests[j]);
input = strdup(s);
if (!input)
return -ENOMEM;
while ((next = strtok_r(state ? NULL : input, ",", &state))) {
tmp = realloc(strs, sizeof(*strs) * (cnt + 1));
if (!tmp)
goto err;
strs = tmp;
if (is_glob_pattern) {
strs[cnt] = strdup(next);
if (!strs[cnt])
goto err;
} else {
strs[cnt] = malloc(strlen(next) + 2 + 1);
if (!strs[cnt])
goto err;
sprintf(strs[cnt], "*%s*", next);
}
cnt++;
free((void *)set->tests[i].subtests);
}
tmp = realloc(set->strs, sizeof(*strs) * (cnt + set->cnt));
if (!tmp)
goto err;
memcpy(tmp + set->cnt, strs, sizeof(*strs) * cnt);
set->strs = (const char **)tmp;
set->cnt += cnt;
free((void *)set->tests);
}
free(input);
free(strs);
return 0;
err:
for (i = 0; i < cnt; i++)
free(strs[i]);
free(strs);
free(input);
return -ENOMEM;
static void free_test_selector(struct test_selector *test_selector)
{
free_test_filter_set(&test_selector->blacklist);
free_test_filter_set(&test_selector->whitelist);
free(test_selector->num_set);
}
extern int extra_prog_load_log_flags;
......@@ -615,33 +626,17 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
}
case ARG_TEST_NAME_GLOB_ALLOWLIST:
case ARG_TEST_NAME: {
char *subtest_str = strchr(arg, '/');
if (subtest_str) {
*subtest_str = '\0';
if (parse_str_list(subtest_str + 1,
&env->subtest_selector.whitelist,
key == ARG_TEST_NAME_GLOB_ALLOWLIST))
return -ENOMEM;
}
if (parse_str_list(arg, &env->test_selector.whitelist,
key == ARG_TEST_NAME_GLOB_ALLOWLIST))
if (parse_test_list(arg,
&env->test_selector.whitelist,
key == ARG_TEST_NAME_GLOB_ALLOWLIST))
return -ENOMEM;
break;
}
case ARG_TEST_NAME_GLOB_DENYLIST:
case ARG_TEST_NAME_BLACKLIST: {
char *subtest_str = strchr(arg, '/');
if (subtest_str) {
*subtest_str = '\0';
if (parse_str_list(subtest_str + 1,
&env->subtest_selector.blacklist,
key == ARG_TEST_NAME_GLOB_DENYLIST))
return -ENOMEM;
}
if (parse_str_list(arg, &env->test_selector.blacklist,
key == ARG_TEST_NAME_GLOB_DENYLIST))
if (parse_test_list(arg,
&env->test_selector.blacklist,
key == ARG_TEST_NAME_GLOB_DENYLIST))
return -ENOMEM;
break;
}
......@@ -1493,12 +1488,8 @@ int main(int argc, char **argv)
out:
if (!env.list_test_names && env.has_testmod)
unload_bpf_testmod();
free_str_set(&env.test_selector.blacklist);
free_str_set(&env.test_selector.whitelist);
free(env.test_selector.num_set);
free_str_set(&env.subtest_selector.blacklist);
free_str_set(&env.subtest_selector.whitelist);
free(env.subtest_selector.num_set);
free_test_selector(&env.test_selector);
if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
return EXIT_NO_TEST;
......
......@@ -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,20 @@ enum verbosity {
VERBOSE_SUPER,
};
struct str_set {
const char **strs;
struct test_filter {
char *name;
char **subtests;
int subtest_cnt;
};
struct test_filter_set {
struct test_filter *tests;
int cnt;
};
struct test_selector {
struct str_set whitelist;
struct str_set blacklist;
struct test_filter_set whitelist;
struct test_filter_set blacklist;
bool *num_set;
int num_set_len;
};
......
......@@ -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,94 @@ 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_filter_set *set,
bool is_glob_pattern)
{
char *input, *state = NULL, *next;
struct test_filter *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;
int glob_chars = 0;
tmp = realloc(tests, sizeof(*tests) * (cnt + 1));
if (!tmp)
goto err;
tests = tmp;
tests[cnt].subtest_cnt = 0;
tests[cnt].subtests = NULL;
if (is_glob_pattern) {
pattern = "%s";
} else {
pattern = "*%s*";
glob_chars = 2;
}
if (subtest_str) {
char **tmp_subtests = NULL;
int subtest_cnt = tests[cnt].subtest_cnt;
*subtest_str = '\0';
subtest_str += 1;
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] =
malloc(strlen(subtest_str) + glob_chars + 1);
if (!tests[cnt].subtests[subtest_cnt])
goto err;
sprintf(tests[cnt].subtests[subtest_cnt],
pattern,
subtest_str);
tests[cnt].subtest_cnt++;
}
tests[cnt].name = malloc(strlen(next) + glob_chars + 1);
if (!tests[cnt].name)
goto err;
sprintf(tests[cnt].name, pattern, next);
cnt++;
}
tmp = realloc(set->tests, sizeof(*tests) * (cnt + set->cnt));
if (!tmp)
goto err;
memcpy(tmp + set->cnt, tests, sizeof(*tests) * cnt);
set->tests = tmp;
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);
......
......@@ -12,3 +12,11 @@ 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_filter_set;
int parse_test_list(const char *s,
struct test_filter_set *test_set,
bool is_glob_pattern);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment