bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "sunyucong@gmail.com" <sunyucong@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Yucong Sun <fallentree@fb.com>,
	Andrii Nakryiko <andrii@kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH v4 bpf-next 4/4] selftests/bpf: Support glob matching for test selector.
Date: Mon, 16 Aug 2021 21:50:38 -0700	[thread overview]
Message-ID: <CAJygYd0ojV0r1WrXtcQMDYzoE8gk742Pmdo275e14raiLifE2g@mail.gmail.com> (raw)
In-Reply-To: <CAEf4Bza73q02zj_O6RNNF9HaQfLWajOFJgZXrztDvAZFV8n8Ug@mail.gmail.com>

On Mon, Aug 16, 2021 at 7:45 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Mon, Aug 16, 2021 at 6:03 PM Yucong Sun <fallentree@fb.com> wrote:
> >
> > This patch adds '-a' and '-d' arguments, supporting exact string match, as well
> > as using '*' wildcard in test/subtests selection. The old '-t' '-b' arguments
> > still supports partial string match, but they can't be used together yet.
>
> typo: support
>
> >
> > Caveat: As same as the current substring matching mechanism, test and subtest
>
> "Same as"
>
> > selector applies independently, 'a*/b*' will execute all tests matches "a*",
>
> s/matches/matching/ same below
>
> > and with subtest name matches "b*", but tests matches "a*" but has no subtests
> > will also be executed.
> >
> > Signed-off-by: Yucong Sun <fallentree@fb.com>
> > ---
> >  tools/testing/selftests/bpf/test_progs.c | 64 +++++++++++++++++++++---
> >  tools/testing/selftests/bpf/test_progs.h |  1 +
> >  2 files changed, 57 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> > index 90539b15b744..f5dbaa29d370 100644
> > --- a/tools/testing/selftests/bpf/test_progs.c
> > +++ b/tools/testing/selftests/bpf/test_progs.c
> > @@ -13,6 +13,28 @@
> >  #include <execinfo.h> /* backtrace */
> >  #include <linux/membarrier.h>
> >
> > +/* Adapted from perf/util/string.c */
> > +static bool glob_match(const char *str, const char *pat)
> > +{
> > +       while (*str && *pat && *pat != '*') {
> > +               if (*str != *pat)
> > +                       return false;
> > +               str++;
> > +               pat++;
> > +       }
> > +       /* Check wild card */
> > +       if (*pat == '*') {
> > +               while (*pat == '*')
> > +                       pat++;
> > +               if (!*pat) /* Tail wild card matches all */
> > +                       return true;
> > +               while (*str)
> > +                       if (glob_match(str++, pat))
> > +                               return true;
> > +       }
> > +       return !*str && !*pat;
> > +}
> > +
> >  #define EXIT_NO_TEST           2
> >  #define EXIT_ERR_SETUP_INFRA   3
> >
> > @@ -55,12 +77,12 @@ static bool should_run(struct test_selector *sel, int num, const char *name)
> >         int i;
> >
> >         for (i = 0; i < sel->blacklist.cnt; i++) {
> > -               if (strstr(name, sel->blacklist.strs[i]))
> > +               if (glob_match(name, sel->blacklist.strs[i]))
> >                         return false;
> >         }
> >
> >         for (i = 0; i < sel->whitelist.cnt; i++) {
> > -               if (strstr(name, sel->whitelist.strs[i]))
> > +               if (glob_match(name, sel->whitelist.strs[i]))
> >                         return true;
> >         }
> >
> > @@ -450,6 +472,8 @@ enum ARG_KEYS {
> >         ARG_VERBOSE = 'v',
> >         ARG_GET_TEST_CNT = 'c',
> >         ARG_LIST_TEST_NAMES = 'l',
> > +       ARG_TEST_NAME_GLOB_ALLOWLIST = 'a',
> > +       ARG_TEST_NAME_GLOB_DENYLIST = 'd',
> >  };
> >
> >  static const struct argp_option opts[] = {
> > @@ -467,6 +491,10 @@ static const struct argp_option opts[] = {
> >           "Get number of selected top-level tests " },
> >         { "list", ARG_LIST_TEST_NAMES, NULL, 0,
> >           "List test names that would run (without running them) " },
> > +       { "allow", ARG_TEST_NAME_GLOB_ALLOWLIST, "NAMES", 0,
> > +         "Run tests with name matching the pattern (support *)." },
> > +       { "deny", ARG_TEST_NAME_GLOB_DENYLIST, "NAMES", 0,
> > +         "Don't run tests with name matching the pattern (support *)." },
> >         {},
> >  };
> >
> > @@ -491,7 +519,7 @@ static void free_str_set(const struct str_set *set)
> >         free(set->strs);
> >  }
> >
> > -static int parse_str_list(const char *s, struct str_set *set)
> > +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 cnt = 0;
> > @@ -508,8 +536,14 @@ static int parse_str_list(const char *s, struct str_set *set)
> >                 if (!tmp)
> >                         goto err;
> >                 strs = tmp;
> > +               if (is_glob_pattern)
> > +                       strs[cnt] = strdup(next);
>
> coding style: if one branch of if/else uses {}, the other has to use it as well
>
> > +               else {
> > +                       char* pat = malloc(strlen(next) + 2 + 1);
>
> coding style: empty line after variable declaration (but I'd just use
> strs[cnt] twice without extra var)
>
> also `char *pat`, please use checkpatch.pl to double check your
> changes until kernel code style becomes second nature (and even then
> it's probably useful to run it from time to time). We do have few
> errors reported in test_progs.c, but the script would reported 2 out
> of 3 of these problems. At some point we should see if it makes sense
> to run checkpatch.pl as one of BPF CI steps.

Sorry, done!

>
> > +                       sprintf(pat, "*%s*", next);
> > +                       strs[cnt] = pat;
> > +               }
> >
> > -               strs[cnt] = strdup(next);
> >                 if (!strs[cnt])
> >                         goto err;
> >
> > @@ -553,29 +587,43 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
> >                 }
> >                 break;
> >         }
> > +       case ARG_TEST_NAME_GLOB_ALLOWLIST:
> >         case ARG_TEST_NAME: {
> > +               if (env->test_selector.whitelist.cnt || env->subtest_selector.whitelist.cnt) {
> > +                       fprintf(stderr, "-a and -t are mutually exclusive, you can only specify one.\n");
> > +                       return -EINVAL;
> > +               }
>
> Why are they still exclusive? They are now just alternative ways to
> specify the same set of filters, one as substring match, another as
> explicit globs. Just like you can use '-t a,b', you can also have '-t
> a -a "*b*"', right? They can totally co-exist and it might be useful
> to use both sometimes.

I added the logic to accept multiple -a -d -t -d switches as requested.

>
> >                 char *subtest_str = strchr(arg, '/');
> >
> >                 if (subtest_str) {
> >                         *subtest_str = '\0';
> >                         if (parse_str_list(subtest_str + 1,
> > -                                          &env->subtest_selector.whitelist))
> > +                                          &env->subtest_selector.whitelist,
> > +                                          key == ARG_TEST_NAME_GLOB_ALLOWLIST))
> >                                 return -ENOMEM;
> >                 }
> > -               if (parse_str_list(arg, &env->test_selector.whitelist))
> > +               if (parse_str_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: {
> > +               if (env->test_selector.blacklist.cnt || env->subtest_selector.blacklist.cnt) {
> > +                       fprintf(stderr, "-d and -b are mutually exclusive, you can only specify one.\n");
> > +                       return -EINVAL;
> > +               }
>
> same, they can organically co-exist
>
> >                 char *subtest_str = strchr(arg, '/');
> >
> >                 if (subtest_str) {
> >                         *subtest_str = '\0';
> >                         if (parse_str_list(subtest_str + 1,
> > -                                          &env->subtest_selector.blacklist))
> > +                                          &env->subtest_selector.blacklist,
> > +                                          key == ARG_TEST_NAME_GLOB_DENYLIST))
> >                                 return -ENOMEM;
> >                 }
> > -               if (parse_str_list(arg, &env->test_selector.blacklist))
> > +               if (parse_str_list(arg, &env->test_selector.blacklist,
> > +                                  key == ARG_TEST_NAME_GLOB_DENYLIST))
> >                         return -ENOMEM;
> >                 break;
> >         }
> > diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> > index c8c2bf878f67..c475d65dce4f 100644
> > --- a/tools/testing/selftests/bpf/test_progs.h
> > +++ b/tools/testing/selftests/bpf/test_progs.h
> > @@ -49,6 +49,7 @@ enum verbosity {
> >  struct str_set {
> >         const char **strs;
> >         int cnt;
> > +       bool is_glob_pattern;
>
>
> leftovers? doesn't seem to be used

Deleted.

>
> >  };
> >
> >  struct test_selector {
> > --
> > 2.30.2
> >

  reply	other threads:[~2021-08-17  4:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17  1:03 [PATCH v4 bpf-next 0/4] selftests/bpf: Improve the usability of Yucong Sun
2021-08-17  1:03 ` [PATCH v4 bpf-next 1/4] selftests/bpf: skip loading bpf_testmod when using -l to list tests Yucong Sun
2021-08-17  1:03 ` [PATCH v4 bpf-next 2/4] selftests/bpf: correctly display subtest skip status Yucong Sun
2021-08-17  1:03 ` [PATCH v4 bpf-next 3/4] selftests/bpf: also print test name in subtest status message Yucong Sun
2021-08-17  1:03 ` [PATCH v4 bpf-next 4/4] selftests/bpf: Support glob matching for test selector Yucong Sun
2021-08-17  2:45   ` Andrii Nakryiko
2021-08-17  4:50     ` sunyucong [this message]
2021-08-17  2:47 ` [PATCH v4 bpf-next 0/4] selftests/bpf: Improve the usability of Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJygYd0ojV0r1WrXtcQMDYzoE8gk742Pmdo275e14raiLifE2g@mail.gmail.com \
    --to=sunyucong@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=fallentree@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).