test: Pass the test state to cmd_ut_category()

Update this function to access a unit-test state, so that the caller can
collect results from running multiple suites.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2025-01-20 14:25:58 -07:00 committed by Tom Rini
parent 374203bd2e
commit bbff0b165c
4 changed files with 38 additions and 36 deletions

View File

@ -9,14 +9,18 @@
struct cmd_tbl; struct cmd_tbl;
struct unit_test; struct unit_test;
struct unit_test_state;
/* 'command' functions normally called do_xxx where xxx is the command name */ /* 'command' functions normally called do_xxx where xxx is the command name */
typedef int (*ut_cmd_func)(struct cmd_tbl *cmd, int flags, int argc, typedef int (*ut_cmd_func)(struct unit_test_state *uts, struct cmd_tbl *cmd,
char *const argv[]); int flags, int argc, char *const argv[]);
/** /**
* cmd_ut_category() - Run a category of unit tests * cmd_ut_category() - Run a category of unit tests
* *
* @uts: Unit-test state, which must be ready for use, i.e. ut_init_state()
* has been called. The caller is responsible for calling
* ut_uninit_state() after this function returns
* @name: Category name * @name: Category name
* @prefix: Prefix of test name * @prefix: Prefix of test name
* @tests: List of tests to run * @tests: List of tests to run
@ -26,14 +30,14 @@ typedef int (*ut_cmd_func)(struct cmd_tbl *cmd, int flags, int argc,
* @argv: Arguments: argv[1] is the test to run (if @argc >= 2) * @argv: Arguments: argv[1] is the test to run (if @argc >= 2)
* Return: 0 if OK, CMD_RET_FAILURE on failure * Return: 0 if OK, CMD_RET_FAILURE on failure
*/ */
int cmd_ut_category(const char *name, const char *prefix, int cmd_ut_category(struct unit_test_state *uts, const char *name,
struct unit_test *tests, int n_ents, const char *prefix, struct unit_test *tests, int n_ents,
int argc, char *const argv[]); int argc, char *const argv[]);
int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc, int do_ut_bootstd(struct unit_test_state *uts, struct cmd_tbl *cmdtp, int flag,
char *const argv[]); int argc, char *const argv[]);
int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, int do_ut_overlay(struct unit_test_state *uts, struct cmd_tbl *cmdtp, int flag,
char *const argv[]); int argc, char *const argv[]);
#endif /* __TEST_SUITES_H__ */ #endif /* __TEST_SUITES_H__ */

View File

@ -94,7 +94,8 @@ void bootstd_reset_usb(void)
usb_started = false; usb_started = false;
} }
int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) int do_ut_bootstd(struct unit_test_state *uts, struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{ {
struct unit_test *tests = UNIT_TEST_SUITE_START(bootstd); struct unit_test *tests = UNIT_TEST_SUITE_START(bootstd);
const int n_ents = UNIT_TEST_SUITE_COUNT(bootstd); const int n_ents = UNIT_TEST_SUITE_COUNT(bootstd);
@ -106,6 +107,6 @@ int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
return CMD_RET_FAILURE; return CMD_RET_FAILURE;
} }
return cmd_ut_category("bootstd", "bootstd_", tests, n_ents, return cmd_ut_category(uts, "bootstd", "bootstd_",
argc, argv); tests, n_ents, argc, argv);
} }

View File

@ -29,17 +29,16 @@ struct suite {
ut_cmd_func cmd; ut_cmd_func cmd;
}; };
static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc, static int do_ut_all(struct unit_test_state *uts, struct cmd_tbl *cmdtp,
char *const argv[]); int flag, int argc, char *const argv[]);
static int do_ut_info(struct cmd_tbl *cmdtp, int flag, int argc, static int do_ut_info(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[]); char *const argv[]);
int cmd_ut_category(const char *name, const char *prefix, int cmd_ut_category(struct unit_test_state *uts, const char *name,
struct unit_test *tests, int n_ents, const char *prefix, struct unit_test *tests, int n_ents,
int argc, char *const argv[]) int argc, char *const argv[])
{ {
struct unit_test_state uts;
const char *test_insert = NULL; const char *test_insert = NULL;
int runs_per_text = 1; int runs_per_text = 1;
bool force_run = false; bool force_run = false;
@ -63,11 +62,9 @@ int cmd_ut_category(const char *name, const char *prefix,
argc--; argc--;
} }
ut_init_state(&uts); ret = ut_run_list(uts, name, prefix, tests, n_ents,
ret = ut_run_list(&uts, name, prefix, tests, n_ents,
cmd_arg1(argc, argv), runs_per_text, force_run, cmd_arg1(argc, argv), runs_per_text, force_run,
test_insert); test_insert);
ut_uninit_state(&uts);
return ret ? CMD_RET_FAILURE : 0; return ret ? CMD_RET_FAILURE : 0;
} }
@ -170,28 +167,29 @@ static bool has_tests(struct suite *ste)
} }
/** run_suite() - Run a suite of tests */ /** run_suite() - Run a suite of tests */
static int run_suite(struct suite *ste, struct cmd_tbl *cmdtp, int flag, static int run_suite(struct unit_test_state *uts, struct suite *ste,
int argc, char *const argv[]) struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{ {
int ret; int ret;
if (ste->cmd) { if (ste->cmd) {
ret = ste->cmd(cmdtp, flag, argc, argv); ret = ste->cmd(uts, cmdtp, flag, argc, argv);
} else { } else {
int n_ents = ste->end - ste->start; int n_ents = ste->end - ste->start;
char prefix[30]; char prefix[30];
/* use a standard prefix */ /* use a standard prefix */
snprintf(prefix, sizeof(prefix), "%s_test", ste->name); snprintf(prefix, sizeof(prefix), "%s_test", ste->name);
ret = cmd_ut_category(ste->name, prefix, ste->start, n_ents, ret = cmd_ut_category(uts, ste->name, prefix, ste->start,
argc, argv); n_ents, argc, argv);
} }
return ret; return ret;
} }
static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc, static int do_ut_all(struct unit_test_state *uts, struct cmd_tbl *cmdtp,
char *const argv[]) int flag, int argc, char *const argv[])
{ {
int i; int i;
int retval; int retval;
@ -203,7 +201,7 @@ static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
if (has_tests(ste)) { if (has_tests(ste)) {
printf("----Running %s tests----\n", ste->name); printf("----Running %s tests----\n", ste->name);
retval = run_suite(ste, cmdtp, flag, 1, argv); retval = run_suite(uts, ste, cmdtp, flag, 1, argv);
if (!any_fail) if (!any_fail)
any_fail = retval; any_fail = retval;
} }
@ -263,6 +261,7 @@ static struct suite *find_suite(const char *name)
static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{ {
struct unit_test_state uts;
struct suite *ste; struct suite *ste;
const char *name; const char *name;
int ret; int ret;
@ -274,9 +273,10 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
argc--; argc--;
argv++; argv++;
ut_init_state(&uts);
name = argv[0]; name = argv[0];
if (!strcmp(name, "all")) { if (!strcmp(name, "all")) {
ret = do_ut_all(cmdtp, flag, argc, argv); ret = do_ut_all(&uts, cmdtp, flag, argc, argv);
} else if (!strcmp(name, "info")) { } else if (!strcmp(name, "info")) {
ret = do_ut_info(cmdtp, flag, argc, argv); ret = do_ut_info(cmdtp, flag, argc, argv);
} else { } else {
@ -290,10 +290,11 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
return CMD_RET_FAILURE; return CMD_RET_FAILURE;
} }
ret = run_suite(ste, cmdtp, flag, argc, argv); ret = run_suite(&uts, ste, cmdtp, flag, argc, argv);
} }
if (ret) if (ret)
return ret; return ret;
ut_uninit_state(&uts);
return 0; return 0;
} }

View File

@ -210,21 +210,17 @@ static int fdt_overlay_stacked(struct unit_test_state *uts)
} }
OVERLAY_TEST(fdt_overlay_stacked, 0); OVERLAY_TEST(fdt_overlay_stacked, 0);
int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) int do_ut_overlay(struct unit_test_state *uts, struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{ {
struct unit_test *tests = UNIT_TEST_SUITE_START(overlay); struct unit_test *tests = UNIT_TEST_SUITE_START(overlay);
const int n_ents = UNIT_TEST_SUITE_COUNT(overlay); const int n_ents = UNIT_TEST_SUITE_COUNT(overlay);
struct unit_test_state *uts;
void *fdt_base = &__dtb_test_fdt_base_begin; void *fdt_base = &__dtb_test_fdt_base_begin;
void *fdt_overlay = &__dtbo_test_fdt_overlay_begin; void *fdt_overlay = &__dtbo_test_fdt_overlay_begin;
void *fdt_overlay_stacked = &__dtbo_test_fdt_overlay_stacked_begin; void *fdt_overlay_stacked = &__dtbo_test_fdt_overlay_stacked_begin;
void *fdt_overlay_copy, *fdt_overlay_stacked_copy; void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
int ret = -ENOMEM; int ret = -ENOMEM;
uts = calloc(1, sizeof(*uts));
if (!uts)
return -ENOMEM;
ut_assertok(fdt_check_header(fdt_base)); ut_assertok(fdt_check_header(fdt_base));
ut_assertok(fdt_check_header(fdt_overlay)); ut_assertok(fdt_check_header(fdt_overlay));
@ -272,7 +268,7 @@ int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
/* Apply the stacked overlay */ /* Apply the stacked overlay */
ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy)); ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy));
ret = cmd_ut_category("overlay", "", tests, n_ents, argc, argv); ret = cmd_ut_category(uts, "overlay", "", tests, n_ents, argc, argv);
free(fdt_overlay_stacked_copy); free(fdt_overlay_stacked_copy);
err3: err3: