test: Pass the test state to cmd_ut_category()
authorSimon Glass <sjg@chromium.org>
Mon, 20 Jan 2025 21:25:58 +0000 (14:25 -0700)
committerTom Rini <trini@konsulko.com>
Fri, 24 Jan 2025 20:34:41 +0000 (14:34 -0600)
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>
include/test/suites.h
test/boot/bootstd_common.c
test/cmd_ut.c
test/overlay/cmd_ut_overlay.c

index 20e6a2a..774dd89 100644 (file)
@@ -9,14 +9,18 @@
 
 struct cmd_tbl;
 struct unit_test;
+struct unit_test_state;
 
 /* '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,
-                          char *const argv[]);
+typedef int (*ut_cmd_func)(struct unit_test_state *uts, struct cmd_tbl *cmd,
+                          int flags, int argc, char *const argv[]);
 
 /**
  * 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
  * @prefix:    Prefix of test name
  * @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)
  * Return: 0 if OK, CMD_RET_FAILURE on failure
  */
-int cmd_ut_category(const char *name, const char *prefix,
-                   struct unit_test *tests, int n_ents,
+int cmd_ut_category(struct unit_test_state *uts, const char *name,
+                   const char *prefix, struct unit_test *tests, int n_ents,
                    int argc, char *const argv[]);
 
-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[]);
 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,
-                 char *const argv[]);
+int do_ut_overlay(struct unit_test_state *uts, struct cmd_tbl *cmdtp, int flag,
+                 int argc, char *const argv[]);
 
 #endif /* __TEST_SUITES_H__ */
index 6bd9bb6..724e3d9 100644 (file)
@@ -94,7 +94,8 @@ void bootstd_reset_usb(void)
        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);
        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_ut_category("bootstd", "bootstd_", tests, n_ents,
-                              argc, argv);
+       return cmd_ut_category(uts, "bootstd", "bootstd_",
+                              tests, n_ents, argc, argv);
 }
index 16490e0..80df954 100644 (file)
@@ -29,17 +29,16 @@ struct suite {
        ut_cmd_func cmd;
 };
 
-static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
-                    char *const argv[]);
+static int do_ut_all(struct unit_test_state *uts, struct cmd_tbl *cmdtp,
+                    int flag, int argc, char *const argv[]);
 
 static int do_ut_info(struct cmd_tbl *cmdtp, int flag, int argc,
                      char *const argv[]);
 
-int cmd_ut_category(const char *name, const char *prefix,
-                   struct unit_test *tests, int n_ents,
+int cmd_ut_category(struct unit_test_state *uts, const char *name,
+                   const char *prefix, struct unit_test *tests, int n_ents,
                    int argc, char *const argv[])
 {
-       struct unit_test_state uts;
        const char *test_insert = NULL;
        int runs_per_text = 1;
        bool force_run = false;
@@ -63,11 +62,9 @@ int cmd_ut_category(const char *name, const char *prefix,
                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,
                          test_insert);
-       ut_uninit_state(&uts);
 
        return ret ? CMD_RET_FAILURE : 0;
 }
@@ -170,28 +167,29 @@ static bool has_tests(struct suite *ste)
 }
 
 /** run_suite() - Run a suite of tests */
-static int run_suite(struct suite *ste, struct cmd_tbl *cmdtp, int flag,
-                    int argc, char *const argv[])
+static int run_suite(struct unit_test_state *uts, struct suite *ste,
+                    struct cmd_tbl *cmdtp, int flag, int argc,
+                    char *const argv[])
 {
        int ret;
 
        if (ste->cmd) {
-               ret = ste->cmd(cmdtp, flag, argc, argv);
+               ret = ste->cmd(uts, cmdtp, flag, argc, argv);
        } else {
                int n_ents = ste->end - ste->start;
                char prefix[30];
 
                /* use a standard prefix */
                snprintf(prefix, sizeof(prefix), "%s_test", ste->name);
-               ret = cmd_ut_category(ste->name, prefix, ste->start, n_ents,
-                                     argc, argv);
+               ret = cmd_ut_category(uts, ste->name, prefix, ste->start,
+                                     n_ents, argc, argv);
        }
 
        return ret;
 }
 
-static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
-                    char *const argv[])
+static int do_ut_all(struct unit_test_state *uts, struct cmd_tbl *cmdtp,
+                    int flag, int argc, char *const argv[])
 {
        int i;
        int retval;
@@ -203,7 +201,7 @@ static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
 
                if (has_tests(ste)) {
                        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)
                                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[])
 {
+       struct unit_test_state uts;
        struct suite *ste;
        const char *name;
        int ret;
@@ -274,9 +273,10 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        argc--;
        argv++;
 
+       ut_init_state(&uts);
        name = argv[0];
        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")) {
                ret = do_ut_info(cmdtp, flag, argc, argv);
        } else {
@@ -290,10 +290,11 @@ static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
                        return CMD_RET_FAILURE;
                }
 
-               ret = run_suite(ste, cmdtp, flag, argc, argv);
+               ret = run_suite(&uts, ste, cmdtp, flag, argc, argv);
        }
        if (ret)
                return ret;
+       ut_uninit_state(&uts);
 
        return 0;
 }
index e0483af..aefa147 100644 (file)
@@ -210,21 +210,17 @@ static int fdt_overlay_stacked(struct unit_test_state *uts)
 }
 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);
        const int n_ents = UNIT_TEST_SUITE_COUNT(overlay);
-       struct unit_test_state *uts;
        void *fdt_base = &__dtb_test_fdt_base_begin;
        void *fdt_overlay = &__dtbo_test_fdt_overlay_begin;
        void *fdt_overlay_stacked = &__dtbo_test_fdt_overlay_stacked_begin;
        void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
        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_overlay));
 
@@ -272,7 +268,7 @@ int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        /* Apply the stacked overlay */
        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);
 err3: