Add a -u flag for U-Boot SPL which requests that unit tests be run. To
make this work, export dm_test_main() and update it to skip test features
that are not used with of-platdata.

To run the tests:

   $ spl/u-boot-spl -u
   U-Boot SPL 2020.10-rc5 (Oct 01 2020 - 07:35:39 -0600)
   Running 0 driver model tests
   Failures: 0

At present there are no SPL unit tests.

Note that there is one wrinkle with these tests. SPL has limited memory
available for allocation. Also malloc_simple does not free memory
(free() is a nop) and running tests repeatedly causes driver-model to
reinit multiple times and allocate memory. Therefore it is not possible
to run more than a few tests at a time. One solution is to increase the
amount of malloc space in sandbox_spl. This is not a problem for pytest,
since it runs each test individually, so for now this is left as is.

Signed-off-by: Simon Glass <s...@chromium.org>
---

 arch/sandbox/cpu/spl.c           |  8 ++++++++
 arch/sandbox/cpu/start.c         |  9 +++++++++
 arch/sandbox/include/asm/state.h |  1 +
 include/test/test.h              | 11 +++++++++++
 test/dm/test-main.c              |  2 +-
 5 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index 7ab8919eb90..48fd1265afe 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -12,6 +12,7 @@
 #include <spl.h>
 #include <asm/spl.h>
 #include <asm/state.h>
+#include <test/test.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -67,6 +68,13 @@ void spl_board_init(void)
                     uclass_next_device(&dev))
                        ;
        }
+
+       if (state->run_unittests) {
+               int ret;
+
+               ret = dm_test_main(NULL);
+               /* continue execution into U-Boot */
+       }
 }
 
 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index c6a2bbe4689..f5e104b127b 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -374,6 +374,15 @@ static int sandbox_cmdline_cb_show_of_platdata(struct 
sandbox_state *state,
 }
 SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
 
+static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
+                                       const char *arg)
+{
+       state->run_unittests = true;
+
+       return 0;
+}
+SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
+
 static void setup_ram_buf(struct sandbox_state *state)
 {
        /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 1bfad305f1a..f828d9d2447 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -92,6 +92,7 @@ struct sandbox_state {
        int default_log_level;          /* Default log level for sandbox */
        bool show_of_platdata;          /* Show of-platdata in SPL */
        bool ram_buf_read;              /* true if we read the RAM buffer */
+       bool run_unittests;             /* Run unit tests */
 
        /* Pointer to information for each SPI bus/cs */
        struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]
diff --git a/include/test/test.h b/include/test/test.h
index 67c7d69d488..03e29290bf4 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -94,4 +94,15 @@ enum {
        TEST_DEVRES_SIZE3       = 37,
 };
 
+/**
+ * dm_test_main() - Run driver model tests
+ *
+ * Run all the available driver model tests, or a selection
+ *
+ * @test_name: Name of single test to run (e.g. "dm_test_fdt_pre_reloc" or just
+ *     "fdt_pre_reloc"), or NULL to run all
+ * @return 0 if all tests passed, 1 if not
+ */
+int dm_test_main(const char *test_name);
+
 #endif /* __TEST_TEST_H */
diff --git a/test/dm/test-main.c b/test/dm/test-main.c
index 5560572c7c6..9d22df8c4dc 100644
--- a/test/dm/test-main.c
+++ b/test/dm/test-main.c
@@ -127,7 +127,7 @@ static bool dm_test_run_on_flattree(struct unit_test *test)
        return !strstr(fname, "video") || strstr(test->name, "video_base");
 }
 
-static int dm_test_main(const char *test_name)
+int dm_test_main(const char *test_name)
 {
        struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
        const int n_ents = ll_entry_count(struct unit_test, dm_test);
-- 
2.28.0.806.g8561365e88-goog

Reply via email to