Skip to contentSkip to conversation

test: Refactor tests to have a single test runner

PATCH New Simon Glass 4 participants 47 messages Mar 8, 2021 – Mar 23, 2021

lore
Share & tools
lore
#0 lore raw

v5 cover test: Refactor tests to have a single test runner

At present U-Boot has two broad sets of tests in the C code: driver model tests which do a lot of pre-/post-init and command tests which do not.

This separation makes it slightly harder to write a test, since there are two different test-state structures and different rules for running the two different test types. At present these rules are determined by where the test is (actually its prefix).

All unit tests can be run from the command line with the 'ut' command. Since SPL does not have commands, it currently calls the test runner directly and offers no control of which tests are run.

This seems like a good time to refactor the tests into a unified test runner, allowing U-Boot proper and SPL to use the same path, perhaps with some different conditions along the way.

This series sets up a unified runner called ut_run_list(), which runs a set of tests from a linker_list. Driver model tests are distinguished by a new UT_TESTF_DM flag so that the necessary init and cleanup can still be done.

The runner is modified to support running SPL tests that are not solely for driver model. An example test for FIT loading is added as a demonstration.

In addition, some documentation is added to explain how to write tests.

This series is available at u-boot-dm/test-working

Changes in v5:
- Update the test so we don't need a flag
- Update based on Heinrich's comments
- Mention UEFI tests
- Update language to make it clear when sandbox is discussed
- Update table based on Heinrich's comments

Changes in v4:
- Fix 'of-pldata' typo
- Rebase on master
- Add correct SPDX header

Changes in v3:
- Add new patch to re-enable test_ofplatdata
- Reword the SPL tests section for clarity
- Use test_set_state() throughout test-main.c instead of direct assignment
- Update the pytest collector as well

Changes in v2:
- Use correct rst format for 'Ad-hoc tests' section
- Expand docs on how each type of test is marked
- Put the docs in tests_sandbox since it is more related to sandbox
- Put in a mention of tests_sandbox in the main testing docs
- Add a note that SPL tests can in fact be run individualy
- Document how to run all C tests with 'ut all'
- Fix 'get list' typo
- Fix conditions so non-DM SPL tests are actually run
- Allow for prefix to be NULL, to match function comment
- Add new patches to cover running an SPL test

Simon Glass (41):
dm: core: Fix allocation of empty of-platdata
doc: Tidy up testing section
doc: Document make tcheck
sandbox: Drop the 'starting...' message
test: Re-enable test_ofplatdata
doc: Explain how to run tests without pytest
doc: Document how sandbox_spl_tests are run
test: Correct setexpr test prefix
test: Mark all driver model tests with a flag
test: Rename test-main.c to test-dm.c
test: Add an overall test runner
test: Create pre/post-run functions
test: Call test_pre/post_run() from driver model tests
test: Move dm_extended_scan() to test_pre_run()
test: Move do_autoprobe() to test_pre_run()
test: Move dm_scan_plat() to test_pre_run()
test: Drop mallinfo() work-around
test: Move console silencing to test_pre_run()
test: Move delay skipping to test_pre_run()
test: Handle driver model reinit in test_pre_run()
test: Drop struct dm_test_state
test: Move dm_test_init() into test-main.c
test: Move dm_test_destroy() into test-main.c
test: Move test running into a separate function
test: Use ut_run_test() to run driver model tests
test: Drop dm_do_test()
test: Add ut_run_test_live_flat() to run tests twice
test: Use a local variable for test state
test: Run driver-model tests using ut_run_list()
test: Use return values in dm_test_run()
test: Move the devicetree check into ut_run_list()
test: Move restoring of driver model state to ut_run_list()
test: log: Rename log main test file to log_ut.c
test: Add a macros for finding tests in linker_lists
test: Rename all linker lists to have a ut_ prefix
test: Allow SPL to run any available test
sandbox: Update os_find_u_boot() to find the .img file
spl: Convert spl_fit to work with sandbox
spl: test: Add a test for spl_load_simple_fit()
test: sandbox: Move sandbox test docs into doc/develop
doc: Explain briefly how to write new tests

 arch/sandbox/cpu/os.c              |   8 +-
 arch/sandbox/cpu/spl.c             |   9 +-
 arch/sandbox/cpu/start.c           |   1 -
 common/spl/spl.c                   |   3 +-
 common/spl/spl_fit.c               |  27 +-
 configs/sandbox_spl_defconfig      |   2 +-
 doc/arch/sandbox.rst               |  48 +---
 doc/develop/index.rst              |   2 +
 doc/develop/py_testing.rst         |   3 +-
 doc/develop/testing.rst            |  46 +++-
 doc/develop/tests_sandbox.rst      | 209 ++++++++++++++
 doc/develop/tests_writing.rst      | 346 +++++++++++++++++++++++
 drivers/core/device.c              |  17 +-
 include/dm/test.h                  |  20 +-
 include/os.h                       |   3 +-
 include/spl.h                      |   9 +
 include/test/test.h                |  38 ++-
 include/test/ut.h                  |  45 +++
 test/Makefile                      |   3 +
 test/bloblist.c                    |   5 +-
 test/bootm.c                       |   4 +-
 test/cmd/mem.c                     |   4 +-
 test/cmd/setexpr.c                 |  28 +-
 test/cmd_ut.c                      |  38 +--
 test/compression.c                 |   5 +-
 test/dm/Makefile                   |   2 +-
 test/dm/core.c                     |  58 ++--
 test/dm/test-dm.c                  |  51 ++++
 test/dm/test-driver.c              |  12 +-
 test/dm/test-main.c                | 230 ----------------
 test/dm/test-uclass.c              |  10 +-
 test/env/cmd_ut_env.c              |   4 +-
 test/image/Makefile                |   5 +
 test/image/spl_load.c              |  91 ++++++
 test/lib/cmd_ut_lib.c              |   4 +-
 test/log/Makefile                  |   2 +-
 test/log/{test-main.c => log_ut.c} |   4 +-
 test/optee/cmd_ut_optee.c          |   5 +-
 test/overlay/cmd_ut_overlay.c      |   5 +-
 test/py/conftest.py                |   2 +-
 test/py/tests/test_log.py          |   1 -
 test/py/tests/test_ofplatdata.py   |   2 +-
 test/str_ut.c                      |   5 +-
 test/test-main.c                   | 426 +++++++++++++++++++++++++++++
 test/unicode_ut.c                  |   4 +-
 test/ut.c                          |   7 +
 46 files changed, 1407 insertions(+), 446 deletions(-)
 create mode 100644 doc/develop/tests_sandbox.rst
 create mode 100644 doc/develop/tests_writing.rst
 create mode 100644 test/dm/test-dm.c
 delete mode 100644 test/dm/test-main.c
 create mode 100644 test/image/Makefile
 create mode 100644 test/image/spl_load.c
 rename test/log/{test-main.c => log_ut.c} (75%)
 create mode 100644 test/test-main.c
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#1 lore raw

PATCH v5 1/41 dm: core: Fix allocation of empty of-platdata

With of-platdata we always have a dtv struct that holds the platform data provided by the driver_info record. However, this struct can be empty if there are no actual devicetree properties provided.

The upshot of empty platform data is that it will end up as a zero-size member in the BSS section, which is fine. But if the driver specifies plat_auto then it expects the correct amount of space to be allocated.

At present this does not happen, since device_bind() assumes that the platform-data size will always be >0. As a result we end up not allocating the space and just use the BSS region, overwriting whatever other contents are present.

Fix this by removing the condition that platform data be non-empty, always allocating space if requested.

This fixes a strange bug that has been lurking since of-platdata was implemented. It has likely never been noticed since devices normally have at least some devicetree properties, BSS is seldom used on SPL, the dtv structs are normally at the end of bss and the overwriting only happens if a driver changes its platform data.

It was discovered using sandbox_spl, which exercises more features than a normal board might, and the critical global_data variable 'gd' happened to be at the end of BSS.

Fixes 9fa28190091 ("dm: core: Expand platdata for of-platdata devices")

---

(no changes since v1)

 drivers/core/device.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/core/device.c +13 -4
index 625134921d6..d1098a3861a 100644
@@ -92,15 +92,19 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
 	if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
 		dev->seq_ = uclass_find_next_free_seq(uc);
 
+	/* Check if we need to allocate plat */
 	if (drv->plat_auto) {
 		bool alloc = !plat;
 
+		/*
+		 * For of-platdata, we try use the existing data, but if
+		 * plat_auto is larger, we must allocate a new space
+		 */
 		if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
-			if (of_plat_size) {
+			if (of_plat_size)
 				dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
-				if (of_plat_size < drv->plat_auto)
-					alloc = true;
-			}
+			if (of_plat_size < drv->plat_auto)
+				alloc = true;
 		}
 		if (alloc) {
 			dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
@@ -109,6 +113,11 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
 				ret = -ENOMEM;
 				goto fail_alloc1;
 			}
+
+			/*
+			 * For of-platdata, copy the old plat into the new
+			 * space
+			 */
 			if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
 				memcpy(ptr, plat, of_plat_size);
 			dev_set_plat(dev, ptr);
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#2 lore raw

PATCH v5 2/41 doc: Tidy up testing section

In reply to Simon Glass

Tweak this so the output looks a little better.

---

(no changes since v2)

Changes in v2:
- Use correct rst format for 'Ad-hoc tests' section

 doc/develop/testing.rst | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
doc/develop/testing.rst +12 -8
index 4bc9ca3a6ae..bc74eb53e35 100644
@@ -8,14 +8,14 @@ tested and what tests you should write when adding a new feature.
 Running tests
 -------------
 
-To run most tests on sandbox, type this:
+To run most tests on sandbox, type this::
 
     make check
 
 in the U-Boot directory. Note that only the pytest suite is run using this
 command.
 
-Some tests take ages to run. To run just the quick ones, type this:
+Some tests take ages to run. To run just the quick ones, type this::
 
     make qcheck
 
@@ -35,9 +35,9 @@ either on sandbox or on real hardware. It relies on the U-Boot console to
 inject test commands and check the result. It is slower to run than C code,
 but provides the ability to unify lots of tests and summarise their results.
 
-You can run the tests on sandbox with:
+You can run the tests on sandbox with::
 
-	./test/py/test.py --bd sandbox --build
+   ./test/py/test.py --bd sandbox --build
 
 This will produce HTML output in build-sandbox/test-log.html
 
@@ -58,10 +58,14 @@ Ad-hoc tests
 
 There are several ad-hoc tests which run outside the pytest environment:
 
-   test/fs	- File system test (shell script)
-   test/image	- FIT and legacy image tests (shell script and Python)
-   test/stdint	- A test that stdint.h can be used in U-Boot (shell script)
-   trace	- Test for the tracing feature (shell script)
+test/fs
+   File system test (shell script)
+test/image
+   FIT and legacy image tests (shell script and Python)
+test/stdint
+   A test that stdint.h can be used in U-Boot (shell script)
+trace
+   Test for the tracing feature (shell script)
 
 TODO: Move these into pytest.
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#3 lore raw

PATCH v5 3/41 doc: Document make tcheck

In reply to Simon Glass

Add a comment about this option in the documentation. Also mention the script that runs these combinations.

---

(no changes since v2)

Changes in v2:
- Expand docs on how each type of test is marked

 doc/develop/testing.rst | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
doc/develop/testing.rst +11 -1
index bc74eb53e35..f01ca4dc408 100644
@@ -15,10 +15,20 @@ To run most tests on sandbox, type this::
 in the U-Boot directory. Note that only the pytest suite is run using this
 command.
 
-Some tests take ages to run. To run just the quick ones, type this::
+Some tests take ages to run and are marked with @pytest.mark.slow. To run just
+the quick ones, type this::
 
     make qcheck
 
+It is also possible to run just the tests for tools (patman, binman, etc.).
+Such tests are included with those tools, i.e. no actual U-Boot unit tests are
+run. Type this::
+
+    make tcheck
+
+All of the above use the test/run script with a paremeter to select which tests
+are run.
+
 
 Sandbox
 -------
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#4 lore raw

PATCH v5 3/41 doc: Document make tcheck

Am 8. M?rz 2021 01:34:39 MEZ schrieb Simon Glass <[email protected]>:

Quoted 23 lines

Add a comment about this option in the documentation. Also mention the script that runs these combinations.

---

(no changes since v2)

Changes in v2:
- Expand docs on how each type of test is marked

doc/develop/testing.rst | 12 +++++++++++-

1 file changed, 11 insertions(+), 1 deletion(-)
doc/develop/testing.rst +0 -0
index bc74eb53e35..f01ca4dc408 100644
@@ -15,10 +15,20 @@ To run most tests on sandbox, type this::

in the U-Boot directory. Note that only the pytest suite is run using this
command.

Quoted 1 line

-Some tests take ages to run. To run just the quick ones, type this::

"Ages" is a bit of an exaggeration when referring to a few seconds.

Quoted 21 lines

+Some tests take ages to run and are marked with @pytest.mark.slow. To run just

doc/develop/testing.rst +2 -0
+the quick ones, type this::
 
    make qcheck
 
+It is also possible to run just the tests for tools (patman, binman,

etc.).
+Such tests are included with those tools, i.e. no actual U-Boot unit tests are

doc/develop/testing.rst +5 -0
+run. Type this::
+
+    make tcheck
+
+All of the above use the test/run script with a paremeter to select

which tests

doc/develop/testing.rst +2 -0
+are run.
+

Sandbox
-------

#5 lore raw

PATCH v5 3/41 doc: Document make tcheck

Hi Heinrich,

Quoted 25 lines · Heinrich Schuchardt

Am 8. M?rz 2021 01:34:39 MEZ schrieb Simon Glass <[email protected]>:

Quoted 23 lines

Add a comment about this option in the documentation. Also mention the script that runs these combinations.

---

(no changes since v2)

Changes in v2:
- Expand docs on how each type of test is marked

doc/develop/testing.rst | 12 +++++++++++-

1 file changed, 11 insertions(+), 1 deletion(-)
doc/develop/testing.rst +0 -0
index bc74eb53e35..f01ca4dc408 100644
@@ -15,10 +15,20 @@ To run most tests on sandbox, type this::

in the U-Boot directory. Note that only the pytest suite is run using this
command.

Quoted 3 lines
Quoted 1 line

-Some tests take ages to run. To run just the quick ones, type this::

"Ages" is a bit of an exaggeration when referring to a few seconds.

It is actually about 53 seconds extra for me all up, which really does feel like ages! A slow test in my book is something that takes a noticeable time, since with close to 1000 tests, it all adds up.

Quoted 2 lines

Regards,
Simon

#6 lore raw

PATCH v5 4/41 sandbox: Drop the 'starting...' message

In reply to Simon Glass

This message is annoying since it is only useful for testing. Drop it and update the test to cope.

---

Changes in v5:
- Update the test so we don't need a flag

 arch/sandbox/cpu/start.c  | 1 -
 test/py/tests/test_log.py | 1 -
 2 files changed, 2 deletions(-)
arch/sandbox/cpu/start.c +0 -1
index c4c4128d465..e87365e800d 100644
@@ -491,7 +491,6 @@ int main(int argc, char *argv[])
 	gd->reloc_off = (ulong)gd->arch.text_base;
 
 	/* sandbox test: log functions called before log_init in board_init_f */
-	log_info("sandbox: starting...\n");
 	log_debug("debug: %s\n", __func__);
 
 	/* Do pre- and post-relocation init */
test/py/tests/test_log.py +0 -1
index f889120f2b3..140dcb9aa2b 100644
@@ -45,5 +45,4 @@ def test_log_dropped(u_boot_console):
     cons = u_boot_console
     cons.restart_uboot()
     output = cons.get_spawn_output().replace('\r', '')
-    assert 'sandbox: starting...' in output
     assert (not 'debug: main' in output)
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#7 lore raw

PATCH v5 5/41 test: Re-enable test_ofplatdata

In reply to Simon Glass

This was inadvertently disabled after a recent change. Re-enable it.

---

(no changes since v3)

Changes in v3:
- Add new patch to re-enable test_ofplatdata

 test/py/tests/test_ofplatdata.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
test/py/tests/test_ofplatdata.py +1 -1
index 92d09b7aa19..e9cce4daf48 100644
@@ -4,7 +4,7 @@
 import pytest
 import u_boot_utils as util
 
- at pytest.mark.boardspec('sandbox')
+ at pytest.mark.boardspec('sandbox_spl')
 @pytest.mark.buildconfigspec('spl_of_platdata')
 def test_spl_devicetree(u_boot_console):
     """Test content of spl device-tree"""
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#8 lore raw

PATCH v5 6/41 doc: Explain how to run tests without pytest

In reply to Simon Glass

Add details about how to run a sandbox test directly, without using pytest. This is more convenient for rapid development, since it is faster and allows easier use of a debugger. Also mention sandbox_flattree as an example of the different sandbox builds available.

---

Changes in v5:
- Update based on Heinrich's comments

Changes in v2:
- Put the docs in tests_sandbox since it is more related to sandbox
- Put in a mention of tests_sandbox in the main testing docs

 doc/develop/index.rst         |  1 +
 doc/develop/testing.rst       |  9 ++++
 doc/develop/tests_sandbox.rst | 80 +++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 doc/develop/tests_sandbox.rst
doc/develop/index.rst +1 -0
index ac57fdb8f30..50b1de3bdff 100644
@@ -33,3 +33,4 @@ Testing
    coccinelle
    testing
    py_testing
+   tests_sandbox
doc/develop/testing.rst +9 -0
index f01ca4dc408..87c90eee271 100644
@@ -36,6 +36,7 @@ U-Boot can be built as a user-space application (e.g. for Linux). This
 allows test to be executed without needing target hardware. The 'sandbox'
 target provides this feature and it is widely used in tests.
 
+See :doc:`tests_sandbox` for more information.
 
 Pytest Suite
 ------------
@@ -51,8 +52,16 @@ You can run the tests on sandbox with::
 
 This will produce HTML output in build-sandbox/test-log.html
 
+Some tests run with other versions of sandbox. For example sandbox_flattree
+runs the tests with livetree (the hierachical devicetree) disabled. You can
+also select particular tests with -k::
+
+   ./test/py/test.py --bd sandbox_flattree --build -k hello
+
 See test/py/README.md for more information about the pytest suite.
 
+See :doc:`tests_sandbox` for how to run tests directly (not through pytest).
+
 
 tbot
 ----
doc/develop/tests_sandbox.rst new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#9 lore raw

PATCH v5 7/41 doc: Document how sandbox_spl_tests are run

In reply to Simon Glass

Add a few notes about the sandbox_spl tests, since they are special.

---

(no changes since v4)

Changes in v4:
- Fix 'of-pldata' typo

Changes in v3:
- Reword the SPL tests section for clarity

Changes in v2:
- Add a note that SPL tests can in fact be run individualy
- Document how to run all C tests with 'ut all'
- Fix 'get list' typo

 doc/develop/testing.rst       |  5 +++
 doc/develop/tests_sandbox.rst | 82 +++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
doc/develop/testing.rst +5 -0
index 87c90eee271..b181c2e2e41 100644
@@ -58,6 +58,11 @@ also select particular tests with -k::
 
    ./test/py/test.py --bd sandbox_flattree --build -k hello
 
+There are some special tests that run in SPL. For this you need the sandbox_spl
+build::
+
+   ./test/py/test.py --bd sandbox_spl --build -k test_spl
+
 See test/py/README.md for more information about the pytest suite.
 
 See :doc:`tests_sandbox` for how to run tests directly (not through pytest).
doc/develop/tests_sandbox.rst +82 -0

85 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#10 lore raw

PATCH v5 8/41 test: Correct setexpr test prefix

In reply to Simon Glass

This prefix should be for setexpr, not mem. This means that trying to select just these tests to run does not work. Fix it.

For some reason this provokes an assertion failure due to memory not being freed. Move the env_set() in setexpr_test_str() to before the malloc() heap size size is recorded and disable the rest in setexpr_test_str_oper().

---

(no changes since v1)

 test/cmd/setexpr.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
test/cmd/setexpr.c +19 -4
index fd6d869c0ed..b483069ff0f 100644
@@ -306,8 +306,8 @@ static int setexpr_test_str(struct unit_test_state *uts)
 	ut_asserteq(1, run_command("setexpr.s fred 0", 0));
 	ut_assertok(ut_check_delta(start_mem));
 
-	start_mem = ut_check_free();
 	ut_assertok(env_set("fred", "12345"));
+	start_mem = ut_check_free();
 	ut_assertok(run_command("setexpr.s fred *0", 0));
 	ut_asserteq_str("hello", env_get("fred"));
 	ut_assertok(ut_check_delta(start_mem));
@@ -345,7 +345,22 @@ static int setexpr_test_str_oper(struct unit_test_state *uts)
 	start_mem = ut_check_free();
 	ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
 	ut_asserteq_str("hello there", env_get("fred"));
-	ut_assertok(ut_check_delta(start_mem));
+
+	/*
+	 * This check does not work with sandbox_flattree, apparently due to
+	 * memory allocations in env_set().
+	 *
+	 * The truetype console produces lots of memory allocations even though
+	 * the LCD display is not visible. But even without these, it does not
+	 * work.
+	 *
+	 * A better test would be for dlmalloc to record the allocs and frees
+	 * for a particular caller, but that is not supported.
+	 *
+	 * For now, drop this test.
+	 *
+	 * ut_assertok(ut_check_delta(start_mem));
+	 */
 
 	unmap_sysmem(buf);
 
@@ -379,6 +394,6 @@ int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 						 setexpr_test);
 	const int n_ents = ll_entry_count(struct unit_test, setexpr_test);
 
-	return cmd_ut_category("cmd_setexpr", "cmd_mem_", tests, n_ents, argc,
-			       argv);
+	return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
+			       argc, argv);
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#11 lore raw

PATCH v5 9/41 test: Mark all driver model tests with a flag

In reply to Simon Glass

Add a flag for driver model tests, so we can do special processing for them.

---

(no changes since v1)

 include/dm/test.h   | 3 ++-
 include/test/test.h | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)
include/dm/test.h +2 -1
index 6ac6672cd6f..dfbc82c756d 100644
@@ -143,7 +143,8 @@ struct dm_test_state {
 };
 
 /* Declare a new driver model test */
-#define DM_TEST(_name, _flags)	UNIT_TEST(_name, _flags, dm_test)
+#define DM_TEST(_name, _flags) \
+	UNIT_TEST(_name, UT_TESTF_DM | (_flags), dm_test)
 
 /*
  * struct sandbox_sdl_plat - Platform data for the SDL video driver
include/test/test.h +2 -0
index 3fdaa2b5e51..27585507d8c 100644
@@ -36,6 +36,8 @@ enum {
 	UT_TESTF_FLAT_TREE	= BIT(3),	/* test needs flat DT */
 	UT_TESTF_LIVE_TREE	= BIT(4),	/* needs live device tree */
 	UT_TESTF_CONSOLE_REC	= BIT(5),	/* needs console recording */
+	/* do extra driver model init and uninit */
+	UT_TESTF_DM		= BIT(6),
 };
 
 /**
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#12 lore raw

PATCH v5 10/41 test: Rename test-main.c to test-dm.c

In reply to Simon Glass

This is the main test function for driver model but not for other tests. Rename the file and the function so this is clear.

---

(no changes since v1)

 arch/sandbox/cpu/spl.c             | 2 +-
 include/test/test.h                | 4 ++--
 test/dm/Makefile                   | 2 +-
 test/dm/{test-main.c => test-dm.c} | 4 ++--
 4 files changed, 6 insertions(+), 6 deletions(-)
 rename test/dm/{test-main.c => test-dm.c} (98%)
arch/sandbox/cpu/spl.c +1 -1
index 6926e244ca2..3779d58c3fe 100644
@@ -65,7 +65,7 @@ void spl_board_init(void)
 	if (state->run_unittests) {
 		int ret;
 
-		ret = dm_test_main(state->select_unittests);
+		ret = dm_test_run(state->select_unittests);
 		/* continue execution into U-Boot */
 	}
 }
include/test/test.h +2 -2
index 27585507d8c..d282cb2362d 100644
@@ -106,7 +106,7 @@ enum {
 struct udevice *testbus_get_clear_removed(void);
 
 /**
- * dm_test_main() - Run driver model tests
+ * dm_test_run() - Run driver model tests
  *
  * Run all the available driver model tests, or a selection
  *
@@ -114,6 +114,6 @@ struct udevice *testbus_get_clear_removed(void);
  *	"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);
+int dm_test_run(const char *test_name);
 
 #endif /* __TEST_TEST_H */
test/dm/Makefile +1 -1
index fd1455109d4..f5cc5540e8a 100644
@@ -2,7 +2,7 @@
 #
 # Copyright (c) 2013 Google, Inc
 
-obj-$(CONFIG_UT_DM) += test-main.o
+obj-$(CONFIG_UT_DM) += test-dm.o
 
 # Tests for particular subsystems - when enabling driver model for a new
 # subsystem you must add sandbox tests here.
test/dm/test-dm.c renamed from test/dm/test-main.c +2 -2
similarity index 98%
rename from test/dm/test-main.c
rename to test/dm/test-dm.c
index 560f8d63ec6..71e9cf6e5da 100644
@@ -146,7 +146,7 @@ static bool test_matches(const char *test_name, const char *find_name)
 	return false;
 }
 
-int dm_test_main(const char *test_name)
+int dm_test_run(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);
@@ -226,5 +226,5 @@ int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	if (argc > 1)
 		test_name = argv[1];
 
-	return dm_test_main(test_name);
+	return dm_test_run(test_name);
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#13 lore raw

PATCH v5 11/41 test: Add an overall test runner

In reply to Simon Glass

Add a new test runner that will eventually be able to run any test. For now, have it run the 'command' unit tests, so that the functionality in cmd_ut_category() moves into it.

---

(no changes since v1)

 include/test/ut.h | 42 ++++++++++++++++++++++++++++++
 test/Makefile     |  2 ++
 test/cmd_ut.c     | 38 ++++-----------------------
 test/test-main.c  | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 115 insertions(+), 33 deletions(-)
 create mode 100644 test/test-main.c
include/test/ut.h +42 -0
index 17400c73ea9..88e75ab791c 100644
@@ -356,4 +356,46 @@ void ut_silence_console(struct unit_test_state *uts);
  */
 void ut_unsilence_console(struct unit_test_state *uts);
 
+/**
+ * ut_run_tests() - Run a set of tests
+ *
+ * This runs the tests, handling any preparation and clean-up needed. It prints
+ * the name of each test before running it.
+ *
+ * @uts: Test state to update. The caller should ensure that this is zeroed for
+ *	the first call to this function. On exit, @uts->fail_count is
+ *	incremented by the number of failures (0, one hopes)
+ * @prefix: String prefix for the tests. Any tests that have this prefix will be
+ *	printed without the prefix, so that it is easier to see the unique part
+ *	of the test name. If NULL, no prefix processing is done
+ * @tests: List of tests to run
+ * @count: Number of tests to run
+ * @select_name: Name of a single test to run (from the list provided). If NULL
+ *	then all tests are run
+ * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
+ *	-EBADF if any failed
+ */
+int ut_run_tests(struct unit_test_state *uts, const char *prefix,
+		 struct unit_test *tests, int count, const char *select_name);
+
+/**
+ * ut_run_tests() - Run a set of tests
+ *
+ * This runs the test, handling any preparation and clean-up needed. It prints
+ * the name of each test before running it.
+ *
+ * @category: Category of these tests. This is a string printed at the start to
+ *	announce the the number of tests
+ * @prefix: String prefix for the tests. Any tests that have this prefix will be
+ *	printed without the prefix, so that it is easier to see the unique part
+ *	of the test name. If NULL, no prefix processing is done
+ * @tests: List of tests to run
+ * @count: Number of tests to run
+ * @select_name: Name of a single test to run (from the list provided). If NULL
+ *	then all tests are run
+ * @return 0 if all tests passed, -1 if any failed
+ */
+int ut_run_list(const char *name, const char *prefix, struct unit_test *tests,
+		int count, const char *select_name);
+
 #endif
test/Makefile +2 -0
index 932e5173831..5cd284e322e 100644
@@ -2,6 +2,8 @@
 #
 # (C) Copyright 2012 The Chromium Authors
 
+obj-y += test-main.o
+
 ifneq ($(CONFIG_$(SPL_)BLOBLIST),)
 obj-$(CONFIG_$(SPL_)CMDLINE) += bloblist.o
 obj-$(CONFIG_$(SPL_)CMDLINE) += bootm.o
test/cmd_ut.c +5 -33
index 8f3089890ea..157f6aa9767 100644
@@ -9,6 +9,7 @@
 #include <console.h>
 #include <test/suites.h>
 #include <test/test.h>
+#include <test/ut.h>
 
 static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
 		     char *const argv[]);
@@ -17,41 +18,12 @@ int cmd_ut_category(const char *name, const char *prefix,
 		    struct unit_test *tests, int n_ents,
 		    int argc, char *const argv[])
 {
-	struct unit_test_state uts = { .fail_count = 0 };
-	struct unit_test *test;
-	int prefix_len = prefix ? strlen(prefix) : 0;
+	int ret;
 
-	if (argc == 1)
-		printf("Running %d %s tests\n", n_ents, name);
+	ret = ut_run_list(name, prefix, tests, n_ents,
+			  argc > 1 ? argv[1] : NULL);
 
-	for (test = tests; test < tests + n_ents; test++) {
-		const char *test_name = test->name;
-
-		/* Remove the prefix */
-		if (prefix && !strncmp(test_name, prefix, prefix_len))
-			test_name += prefix_len;
-
-		if (argc > 1 && strcmp(argv[1], test_name))
-			continue;
-		printf("Test: %s\n", test->name);
-
-		if (test->flags & UT_TESTF_CONSOLE_REC) {
-			int ret = console_record_reset_enable();
-
-			if (ret) {
-				printf("Skipping: Console recording disabled\n");
-				continue;
-			}
-		}
-
-		uts.start = mallinfo();
-
-		test->func(&uts);
-	}
-
-	printf("Failures: %d\n", uts.fail_count);
-
-	return uts.fail_count ? CMD_RET_FAILURE : 0;
+	return ret ? CMD_RET_FAILURE : 0;
 }
 
 static struct cmd_tbl cmd_ut_sub[] = {
test/test-main.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#14 lore raw

PATCH v5 12/41 test: Create pre/post-run functions

In reply to Simon Glass

Split out the test preparation into a separation function before expanding it. Add a post-run function as well, currently empty.

---

(no changes since v1)

 include/test/ut.h | 20 ++++++++++++++++++++
 test/test-main.c  | 41 +++++++++++++++++++++++++++++++----------
 2 files changed, 51 insertions(+), 10 deletions(-)
include/test/ut.h +20 -0
index 88e75ab791c..7cb5e10f3af 100644
@@ -356,6 +356,26 @@ void ut_silence_console(struct unit_test_state *uts);
  */
 void ut_unsilence_console(struct unit_test_state *uts);
 
+/**
+ * test_pre_run() - Handle any preparation needed to run a test
+ *
+ * @uts: Test state
+ * @test: Test to prepare for
+ * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
+ *	available, other -ve on error (meaning that testing cannot likely
+ *	continue)
+ */
+int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
+
+/**
+ * test_post_run() - Handle cleaning up after a test
+ *
+ * @uts: Test state
+ * @test: Test to clean up after
+ * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
+ */
+int test_post_run(struct unit_test_state *uts, struct unit_test *test);
+
 /**
  * ut_run_tests() - Run a set of tests
  *
test/test-main.c +31 -10

61 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#15 lore raw

PATCH v5 13/41 test: Call test_pre/post_run() from driver model tests

In reply to Simon Glass

Ultimately we want to get rid of the special driver model test init and use test_pre_run() and test_post_run() for all tests. As a first step, use those function to handle console recording.

For now we need a special case for setting uts->start, but that wil go away once all init is in one place.

---

(no changes since v1)

 include/dm/test.h |  2 +-
 test/dm/test-dm.c | 10 +++++-----
 test/test-main.c  |  8 +++++++-
 3 files changed, 13 insertions(+), 7 deletions(-)
include/dm/test.h +1 -1
index dfbc82c756d..c0b463cc0f1 100644
@@ -144,7 +144,7 @@ struct dm_test_state {
 
 /* Declare a new driver model test */
 #define DM_TEST(_name, _flags) \
-	UNIT_TEST(_name, UT_TESTF_DM | (_flags), dm_test)
+	UNIT_TEST(_name, UT_TESTF_DM | UT_TESTF_CONSOLE_REC | (_flags), dm_test)
 
 /*
  * struct sandbox_sdl_plat - Platform data for the SDL video driver
test/dm/test-dm.c +5 -5
index 71e9cf6e5da..69a0349d04c 100644
@@ -97,14 +97,14 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 	    (test->flags & UT_TESTF_SCAN_FDT))
 		ut_assertok(dm_extended_scan(false));
 
-	/*
-	 * Silence the console and rely on console recording to get
-	 * our output.
-	 */
-	console_record_reset_enable();
+	ut_assertok(test_pre_run(uts, test));
+
 	if (!state->show_test_output)
 		gd->flags |= GD_FLG_SILENT;
 	test->func(uts);
+
+	ut_assertok(test_post_run(uts, test));
+
 	gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
 	state_set_skip_delays(false);
 
test/test-main.c +7 -1
index 7961fd8aa3e..9c600094740 100644
@@ -8,9 +8,13 @@
 #include <console.h>
 #include <test/test.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
-	uts->start = mallinfo();
+	/* DM tests have already done this */
+	if (!(test->flags & UT_TESTF_DM))
+		uts->start = mallinfo();
 
 	if (test->flags & UT_TESTF_CONSOLE_REC) {
 		int ret = console_record_reset_enable();
@@ -26,6 +30,8 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 
 int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 {
+	gd->flags &= ~GD_FLG_RECORD;
+
 	return 0;
 }
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#16 lore raw

PATCH v5 14/41 test: Move dm_extended_scan() to test_pre_run()

In reply to Simon Glass

Move this step over to the pre-run function.

---

(no changes since v1)

 test/dm/test-dm.c | 3 ---
 test/test-main.c  | 7 +++++++
 2 files changed, 7 insertions(+), 3 deletions(-)
test/dm/test-dm.c +0 -3
index 69a0349d04c..4cb0da13b7c 100644
@@ -93,9 +93,6 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 		ut_assertok(dm_scan_plat(false));
 	if (test->flags & UT_TESTF_PROBE_TEST)
 		ut_assertok(do_autoprobe(uts));
-	if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
-	    (test->flags & UT_TESTF_SCAN_FDT))
-		ut_assertok(dm_extended_scan(false));
 
 	ut_assertok(test_pre_run(uts, test));
 
test/test-main.c +7 -0
index 9c600094740..a971fe0e9c8 100644
@@ -6,7 +6,10 @@
 
 #include <common.h>
 #include <console.h>
+#include <dm.h>
+#include <dm/root.h>
 #include <test/test.h>
+#include <test/ut.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -16,6 +19,10 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 	if (!(test->flags & UT_TESTF_DM))
 		uts->start = mallinfo();
 
+	if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
+	    (test->flags & UT_TESTF_SCAN_FDT))
+		ut_assertok(dm_extended_scan(false));
+
 	if (test->flags & UT_TESTF_CONSOLE_REC) {
 		int ret = console_record_reset_enable();
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#17 lore raw

PATCH v5 15/41 test: Move do_autoprobe() to test_pre_run()

In reply to Simon Glass

Move this step over to the pre-run function.

---

(no changes since v1)

 test/dm/test-dm.c | 17 -----------------
 test/test-main.c  | 18 ++++++++++++++++++
 2 files changed, 18 insertions(+), 17 deletions(-)
test/dm/test-dm.c +0 -17
index 4cb0da13b7c..c2e1a1b9207 100644
@@ -43,21 +43,6 @@ static int dm_test_init(struct unit_test_state *uts, bool of_live)
 	return 0;
 }
 
-/* Ensure all the test devices are probed */
-static int do_autoprobe(struct unit_test_state *uts)
-{
-	struct udevice *dev;
-	int ret;
-
-	/* Scanning the uclass is enough to probe all the devices */
-	for (ret = uclass_first_device(UCLASS_TEST, &dev);
-	     dev;
-	     ret = uclass_next_device(&dev))
-		;
-
-	return ret;
-}
-
 static int dm_test_destroy(struct unit_test_state *uts)
 {
 	int id;
@@ -91,8 +76,6 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 	uts->start = mallinfo();
 	if (test->flags & UT_TESTF_SCAN_PDATA)
 		ut_assertok(dm_scan_plat(false));
-	if (test->flags & UT_TESTF_PROBE_TEST)
-		ut_assertok(do_autoprobe(uts));
 
 	ut_assertok(test_pre_run(uts, test));
 
test/test-main.c +18 -0
index a971fe0e9c8..bd2f08a2b42 100644
@@ -13,12 +13,30 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/* Ensure all the test devices are probed */
+static int do_autoprobe(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+	int ret;
+
+	/* Scanning the uclass is enough to probe all the devices */
+	for (ret = uclass_first_device(UCLASS_TEST, &dev);
+	     dev;
+	     ret = uclass_next_device(&dev))
+		;
+
+	return ret;
+}
+
 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
 	/* DM tests have already done this */
 	if (!(test->flags & UT_TESTF_DM))
 		uts->start = mallinfo();
 
+	if (test->flags & UT_TESTF_PROBE_TEST)
+		ut_assertok(do_autoprobe(uts));
+
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
 	    (test->flags & UT_TESTF_SCAN_FDT))
 		ut_assertok(dm_extended_scan(false));
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#18 lore raw

PATCH v5 16/41 test: Move dm_scan_plat() to test_pre_run()

In reply to Simon Glass

Move this step over to the pre-run function.

---

(no changes since v1)

 test/dm/test-dm.c | 2 --
 test/test-main.c  | 3 +++
 2 files changed, 3 insertions(+), 2 deletions(-)
test/dm/test-dm.c +0 -2
index c2e1a1b9207..18877c7ae56 100644
@@ -74,8 +74,6 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 	ut_assertok(dm_test_init(uts, of_live));
 
 	uts->start = mallinfo();
-	if (test->flags & UT_TESTF_SCAN_PDATA)
-		ut_assertok(dm_scan_plat(false));
 
 	ut_assertok(test_pre_run(uts, test));
 
test/test-main.c +3 -0
index bd2f08a2b42..fe96d739dc4 100644
@@ -34,6 +34,9 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 	if (!(test->flags & UT_TESTF_DM))
 		uts->start = mallinfo();
 
+	if (test->flags & UT_TESTF_SCAN_PDATA)
+		ut_assertok(dm_scan_plat(false));
+
 	if (test->flags & UT_TESTF_PROBE_TEST)
 		ut_assertok(do_autoprobe(uts));
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#19 lore raw

PATCH v5 17/41 test: Drop mallinfo() work-around

In reply to Simon Glass

This is not needed now. Drop it.

---

(no changes since v1)

 test/dm/test-dm.c | 2 --
 test/test-main.c  | 4 +---
 2 files changed, 1 insertion(+), 5 deletions(-)
test/dm/test-dm.c +0 -2
index 18877c7ae56..d1d83e34782 100644
@@ -73,8 +73,6 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 	       !of_live ? " (flat tree)" : "");
 	ut_assertok(dm_test_init(uts, of_live));
 
-	uts->start = mallinfo();
-
 	ut_assertok(test_pre_run(uts, test));
 
 	if (!state->show_test_output)
test/test-main.c +1 -3
index fe96d739dc4..db0d82e36c3 100644
@@ -30,9 +30,7 @@ static int do_autoprobe(struct unit_test_state *uts)
 
 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
-	/* DM tests have already done this */
-	if (!(test->flags & UT_TESTF_DM))
-		uts->start = mallinfo();
+	uts->start = mallinfo();
 
 	if (test->flags & UT_TESTF_SCAN_PDATA)
 		ut_assertok(dm_scan_plat(false));
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#20 lore raw

PATCH v5 18/41 test: Move console silencing to test_pre_run()

In reply to Simon Glass

We already have a function for silencing the console during tests. Use this from test_pre_run() and drop this code from the driver model tests.

---

(no changes since v1)

 test/dm/test-dm.c | 4 ----
 test/test-main.c  | 3 ++-
 2 files changed, 2 insertions(+), 5 deletions(-)
test/dm/test-dm.c +0 -4
index d1d83e34782..fdd35f663e4 100644
@@ -66,7 +66,6 @@ static int dm_test_destroy(struct unit_test_state *uts)
 static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 		      bool of_live)
 {
-	struct sandbox_state *state = state_get_current();
 	const char *fname = strrchr(test->file, '/') + 1;
 
 	printf("Test: %s: %s%s\n", test->name, fname,
@@ -75,13 +74,10 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 
 	ut_assertok(test_pre_run(uts, test));
 
-	if (!state->show_test_output)
-		gd->flags |= GD_FLG_SILENT;
 	test->func(uts);
 
 	ut_assertok(test_post_run(uts, test));
 
-	gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
 	state_set_skip_delays(false);
 
 	ut_assertok(dm_test_destroy(uts));
test/test-main.c +2 -1
index db0d82e36c3..e273777b6e2 100644
@@ -50,13 +50,14 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 			return -EAGAIN;
 		}
 	}
+	ut_silence_console(uts);
 
 	return 0;
 }
 
 int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 {
-	gd->flags &= ~GD_FLG_RECORD;
+	ut_unsilence_console(uts);
 
 	return 0;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#21 lore raw

PATCH v5 19/41 test: Move delay skipping to test_pre_run()

In reply to Simon Glass

This allows delays to be skipped in sandbox tests. Move it to the common pre-init function.

---

(no changes since v1)

 include/test/ut.h | 11 +++++++++++
 test/dm/test-dm.c |  2 --
 test/test-main.c  |  2 ++
 test/ut.c         |  7 +++++++
 4 files changed, 20 insertions(+), 2 deletions(-)
include/test/ut.h +11 -0
index 7cb5e10f3af..e5ec18e60b0 100644
@@ -356,6 +356,17 @@ void ut_silence_console(struct unit_test_state *uts);
  */
 void ut_unsilence_console(struct unit_test_state *uts);
 
+/**
+ * ut_set_skip_delays() - Sets whether delays should be skipped
+ *
+ * Normally functions like mdelay() cause U-Boot to wait for a while. This
+ * allows all such delays to be skipped on sandbox, to speed up tests
+ *
+ * @uts: Test state (in case in future we want to keep state here)
+ * @skip_delays: true to skip delays, false to process them normally
+ */
+void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
+
 /**
  * test_pre_run() - Handle any preparation needed to run a test
  *
test/dm/test-dm.c +0 -2
index fdd35f663e4..569ffbbad93 100644
@@ -78,8 +78,6 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 
 	ut_assertok(test_post_run(uts, test));
 
-	state_set_skip_delays(false);
-
 	ut_assertok(dm_test_destroy(uts));
 
 	return 0;
test/test-main.c +2 -0
index e273777b6e2..6f0d32f7e27 100644
@@ -30,6 +30,8 @@ static int do_autoprobe(struct unit_test_state *uts)
 
 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
+	ut_set_skip_delays(uts, false);
+
 	uts->start = mallinfo();
 
 	if (test->flags & UT_TESTF_SCAN_PDATA)
test/ut.c +7 -0
index 7328338731c..ea0af153e4a 100644
@@ -133,3 +133,10 @@ void ut_unsilence_console(struct unit_test_state *uts)
 {
 	gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
 }
+
+void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
+{
+#ifdef CONFIG_SANDBOX
+	state_set_skip_delays(skip_delays);
+#endif
+}
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#22 lore raw

PATCH v5 20/41 test: Handle driver model reinit in test_pre_run()

In reply to Simon Glass

For driver model tests we want to reinit the data structures so that everything is in a known state before the test runs. This avoids one test changing something that breaks a subsequent tests.

Move the call for this into test_pre_run().

---

(no changes since v1)

 include/test/test.h |  2 ++
 include/test/ut.h   | 10 ++++++++++
 test/dm/test-dm.c   |  6 +++---
 test/test-main.c    |  3 +++
 4 files changed, 18 insertions(+), 3 deletions(-)
include/test/test.h +2 -0
index d282cb2362d..6997568cc07 100644
@@ -15,6 +15,7 @@
  * @fail_count: Number of tests that failed
  * @start: Store the starting mallinfo when doing leak test
  * @priv: A pointer to some other info some suites want to track
+ * @of_live: true to use livetree if available, false to use flattree
  * @of_root: Record of the livetree root node (used for setting up tests)
  * @expect_str: Temporary string used to hold expected string value
  * @actual_str: Temporary string used to hold actual string value
@@ -24,6 +25,7 @@ struct unit_test_state {
 	struct mallinfo start;
 	void *priv;
 	struct device_node *of_root;
+	bool of_live;
 	char expect_str[256];
 	char actual_str[256];
 };
include/test/ut.h +10 -0
index e5ec18e60b0..6e56ca99c31 100644
@@ -387,6 +387,16 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
  */
 int test_post_run(struct unit_test_state *uts, struct unit_test *test);
 
+/**
+ * dm_test_init() - Get ready to run a driver model test
+ *
+ * This clears out the driver model data structures. For sandbox it resets the
+ * state structure.
+ *
+ * @uts: Test state
+ */
+int dm_test_init(struct unit_test_state *uts);
+
 /**
  * ut_run_tests() - Run a set of tests
  *
test/dm/test-dm.c +3 -3
index 569ffbbad93..ceeac3fd361 100644
@@ -24,10 +24,10 @@ DECLARE_GLOBAL_DATA_PTR;
 struct unit_test_state global_dm_test_state;
 static struct dm_test_state _global_priv_dm_test_state;
 
-/* Get ready for testing */
-static int dm_test_init(struct unit_test_state *uts, bool of_live)
+int dm_test_init(struct unit_test_state *uts)
 {
 	struct dm_test_state *dms = uts->priv;
+	bool of_live = uts->of_live;
 
 	memset(dms, '\0', sizeof(*dms));
 	gd->dm_root = NULL;
@@ -70,7 +70,7 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 
 	printf("Test: %s: %s%s\n", test->name, fname,
 	       !of_live ? " (flat tree)" : "");
-	ut_assertok(dm_test_init(uts, of_live));
+	uts->of_live = of_live;
 
 	ut_assertok(test_pre_run(uts, test));
 
test/test-main.c +3 -0
index 6f0d32f7e27..f14b7b09f79 100644
@@ -30,6 +30,9 @@ static int do_autoprobe(struct unit_test_state *uts)
 
 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
+	if (test->flags & UT_TESTF_DM)
+		ut_assertok(dm_test_init(uts));
+
 	ut_set_skip_delays(uts, false);
 
 	uts->start = mallinfo();
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#23 lore raw

PATCH v5 21/41 test: Drop struct dm_test_state

In reply to Simon Glass

Driver model is a core part of U-Boot. We don't really need to have a separate test structure for the driver model tests and it makes it harder to write a test if you have to think about which type of test it is.

Subsume the fields from struct dm_test_state into struct unit_test_state and delete the former.

---

(no changes since v4)

Changes in v4:
- Rebase on master

 include/dm/test.h     | 17 -------------
 include/test/test.h   | 10 ++++++--
 test/dm/core.c        | 58 ++++++++++++++++++-------------------------
 test/dm/test-dm.c     | 10 ++++----
 test/dm/test-driver.c |  4 +--
 test/dm/test-uclass.c |  3 +--
 6 files changed, 39 insertions(+), 63 deletions(-)
include/dm/test.h +0 -17
index c0b463cc0f1..fe1cc2e278c 100644
@@ -125,23 +125,6 @@ extern int dm_testdrv_op_count[DM_TEST_OP_COUNT];
 
 extern struct unit_test_state global_dm_test_state;
 
-/*
- * struct dm_test_state - Entire state of dm test system
- *
- * This is often abreviated to dms.
- *
- * @root: Root device
- * @testdev: Test device
- * @force_fail_alloc: Force all memory allocs to fail
- * @skip_post_probe: Skip uclass post-probe processing
- */
-struct dm_test_state {
-	struct udevice *root;
-	struct udevice *testdev;
-	int force_fail_alloc;
-	int skip_post_probe;
-};
-
 /* Declare a new driver model test */
 #define DM_TEST(_name, _flags) \
 	UNIT_TEST(_name, UT_TESTF_DM | UT_TESTF_CONSOLE_REC | (_flags), dm_test)
include/test/test.h +8 -2
index 6997568cc07..5eeec35f525 100644
@@ -14,18 +14,24 @@
  *
  * @fail_count: Number of tests that failed
  * @start: Store the starting mallinfo when doing leak test
- * @priv: A pointer to some other info some suites want to track
  * @of_live: true to use livetree if available, false to use flattree
  * @of_root: Record of the livetree root node (used for setting up tests)
+ * @root: Root device
+ * @testdev: Test device
+ * @force_fail_alloc: Force all memory allocs to fail
+ * @skip_post_probe: Skip uclass post-probe processing
  * @expect_str: Temporary string used to hold expected string value
  * @actual_str: Temporary string used to hold actual string value
  */
 struct unit_test_state {
 	int fail_count;
 	struct mallinfo start;
-	void *priv;
 	struct device_node *of_root;
 	bool of_live;
+	struct udevice *root;
+	struct udevice *testdev;
+	int force_fail_alloc;
+	int skip_post_probe;
 	char expect_str[256];
 	char actual_str[256];
 };
test/dm/core.c +24 -34

219 lines not on this page · raw message

test/dm/test-dm.c +5 -5
index ceeac3fd361..15adc53f533 100644
@@ -22,14 +22,15 @@
 DECLARE_GLOBAL_DATA_PTR;
 
 struct unit_test_state global_dm_test_state;
-static struct dm_test_state _global_priv_dm_test_state;
 
 int dm_test_init(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	bool of_live = uts->of_live;
 
-	memset(dms, '\0', sizeof(*dms));
+	uts->root = NULL;
+	uts->testdev = NULL;
+	uts->force_fail_alloc = false;
+	uts->skip_post_probe = false;
 	gd->dm_root = NULL;
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
 		memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
@@ -38,7 +39,7 @@ int dm_test_init(struct unit_test_state *uts)
 	/* Determine whether to make the live tree available */
 	gd_set_of_root(of_live ? uts->of_root : NULL);
 	ut_assertok(dm_init(of_live));
-	dms->root = dm_root();
+	uts->root = dm_root();
 
 	return 0;
 }
@@ -124,7 +125,6 @@ int dm_test_run(const char *test_name)
 	struct unit_test *test;
 	int found;
 
-	uts->priv = &_global_priv_dm_test_state;
 	uts->fail_count = 0;
 
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
test/dm/test-driver.c +1 -3
index ca7626a0668..63dc9d335ae 100644
@@ -116,10 +116,8 @@ static int test_manual_bind(struct udevice *dev)
 
 static int test_manual_probe(struct udevice *dev)
 {
-	struct dm_test_state *dms = uts->priv;
-
 	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
-	if (!dms->force_fail_alloc)
+	if (!uts->force_fail_alloc)
 		dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
 	if (!dev_get_priv(dev))
 		return -ENOMEM;
test/dm/test-uclass.c +1 -2
index f1b7aaa727f..f4b540c9278 100644
@@ -71,13 +71,12 @@ static int test_post_probe(struct udevice *dev)
 
 	struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
 	struct uclass *uc = dev->uclass;
-	struct dm_test_state *dms = uts->priv;
 
 	dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++;
 	ut_assert(priv);
 	ut_assert(device_active(dev));
 	priv->base_add = 0;
-	if (dms->skip_post_probe)
+	if (uts->skip_post_probe)
 		return 0;
 	if (&prev->uclass_node != &uc->dev_head) {
 		struct dm_test_uclass_perdev_priv *prev_uc_priv
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#24 lore raw

PATCH v5 22/41 test: Move dm_test_init() into test-main.c

In reply to Simon Glass

Move this function into test-main so that all the init is in one place. Rename it so that its purpose is clearer.

---

(no changes since v1)

 include/test/ut.h |  9 ---------
 test/dm/test-dm.c | 22 ----------------------
 test/test-main.c  | 33 ++++++++++++++++++++++++++++++++-
 3 files changed, 32 insertions(+), 32 deletions(-)
include/test/ut.h +0 -9
index 6e56ca99c31..4e0aba9f700 100644
@@ -387,15 +387,6 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
  */
 int test_post_run(struct unit_test_state *uts, struct unit_test *test);
 
-/**
- * dm_test_init() - Get ready to run a driver model test
- *
- * This clears out the driver model data structures. For sandbox it resets the
- * state structure.
- *
- * @uts: Test state
- */
-int dm_test_init(struct unit_test_state *uts);
 
 /**
  * ut_run_tests() - Run a set of tests
test/dm/test-dm.c +0 -22
index 15adc53f533..d601e497522 100644
@@ -12,7 +12,6 @@
 #include <malloc.h>
 #include <asm/global_data.h>
 #include <asm/state.h>
-#include <dm/test.h>
 #include <dm/root.h>
 #include <dm/uclass-internal.h>
 #include <test/test.h>
@@ -23,27 +22,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
 struct unit_test_state global_dm_test_state;
 
-int dm_test_init(struct unit_test_state *uts)
-{
-	bool of_live = uts->of_live;
-
-	uts->root = NULL;
-	uts->testdev = NULL;
-	uts->force_fail_alloc = false;
-	uts->skip_post_probe = false;
-	gd->dm_root = NULL;
-	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
-		memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
-	state_reset_for_test(state_get_current());
-
-	/* Determine whether to make the live tree available */
-	gd_set_of_root(of_live ? uts->of_root : NULL);
-	ut_assertok(dm_init(of_live));
-	uts->root = dm_root();
-
-	return 0;
-}
-
 static int dm_test_destroy(struct unit_test_state *uts)
 {
 	int id;
test/test-main.c +32 -1
index f14b7b09f79..8b0121bdcec 100644
@@ -7,12 +7,43 @@
 #include <common.h>
 #include <console.h>
 #include <dm.h>
+#include <asm/state.h>
 #include <dm/root.h>
+#include <dm/test.h>
 #include <test/test.h>
 #include <test/ut.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/**
+ * dm_test_pre_run() - Get ready to run a driver model test
+ *
+ * This clears out the driver model data structures. For sandbox it resets the
+ * state structure
+ *
+ * @uts: Test state
+ */
+static int dm_test_pre_run(struct unit_test_state *uts)
+{
+	bool of_live = uts->of_live;
+
+	uts->root = NULL;
+	uts->testdev = NULL;
+	uts->force_fail_alloc = false;
+	uts->skip_post_probe = false;
+	gd->dm_root = NULL;
+	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
+		memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
+	state_reset_for_test(state_get_current());
+
+	/* Determine whether to make the live tree available */
+	gd_set_of_root(of_live ? uts->of_root : NULL);
+	ut_assertok(dm_init(of_live));
+	uts->root = dm_root();
+
+	return 0;
+}
+
 /* Ensure all the test devices are probed */
 static int do_autoprobe(struct unit_test_state *uts)
 {
@@ -31,7 +62,7 @@ static int do_autoprobe(struct unit_test_state *uts)
 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
 	if (test->flags & UT_TESTF_DM)
-		ut_assertok(dm_test_init(uts));
+		ut_assertok(dm_test_pre_run(uts));
 
 	ut_set_skip_delays(uts, false);
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#25 lore raw

PATCH v5 22/41 test: Move dm_test_init() into test-main.c

Quoted 78 lines · Simon Glass

Move this function into test-main so that all the init is in one place. Rename it so that its purpose is clearer.

---

(no changes since v1)

  include/test/ut.h |  9 ---------
  test/dm/test-dm.c | 22 ----------------------
  test/test-main.c  | 33 ++++++++++++++++++++++++++++++++-
  3 files changed, 32 insertions(+), 32 deletions(-)
include/test/ut.h +0 -9
index 6e56ca99c31..4e0aba9f700 100644
@@ -387,15 +387,6 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
   */
  int test_post_run(struct unit_test_state *uts, struct unit_test *test);
 
-/**
- * dm_test_init() - Get ready to run a driver model test
- *
- * This clears out the driver model data structures. For sandbox it resets the
- * state structure.
- *
- * @uts: Test state
- */
-int dm_test_init(struct unit_test_state *uts);
 
  /**
   * ut_run_tests() - Run a set of tests
test/dm/test-dm.c +0 -22
index 15adc53f533..d601e497522 100644
@@ -12,7 +12,6 @@
  #include <malloc.h>
  #include <asm/global_data.h>
  #include <asm/state.h>
-#include <dm/test.h>
  #include <dm/root.h>
  #include <dm/uclass-internal.h>
  #include <test/test.h>
@@ -23,27 +22,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
  struct unit_test_state global_dm_test_state;
 
-int dm_test_init(struct unit_test_state *uts)
-{
-	bool of_live = uts->of_live;
-
-	uts->root = NULL;
-	uts->testdev = NULL;
-	uts->force_fail_alloc = false;
-	uts->skip_post_probe = false;
-	gd->dm_root = NULL;
-	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
-		memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
-	state_reset_for_test(state_get_current());
-
-	/* Determine whether to make the live tree available */
-	gd_set_of_root(of_live ? uts->of_root : NULL);
-	ut_assertok(dm_init(of_live));
-	uts->root = dm_root();
-
-	return 0;
-}
-
  static int dm_test_destroy(struct unit_test_state *uts)
  {
  	int id;
test/test-main.c +0 -0
index f14b7b09f79..8b0121bdcec 100644

test/test-main.c line 10

Quoted 4 lines
test/test-main.c +1 -0
@@ -7,12 +7,43 @@
  #include <common.h>
  #include <console.h>
  #include <dm.h>
+#include <asm/state.h>

This breaks non-sandbox unit testing, since they do not have a state.h.

--Sean

Quoted 49 lines
test/test-main.c +31 -1
  #include <dm/root.h>
+#include <dm/test.h>
  #include <test/test.h>
  #include <test/ut.h>
 
  DECLARE_GLOBAL_DATA_PTR;
 
+/**
+ * dm_test_pre_run() - Get ready to run a driver model test
+ *
+ * This clears out the driver model data structures. For sandbox it resets the
+ * state structure
+ *
+ * @uts: Test state
+ */
+static int dm_test_pre_run(struct unit_test_state *uts)
+{
+	bool of_live = uts->of_live;
+
+	uts->root = NULL;
+	uts->testdev = NULL;
+	uts->force_fail_alloc = false;
+	uts->skip_post_probe = false;
+	gd->dm_root = NULL;
+	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
+		memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
+	state_reset_for_test(state_get_current());
+
+	/* Determine whether to make the live tree available */
+	gd_set_of_root(of_live ? uts->of_root : NULL);
+	ut_assertok(dm_init(of_live));
+	uts->root = dm_root();
+
+	return 0;
+}
+
  /* Ensure all the test devices are probed */
  static int do_autoprobe(struct unit_test_state *uts)
  {
@@ -31,7 +62,7 @@ static int do_autoprobe(struct unit_test_state *uts)
  int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
  {
  	if (test->flags & UT_TESTF_DM)
-		ut_assertok(dm_test_init(uts));
+		ut_assertok(dm_test_pre_run(uts));
 
  	ut_set_skip_delays(uts, false);
 
#26 lore raw

PATCH v5 22/41 test: Move dm_test_init() into test-main.c

Hi Sean,

Quoted 78 lines · Sean Anderson
Quoted 76 lines · Simon Glass

Move this function into test-main so that all the init is in one place. Rename it so that its purpose is clearer.

---

(no changes since v1)

  include/test/ut.h |  9 ---------
  test/dm/test-dm.c | 22 ----------------------
  test/test-main.c  | 33 ++++++++++++++++++++++++++++++++-
  3 files changed, 32 insertions(+), 32 deletions(-)
include/test/ut.h +0 -9
index 6e56ca99c31..4e0aba9f700 100644
@@ -387,15 +387,6 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
   */
  int test_post_run(struct unit_test_state *uts, struct unit_test *test);
 
-/**
- * dm_test_init() - Get ready to run a driver model test
- *
- * This clears out the driver model data structures. For sandbox it resets the
- * state structure.
- *
- * @uts: Test state
- */
-int dm_test_init(struct unit_test_state *uts);
 
  /**
   * ut_run_tests() - Run a set of tests
test/dm/test-dm.c +0 -22
index 15adc53f533..d601e497522 100644
@@ -12,7 +12,6 @@
  #include <malloc.h>
  #include <asm/global_data.h>
  #include <asm/state.h>
-#include <dm/test.h>
  #include <dm/root.h>
  #include <dm/uclass-internal.h>
  #include <test/test.h>
@@ -23,27 +22,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
  struct unit_test_state global_dm_test_state;
 
-int dm_test_init(struct unit_test_state *uts)
-{
-     bool of_live = uts->of_live;
-
-     uts->root = NULL;
-     uts->testdev = NULL;
-     uts->force_fail_alloc = false;
-     uts->skip_post_probe = false;
-     gd->dm_root = NULL;
-     if (!CONFIG_IS_ENABLED(OF_PLATDATA))
-             memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
-     state_reset_for_test(state_get_current());
-
-     /* Determine whether to make the live tree available */
-     gd_set_of_root(of_live ? uts->of_root : NULL);
-     ut_assertok(dm_init(of_live));
-     uts->root = dm_root();
-
-     return 0;
-}
-
  static int dm_test_destroy(struct unit_test_state *uts)
  {
      int id;
test/test-main.c +0 -1
index f14b7b09f79..8b0121bdcec 100644
--- a/test/test-main.c
Quoted 8 lines

test/test-main.c line 10

Quoted 6 lines

+++ b/test/test-main.c

test/test-main.c +1 -0
@@ -7,12 +7,43 @@
  #include <common.h>
  #include <console.h>
  #include <dm.h>
+#include <asm/state.h>

This breaks non-sandbox unit testing, since they do not have a state.h.

OK thanks I will take a look.

Regards,
Simon

#27 lore raw

PATCH v5 23/41 test: Move dm_test_destroy() into test-main.c

In reply to Simon Glass

Move this function into the common test runner and rename it to dm_test_post_run() so that its purpose is clear.

---

(no changes since v1)

 test/dm/test-dm.c | 22 ----------------------
 test/test-main.c  | 23 +++++++++++++++++++++++
 2 files changed, 23 insertions(+), 22 deletions(-)
test/dm/test-dm.c +0 -22
index d601e497522..df938395bb6 100644
@@ -22,26 +22,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
 struct unit_test_state global_dm_test_state;
 
-static int dm_test_destroy(struct unit_test_state *uts)
-{
-	int id;
-
-	for (id = 0; id < UCLASS_COUNT; id++) {
-		struct uclass *uc;
-
-		/*
-		 * If the uclass doesn't exist we don't want to create it. So
-		 * check that here before we call uclass_find_device().
-		 */
-		uc = uclass_find(id);
-		if (!uc)
-			continue;
-		ut_assertok(uclass_destroy(uc));
-	}
-
-	return 0;
-}
-
 static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 		      bool of_live)
 {
@@ -57,8 +37,6 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 
 	ut_assertok(test_post_run(uts, test));
 
-	ut_assertok(dm_test_destroy(uts));
-
 	return 0;
 }
 
test/test-main.c +23 -0
index 8b0121bdcec..3806c2ad89c 100644
@@ -10,6 +10,7 @@
 #include <asm/state.h>
 #include <dm/root.h>
 #include <dm/test.h>
+#include <dm/uclass-internal.h>
 #include <test/test.h>
 #include <test/ut.h>
 
@@ -44,6 +45,26 @@ static int dm_test_pre_run(struct unit_test_state *uts)
 	return 0;
 }
 
+static int dm_test_post_run(struct unit_test_state *uts)
+{
+	int id;
+
+	for (id = 0; id < UCLASS_COUNT; id++) {
+		struct uclass *uc;
+
+		/*
+		 * If the uclass doesn't exist we don't want to create it. So
+		 * check that here before we call uclass_find_device().
+		 */
+		uc = uclass_find(id);
+		if (!uc)
+			continue;
+		ut_assertok(uclass_destroy(uc));
+	}
+
+	return 0;
+}
+
 /* Ensure all the test devices are probed */
 static int do_autoprobe(struct unit_test_state *uts)
 {
@@ -94,6 +115,8 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 {
 	ut_unsilence_console(uts);
+	if (test->flags & UT_TESTF_DM)
+		ut_assertok(dm_test_post_run(uts));
 
 	return 0;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#28 lore raw

PATCH v5 24/41 test: Move test running into a separate function

In reply to Simon Glass

Add a function to handle the preparation for running a test and the post-test clean-up.

---

(no changes since v1)

 include/test/ut.h | 16 ++++++++++++++++
 test/test-main.c  | 32 +++++++++++++++++++++++---------
 2 files changed, 39 insertions(+), 9 deletions(-)
include/test/ut.h +16 -0
index 4e0aba9f700..98f699cbba2 100644
@@ -387,6 +387,22 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
  */
 int test_post_run(struct unit_test_state *uts, struct unit_test *test);
 
+/**
+ * ut_run_test() - Run a single test
+ *
+ * This runs the test, handling any preparation and clean-up needed. It prints
+ * the name of each test before running it.
+ *
+ * @uts: Test state to update. The caller should ensure that this is zeroed for
+ *	the first call to this function. On exit, @uts->fail_count is
+ *	incremented by the number of failures (0, one hopes)
+ * @test: Test to run
+ * @name: Name of test, possibly skipping a prefix that should not be displayed
+ * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
+ *	any failed
+ */
+int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
+		const char *name);
 
 /**
  * ut_run_tests() - Run a set of tests
test/test-main.c +23 -9
index 3806c2ad89c..dee28d35d82 100644
@@ -121,6 +121,28 @@ int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 	return 0;
 }
 
+int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
+		const char *test_name)
+{
+	int ret;
+
+	printf("Test: %s\n", test_name);
+
+	ret = test_pre_run(uts, test);
+	if (ret == -EAGAIN)
+		return -EAGAIN;
+	if (ret)
+		return ret;
+
+	test->func(uts);
+
+	ret = test_post_run(uts, test);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 		 struct unit_test *tests, int count, const char *select_name)
 {
@@ -138,20 +160,12 @@ int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 
 		if (select_name && strcmp(select_name, test_name))
 			continue;
-		printf("Test: %s\n", test_name);
+		ret = ut_run_test(uts, test, test_name);
 		found++;
-
-		ret = test_pre_run(uts, test);
 		if (ret == -EAGAIN)
 			continue;
 		if (ret)
 			return ret;
-
-		test->func(uts);
-
-		ret = test_post_run(uts, test);
-		if (ret)
-			return ret;
 	}
 	if (select_name && !found)
 		return -ENOENT;
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#29 lore raw

PATCH v5 25/41 test: Use ut_run_test() to run driver model tests

In reply to Simon Glass

Instead of having a separate function for running driver model tests, use the common one. Make the pre/post-run functions private since we don't need these outside of test-main.c

---

(no changes since v1)

 include/test/ut.h | 20 --------------------
 test/dm/test-dm.c | 11 +----------
 test/test-main.c  | 26 +++++++++++++++++++++++---
 3 files changed, 24 insertions(+), 33 deletions(-)
include/test/ut.h +0 -20
index 98f699cbba2..adef0b7e1cf 100644
@@ -367,26 +367,6 @@ void ut_unsilence_console(struct unit_test_state *uts);
  */
 void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
 
-/**
- * test_pre_run() - Handle any preparation needed to run a test
- *
- * @uts: Test state
- * @test: Test to prepare for
- * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
- *	available, other -ve on error (meaning that testing cannot likely
- *	continue)
- */
-int test_pre_run(struct unit_test_state *uts, struct unit_test *test);
-
-/**
- * test_post_run() - Handle cleaning up after a test
- *
- * @uts: Test state
- * @test: Test to clean up after
- * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
- */
-int test_post_run(struct unit_test_state *uts, struct unit_test *test);
-
 /**
  * ut_run_test() - Run a single test
  *
test/dm/test-dm.c +1 -10
index df938395bb6..b01123c7409 100644
@@ -25,17 +25,8 @@ struct unit_test_state global_dm_test_state;
 static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
 		      bool of_live)
 {
-	const char *fname = strrchr(test->file, '/') + 1;
-
-	printf("Test: %s: %s%s\n", test->name, fname,
-	       !of_live ? " (flat tree)" : "");
 	uts->of_live = of_live;
-
-	ut_assertok(test_pre_run(uts, test));
-
-	test->func(uts);
-
-	ut_assertok(test_post_run(uts, test));
+	ut_assertok(ut_run_test(uts, test, test->name));
 
 	return 0;
 }
test/test-main.c +23 -3
index dee28d35d82..32c4d4b1996 100644
@@ -80,7 +80,16 @@ static int do_autoprobe(struct unit_test_state *uts)
 	return ret;
 }
 
-int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
+/**
+ * test_pre_run() - Handle any preparation needed to run a test
+ *
+ * @uts: Test state
+ * @test: Test to prepare for
+ * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
+ *	available, other -ve on error (meaning that testing cannot likely
+ *	continue)
+ */
+static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 {
 	if (test->flags & UT_TESTF_DM)
 		ut_assertok(dm_test_pre_run(uts));
@@ -112,7 +121,14 @@ int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
 	return 0;
 }
 
-int test_post_run(struct unit_test_state *uts, struct unit_test *test)
+/**
+ * test_post_run() - Handle cleaning up after a test
+ *
+ * @uts: Test state
+ * @test: Test to clean up after
+ * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
+ */
+static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 {
 	ut_unsilence_console(uts);
 	if (test->flags & UT_TESTF_DM)
@@ -124,9 +140,13 @@ int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 		const char *test_name)
 {
+	const char *fname = strrchr(test->file, '/') + 1;
+	const char *note = "";
 	int ret;
 
-	printf("Test: %s\n", test_name);
+	if ((test->flags & UT_TESTF_DM) && !uts->of_live)
+		note = " (flat tree)";
+	printf("Test: %s: %s%s\n", test_name, fname, note);
 
 	ret = test_pre_run(uts, test);
 	if (ret == -EAGAIN)
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#30 lore raw

PATCH v5 26/41 test: Drop dm_do_test()

In reply to Simon Glass

In an effort to make use of a common test runner, use ut_run_test() directly to run driver model tests.

---

(no changes since v1)

 test/dm/test-dm.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
test/dm/test-dm.c +4 -11
index b01123c7409..de41fc09db0 100644
@@ -22,15 +22,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
 struct unit_test_state global_dm_test_state;
 
-static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
-		      bool of_live)
-{
-	uts->of_live = of_live;
-	ut_assertok(ut_run_test(uts, test, test->name));
-
-	return 0;
-}
-
 /**
  * dm_test_run_on_flattree() - Check if we should run a test with flat DT
  *
@@ -103,7 +94,8 @@ int dm_test_run(const char *test_name)
 		runs = 0;
 		if (CONFIG_IS_ENABLED(OF_LIVE)) {
 			if (!(test->flags & UT_TESTF_FLAT_TREE)) {
-				ut_assertok(dm_do_test(uts, test, true));
+				uts->of_live = true;
+				ut_assertok(ut_run_test(uts, test, test->name));
 				runs++;
 			}
 		}
@@ -114,7 +106,8 @@ int dm_test_run(const char *test_name)
 		 */
 		if (!(test->flags & UT_TESTF_LIVE_TREE) &&
 		    (!runs || dm_test_run_on_flattree(test))) {
-			ut_assertok(dm_do_test(uts, test, false));
+			uts->of_live = false;
+			ut_assertok(ut_run_test(uts, test, test->name));
 			runs++;
 		}
 		found++;
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#31 lore raw

PATCH v5 27/41 test: Add ut_run_test_live_flat() to run tests twice

In reply to Simon Glass

Driver model tests are generally run twice, once with livetree enable and again with it disabled. Add a function to handle this and call it from the driver model test runner.

Make ut_run_test() private since it is not used outside test-main.c now.

---

(no changes since v2)

Changes in v2:
- Fix conditions so non-DM SPL tests are actually run

 include/test/ut.h | 13 +++++----
 test/dm/test-dm.c | 37 +-------------------------
 test/test-main.c  | 67 ++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 73 insertions(+), 44 deletions(-)
include/test/ut.h +8 -5
index adef0b7e1cf..d06bc5089be 100644
@@ -368,10 +368,13 @@ void ut_unsilence_console(struct unit_test_state *uts);
 void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
 
 /**
- * ut_run_test() - Run a single test
+ * ut_run_test_live_flat() - Run a test with both live and flat tree
  *
- * This runs the test, handling any preparation and clean-up needed. It prints
- * the name of each test before running it.
+ * This calls ut_run_test() with livetree enabled, which is the standard setup
+ * for runnig tests. Then, for driver model test, it calls it again with
+ * livetree disabled. This allows checking of flattree being used when OF_LIVE
+ * is enabled, as is the case in U-Boot proper before relocation, as well as in
+ * SPL.
  *
  * @uts: Test state to update. The caller should ensure that this is zeroed for
  *	the first call to this function. On exit, @uts->fail_count is
@@ -381,8 +384,8 @@ void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
  * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  *	any failed
  */
-int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
-		const char *name);
+int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
+			  const char *name);
 
 /**
  * ut_run_tests() - Run a set of tests
test/dm/test-dm.c +1 -36
index de41fc09db0..826b64565e8 100644
@@ -22,21 +22,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
 struct unit_test_state global_dm_test_state;
 
-/**
- * dm_test_run_on_flattree() - Check if we should run a test with flat DT
- *
- * This skips long/slow tests where there is not much value in running a flat
- * DT test in addition to a live DT test.
- *
- * @return true to run the given test on the flat device tree
- */
-static bool dm_test_run_on_flattree(struct unit_test *test)
-{
-	const char *fname = strrchr(test->file, '/') + 1;
-
-	return !strstr(fname, "video") || strstr(test->name, "video_base");
-}
-
 static bool test_matches(const char *test_name, const char *find_name)
 {
 	if (!find_name)
@@ -85,31 +70,11 @@ int dm_test_run(const char *test_name)
 	uts->of_root = gd_of_root();
 	for (test = tests; test < tests + n_ents; test++) {
 		const char *name = test->name;
-		int runs;
 
 		if (!test_matches(name, test_name))
 			continue;
 
-		/* Run with the live tree if possible */
-		runs = 0;
-		if (CONFIG_IS_ENABLED(OF_LIVE)) {
-			if (!(test->flags & UT_TESTF_FLAT_TREE)) {
-				uts->of_live = true;
-				ut_assertok(ut_run_test(uts, test, test->name));
-				runs++;
-			}
-		}
-
-		/*
-		 * Run with the flat tree if we couldn't run it with live tree,
-		 * or it is a core test.
-		 */
-		if (!(test->flags & UT_TESTF_LIVE_TREE) &&
-		    (!runs || dm_test_run_on_flattree(test))) {
-			uts->of_live = false;
-			ut_assertok(ut_run_test(uts, test, test->name));
-			runs++;
-		}
+		ut_assertok(ut_run_test_live_flat(uts, test, test->name));
 		found++;
 	}
 
test/test-main.c +64 -3

91 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#32 lore raw

PATCH v5 28/41 test: Use a local variable for test state

In reply to Simon Glass

At present we use a global test state for all driver-model tests. Make use of a local struct like we do with the other tests.

To make this work, add functions to get and set this state. When a test starts, the state is set (so it can be used in the test). When a test finishes, the state is unset, so it cannot be used by mistake.

---

(no changes since v3)

Changes in v3:
- Use test_set_state() throughout test-main.c instead of direct assignment

 include/test/ut.h     | 14 ++++++++++++++
 test/dm/test-dm.c     |  4 +---
 test/dm/test-driver.c | 10 +++++++++-
 test/dm/test-uclass.c |  7 +++++--
 test/test-main.c      | 18 ++++++++++++++++++
 5 files changed, 47 insertions(+), 6 deletions(-)
include/test/ut.h +14 -0
index d06bc5089be..bed0e6eb5f6 100644
@@ -367,6 +367,20 @@ void ut_unsilence_console(struct unit_test_state *uts);
  */
 void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
 
+/**
+ * test_get_state() - Get the active test state
+ *
+ * @return the currently active test state, or NULL if none
+ */
+struct unit_test_state *test_get_state(void);
+
+/**
+ * test_set_state() - Set the active test state
+ *
+ * @uts: Test state to use as currently active test state, or NULL if none
+ */
+void test_set_state(struct unit_test_state *uts);
+
 /**
  * ut_run_test_live_flat() - Run a test with both live and flat tree
  *
test/dm/test-dm.c +1 -3
index 826b64565e8..cdaf27bf987 100644
@@ -20,8 +20,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-struct unit_test_state global_dm_test_state;
-
 static bool test_matches(const char *test_name, const char *find_name)
 {
 	if (!find_name)
@@ -44,7 +42,7 @@ int dm_test_run(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);
-	struct unit_test_state *uts = &global_dm_test_state;
+	struct unit_test_state uts_s = { .fail_count = 0 }, *uts = &uts_s;
 	struct unit_test *test;
 	int found;
 
test/dm/test-driver.c +9 -1
index 63dc9d335ae..02cb974b0f7 100644
@@ -18,7 +18,6 @@
 #include <test/ut.h>
 
 int dm_testdrv_op_count[DM_TEST_OP_COUNT];
-static struct unit_test_state *uts = &global_dm_test_state;
 
 static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
 {
@@ -37,6 +36,8 @@ static const struct test_ops test_ops = {
 
 static int test_bind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should not be allocated */
 	ut_assert(!dev_get_priv(dev));
 
@@ -46,6 +47,7 @@ static int test_bind(struct udevice *dev)
 
 static int test_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct dm_test_priv *priv = dev_get_priv(dev);
 
 	/* Private data should be allocated */
@@ -58,6 +60,8 @@ static int test_probe(struct udevice *dev)
 
 static int test_remove(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should still be allocated */
 	ut_assert(dev_get_priv(dev));
 
@@ -67,6 +71,8 @@ static int test_remove(struct udevice *dev)
 
 static int test_unbind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should not be allocated */
 	ut_assert(!dev_get_priv(dev));
 
@@ -116,6 +122,8 @@ static int test_manual_bind(struct udevice *dev)
 
 static int test_manual_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
 	if (!uts->force_fail_alloc)
 		dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
test/dm/test-uclass.c +5 -2
index f4b540c9278..067701734a0 100644
@@ -17,8 +17,6 @@
 #include <test/test.h>
 #include <test/ut.h>
 
-static struct unit_test_state *uts = &global_dm_test_state;
-
 int test_ping(struct udevice *dev, int pingval, int *pingret)
 {
 	const struct test_ops *ops = device_get_ops(dev);
@@ -31,6 +29,7 @@ int test_ping(struct udevice *dev, int pingval, int *pingret)
 
 static int test_post_bind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct dm_test_perdev_uc_pdata *uc_pdata;
 
 	dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
@@ -56,6 +55,7 @@ static int test_pre_unbind(struct udevice *dev)
 static int test_pre_probe(struct udevice *dev)
 {
 	struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
+	struct unit_test_state *uts = test_get_state();
 
 	dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
 	ut_assert(priv);
@@ -66,6 +66,7 @@ static int test_pre_probe(struct udevice *dev)
 
 static int test_post_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct udevice *prev = list_entry(dev->uclass_node.prev,
 					    struct udevice, uclass_node);
 
@@ -100,6 +101,8 @@ static int test_pre_remove(struct udevice *dev)
 
 static int test_init(struct uclass *uc)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	dm_testdrv_op_count[DM_TEST_OP_INIT]++;
 	ut_assert(uclass_get_priv(uc));
 
test/test-main.c +18 -0
index 4e17c9edb28..139fc1f6f18 100644
@@ -16,6 +16,19 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/* This is valid when a test is running, NULL otherwise */
+static struct unit_test_state *cur_test_state;
+
+struct unit_test_state *test_get_state(void)
+{
+	return cur_test_state;
+}
+
+void test_set_state(struct unit_test_state *uts)
+{
+	cur_test_state = uts;
+}
+
 /**
  * dm_test_pre_run() - Get ready to run a driver model test
  *
@@ -180,6 +193,9 @@ static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 		note = " (flat tree)";
 	printf("Test: %s: %s%s\n", test_name, fname, note);
 
+	/* Allow access to test state from drivers */
+	test_set_state(uts);
+
 	ret = test_pre_run(uts, test);
 	if (ret == -EAGAIN)
 		return -EAGAIN;
@@ -192,6 +208,8 @@ static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 	if (ret)
 		return ret;
 
+	test_set_state( NULL);
+
 	return 0;
 }
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#33 lore raw

PATCH v5 29/41 test: Run driver-model tests using ut_run_list()

In reply to Simon Glass

Use this function instead of implementing it separately for driver model.

Make ut_run_tests() private since it is only used in test-main.c

---

(no changes since v2)

Changes in v2:
- Allow for prefix to be NULL, to match function comment

 include/test/ut.h | 42 -----------------------
 test/dm/test-dm.c | 45 +++---------------------
 test/test-main.c  | 87 +++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 81 insertions(+), 93 deletions(-)
include/test/ut.h +0 -42
index bed0e6eb5f6..fbbba286ee0 100644
@@ -381,48 +381,6 @@ struct unit_test_state *test_get_state(void);
  */
 void test_set_state(struct unit_test_state *uts);
 
-/**
- * ut_run_test_live_flat() - Run a test with both live and flat tree
- *
- * This calls ut_run_test() with livetree enabled, which is the standard setup
- * for runnig tests. Then, for driver model test, it calls it again with
- * livetree disabled. This allows checking of flattree being used when OF_LIVE
- * is enabled, as is the case in U-Boot proper before relocation, as well as in
- * SPL.
- *
- * @uts: Test state to update. The caller should ensure that this is zeroed for
- *	the first call to this function. On exit, @uts->fail_count is
- *	incremented by the number of failures (0, one hopes)
- * @test: Test to run
- * @name: Name of test, possibly skipping a prefix that should not be displayed
- * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
- *	any failed
- */
-int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
-			  const char *name);
-
-/**
- * ut_run_tests() - Run a set of tests
- *
- * This runs the tests, handling any preparation and clean-up needed. It prints
- * the name of each test before running it.
- *
- * @uts: Test state to update. The caller should ensure that this is zeroed for
- *	the first call to this function. On exit, @uts->fail_count is
- *	incremented by the number of failures (0, one hopes)
- * @prefix: String prefix for the tests. Any tests that have this prefix will be
- *	printed without the prefix, so that it is easier to see the unique part
- *	of the test name. If NULL, no prefix processing is done
- * @tests: List of tests to run
- * @count: Number of tests to run
- * @select_name: Name of a single test to run (from the list provided). If NULL
- *	then all tests are run
- * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
- *	-EBADF if any failed
- */
-int ut_run_tests(struct unit_test_state *uts, const char *prefix,
-		 struct unit_test *tests, int count, const char *select_name);
-
 /**
  * ut_run_tests() - Run a set of tests
  *
test/dm/test-dm.c +5 -40

65 lines not on this page · raw message

test/test-main.c +76 -11

120 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#34 lore raw

PATCH v5 30/41 test: Use return values in dm_test_run()

In reply to Simon Glass

Update this function to use the return value of ut_run_list() to check for success/failure, so that they are in sync. Also return a command success code so that the caller gets what it expects.

---

(no changes since v1)

 test/dm/test-dm.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
test/dm/test-dm.c +6 -8
index 20af1c13b3c..54e6577b009 100644
@@ -20,16 +20,13 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-struct unit_test_state global_dm_test_state;
-
 int dm_test_run(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);
 	struct unit_test_state uts_s = { .fail_count = 0 }, *uts = &uts_s;
 	struct device_node *of_root;
-
-	uts->fail_count = 0;
+	int ret;
 
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
 		/*
@@ -39,22 +36,23 @@ int dm_test_run(const char *test_name)
 		if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
 			puts("Please run with test device tree:\n"
 			     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
-			ut_assert(gd->fdt_blob);
+			return CMD_RET_FAILURE;
 		}
 	}
 
 	of_root = gd_of_root();
-	ut_run_list("driver model", "dm_test_", tests, n_ents, test_name);
+	ret = ut_run_list("driver model", "dm_test_", tests, n_ents, test_name);
 
 	/* Put everything back to normal so that sandbox works as expected */
 	gd_set_of_root(of_root);
 	gd->dm_root = NULL;
-	ut_assertok(dm_init(CONFIG_IS_ENABLED(OF_LIVE)));
+	if (dm_init(CONFIG_IS_ENABLED(OF_LIVE)))
+		return CMD_RET_FAILURE;
 	dm_scan_plat(false);
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
 		dm_scan_fdt(false);
 
-	return uts->fail_count ? CMD_RET_FAILURE : 0;
+	return ret ? CMD_RET_FAILURE : 0;
 }
 
 int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#35 lore raw

PATCH v5 31/41 test: Move the devicetree check into ut_run_list()

In reply to Simon Glass

Add a check to ut_run_list() as to whether a list has driver model tests. Move the logic for the test devicetree into that function, in an effort to eventually remove all logic from dm_test_run().

---

(no changes since v1)

 test/dm/test-dm.c | 13 -------------
 test/test-main.c  | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 13 deletions(-)
test/dm/test-dm.c +0 -13
index 54e6577b009..8cb99ed80cc 100644
@@ -24,22 +24,9 @@ int dm_test_run(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);
-	struct unit_test_state uts_s = { .fail_count = 0 }, *uts = &uts_s;
 	struct device_node *of_root;
 	int ret;
 
-	if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
-		/*
-		 * If we have no device tree, or it only has a root node, then
-		 * these * tests clearly aren't going to work...
-		 */
-		if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
-			puts("Please run with test device tree:\n"
-			     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
-			return CMD_RET_FAILURE;
-		}
-	}
-
 	of_root = gd_of_root();
 	ret = ut_run_list("driver model", "dm_test_", tests, n_ents, test_name);
 
test/test-main.c +32 -0
index 16c0d13ea55..8138fb43875 100644
@@ -143,6 +143,25 @@ static bool test_matches(const char *prefix, const char *test_name,
 	return false;
 }
 
+/*
+ * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
+ *
+ * @tests: List of tests to run
+ * @count: Number of tests to ru
+ * @return true if any of the tests have the UT_TESTF_DM flag
+ */
+static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
+{
+	struct unit_test *test;
+
+	for (test = tests; test < tests + count; test++) {
+		if (test->flags & UT_TESTF_DM)
+			return true;
+	}
+
+	return false;
+}
+
 /**
  * test_pre_run() - Handle any preparation needed to run a test
  *
@@ -342,6 +361,19 @@ int ut_run_list(const char *category, const char *prefix,
 	struct unit_test_state uts = { .fail_count = 0 };
 	int ret;
 
+	if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
+	    ut_list_has_dm_tests(tests, count)) {
+		/*
+		 * If we have no device tree, or it only has a root node, then
+		 * these * tests clearly aren't going to work...
+		 */
+		if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
+			puts("Please run with test device tree:\n"
+			     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
+			return CMD_RET_FAILURE;
+		}
+	}
+
 	if (!select_name)
 		printf("Running %d %s tests\n", count, category);
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#36 lore raw

PATCH v5 32/41 test: Move restoring of driver model state to ut_run_list()

In reply to Simon Glass

Add this functionality to ut_run_list() so it can be removed from dm_test_run().

At this point all tests are run through ut_run_list().

---

(no changes since v1)

 test/dm/test-dm.c | 11 -----------
 test/test-main.c  | 30 +++++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 12 deletions(-)
test/dm/test-dm.c +0 -11
index 8cb99ed80cc..cb4f99537df 100644
@@ -24,21 +24,10 @@ int dm_test_run(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);
-	struct device_node *of_root;
 	int ret;
 
-	of_root = gd_of_root();
 	ret = ut_run_list("driver model", "dm_test_", tests, n_ents, test_name);
 
-	/* Put everything back to normal so that sandbox works as expected */
-	gd_set_of_root(of_root);
-	gd->dm_root = NULL;
-	if (dm_init(CONFIG_IS_ENABLED(OF_LIVE)))
-		return CMD_RET_FAILURE;
-	dm_scan_plat(false);
-	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
-		dm_scan_fdt(false);
-
 	return ret ? CMD_RET_FAILURE : 0;
 }
 
test/test-main.c +29 -1
index 8138fb43875..6edd49f0b61 100644
@@ -143,7 +143,7 @@ static bool test_matches(const char *prefix, const char *test_name,
 	return false;
 }
 
-/*
+/**
  * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
  *
  * @tests: List of tests to run
@@ -162,6 +162,28 @@ static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
 	return false;
 }
 
+/**
+ * dm_test_restore() Put things back to normal so sandbox works as expected
+ *
+ * @of_root: Value to set for of_root
+ * @return 0 if OK, -ve on error
+ */
+static int dm_test_restore(struct device_node *of_root)
+{
+	int ret;
+
+	gd_set_of_root(of_root);
+	gd->dm_root = NULL;
+	ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
+	if (ret)
+		return ret;
+	dm_scan_plat(false);
+	if (!CONFIG_IS_ENABLED(OF_PLATDATA))
+		dm_scan_fdt(false);
+
+	return 0;
+}
+
 /**
  * test_pre_run() - Handle any preparation needed to run a test
  *
@@ -359,10 +381,12 @@ int ut_run_list(const char *category, const char *prefix,
 		struct unit_test *tests, int count, const char *select_name)
 {
 	struct unit_test_state uts = { .fail_count = 0 };
+	bool has_dm_tests = false;
 	int ret;
 
 	if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
 	    ut_list_has_dm_tests(tests, count)) {
+		has_dm_tests = true;
 		/*
 		 * If we have no device tree, or it only has a root node, then
 		 * these * tests clearly aren't going to work...
@@ -385,5 +409,9 @@ int ut_run_list(const char *category, const char *prefix,
 	else
 		printf("Failures: %d\n", uts.fail_count);
 
+	/* Best efforts only...ignore errors */
+	if (has_dm_tests)
+		dm_test_restore(uts.of_root);
+
 	return ret;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#37 lore raw

PATCH v5 33/41 test: log: Rename log main test file to log_ut.c

In reply to Simon Glass

The current name is the same as the main test runner file. Rename it to avoid confusion.

---

(no changes since v1)

 test/log/Makefile                  | 2 +-
 test/log/{test-main.c => log_ut.c} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename test/log/{test-main.c => log_ut.c} (100%)
test/log/Makefile +1 -1
index 3f09deb644b..a3dedace043 100644
@@ -7,7 +7,7 @@ obj-$(CONFIG_CMD_LOG) += log_filter.o
 
 ifdef CONFIG_UT_LOG
 
-obj-y += test-main.o
+obj-y += log_ut.o
 
 ifdef CONFIG_SANDBOX
 obj-$(CONFIG_LOG_SYSLOG) += syslog_test.o
test/log/log_ut.c renamed from test/log/test-main.c +0 -0
similarity index 100%
rename from test/log/test-main.c
rename to test/log/log_ut.c
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#38 lore raw

PATCH v5 34/41 test: Add a macros for finding tests in linker_lists

In reply to Simon Glass

At present we use the linker list directly. This is not very friendly, so add a helpful macro instead. This will also allow us to change the naming later without updating this code.

---

(no changes since v1)

 include/test/test.h           | 6 ++++++
 test/bloblist.c               | 5 ++---
 test/bootm.c                  | 4 ++--
 test/cmd/mem.c                | 4 ++--
 test/cmd/setexpr.c            | 5 ++---
 test/compression.c            | 5 ++---
 test/dm/test-dm.c             | 4 ++--
 test/env/cmd_ut_env.c         | 4 ++--
 test/lib/cmd_ut_lib.c         | 4 ++--
 test/log/log_ut.c             | 4 ++--
 test/optee/cmd_ut_optee.c     | 5 ++---
 test/overlay/cmd_ut_overlay.c | 5 ++---
 test/str_ut.c                 | 5 ++---
 test/unicode_ut.c             | 4 ++--
 14 files changed, 32 insertions(+), 32 deletions(-)
include/test/test.h +6 -0
index 5eeec35f525..b16c9135f2c 100644
@@ -93,6 +93,12 @@ struct unit_test {
 		.func = _name,						\
 	}
 
+/* Get the start of a list of unit tests for a particular category */
+#define UNIT_TEST_SUITE_START(_suite) \
+	ll_entry_start(struct unit_test, _suite)
+#define UNIT_TEST_SUITE_COUNT(_suite) \
+	ll_entry_count(struct unit_test, _suite)
+
 /* Sizes for devres tests */
 enum {
 	TEST_DEVRES_SIZE	= 100,
test/bloblist.c +2 -3
index 6953d3010a6..d876b639184 100644
@@ -387,9 +387,8 @@ BLOBLIST_TEST(bloblist_test_reloc, 0);
 int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test,
-						 bloblist_test);
-	const int n_ents = ll_entry_count(struct unit_test, bloblist_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(bloblist_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(bloblist_test);
 
 	return cmd_ut_category("bloblist", "bloblist_test_",
 			       tests, n_ents, argc, argv);
test/bootm.c +2 -2
index 563d6ebaa5a..8528982ae11 100644
@@ -240,8 +240,8 @@ BOOTM_TEST(bootm_test_subst_both, 0);
 
 int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test, bootm_test);
-	const int n_ents = ll_entry_count(struct unit_test, bootm_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(bootm_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(bootm_test);
 
 	return cmd_ut_category("bootm", "bootm_test_", tests, n_ents,
 			       argc, argv);
test/cmd/mem.c +2 -2
index fbaa8a4b3cf..d76f47cf311 100644
@@ -12,8 +12,8 @@
 
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test, mem_test);
-	const int n_ents = ll_entry_count(struct unit_test, mem_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(mem_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(mem_test);
 
 	return cmd_ut_category("cmd_mem", "mem_test_", tests, n_ents, argc,
 			       argv);
test/cmd/setexpr.c +2 -3
index b483069ff0f..27113c083c5 100644
@@ -390,9 +390,8 @@ SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
 
 int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test,
-						 setexpr_test);
-	const int n_ents = ll_entry_count(struct unit_test, setexpr_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(setexpr_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(setexpr_test);
 
 	return cmd_ut_category("cmd_setexpr", "setexpr_test_", tests, n_ents,
 			       argc, argv);
test/compression.c +2 -3
index a2a4b9ff9e8..4cd1be564f3 100644
@@ -539,9 +539,8 @@ COMPRESSION_TEST(compression_test_bootm_none, 0);
 int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc,
 		      char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test,
-						 compression_test);
-	const int n_ents = ll_entry_count(struct unit_test, compression_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(compression_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(compression_test);
 
 	return cmd_ut_category("compression", "compression_test_",
 			       tests, n_ents, argc, argv);
test/dm/test-dm.c +2 -2
index cb4f99537df..729becf57b2 100644
@@ -22,8 +22,8 @@ DECLARE_GLOBAL_DATA_PTR;
 
 int dm_test_run(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);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(dm_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(dm_test);
 	int ret;
 
 	ret = ut_run_list("driver model", "dm_test_", tests, n_ents, test_name);
test/env/cmd_ut_env.c +2 -2
index a440b1bfb0e..d65a32179ce 100644
@@ -12,8 +12,8 @@
 
 int do_ut_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test, env_test);
-	const int n_ents = ll_entry_count(struct unit_test, env_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(env_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(env_test);
 
 	return cmd_ut_category("environment", "env_test_",
 			       tests, n_ents, argc, argv);
test/lib/cmd_ut_lib.c +2 -2
index f5c7bf3d3b0..f1ac015b2c8 100644
@@ -13,8 +13,8 @@
 
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test, lib_test);
-	const int n_ents = ll_entry_count(struct unit_test, lib_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(lib_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(lib_test);
 
 	return cmd_ut_category("lib", "lib_test_", tests, n_ents, argc, argv);
 }
test/log/log_ut.c +2 -2
index c534add493e..5aa3a184004 100644
@@ -13,8 +13,8 @@
 
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test, log_test);
-	const int n_ents = ll_entry_count(struct unit_test, log_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(log_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(log_test);
 
 	return cmd_ut_category("log", "log_test_",
 			       tests, n_ents, argc, argv);
test/optee/cmd_ut_optee.c +2 -3
index 9fa4c91e0dd..c3887ab11d9 100644
@@ -94,9 +94,8 @@ OPTEE_TEST(optee_fdt_protected_memory, 0);
 
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test,
-						 optee_test);
-	const int n_ents = ll_entry_count(struct unit_test, optee_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(optee_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(optee_test);
 	struct unit_test_state *uts;
 	void *fdt_optee = &__dtb_test_optee_optee_begin;
 	void *fdt_no_optee = &__dtb_test_optee_no_optee_begin;
test/overlay/cmd_ut_overlay.c +2 -3
index c001fb183fe..56a3df17138 100644
@@ -213,9 +213,8 @@ OVERLAY_TEST(fdt_overlay_stacked, 0);
 
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test,
-						 overlay_test);
-	const int n_ents = ll_entry_count(struct unit_test, overlay_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(overlay_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(overlay_test);
 	struct unit_test_state *uts;
 	void *fdt_base = &__dtb_test_fdt_base_begin;
 	void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
test/str_ut.c +2 -3
index cd5045516d1..359d7d4ea1f 100644
@@ -107,9 +107,8 @@ STR_TEST(str_simple_strtoul, 0);
 
 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test,
-						 str_test);
-	const int n_ents = ll_entry_count(struct unit_test, str_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
 
 	return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
 }
test/unicode_ut.c +2 -2
index 6130ef0b549..7b9c020eff9 100644
@@ -615,8 +615,8 @@ UNICODE_TEST(unicode_test_efi_create_indexed_name);
 
 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
-	const int n_ents = ll_entry_count(struct unit_test, unicode_test);
+	struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test);
 
 	return cmd_ut_category("Unicode", "unicode_test_",
 			       tests, n_ents, argc, argv);
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#39 lore raw

PATCH v5 35/41 test: Rename all linker lists to have a ut_ prefix

In reply to Simon Glass

At present each test suite has its own portion of the linker_list section of the image, but other lists are interspersed. This makes it hard to enumerate all the available tests without knowing the suites that each one is in.

Place all tests together in a single contiguous list by giving them common prefix not used elsewhere in U-Boot. This makes it possible to find the start and end of all tests.

---

(no changes since v3)

Changes in v3:
- Update the pytest collector as well

 include/test/test.h | 8 ++++----
 test/py/conftest.py | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
include/test/test.h +4 -4
index b16c9135f2c..3330dcc72d3 100644
@@ -86,18 +86,18 @@ struct unit_test {
  * @_suite:	name of the test suite concatenated with "_test"
  */
 #define UNIT_TEST(_name, _flags, _suite)				\
-	ll_entry_declare(struct unit_test, _name, _suite) = {		\
+	ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = {	\
 		.file = __FILE__,					\
 		.name = #_name,						\
 		.flags = _flags,					\
 		.func = _name,						\
 	}
 
-/* Get the start of a list of unit tests for a particular category */
+/* Get the start of a list of unit tests for a particular suite */
 #define UNIT_TEST_SUITE_START(_suite) \
-	ll_entry_start(struct unit_test, _suite)
+	ll_entry_start(struct unit_test, ut_ ## _suite)
 #define UNIT_TEST_SUITE_COUNT(_suite) \
-	ll_entry_count(struct unit_test, _suite)
+	ll_entry_count(struct unit_test, ut_ ## _suite)
 
 /* Sizes for devres tests */
 enum {
test/py/conftest.py +1 -1
index 9bfd9263455..1b909cde9d3 100644
@@ -226,7 +226,7 @@ def pytest_configure(config):
         import u_boot_console_exec_attach
         console = u_boot_console_exec_attach.ConsoleExecAttach(log, ubconfig)
 
-re_ut_test_list = re.compile(r'_u_boot_list_2_(.*)_test_2_\1_test_(.*)\s*$')
+re_ut_test_list = re.compile(r'_u_boot_list_2_ut_(.*)_test_2_\1_test_(.*)\s*$')
 def generate_ut_subtest(metafunc, fixture_name, sym_path):
     """Provide parametrization for a ut_subtest fixture.
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#40 lore raw

PATCH v5 36/41 test: Allow SPL to run any available test

In reply to Simon Glass

At present SPL only runs driver model tests. Update it to run all available tests, i.e. in any test suite.

---

(no changes since v1)

 arch/sandbox/cpu/spl.c |  7 +++++--
 include/test/test.h    | 16 +++++-----------
 test/dm/test-dm.c      | 11 ++++++++++-
 test/test-main.c       | 17 +++++++++++++----
 4 files changed, 33 insertions(+), 18 deletions(-)
arch/sandbox/cpu/spl.c +5 -2
index 3779d58c3fe..ce5aae87fb6 100644
@@ -13,7 +13,7 @@
 #include <asm/global_data.h>
 #include <asm/spl.h>
 #include <asm/state.h>
-#include <test/test.h>
+#include <test/ut.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -63,9 +63,12 @@ void spl_board_init(void)
 	preloader_console_init();
 
 	if (state->run_unittests) {
+		struct unit_test *tests = UNIT_TEST_ALL_START();
+		const int count = UNIT_TEST_ALL_COUNT();
 		int ret;
 
-		ret = dm_test_run(state->select_unittests);
+		ret = ut_run_list("spl", NULL, tests, count,
+				  state->select_unittests);
 		/* continue execution into U-Boot */
 	}
 }
include/test/test.h +5 -11
index 3330dcc72d3..0b124edd601 100644
@@ -99,6 +99,11 @@ struct unit_test {
 #define UNIT_TEST_SUITE_COUNT(_suite) \
 	ll_entry_count(struct unit_test, ut_ ## _suite)
 
+/* Use ! and ~ so that all tests will be sorted between these two values */
+#define UNIT_TEST_ALL_START()	ll_entry_start(struct unit_test, ut_!)
+#define UNIT_TEST_ALL_END()	ll_entry_start(struct unit_test, ut_~)
+#define UNIT_TEST_ALL_COUNT()	(UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
+
 /* Sizes for devres tests */
 enum {
 	TEST_DEVRES_SIZE	= 100,
@@ -119,15 +124,4 @@ enum {
  */
 struct udevice *testbus_get_clear_removed(void);
 
-/**
- * dm_test_run() - 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_run(const char *test_name);
-
 #endif /* __TEST_TEST_H */
test/dm/test-dm.c +10 -1
index 729becf57b2..9ba2ba23d5d 100644
@@ -20,7 +20,16 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int dm_test_run(const char *test_name)
+/**
+ * dm_test_run() - 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
+ */
+static int dm_test_run(const char *test_name)
 {
 	struct unit_test *tests = UNIT_TEST_SUITE_START(dm_test);
 	const int n_ents = UNIT_TEST_SUITE_COUNT(dm_test);
test/test-main.c +13 -4
index 6edd49f0b61..e1b49e091ab 100644
@@ -119,7 +119,8 @@ static bool ut_test_run_on_flattree(struct unit_test *test)
  *
  * @prefix: String prefix for the tests. Any tests that have this prefix will be
  *	printed without the prefix, so that it is easier to see the unique part
- *	of the test name. If NULL, no prefix processing is done
+ *	of the test name. If NULL, any suite name (xxx_test) is considered to be
+ *	a prefix.
  * @test_name: Name of current test
  * @select_name: Name of test to run (or NULL for all)
  * @return true to run this test, false to skip it
@@ -133,9 +134,17 @@ static bool test_matches(const char *prefix, const char *test_name,
 	if (!strcmp(test_name, select_name))
 		return true;
 
-	/* All tests have this prefix */
-	if (prefix && !strncmp(test_name, prefix, strlen(prefix)))
-		test_name += strlen(prefix);
+	if (!prefix) {
+		const char *p = strstr(test_name, "_test_");
+
+		/* convert xxx_test_yyy to yyy, i.e. remove the suite name */
+		if (p)
+			test_name = p + 6;
+	} else {
+		/* All tests have this prefix */
+		if (!strncmp(test_name, prefix, strlen(prefix)))
+			test_name += strlen(prefix);
+	}
 
 	if (!strcmp(test_name, select_name))
 		return true;
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#41 lore raw

PATCH v5 37/41 sandbox: Update os_find_u_boot() to find the .img file

In reply to Simon Glass

At present this function can only locate the u-boot ELF file. For SPL it is handy to be able to locate u-boot.img since this is what would normally be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL, since that is the next stage.

---

(no changes since v1)

 arch/sandbox/cpu/os.c  | 8 +++++---
 arch/sandbox/cpu/spl.c | 2 +-
 include/os.h           | 3 ++-
 3 files changed, 8 insertions(+), 5 deletions(-)
arch/sandbox/cpu/os.c +5 -3
index f5000e64966..2d9583c17ca 100644
@@ -777,7 +777,7 @@ int os_jump_to_image(const void *dest, int size)
 	return os_jump_to_file(fname);
 }
 
-int os_find_u_boot(char *fname, int maxlen)
+int os_find_u_boot(char *fname, int maxlen, bool use_img)
 {
 	struct sandbox_state *state = state_get_current();
 	const char *progname = state->argv[0];
@@ -801,8 +801,8 @@ int os_find_u_boot(char *fname, int maxlen)
 			return 0;
 		}
 
-		/* Look for 'u-boot-tpl' in the tpl/ directory */
-		p = strstr(fname, "/tpl/");
+		/* Look for 'u-boot-spl' in the spl/ directory */
+		p = strstr(fname, "/spl/");
 		if (p) {
 			p[1] = 's';
 			fd = os_open(fname, O_RDONLY);
@@ -829,6 +829,8 @@ int os_find_u_boot(char *fname, int maxlen)
 	if (p) {
 		/* Remove the "spl" characters */
 		memmove(p, p + 4, strlen(p + 4) + 1);
+		if (use_img)
+			strcat(p, ".img");
 		fd = os_open(fname, O_RDONLY);
 		if (fd >= 0) {
 			close(fd);
arch/sandbox/cpu/spl.c +1 -1
index ce5aae87fb6..f82b0d3de16 100644
@@ -37,7 +37,7 @@ static int spl_board_load_image(struct spl_image_info *spl_image,
 	char fname[256];
 	int ret;
 
-	ret = os_find_u_boot(fname, sizeof(fname));
+	ret = os_find_u_boot(fname, sizeof(fname), false);
 	if (ret) {
 		printf("(%s not found, error %d)\n", fname, ret);
 		return ret;
include/os.h +2 -1
index d2a4afeca0f..77d8bd89d0f 100644
@@ -324,9 +324,10 @@ int os_jump_to_image(const void *dest, int size);
  *
  * @fname:	place to put full path to U-Boot
  * @maxlen:	maximum size of @fname
+ * @use_img:	select the 'u-boot.img' file instead of the 'u-boot' ELF file
  * Return:	0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
  */
-int os_find_u_boot(char *fname, int maxlen);
+int os_find_u_boot(char *fname, int maxlen, bool use_img);
 
 /**
  * os_spl_to_uboot() - Run U-Boot proper
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#42 lore raw

PATCH v5 38/41 spl: Convert spl_fit to work with sandbox

In reply to Simon Glass

At present this casts addresses to pointers so cannot work with sandbox. Update the code to use map_sysmem() instead.

As part of this change, the existing load_ptr is renamed to src_ptr since it is not a pointer to load_addr. It is confusing to use a similar name for something that is not actually related. For the alignment code, ALIGN() is used instead of open-coded alignment. Add a comment to the line that casts away a const.

Use a (new) load_ptr variable to access memory at address load_addr.

---

(no changes since v1)

 common/spl/spl.c     |  3 ++-
 common/spl/spl_fit.c | 27 +++++++++++++++------------
 2 files changed, 17 insertions(+), 13 deletions(-)
common/spl/spl.c +2 -1
index bb91b761fc7..5f51098a88f 100644
@@ -16,6 +16,7 @@
 #include <init.h>
 #include <irq_func.h>
 #include <log.h>
+#include <mapmem.h>
 #include <serial.h>
 #include <spl.h>
 #include <asm/global_data.h>
@@ -168,7 +169,7 @@ __weak void spl_board_prepare_for_boot(void)
 
 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
 {
-	return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
+	return map_sysmem(CONFIG_SYS_TEXT_BASE + offset, 0);
 }
 
 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
common/spl/spl_fit.c +15 -12
index 75c8ff065bb..49508fc5180 100644
@@ -11,6 +11,7 @@
 #include <image.h>
 #include <log.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <spl.h>
 #include <sysinfo.h>
 #include <asm/cache.h>
@@ -235,11 +236,11 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
 	size_t length;
 	int len;
 	ulong size;
-	ulong load_addr, load_ptr;
+	ulong load_addr;
+	void *load_ptr;
 	void *src;
 	ulong overhead;
 	int nr_sectors;
-	int align_len = ARCH_DMA_MINALIGN - 1;
 	uint8_t image_comp = -1, type = -1;
 	const void *data;
 	const void *fit = ctx->fit;
@@ -269,11 +270,13 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
 	}
 
 	if (external_data) {
+		void *src_ptr;
+
 		/* External data */
 		if (fit_image_get_data_size(fit, node, &len))
 			return -ENOENT;
 
-		load_ptr = (load_addr + align_len) & ~align_len;
+		src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
 		length = len;
 
 		overhead = get_aligned_image_overhead(info, offset);
@@ -281,12 +284,12 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
 
 		if (info->read(info,
 			       sector + get_aligned_image_offset(info, offset),
-			       nr_sectors, (void *)load_ptr) != nr_sectors)
+			       nr_sectors, src_ptr) != nr_sectors)
 			return -EIO;
 
-		debug("External data: dst=%lx, offset=%x, size=%lx\n",
-		      load_ptr, offset, (unsigned long)length);
-		src = (void *)load_ptr + overhead;
+		debug("External data: dst=%p, offset=%x, size=%lx\n",
+		      src_ptr, offset, (unsigned long)length);
+		src = src_ptr + overhead;
 	} else {
 		/* Embedded data */
 		if (fit_image_get_data(fit, node, &data, &length)) {
@@ -295,7 +298,7 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
 		}
 		debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
 		      (unsigned long)length);
-		src = (void *)data;
+		src = (void *)data;	/* cast away const */
 	}
 
 	if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
@@ -309,16 +312,16 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
 	if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
 		board_fit_image_post_process(&src, &length);
 
+	load_ptr = map_sysmem(load_addr, length);
 	if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
 		size = length;
-		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
-			   src, &size)) {
+		if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
 			puts("Uncompressing error\n");
 			return -EIO;
 		}
 		length = size;
 	} else {
-		memcpy((void *)load_addr, src, length);
+		memcpy(load_ptr, src, length);
 	}
 
 	if (image_info) {
@@ -383,7 +386,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
 	}
 
 	/* Make the load-address of the FDT available for the SPL framework */
-	spl_image->fdt_addr = (void *)image_info.load_addr;
+	spl_image->fdt_addr = map_sysmem(image_info.load_addr, 0);
 	if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
 		return 0;
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#43 lore raw

PATCH v5 39/41 spl: test: Add a test for spl_load_simple_fit()

In reply to Simon Glass

As an example of an SPL test, add a new test for loading a FIT within SPL. This runs on sandbox_spl. For this to work, the text base is adjusted so that there is plenty of space available.

While we are here, document struct spl_load_info properly, since this is currently ambiguous.

This test only verifies the logic path. It does not actually check that the image is loaded correctly. It is not possible for sandbox's SPL to actually run u-boot.img since it currently includes u-boot.bin rather than u-boot. Further work could expand the test in that direction.

The need for this was noted at:

http://patchwork.ozlabs.org/project/uboot/patch/20201216000944.2832585-3-mr.nuke.me at gmail.com/

---

(no changes since v1)

 configs/sandbox_spl_defconfig |  2 +-
 doc/arch/sandbox.rst          |  4 +-
 include/spl.h                 |  9 ++++
 test/Makefile                 |  1 +
 test/image/Makefile           |  5 ++
 test/image/spl_load.c         | 91 +++++++++++++++++++++++++++++++++++
 6 files changed, 110 insertions(+), 2 deletions(-)
 create mode 100644 test/image/Makefile
 create mode 100644 test/image/spl_load.c
configs/sandbox_spl_defconfig +1 -1
index c0118702a81..2696d0b6cdd 100644
@@ -1,4 +1,4 @@
-CONFIG_SYS_TEXT_BASE=0
+CONFIG_SYS_TEXT_BASE=0x200000
 CONFIG_SPL_LIBCOMMON_SUPPORT=y
 CONFIG_SPL_LIBGENERIC_SUPPORT=y
 CONFIG_NR_DRAM_BANKS=1
doc/arch/sandbox.rst +3 -1
index 60ee1e0741a..1ccb5344ac6 100644
@@ -537,5 +537,7 @@ Addr      Config                     Usage
    e000   CONFIG_BLOBLIST_ADDR       Blob list
   10000   CONFIG_MALLOC_F_ADDR       Early memory allocation
   f0000   CONFIG_PRE_CON_BUF_ADDR    Pre-console buffer
- 100000   CONFIG_TRACE_EARLY_ADDR    Early trace buffer (if enabled)
+ 100000   CONFIG_TRACE_EARLY_ADDR    Early trace buffer (if enabled). Also used
+                                     as the SPL load buffer in spl_test_load().
+ 200000   CONFIG_SYS_TEXT_BASE       Load buffer for U-Boot (sandbox_spl only)
 =======   ========================   ===============================
include/spl.h +9 -0
index 0d134587de2..4f6e0e53f5d 100644
@@ -222,6 +222,15 @@ struct spl_load_info {
 	void *priv;
 	int bl_len;
 	const char *filename;
+	/**
+	 * read() - Read from device
+	 *
+	 * @load: Information about the load state
+	 * @sector: Sector number to read from (each @load->bl_len bytes)
+	 * @count: Number of sectors to read
+	 * @buf: Buffer to read into
+	 * @return number of sectors read, 0 on error
+	 */
 	ulong (*read)(struct spl_load_info *load, ulong sector, ulong count,
 		      void *buf);
 };
test/Makefile +1 -0
index 5cd284e322e..a26e915e050 100644
@@ -3,6 +3,7 @@
 # (C) Copyright 2012 The Chromium Authors
 
 obj-y += test-main.o
+obj-$(CONFIG_SANDBOX) += image/
 
 ifneq ($(CONFIG_$(SPL_)BLOBLIST),)
 obj-$(CONFIG_$(SPL_)CMDLINE) += bloblist.o
test/image/Makefile new file +5 -0
new file mode 100644
index 00000000000..c4039df707f
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2021 Google LLC
+
+obj-$(CONFIG_SPL_BUILD) += spl_load.o
test/image/spl_load.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#44 lore raw

PATCH v5 40/41 test: sandbox: Move sandbox test docs into doc/develop

In reply to Simon Glass

At present some of the documentation about running sandbox tests is in the sandbox docs. It makes more sense to put it in with the other testing docs, with a link there from sandbox. Update the documentation accordingly.

Also add a paragraph explaining why sandbox exists and the test philosophy that it uses.

---

(no changes since v1)

 doc/arch/sandbox.rst          | 44 +++++++----------------------------
 doc/develop/tests_sandbox.rst | 44 +++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 37 deletions(-)
doc/arch/sandbox.rst +9 -35
index 1ccb5344ac6..1638b050002 100644
@@ -17,6 +17,12 @@ of the sandbox U-Boot. The purpose of running U-Boot under Linux is to test
 all the generic code, not specific to any one architecture. The idea is to
 create unit tests which we can run to test this upper level code.
 
+Sandbox allows development of many types of new features in a traditional way,
+rather than needing to test each iteration on real hardware. Many U-Boot
+features were developed on sandbox, including the core driver model, most
+uclasses, verified boot, bloblist, logging and dozens of others. Sandbox has
+enabled many large-scale code refactors as well.
+
 CONFIG_SANDBOX is defined when building a native board.
 
 The board name is 'sandbox' but the vendor name is unset, so there is a
@@ -486,42 +492,10 @@ Testing
 -------
 
 U-Boot sandbox can be used to run various tests, mostly in the test/
-directory. These include:
-
-command_ut:
-  Unit tests for command parsing and handling
-compression:
-  Unit tests for U-Boot's compression algorithms, useful for
-  security checking. It supports gzip, bzip2, lzma and lzo.
-driver model:
-  Run this pytest::
-
-   ./test/py/test.py --bd sandbox --build -k ut_dm -v
-
-image:
-  Unit tests for images:
-  test/image/test-imagetools.sh - multi-file images
-  test/image/test-fit.py        - FIT images
-tracing:
-  test/trace/test-trace.sh tests the tracing system (see README.trace)
-verified boot:
-  See test/vboot/vboot_test.sh for this
-
-If you change or enhance any of the above subsystems, you shold write or
-expand a test and include it with your patch series submission. Test
-coverage in U-Boot is limited, as we need to work to improve it.
-
-Note that many of these tests are implemented as commands which you can
-run natively on your board if desired (and enabled).
-
-To run all tests use "make check".
-
-To run a single test in an existing sandbox build, you can use -T to use the
-test device tree, and -c to select the test:
-
-  /tmp/b/sandbox/u-boot -T -c "ut dm pci_busdev"
+directory.
 
-This runs dm_test_pci_busdev() which is in test/dm/pci.c
+See :doc:`../develop/tests_sandbox` for more information and
+:doc:`../develop/testing` for information about testing generally.
 
 
 Memory Map
doc/develop/tests_sandbox.rst +42 -2

49 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#45 lore raw

PATCH v5 41/41 doc: Explain briefly how to write new tests

In reply to Simon Glass

Add a second on writing tests, covering when to use Python and C, where to put the tests, etc. Add a link to the existing Python test documentation.

---

Changes in v5:
- Mention UEFI tests
- Update language to make it clear when sandbox is discussed
- Update table based on Heinrich's comments

Changes in v4:
- Add correct SPDX header

Changes in v2:
- Add new patches to cover running an SPL test

 doc/develop/index.rst         |   1 +
 doc/develop/py_testing.rst    |   3 +-
 doc/develop/testing.rst       |   2 +
 doc/develop/tests_sandbox.rst |   7 +
 doc/develop/tests_writing.rst | 346 ++++++++++++++++++++++++++++++++++
 5 files changed, 358 insertions(+), 1 deletion(-)
 create mode 100644 doc/develop/tests_writing.rst
doc/develop/index.rst +1 -0
index 50b1de3bdff..41c0ba1ebd9 100644
@@ -33,4 +33,5 @@ Testing
    coccinelle
    testing
    py_testing
+   tests_writing
    tests_sandbox
doc/develop/py_testing.rst +2 -1
index 7f01858cfda..c4cecc0a01b 100644
@@ -13,7 +13,8 @@ results. Advantages of this approach are:
   U-Boot; there can be no disconnect.
 - There is no need to write or embed test-related code into U-Boot itself.
   It is asserted that writing test-related code in Python is simpler and more
-  flexible than writing it all in C.
+  flexible than writing it all in C. But see :doc:`tests_writing` for caveats
+  and more discussion / analysis.
 - It is reasonably simple to interact with U-Boot in this way.
 
 Requirements
doc/develop/testing.rst +2 -0
index b181c2e2e41..ced13ac8bb4 100644
@@ -117,6 +117,8 @@ or is covered sparingly. So here are some suggestions:
   is much easier to add onto a test - writing a new large test can seem
   daunting to most contributors.
 
+See doc:`tests_writing` for how to write tests.
+
 
 Future work
 -----------
doc/develop/tests_sandbox.rst +7 -0
index dd15692f656..84608dcb840 100644
@@ -200,3 +200,10 @@ linker_list::
    000000000001f240 D _u_boot_list_2_dm_test_2_dm_test_of_plat_parent
    000000000001f260 D _u_boot_list_2_dm_test_2_dm_test_of_plat_phandle
    000000000001f280 D _u_boot_list_2_dm_test_2_dm_test_of_plat_props
+
+
+Writing tests
+-------------
+
+See :doc:`tests_writing` for how to write new tests.
+
doc/develop/tests_writing.rst new file +346 -0

346 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#46 lore raw

v5 cover test: Refactor tests to have a single test runner

In reply to Simon Glass
Quoted 21 lines · Simon Glass

At present U-Boot has two broad sets of tests in the C code: driver model tests which do a lot of pre-/post-init and command tests which do not.

This separation makes it slightly harder to write a test, since there are two different test-state structures and different rules for running the two different test types. At present these rules are determined by where the test is (actually its prefix).

All unit tests can be run from the command line with the 'ut' command. Since SPL does not have commands, it currently calls the test runner directly and offers no control of which tests are run.

This seems like a good time to refactor the tests into a unified test runner, allowing U-Boot proper and SPL to use the same path, perhaps with some different conditions along the way.

This series sets up a unified runner called ut_run_list(), which runs a set of tests from a linker_list. Driver model tests are distinguished by a new UT_TESTF_DM flag so that the necessary init and cleanup can still be done.

Quoted 5 lines

The runner is modified to support running SPL tests that are not solely for driver model. An example test for FIT loading is added as a demonstration.

In addition, some documentation is added to explain how to write tests.

For the series, applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210312/912e4bca/attachment.sig>