Commit 2324257d authored by Mykola Lysenko's avatar Mykola Lysenko Committed by Andrii Nakryiko

selftests/bpf: Refactor prog_tests logging and test execution

This is a pre-req to add separate logging for each subtest in
test_progs.

Move all the mutable test data to the test_result struct.
Move per-test init/de-init into the run_one_test function.
Consolidate data aggregation and final log output in
calculate_and_print_summary function.
As a side effect, this patch fixes double counting of errors
for subtests and possible duplicate output of subtest log
on failures.

Also, add prog_tests_framework.c test to verify some of the
counting logic.

As part of verification, confirmed that number of reported
tests is the same before and after the change for both parallel
and sequential test execution.
Signed-off-by: default avatarMykola Lysenko <mykolal@fb.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220418222507.1726259-1-mykolal@fb.com
parent 0fb53aab
...@@ -36,13 +36,13 @@ struct test_config { ...@@ -36,13 +36,13 @@ struct test_config {
void (*bpf_destroy)(void *); void (*bpf_destroy)(void *);
}; };
enum test_state { enum bpf_test_state {
_TS_INVALID, _TS_INVALID,
TS_MODULE_LOAD, TS_MODULE_LOAD,
TS_MODULE_LOAD_FAIL, TS_MODULE_LOAD_FAIL,
}; };
static _Atomic enum test_state state = _TS_INVALID; static _Atomic enum bpf_test_state state = _TS_INVALID;
static int sys_finit_module(int fd, const char *param_values, int flags) static int sys_finit_module(int fd, const char *param_values, int flags)
{ {
......
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
#include "test_progs.h"
#include "testing_helpers.h"
static void clear_test_state(struct test_state *state)
{
state->error_cnt = 0;
state->sub_succ_cnt = 0;
state->skip_cnt = 0;
}
void test_prog_tests_framework(void)
{
struct test_state *state = env.test_state;
/* in all the ASSERT calls below we need to return on the first
* error due to the fact that we are cleaning the test state after
* each dummy subtest
*/
/* test we properly count skipped tests with subtests */
if (test__start_subtest("test_good_subtest"))
test__end_subtest();
if (!ASSERT_EQ(state->skip_cnt, 0, "skip_cnt_check"))
return;
if (!ASSERT_EQ(state->error_cnt, 0, "error_cnt_check"))
return;
if (!ASSERT_EQ(state->subtest_num, 1, "subtest_num_check"))
return;
clear_test_state(state);
if (test__start_subtest("test_skip_subtest")) {
test__skip();
test__end_subtest();
}
if (test__start_subtest("test_skip_subtest")) {
test__skip();
test__end_subtest();
}
if (!ASSERT_EQ(state->skip_cnt, 2, "skip_cnt_check"))
return;
if (!ASSERT_EQ(state->subtest_num, 3, "subtest_num_check"))
return;
clear_test_state(state);
if (test__start_subtest("test_fail_subtest")) {
test__fail();
test__end_subtest();
}
if (!ASSERT_EQ(state->error_cnt, 1, "error_cnt_check"))
return;
if (!ASSERT_EQ(state->subtest_num, 4, "subtest_num_check"))
return;
clear_test_state(state);
}
This diff is collapsed.
...@@ -64,6 +64,25 @@ struct test_selector { ...@@ -64,6 +64,25 @@ struct test_selector {
int num_set_len; int num_set_len;
}; };
struct test_state {
bool tested;
bool force_log;
int error_cnt;
int skip_cnt;
int subtest_skip_cnt;
int sub_succ_cnt;
char *subtest_name;
int subtest_num;
/* store counts before subtest started */
int old_error_cnt;
size_t log_cnt;
char *log_buf;
};
struct test_env { struct test_env {
struct test_selector test_selector; struct test_selector test_selector;
struct test_selector subtest_selector; struct test_selector subtest_selector;
...@@ -76,12 +95,11 @@ struct test_env { ...@@ -76,12 +95,11 @@ struct test_env {
bool get_test_cnt; bool get_test_cnt;
bool list_test_names; bool list_test_names;
struct prog_test_def *test; /* current running tests */ struct prog_test_def *test; /* current running test */
struct test_state *test_state; /* current running test result */
FILE *stdout; FILE *stdout;
FILE *stderr; FILE *stderr;
char *log_buf;
size_t log_cnt;
int nr_cpus; int nr_cpus;
int succ_cnt; /* successful tests */ int succ_cnt; /* successful tests */
...@@ -126,11 +144,12 @@ struct msg { ...@@ -126,11 +144,12 @@ struct msg {
extern struct test_env env; extern struct test_env env;
extern void test__force_log(); void test__force_log(void);
extern bool test__start_subtest(const char *name); bool test__start_subtest(const char *name);
extern void test__skip(void); void test__end_subtest(void);
extern void test__fail(void); void test__skip(void);
extern int test__join_cgroup(const char *path); void test__fail(void);
int test__join_cgroup(const char *path);
#define PRINT_FAIL(format...) \ #define PRINT_FAIL(format...) \
({ \ ({ \
......
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