From: Eduard Zingerman <eddyz87@gmail.com>
To: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com
Cc: Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next v2 2/2] selftests/bpf: introduce veristat test
Date: Mon, 10 Feb 2025 19:01:41 -0800 [thread overview]
Message-ID: <37033e12b0aad918a1787d2e0ef4a8b5e67c7413.camel@gmail.com> (raw)
In-Reply-To: <20250210135129.719119-3-mykyta.yatsenko5@gmail.com>
On Mon, 2025-02-10 at 13:51 +0000, Mykyta Yatsenko wrote:
[...]
> +void test_veristat_set_global_vars_succeeds(void)
> +{
test_progs based tests are usually organized as a hierarchy of tests and sub-tests.
E.g. take a look at tools/testing/selftests/bpf/prog_tests/ksyms_btf.c:
- it defines an entry point test_ksyms_btf;
- and a bunch of sub-tests declared as static void functions,
called from entry point;
- test__start_subtest() function is used to check if sub-test has to
be executed.
> + char command[512];
> + struct fixture *fix = init_fixture();
> +
> + snprintf(command, sizeof(command),
> + "./veristat set_global_vars.bpf.o"\
> + " -G \"var_s64 = 0xf000000000000001\" "\
> + " -G \"var_u64 = 0xfedcba9876543210\" "\
> + " -G \"var_s32 = -0x80000000\" "\
> + " -G \"var_u32 = 0x76543210\" "\
> + " -G \"var_s16 = -32768\" "\
> + " -G \"var_u16 = 60652\" "\
> + " -G \"var_s8 = -128\" "\
> + " -G \"var_u8 = 255\" "\
> + " -G \"var_ea = EA2\" "\
> + " -G \"var_eb = EB2\" "\
> + " -G \"var_ec = EC2\" "\
> + " -G \"var_b = 1\" "\
> + "-vl2 > %s", fix->tmpfile);
> + if (!ASSERT_EQ(0, system(command), "command"))
> + goto out;
Nit: there is SYS macro in test_progs.h, it combines
snprintf/system/ASSERT_OK/goto.
> +
> + read(fix->fd, fix->output, fix->sz);
Nit: check error for read() call (same read()/write() in tests below).
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xf000000000000001 "),
> + "var_s64 = 0xf000000000000001");
Nit: I'd do these checks as below:
#define __CHECK_STR(str, name) \
if (!ASSERT_HAS_SUBSTR(fix->output, (str), (str))) goto out
__CHECK_STR("_w=0xf000000000000001 ");
...
#undef __CHECK_STR
this way fix->output would be printed if sub-string is not found.
For other tests I suggest using ASSERT_HAS_SUBSTR as well,
as it prints the string where sub-string was looked for.
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xfedcba9876543210 "),
> + "var_u64 = 0xfedcba9876543210");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x80000000 "), "var_s32 = -0x80000000");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x76543210 "), "var_u32 = 0x76543210");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=128 "), "var_s8 = -128");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=255 "), "var_u8 = 255");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=11 "), "var_ea = EA2");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=12 "), "var_eb = EB2");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=13 "), "var_ec = EC2");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=1 "), "var_b = 1");
> +
> +out:
> + teardown_fixture(fix);
> +}
> +
> +void test_veristat_set_global_vars_from_file_succeeds(void)
> +{
> + struct fixture *fix = init_fixture();
> + char command[512];
> + char input_file[80];
> + const char *vars = "var_s16 = -32768\nvar_u16 = 60652";
> + int fd;
> +
> + snprintf(input_file, sizeof(input_file), "/tmp/veristat_input.XXXXXX");
> + fd = mkstemp(input_file);
> + if (!ASSERT_GT(fd, 0, "valid fd"))
Nit: ASSERT_GE.
> + goto out;
> +
> + write(fd, vars, strlen(vars));
> + snprintf(command, sizeof(command),
> + "./veristat set_global_vars.bpf.o -G \"@%s\" -vl2 > %s",
> + input_file, fix->tmpfile);
> +
> + ASSERT_EQ(0, system(command), "command");
> + read(fix->fd, fix->output, fix->sz);
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0x8000 "), "var_s16 = -32768");
> + ASSERT_NEQ(NULL, strstr(fix->output, "_w=0xecec "), "var_u16 = 60652");
> +
> +out:
> + close(fd);
> + remove(input_file);
> + teardown_fixture(fix);
> +}
[...]
next prev parent reply other threads:[~2025-02-11 3:01 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-10 13:51 [PATCH bpf-next v2 0/2] selftests/bpf: implement setting global variables in veristat Mykyta Yatsenko
2025-02-10 13:51 ` [PATCH bpf-next v2 1/2] " Mykyta Yatsenko
2025-02-11 1:13 ` Andrii Nakryiko
2025-02-11 1:44 ` Eduard Zingerman
2025-02-11 20:55 ` Andrii Nakryiko
2025-02-11 15:00 ` Mykyta Yatsenko
2025-02-11 20:53 ` Andrii Nakryiko
2025-02-11 1:24 ` Eduard Zingerman
2025-02-11 15:07 ` Mykyta Yatsenko
2025-02-10 13:51 ` [PATCH bpf-next v2 2/2] selftests/bpf: introduce veristat test Mykyta Yatsenko
2025-02-11 3:01 ` Eduard Zingerman [this message]
2025-02-11 15:09 ` Mykyta Yatsenko
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=37033e12b0aad918a1787d2e0ef4a8b5e67c7413.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@meta.com \
--cc=kernel-team@meta.com \
--cc=mykyta.yatsenko5@gmail.com \
--cc=yatsenko@meta.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