bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Andrey Ignatov <rdna@fb.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>, Martin Lau <kafai@fb.com>,
	Andrii Nakryiko <andriin@fb.com>,
	Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH v3 bpf-next 5/6] selftests/bpf: Convert test_cgroup_attach to prog_tests
Date: Wed, 18 Dec 2019 22:11:00 -0800	[thread overview]
Message-ID: <CAEf4Bzb2_UqGJxxXvqqpdymzrE06dhj4-XWg5ndsgDndNw787w@mail.gmail.com> (raw)
In-Reply-To: <a13336d12dff699d2b437ffa024adc1d95c97fcd.1576720240.git.rdna@fb.com>

On Wed, Dec 18, 2019 at 6:14 PM Andrey Ignatov <rdna@fb.com> wrote:
>
> Convert test_cgroup_attach to prog_tests.
>
> This change does a lot of things but in many cases it's pretty expensive
> to separate them, so they go in one commit. Nevertheless the logic is
> ketp as is and changes made are just moving things around, simplifying
> them (w/o changing the meaning of the tests) and making prog_tests
> compatible:
>
> * split the 3 tests in the file into 3 separate files in prog_tests/;
>
> * rename the test functions to test_<file_base_name>;
>
> * remove unused includes, constants, variables and functions from every
>   test;
>
> * replace `if`-s with or `if (CHECK())` where additional context should
>   be logged and with `if (CHECK_FAIL())` where line number is enough;
>
> * switch from `log_err()` to logging via `CHECK()`;
>
> * replace `assert`-s with `CHECK_FAIL()` to avoid crashing the whole
>   test_progs if one assertion fails;
>
> * replace cgroup_helpers with test__join_cgroup() in
>   cgroup_attach_override only, other tests need more fine-grained
>   control for cgroup creation/deletion so cgroup_helpers are still used
>   there;
>
> * simplify cgroup_attach_autodetach by switching to easiest possible
>   program since this test doesn't really need such a complicated program
>   as cgroup_attach_multi does;
>
> * remove test_cgroup_attach.c itself.
>
> Signed-off-by: Andrey Ignatov <rdna@fb.com>
> ---
>  tools/testing/selftests/bpf/.gitignore        |   1 -
>  tools/testing/selftests/bpf/Makefile          |   3 +-
>  .../bpf/prog_tests/cgroup_attach_autodetach.c | 111 ++++
>  .../bpf/prog_tests/cgroup_attach_multi.c      | 238 ++++++++
>  .../bpf/prog_tests/cgroup_attach_override.c   | 148 +++++
>  .../selftests/bpf/test_cgroup_attach.c        | 571 ------------------
>  6 files changed, 498 insertions(+), 574 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c
>  delete mode 100644 tools/testing/selftests/bpf/test_cgroup_attach.c
>

[...]

> +
> +       if (CHECK_FAIL(setup_cgroup_environment()))
> +               goto err;
> +
> +       cg1 = create_and_get_cgroup("/cg1");
> +       if (CHECK_FAIL(cg1 < 0))
> +               goto err;
> +       cg2 = create_and_get_cgroup("/cg1/cg2");
> +       if (CHECK_FAIL(cg2 < 0))
> +               goto err;
> +       cg3 = create_and_get_cgroup("/cg1/cg2/cg3");
> +       if (CHECK_FAIL(cg3 < 0))
> +               goto err;
> +       cg4 = create_and_get_cgroup("/cg1/cg2/cg3/cg4");
> +       if (CHECK_FAIL(cg4 < 0))
> +               goto err;
> +       cg5 = create_and_get_cgroup("/cg1/cg2/cg3/cg4/cg5");
> +       if (CHECK_FAIL(cg5 < 0))
> +               goto err;
> +
> +       if (CHECK_FAIL(join_cgroup("/cg1/cg2/cg3/cg4/cg5")))
> +               goto err;
> +
> +       if (CHECK(bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS,
> +                                 BPF_F_ALLOW_MULTI),
> +                 "prog0_attach_to_cg1_multi", "errno=%d\n", errno))
> +               goto err;
> +
> +       if (CHECK(!bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS,
> +                                  BPF_F_ALLOW_MULTI),
> +                 "fail_same_prog_attach_to_cg1", "unexpected success\n"))
> +               goto err;
> +
> +       if (CHECK(bpf_prog_attach(allow_prog[1], cg1, BPF_CGROUP_INET_EGRESS,
> +                                 BPF_F_ALLOW_MULTI),
> +                 "prog1_attach_to_cg1_multi", "errno=%d\n", errno))
> +               goto err;
> +
> +       if (CHECK(bpf_prog_attach(allow_prog[2], cg2, BPF_CGROUP_INET_EGRESS,
> +                                 BPF_F_ALLOW_OVERRIDE),
> +                 "prog2_attach_to_cg2_override", "errno=%d\n", errno))
> +               goto err;
> +
> +       if (CHECK(bpf_prog_attach(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS,
> +                                 BPF_F_ALLOW_MULTI),
> +                 "prog3_attach_to_cg3_multi", "errno=%d\n", errno))
> +               goto err;
> +
> +       if (CHECK(bpf_prog_attach(allow_prog[4], cg4, BPF_CGROUP_INET_EGRESS,
> +                           BPF_F_ALLOW_OVERRIDE),
> +                 "prog4_attach_to_cg4_override", "errno=%d\n", errno))
> +               goto err;
> +
> +       if (CHECK(bpf_prog_attach(allow_prog[5], cg5, BPF_CGROUP_INET_EGRESS, 0),
> +                 "prog5_attach_to_cg5_none", "errno=%d\n", errno))
> +               goto err;

nit: this looks like a good candidate for a loop...

> +
> +       CHECK_FAIL(system(PING_CMD));
> +       CHECK_FAIL(bpf_map_lookup_elem(map_fd, &key, &value));
> +       CHECK_FAIL(value != 1 + 2 + 8 + 32);
> +

[...]

> -int main(void)
> -{
> -       int (*tests[])(void) = {
> -               test_foo_bar,
> -               test_multiprog,
> -               test_autodetach,
> -       };
> -       int errors = 0;
> -       int i;
> -
> -       for (i = 0; i < ARRAY_SIZE(tests); i++)
> -               if (tests[i]())
> -                       errors++;

Depending on what you think is better structure (I couldn't follow
through entire test logic...), you could have done this as a single
test with 3 subtests. Search for test__start_subtest(). If that saves
you some duplication, I think it's worth converting (which will be
1-to-1 with original structure).

But either way thanks a lot for doing the convertion, brings as
another step closer to more uniform and consolidated testing infra.

> -
> -       if (errors)
> -               printf("test_cgroup_attach:FAIL\n");
> -       else
> -               printf("test_cgroup_attach:PASS\n");
> -
> -       return errors ? EXIT_FAILURE : EXIT_SUCCESS;
> -}
> --
> 2.17.1
>

  reply	other threads:[~2019-12-19  6:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-19  1:55 [PATCH v3 bpf-next 0/6] bpf: Support replacing cgroup-bpf program in MULTI mode Andrey Ignatov
2019-12-19  1:55 ` [PATCH v3 bpf-next 1/6] bpf: Simplify __cgroup_bpf_attach Andrey Ignatov
2019-12-19  1:55 ` [PATCH v3 bpf-next 2/6] bpf: Remove unused new_flags in hierarchy_allows_attach() Andrey Ignatov
2019-12-19  1:56 ` [PATCH v3 bpf-next 3/6] bpf: Support replacing cgroup-bpf program in MULTI mode Andrey Ignatov
2019-12-19  1:56 ` [PATCH v3 bpf-next 4/6] libbpf: Introduce bpf_prog_attach_xattr Andrey Ignatov
2019-12-19  5:49   ` Andrii Nakryiko
2019-12-19  7:03     ` Andrey Ignatov
2019-12-19  1:56 ` [PATCH v3 bpf-next 5/6] selftests/bpf: Convert test_cgroup_attach to prog_tests Andrey Ignatov
2019-12-19  6:11   ` Andrii Nakryiko [this message]
2019-12-19  7:20     ` Andrey Ignatov
2019-12-19 20:40       ` Andrii Nakryiko
2019-12-19  1:56 ` [PATCH v3 bpf-next 6/6] selftests/bpf: Test BPF_F_REPLACE in cgroup_attach_multi Andrey Ignatov
2019-12-19  5:58   ` Andrii Nakryiko
2019-12-19  7:35     ` Andrey Ignatov
2019-12-19 20:44       ` Andrii Nakryiko
2019-12-19 21:09         ` Andrey Ignatov
2019-12-19 21:19           ` Andrii Nakryiko
2019-12-19 21:25             ` Andrey Ignatov

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=CAEf4Bzb2_UqGJxxXvqqpdymzrE06dhj4-XWg5ndsgDndNw787w@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    --cc=rdna@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).