All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 00/42] test: Refactor tests to have a single test runner
@ 2021-03-04 13:50 Simon Glass
  2021-03-04 13:50 ` [PATCH v4 01/42] dm: core: Fix allocation of empty of-platdata Simon Glass
                   ` (41 more replies)
  0 siblings, 42 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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 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 (42):
  dm: core: Fix allocation of empty of-platdata
  doc: Tidy up testing section
  doc: Document make tcheck
  sandbox: Drop the 'starting...' message unless testing
  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
  doc: Move coccinelle into its own section
  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           |  15 +-
 arch/sandbox/include/asm/state.h   |   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              |  11 +-
 doc/develop/py_testing.rst         |   3 +-
 doc/develop/testing.rst            |  46 +++-
 doc/develop/tests_sandbox.rst      | 208 ++++++++++++++
 doc/develop/tests_writing.rst      | 339 +++++++++++++++++++++++
 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          |   2 +-
 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 +
 47 files changed, 1422 insertions(+), 448 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

-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 01/42] dm: core: Fix allocation of empty of-platdata
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 02/42] doc: Tidy up testing section Simon Glass
                   ` (40 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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")
Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 drivers/core/device.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index 625134921d6..d1098a3861a 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -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);
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 02/42] doc: Tidy up testing section
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
  2021-03-04 13:50 ` [PATCH v4 01/42] dm: core: Fix allocation of empty of-platdata Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 15:32   ` Heinrich Schuchardt
  2021-03-04 13:50 ` [PATCH v4 03/42] doc: Document make tcheck Simon Glass
                   ` (39 subsequent siblings)
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

Tweak this so the output looks a little better.

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

(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(-)

diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
index 4bc9ca3a6ae..bc74eb53e35 100644
--- a/doc/develop/testing.rst
+++ b/doc/develop/testing.rst
@@ -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.
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 03/42] doc: Document make tcheck
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
  2021-03-04 13:50 ` [PATCH v4 01/42] dm: core: Fix allocation of empty of-platdata Simon Glass
  2021-03-04 13:50 ` [PATCH v4 02/42] doc: Tidy up testing section Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 04/42] sandbox: Drop the 'starting...' message unless testing Simon Glass
                   ` (38 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
index bc74eb53e35..f01ca4dc408 100644
--- a/doc/develop/testing.rst
+++ b/doc/develop/testing.rst
@@ -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
 -------
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 04/42] sandbox: Drop the 'starting...' message unless testing
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (2 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 03/42] doc: Document make tcheck Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 15:38   ` Heinrich Schuchardt
  2021-03-04 13:50 ` [PATCH v4 05/42] test: Re-enable test_ofplatdata Simon Glass
                   ` (37 subsequent siblings)
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

This message is annoying since it is only useful for testing. Add a flag
to control whether it is emitted or not, so it can be supressed by
default.

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

(no changes since v1)

 arch/sandbox/cpu/start.c         | 15 +++++++++++++--
 arch/sandbox/include/asm/state.h |  1 +
 test/py/tests/test_log.py        |  2 +-
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index c4c4128d465..3dcab6a0cc8 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -389,6 +389,15 @@ static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
 }
 SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
 
+static int sandbox_cmdline_cb_log_test(struct sandbox_state *state,
+				       const char *arg)
+{
+	state->log_test = true;
+
+	return 0;
+}
+SANDBOX_CMDLINE_OPT(log_test, 0, "Show logging info on startup (for testing)");
+
 static void setup_ram_buf(struct sandbox_state *state)
 {
 	/* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
@@ -491,8 +500,10 @@ 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__);
+	if (state->log_test) {
+		log_info("sandbox: starting...\n");
+		log_debug("debug: %s\n", __func__);
+	}
 
 	/* Do pre- and post-relocation init */
 	board_init_f(0);
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index bca13069824..d0e7fdbd5b0 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -105,6 +105,7 @@ struct sandbox_state {
 	struct list_head mapmem_head;	/* struct sandbox_mapmem_entry */
 	bool hwspinlock;		/* Hardware Spinlock status */
 	bool allow_memio;		/* Allow readl() etc. to work */
+	bool log_test;			/* Output some log info for testing */
 
 	/*
 	 * This struct is getting large.
diff --git a/test/py/tests/test_log.py b/test/py/tests/test_log.py
index f889120f2b3..e2955ab0770 100644
--- a/test/py/tests/test_log.py
+++ b/test/py/tests/test_log.py
@@ -43,7 +43,7 @@ def test_log_dropped(u_boot_console):
     """Test dropped 'log' message when debug_uart is activated"""
 
     cons = u_boot_console
-    cons.restart_uboot()
+    cons.restart_uboot_with_flags(['--log_test'])
     output = cons.get_spawn_output().replace('\r', '')
     assert 'sandbox: starting...' in output
     assert (not 'debug: main' in output)
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 05/42] test: Re-enable test_ofplatdata
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (3 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 04/42] sandbox: Drop the 'starting...' message unless testing Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 06/42] doc: Explain how to run tests without pytest Simon Glass
                   ` (36 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/test/py/tests/test_ofplatdata.py b/test/py/tests/test_ofplatdata.py
index 92d09b7aa19..e9cce4daf48 100644
--- a/test/py/tests/test_ofplatdata.py
+++ b/test/py/tests/test_ofplatdata.py
@@ -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"""
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 06/42] doc: Explain how to run tests without pytest
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (4 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 05/42] test: Re-enable test_ofplatdata Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 16:13   ` Heinrich Schuchardt
  2021-03-04 13:50 ` [PATCH v4 07/42] doc: Document how sandbox_spl_tests are run Simon Glass
                   ` (35 subsequent siblings)
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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.

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

(no changes since v2)

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 | 79 +++++++++++++++++++++++++++++++++++
 3 files changed, 89 insertions(+)
 create mode 100644 doc/develop/tests_sandbox.rst

diff --git a/doc/develop/index.rst b/doc/develop/index.rst
index ac57fdb8f30..50b1de3bdff 100644
--- a/doc/develop/index.rst
+++ b/doc/develop/index.rst
@@ -33,3 +33,4 @@ Testing
    coccinelle
    testing
    py_testing
+   tests_sandbox
diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
index f01ca4dc408..87c90eee271 100644
--- a/doc/develop/testing.rst
+++ b/doc/develop/testing.rst
@@ -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
 ----
diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
new file mode 100644
index 00000000000..85bbd4f6734
--- /dev/null
+++ b/doc/develop/tests_sandbox.rst
@@ -0,0 +1,79 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+Tests Under the Hood
+====================
+
+Running sandbox tests directly
+------------------------------
+
+Typically tests are run using the pytest suite. This is easy and always gets
+things right.
+
+But it is also possible to run some sandbox tests directly. For example, this
+runs the dm_test_gpio() test which you can find in test/dm/gpio.c::
+
+   $ ./u-boot -T -c "ut dm gpio"
+
+
+   U-Boot 2021.01
+
+   Model: sandbox
+   DRAM:  128 MiB
+   WDT:   Started with servicing (60s timeout)
+   MMC:   mmc2: 2 (SD), mmc1: 1 (SD), mmc0: 0 (SD)
+   In:    serial
+   Out:   vidconsole
+   Err:   vidconsole
+   Model: sandbox
+   SCSI:
+   Net:   eth0: eth at 10002000, eth5: eth at 10003000, eth3: sbe5, eth6: eth at 10004000
+   Test: dm_test_gpio: gpio.c
+   Test: dm_test_gpio: gpio.c (flat tree)
+   Failures: 0
+
+The -T option tells U-Boot to run with the 'test' devicetree (test.dts) instead
+of -D which selects the normal sandbox.dts - this is necessary because many
+tests rely on things in the test devicetree. If you try running tests without
+-T then you may see failures, like::
+
+   $ ./u-boot -c "ut dm gpio"
+
+
+   U-Boot 2021.01
+
+   DRAM:  128 MiB
+   WDT:   Not found!
+   MMC:
+   In:    serial
+   Out:   serial
+   Err:   serial
+   SCSI:
+   Net:   No ethernet found.
+   Please run with test device tree:
+       ./u-boot -d arch/sandbox/dts/test.dtb
+   Test: dm_test_gpio: gpio.c
+   test/dm/gpio.c:37, dm_test_gpio(): 0 == gpio_lookup_name("b4", &dev, &offset, &gpio): Expected 0x0 (0), got 0xffffffea (-22)
+   Test: dm_test_gpio: gpio.c (flat tree)
+   test/dm/gpio.c:37, dm_test_gpio(): 0 == gpio_lookup_name("b4", &dev, &offset, &gpio): Expected 0x0 (0), got 0xffffffea (-22)
+   Failures: 2
+
+The message above should provide a hint if you forget. Even running with -D
+will produce different results.
+
+You can easily use gdb on these tests, without needing --gdbserver::
+
+   $ gdb u-boot --args -T -c "ut dm gpio"
+   ...
+   (gdb) break dm_test_gpio
+   Breakpoint 1 at 0x1415bd: file test/dm/gpio.c, line 37.
+   (gdb) run -T -c "ut dm gpio"
+   Starting program: u-boot -T -c "ut dm gpio"
+   Test: dm_test_gpio: gpio.c
+
+   Breakpoint 1, dm_test_gpio (uts=0x5555558029a0 <global_dm_test_state>)
+       at files/test/dm/gpio.c:37
+   37		ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
+   (gdb)
+
+You can then single-step and look at variables as needed.
+
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 07/42] doc: Document how sandbox_spl_tests are run
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (5 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 06/42] doc: Explain how to run tests without pytest Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 08/42] test: Correct setexpr test prefix Simon Glass
                   ` (34 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Pratyush Yadav <p.yadav@ti.com>
---

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(+)

diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
index 87c90eee271..b181c2e2e41 100644
--- a/doc/develop/testing.rst
+++ b/doc/develop/testing.rst
@@ -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).
diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
index 85bbd4f6734..daf87e53a99 100644
--- a/doc/develop/tests_sandbox.rst
+++ b/doc/develop/tests_sandbox.rst
@@ -77,3 +77,85 @@ You can easily use gdb on these tests, without needing --gdbserver::
 
 You can then single-step and look at variables as needed.
 
+
+Running sandbox_spl tests directly
+----------------------------------
+
+SPL is the phase before U-Boot proper. It is present in the sandbox_spl build,
+so you can run SPL like this::
+
+   ./spl/u-boot-spl
+
+SPL tests are special in that they run (only in the SPL phase, of course) if the
+-u flag is given::
+
+   ./spl/u-boot-spl -u
+
+   U-Boot SPL 2021.01-00723-g43c77b51be5-dirty (Jan 24 2021 - 16:38:24 -0700)
+   Running 5 driver model tests
+   Test: dm_test_of_plat_base: of_platdata.c (flat tree)
+   Test: dm_test_of_plat_dev: of_platdata.c (flat tree)
+   Test: dm_test_of_plat_parent: of_platdata.c (flat tree)
+   Test: dm_test_of_plat_phandle: of_platdata.c (flat tree)
+   Test: dm_test_of_plat_props: of_platdata.c (flat tree)
+   Failures: 0
+
+
+   U-Boot 2021.01-00723-g43c77b51be5-dirty (Jan 24 2021 - 16:38:24 -0700)
+
+   DRAM:  128 MiB
+   ...
+
+It is not possible to run SPL tests in U-Boot proper, firstly because they are
+not built into U-Boot proper and secondly because the environment is very
+different, e.g. some SPL tests rely on of-platdata which is only available in
+SPL.
+
+Note that after running, SPL continues to boot into U-Boot proper. You can add
+'-c exit' to make U-Boot quit without doing anything further. It is not
+currently possible to run SPL tests and then stop, since the pytests require
+that U-Boot produces the expected banner.
+
+You can use the -k flag to select which tests run::
+
+   ./spl/u-boot-spl -u -k dm_test_of_plat_parent
+
+Of course you can use gdb with sandbox_spl, just as with sandbox.
+
+
+Running all tests directly
+--------------------------
+
+A fast way to run all sandbox tests is::
+
+   ./u-boot -T -c "ut all"
+
+It typically runs single-thread in 6 seconds on 2021 hardware, with 2s of that
+to the delays in the time test.
+
+This should not be considered a substitute for 'make check', but can be helpful
+for git bisect, etc.
+
+
+What tests are built in?
+------------------------
+
+Whatever sandbox build is used, which tests are present is determined by which
+source files are built. For sandbox_spl, the of_platdata tests are built
+because of the build rule in test/dm/Makefile::
+
+   ifeq ($(CONFIG_SPL_BUILD),y)
+   obj-$(CONFIG_SPL_OF_PLATDATA) += of_platdata.o
+   else
+   ...other tests for non-spl
+   endif
+
+You can get a list of tests in a U-Boot ELF file by looking for the
+linker_list::
+
+   $ nm /tmp/b/sandbox_spl/spl/u-boot-spl |grep 2_dm_test
+   000000000001f200 D _u_boot_list_2_dm_test_2_dm_test_of_plat_base
+   000000000001f220 D _u_boot_list_2_dm_test_2_dm_test_of_plat_dev
+   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
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 08/42] test: Correct setexpr test prefix
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (6 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 07/42] doc: Document how sandbox_spl_tests are run Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 09/42] test: Mark all driver model tests with a flag Simon Glass
                   ` (33 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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().

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

(no changes since v1)

 test/cmd/setexpr.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
index fd6d869c0ed..b483069ff0f 100644
--- a/test/cmd/setexpr.c
+++ b/test/cmd/setexpr.c
@@ -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);
 }
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 09/42] test: Mark all driver model tests with a flag
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (7 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 08/42] test: Correct setexpr test prefix Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 10/42] test: Rename test-main.c to test-dm.c Simon Glass
                   ` (32 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(no changes since v1)

 include/dm/test.h   | 3 ++-
 include/test/test.h | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/dm/test.h b/include/dm/test.h
index 6ac6672cd6f..dfbc82c756d 100644
--- a/include/dm/test.h
+++ b/include/dm/test.h
@@ -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
diff --git a/include/test/test.h b/include/test/test.h
index 3fdaa2b5e51..27585507d8c 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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),
 };
 
 /**
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 10/42] test: Rename test-main.c to test-dm.c
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (8 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 09/42] test: Mark all driver model tests with a flag Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 11/42] test: Add an overall test runner Simon Glass
                   ` (31 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(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%)

diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index 6926e244ca2..3779d58c3fe 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -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 */
 	}
 }
diff --git a/include/test/test.h b/include/test/test.h
index 27585507d8c..d282cb2362d 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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 */
diff --git a/test/dm/Makefile b/test/dm/Makefile
index fd1455109d4..f5cc5540e8a 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -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.
diff --git a/test/dm/test-main.c b/test/dm/test-dm.c
similarity index 98%
rename from test/dm/test-main.c
rename to test/dm/test-dm.c
index 560f8d63ec6..71e9cf6e5da 100644
--- a/test/dm/test-main.c
+++ b/test/dm/test-dm.c
@@ -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);
 }
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 11/42] test: Add an overall test runner
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (9 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 10/42] test: Rename test-main.c to test-dm.c Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 12/42] test: Create pre/post-run functions Simon Glass
                   ` (30 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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.

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

(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

diff --git a/include/test/ut.h b/include/test/ut.h
index 17400c73ea9..88e75ab791c 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
diff --git a/test/Makefile b/test/Makefile
index 932e5173831..5cd284e322e 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -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
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 8f3089890ea..157f6aa9767 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -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[] = {
diff --git a/test/test-main.c b/test/test-main.c
new file mode 100644
index 00000000000..376e7ebd3d2
--- /dev/null
+++ b/test/test-main.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <console.h>
+#include <test/test.h>
+
+int ut_run_tests(struct unit_test_state *uts, const char *prefix,
+		 struct unit_test *tests, int count, const char *select_name)
+{
+	struct unit_test *test;
+	int prefix_len = prefix ? strlen(prefix) : 0;
+	int found = 0;
+
+	for (test = tests; test < tests + count; test++) {
+		const char *test_name = test->name;
+
+		/* Remove the prefix */
+		if (prefix && !strncmp(test_name, prefix, prefix_len))
+			test_name += prefix_len;
+
+		if (select_name && strcmp(select_name, test_name))
+			continue;
+		printf("Test: %s\n", test_name);
+		found++;
+
+		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);
+	}
+	if (select_name && !found)
+		return -ENOENT;
+
+	return uts->fail_count ? -EBADF : 0;
+}
+
+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 };
+	int ret;
+
+	if (!select_name)
+		printf("Running %d %s tests\n", count, category);
+
+	ret = ut_run_tests(&uts, prefix, tests, count, select_name);
+
+	if (ret == -ENOENT)
+		printf("Test '%s' not found\n", select_name);
+	else
+		printf("Failures: %d\n", uts.fail_count);
+
+	return ret;
+}
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 12/42] test: Create pre/post-run functions
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (10 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 11/42] test: Add an overall test runner Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 13/42] test: Call test_pre/post_run() from driver model tests Simon Glass
                   ` (29 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(no changes since v1)

 include/test/ut.h | 20 ++++++++++++++++++++
 test/test-main.c  | 41 +++++++++++++++++++++++++++++++----------
 2 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index 88e75ab791c..7cb5e10f3af 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
  *
diff --git a/test/test-main.c b/test/test-main.c
index 376e7ebd3d2..7961fd8aa3e 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -8,6 +8,27 @@
 #include <console.h>
 #include <test/test.h>
 
+int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
+{
+	uts->start = mallinfo();
+
+	if (test->flags & UT_TESTF_CONSOLE_REC) {
+		int ret = console_record_reset_enable();
+
+		if (ret) {
+			printf("Skipping: Console recording disabled\n");
+			return -EAGAIN;
+		}
+	}
+
+	return 0;
+}
+
+int test_post_run(struct unit_test_state *uts, struct unit_test *test)
+{
+	return 0;
+}
+
 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 		 struct unit_test *tests, int count, const char *select_name)
 {
@@ -17,6 +38,7 @@ int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 
 	for (test = tests; test < tests + count; test++) {
 		const char *test_name = test->name;
+		int ret;
 
 		/* Remove the prefix */
 		if (prefix && !strncmp(test_name, prefix, prefix_len))
@@ -27,18 +49,17 @@ int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 		printf("Test: %s\n", test_name);
 		found++;
 
-		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();
+		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;
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 13/42] test: Call test_pre/post_run() from driver model tests
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (11 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 12/42] test: Create pre/post-run functions Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 14/42] test: Move dm_extended_scan() to test_pre_run() Simon Glass
                   ` (28 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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.

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

(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(-)

diff --git a/include/dm/test.h b/include/dm/test.h
index dfbc82c756d..c0b463cc0f1 100644
--- a/include/dm/test.h
+++ b/include/dm/test.h
@@ -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
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 71e9cf6e5da..69a0349d04c 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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);
 
diff --git a/test/test-main.c b/test/test-main.c
index 7961fd8aa3e..9c600094740 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
 }
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 14/42] test: Move dm_extended_scan() to test_pre_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (12 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 13/42] test: Call test_pre/post_run() from driver model tests Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 15/42] test: Move do_autoprobe() " Simon Glass
                   ` (27 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

Move this step over to the pre-run function.

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

(no changes since v1)

 test/dm/test-dm.c | 3 ---
 test/test-main.c  | 7 +++++++
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 69a0349d04c..4cb0da13b7c 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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));
 
diff --git a/test/test-main.c b/test/test-main.c
index 9c600094740..a971fe0e9c8 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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();
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 15/42] test: Move do_autoprobe() to test_pre_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (13 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 14/42] test: Move dm_extended_scan() to test_pre_run() Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 16/42] test: Move dm_scan_plat() " Simon Glass
                   ` (26 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

Move this step over to the pre-run function.

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

(no changes since v1)

 test/dm/test-dm.c | 17 -----------------
 test/test-main.c  | 18 ++++++++++++++++++
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 4cb0da13b7c..c2e1a1b9207 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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));
 
diff --git a/test/test-main.c b/test/test-main.c
index a971fe0e9c8..bd2f08a2b42 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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));
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 16/42] test: Move dm_scan_plat() to test_pre_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (14 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 15/42] test: Move do_autoprobe() " Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 17/42] test: Drop mallinfo() work-around Simon Glass
                   ` (25 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

Move this step over to the pre-run function.

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

(no changes since v1)

 test/dm/test-dm.c | 2 --
 test/test-main.c  | 3 +++
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index c2e1a1b9207..18877c7ae56 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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));
 
diff --git a/test/test-main.c b/test/test-main.c
index bd2f08a2b42..fe96d739dc4 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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));
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 17/42] test: Drop mallinfo() work-around
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (15 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 16/42] test: Move dm_scan_plat() " Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 18/42] test: Move console silencing to test_pre_run() Simon Glass
                   ` (24 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

This is not needed now. Drop it.

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

(no changes since v1)

 test/dm/test-dm.c | 2 --
 test/test-main.c  | 4 +---
 2 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 18877c7ae56..d1d83e34782 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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)
diff --git a/test/test-main.c b/test/test-main.c
index fe96d739dc4..db0d82e36c3 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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));
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 18/42] test: Move console silencing to test_pre_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (16 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 17/42] test: Drop mallinfo() work-around Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 19/42] test: Move delay skipping " Simon Glass
                   ` (23 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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.

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

(no changes since v1)

 test/dm/test-dm.c | 4 ----
 test/test-main.c  | 3 ++-
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index d1d83e34782..fdd35f663e4 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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));
diff --git a/test/test-main.c b/test/test-main.c
index db0d82e36c3..e273777b6e2 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 19/42] test: Move delay skipping to test_pre_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (17 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 18/42] test: Move console silencing to test_pre_run() Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 20/42] test: Handle driver model reinit in test_pre_run() Simon Glass
                   ` (22 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index 7cb5e10f3af..e5ec18e60b0 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
  *
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index fdd35f663e4..569ffbbad93 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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;
diff --git a/test/test-main.c b/test/test-main.c
index e273777b6e2..6f0d32f7e27 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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)
diff --git a/test/ut.c b/test/ut.c
index 7328338731c..ea0af153e4a 100644
--- a/test/ut.c
+++ b/test/ut.c
@@ -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
+}
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 20/42] test: Handle driver model reinit in test_pre_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (18 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 19/42] test: Move delay skipping " Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 21/42] test: Drop struct dm_test_state Simon Glass
                   ` (21 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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().

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

(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(-)

diff --git a/include/test/test.h b/include/test/test.h
index d282cb2362d..6997568cc07 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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];
 };
diff --git a/include/test/ut.h b/include/test/ut.h
index e5ec18e60b0..6e56ca99c31 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
  *
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 569ffbbad93..ceeac3fd361 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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));
 
diff --git a/test/test-main.c b/test/test-main.c
index 6f0d32f7e27..f14b7b09f79 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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();
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 21/42] test: Drop struct dm_test_state
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (19 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 20/42] test: Handle driver model reinit in test_pre_run() Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 22/42] test: Move dm_test_init() into test-main.c Simon Glass
                   ` (20 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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.

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

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(-)

diff --git a/include/dm/test.h b/include/dm/test.h
index c0b463cc0f1..fe1cc2e278c 100644
--- a/include/dm/test.h
+++ b/include/dm/test.h
@@ -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)
diff --git a/include/test/test.h b/include/test/test.h
index 6997568cc07..5eeec35f525 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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];
 };
diff --git a/test/dm/core.c b/test/dm/core.c
index 35ca689d646..2210345dd14 100644
--- a/test/dm/core.c
+++ b/test/dm/core.c
@@ -117,14 +117,13 @@ int dm_leak_check_end(struct unit_test_state *uts)
 /* Test that binding with plat occurs correctly */
 static int dm_test_autobind(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *dev;
 
 	/*
 	 * We should have a single class (UCLASS_ROOT) and a single root
 	 * device with no children.
 	 */
-	ut_assert(dms->root);
+	ut_assert(uts->root);
 	ut_asserteq(1, list_count_items(gd->uclass_root));
 	ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
@@ -207,7 +206,6 @@ DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
 /* Test that autoprobe finds all the expected devices */
 static int dm_test_autoprobe(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	int expected_base_add;
 	struct udevice *dev;
 	struct uclass *uc;
@@ -221,7 +219,7 @@ static int dm_test_autoprobe(struct unit_test_state *uts)
 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
 
 	/* The root device should not be activated until needed */
-	ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
+	ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
 
 	/*
 	 * We should be able to find the three test devices, and they should
@@ -241,7 +239,7 @@ static int dm_test_autoprobe(struct unit_test_state *uts)
 
 		/* Activating a device should activate the root device */
 		if (!i)
-			ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
+			ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
 	}
 
 	/*
@@ -293,7 +291,6 @@ DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
 /* Test that we can bind, probe, remove, unbind a driver */
 static int dm_test_lifecycle(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	int op_count[DM_TEST_OP_COUNT];
 	struct udevice *dev, *test_dev;
 	int pingret;
@@ -301,7 +298,7 @@ static int dm_test_lifecycle(struct unit_test_state *uts)
 
 	memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
 
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&dev));
 	ut_assert(dev);
 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
@@ -309,7 +306,7 @@ static int dm_test_lifecycle(struct unit_test_state *uts)
 	ut_assert(!dev_get_priv(dev));
 
 	/* Probe the device - it should fail allocating private data */
-	dms->force_fail_alloc = 1;
+	uts->force_fail_alloc = 1;
 	ret = device_probe(dev);
 	ut_assert(ret == -ENOMEM);
 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
@@ -317,7 +314,7 @@ static int dm_test_lifecycle(struct unit_test_state *uts)
 	ut_assert(!dev_get_priv(dev));
 
 	/* Try again without the alloc failure */
-	dms->force_fail_alloc = 0;
+	uts->force_fail_alloc = 0;
 	ut_assertok(device_probe(dev));
 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
 			== op_count[DM_TEST_OP_PROBE] + 2);
@@ -349,19 +346,18 @@ DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
 /* Test that we can bind/unbind and the lists update correctly */
 static int dm_test_ordering(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
 	int pingret;
 
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&dev));
 	ut_assert(dev);
 
 	/* Bind two new devices (numbers 4 and 5) */
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&dev_penultimate));
 	ut_assert(dev_penultimate);
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&dev_last));
 	ut_assert(dev_last);
 
@@ -376,7 +372,7 @@ static int dm_test_ordering(struct unit_test_state *uts)
 	ut_assert(dev_last == test_dev);
 
 	/* Add back the original device 3, now in position 5 */
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&dev));
 	ut_assert(dev);
 
@@ -568,7 +564,6 @@ static int create_children(struct unit_test_state *uts, struct udevice *parent,
 
 static int dm_test_children(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *top[NODE_COUNT];
 	struct udevice *child[NODE_COUNT];
 	struct udevice *grandchild[NODE_COUNT];
@@ -578,12 +573,12 @@ static int dm_test_children(struct unit_test_state *uts)
 	int i;
 
 	/* We don't care about the numbering for this test */
-	dms->skip_post_probe = 1;
+	uts->skip_post_probe = 1;
 
 	ut_assert(NODE_COUNT > 5);
 
 	/* First create 10 top-level children */
-	ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
+	ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
 
 	/* Now a few have their own children */
 	ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
@@ -654,7 +649,6 @@ DM_TEST(dm_test_children, 0);
 
 static int dm_test_device_reparent(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *top[NODE_COUNT];
 	struct udevice *child[NODE_COUNT];
 	struct udevice *grandchild[NODE_COUNT];
@@ -664,12 +658,12 @@ static int dm_test_device_reparent(struct unit_test_state *uts)
 	int i;
 
 	/* We don't care about the numbering for this test */
-	dms->skip_post_probe = 1;
+	uts->skip_post_probe = 1;
 
 	ut_assert(NODE_COUNT > 5);
 
 	/* First create 10 top-level children */
-	ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
+	ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
 
 	/* Now a few have their own children */
 	ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
@@ -815,15 +809,14 @@ DM_TEST(dm_test_device_reparent, 0);
 /* Test that pre-relocation devices work as expected */
 static int dm_test_pre_reloc(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *dev;
 
 	/* The normal driver should refuse to bind before relocation */
-	ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
+	ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
 						&driver_info_manual, &dev));
 
 	/* But this one is marked pre-reloc */
-	ut_assertok(device_bind_by_name(dms->root, true,
+	ut_assertok(device_bind_by_name(uts->root, true,
 					&driver_info_pre_reloc, &dev));
 
 	return 0;
@@ -836,10 +829,9 @@ DM_TEST(dm_test_pre_reloc, 0);
  */
 static int dm_test_remove_active_dma(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *dev;
 
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
 					&dev));
 	ut_assert(dev);
 
@@ -872,7 +864,7 @@ static int dm_test_remove_active_dma(struct unit_test_state *uts)
 	 * the active DMA remove call
 	 */
 	ut_assertok(device_unbind(dev));
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&dev));
 	ut_assert(dev);
 
@@ -895,25 +887,24 @@ DM_TEST(dm_test_remove_active_dma, 0);
 /* Test removal of 'vital' devices */
 static int dm_test_remove_vital(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *normal, *dma, *vital, *dma_vital;
 
 	/* Skip the behaviour in test_post_probe() */
-	dms->skip_post_probe = 1;
+	uts->skip_post_probe = 1;
 
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
 					&normal));
 	ut_assertnonnull(normal);
 
-	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
+	ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
 					&dma));
 	ut_assertnonnull(dma);
 
-	ut_assertok(device_bind_by_name(dms->root, false,
+	ut_assertok(device_bind_by_name(uts->root, false,
 					&driver_info_vital_clk, &vital));
 	ut_assertnonnull(vital);
 
-	ut_assertok(device_bind_by_name(dms->root, false,
+	ut_assertok(device_bind_by_name(uts->root, false,
 					&driver_info_act_dma_vital_clk,
 					&dma_vital));
 	ut_assertnonnull(dma_vital);
@@ -1133,11 +1124,10 @@ DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
 
 static int dm_test_inactive_child(struct unit_test_state *uts)
 {
-	struct dm_test_state *dms = uts->priv;
 	struct udevice *parent, *dev1, *dev2;
 
 	/* Skip the behaviour in test_post_probe() */
-	dms->skip_post_probe = 1;
+	uts->skip_post_probe = 1;
 
 	ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
 
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index ceeac3fd361..15adc53f533 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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)) {
diff --git a/test/dm/test-driver.c b/test/dm/test-driver.c
index ca7626a0668..63dc9d335ae 100644
--- a/test/dm/test-driver.c
+++ b/test/dm/test-driver.c
@@ -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;
diff --git a/test/dm/test-uclass.c b/test/dm/test-uclass.c
index f1b7aaa727f..f4b540c9278 100644
--- a/test/dm/test-uclass.c
+++ b/test/dm/test-uclass.c
@@ -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
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 22/42] test: Move dm_test_init() into test-main.c
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (20 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 21/42] test: Drop struct dm_test_state Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:50 ` [PATCH v4 23/42] test: Move dm_test_destroy() " Simon Glass
                   ` (19 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index 6e56ca99c31..4e0aba9f700 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 15adc53f533..d601e497522 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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;
diff --git a/test/test-main.c b/test/test-main.c
index f14b7b09f79..8b0121bdcec 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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);
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 23/42] test: Move dm_test_destroy() into test-main.c
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (21 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 22/42] test: Move dm_test_init() into test-main.c Simon Glass
@ 2021-03-04 13:50 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 24/42] test: Move test running into a separate function Simon Glass
                   ` (18 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:50 UTC (permalink / raw)
  To: u-boot

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

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

(no changes since v1)

 test/dm/test-dm.c | 22 ----------------------
 test/test-main.c  | 23 +++++++++++++++++++++++
 2 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index d601e497522..df938395bb6 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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;
 }
 
diff --git a/test/test-main.c b/test/test-main.c
index 8b0121bdcec..3806c2ad89c 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 24/42] test: Move test running into a separate function
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (22 preceding siblings ...)
  2021-03-04 13:50 ` [PATCH v4 23/42] test: Move dm_test_destroy() " Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 25/42] test: Use ut_run_test() to run driver model tests Simon Glass
                   ` (17 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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

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

(no changes since v1)

 include/test/ut.h | 16 ++++++++++++++++
 test/test-main.c  | 32 +++++++++++++++++++++++---------
 2 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index 4e0aba9f700..98f699cbba2 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
diff --git a/test/test-main.c b/test/test-main.c
index 3806c2ad89c..dee28d35d82 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 25/42] test: Use ut_run_test() to run driver model tests
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (23 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 24/42] test: Move test running into a separate function Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 26/42] test: Drop dm_do_test() Simon Glass
                   ` (16 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index 98f699cbba2..adef0b7e1cf 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
  *
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index df938395bb6..b01123c7409 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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;
 }
diff --git a/test/test-main.c b/test/test-main.c
index dee28d35d82..32c4d4b1996 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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)
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 26/42] test: Drop dm_do_test()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (24 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 25/42] test: Use ut_run_test() to run driver model tests Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 27/42] test: Add ut_run_test_live_flat() to run tests twice Simon Glass
                   ` (15 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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

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

(no changes since v1)

 test/dm/test-dm.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index b01123c7409..de41fc09db0 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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++;
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 27/42] test: Add ut_run_test_live_flat() to run tests twice
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (25 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 26/42] test: Drop dm_do_test() Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 28/42] test: Use a local variable for test state Simon Glass
                   ` (14 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(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(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index adef0b7e1cf..d06bc5089be 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index de41fc09db0..826b64565e8 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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++;
 	}
 
diff --git a/test/test-main.c b/test/test-main.c
index 32c4d4b1996..4e17c9edb28 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -80,6 +80,24 @@ static int do_autoprobe(struct unit_test_state *uts)
 	return ret;
 }
 
+/*
+ * ut_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 ut_test_run_on_flattree(struct unit_test *test)
+{
+	const char *fname = strrchr(test->file, '/') + 1;
+
+	if (!(test->flags & UT_TESTF_DM))
+		return false;
+
+	return !strstr(fname, "video") || strstr(test->name, "video_base");
+}
+
 /**
  * test_pre_run() - Handle any preparation needed to run a test
  *
@@ -137,8 +155,22 @@ static 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)
+/**
+ * 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_name: 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
+ */
+static 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 = "";
@@ -163,6 +195,35 @@ int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 	return 0;
 }
 
+int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
+			  const char *name)
+{
+	int runs;
+
+	/* 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 || ut_test_run_on_flattree(test))) {
+		uts->of_live = false;
+		ut_assertok(ut_run_test(uts, test, test->name));
+		runs++;
+	}
+
+	return 0;
+}
+
 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 		 struct unit_test *tests, int count, const char *select_name)
 {
@@ -180,7 +241,7 @@ int ut_run_tests(struct unit_test_state *uts, const char *prefix,
 
 		if (select_name && strcmp(select_name, test_name))
 			continue;
-		ret = ut_run_test(uts, test, test_name);
+		ret = ut_run_test_live_flat(uts, test, test_name);
 		found++;
 		if (ret == -EAGAIN)
 			continue;
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 28/42] test: Use a local variable for test state
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (26 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 27/42] test: Add ut_run_test_live_flat() to run tests twice Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 29/42] test: Run driver-model tests using ut_run_list() Simon Glass
                   ` (13 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(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(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index d06bc5089be..bed0e6eb5f6 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
  *
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 826b64565e8..cdaf27bf987 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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;
 
diff --git a/test/dm/test-driver.c b/test/dm/test-driver.c
index 63dc9d335ae..02cb974b0f7 100644
--- a/test/dm/test-driver.c
+++ b/test/dm/test-driver.c
@@ -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)));
diff --git a/test/dm/test-uclass.c b/test/dm/test-uclass.c
index f4b540c9278..067701734a0 100644
--- a/test/dm/test-uclass.c
+++ b/test/dm/test-uclass.c
@@ -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));
 
diff --git a/test/test-main.c b/test/test-main.c
index 4e17c9edb28..139fc1f6f18 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
 }
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 29/42] test: Run driver-model tests using ut_run_list()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (27 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 28/42] test: Use a local variable for test state Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 30/42] test: Use return values in dm_test_run() Simon Glass
                   ` (12 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index bed0e6eb5f6..fbbba286ee0 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -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
  *
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index cdaf27bf987..20af1c13b3c 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -20,31 +20,14 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static bool test_matches(const char *test_name, const char *find_name)
-{
-	if (!find_name)
-		return true;
-
-	if (!strcmp(test_name, find_name))
-		return true;
-
-	/* All tests have this prefix */
-	if (!strncmp(test_name, "dm_test_", 8))
-		test_name += 8;
-
-	if (!strcmp(test_name, find_name))
-		return true;
-
-	return false;
-}
+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 unit_test *test;
-	int found;
+	struct device_node *of_root;
 
 	uts->fail_count = 0;
 
@@ -60,29 +43,11 @@ int dm_test_run(const char *test_name)
 		}
 	}
 
-	if (!test_name)
-		printf("Running %d driver model tests\n", n_ents);
-	else
-
-	found = 0;
-	uts->of_root = gd_of_root();
-	for (test = tests; test < tests + n_ents; test++) {
-		const char *name = test->name;
-
-		if (!test_matches(name, test_name))
-			continue;
-
-		ut_assertok(ut_run_test_live_flat(uts, test, test->name));
-		found++;
-	}
-
-	if (test_name && !found)
-		printf("Test '%s' not found\n", test_name);
-	else
-		printf("Failures: %d\n", uts->fail_count);
+	of_root = gd_of_root();
+	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(uts->of_root);
+	gd_set_of_root(of_root);
 	gd->dm_root = NULL;
 	ut_assertok(dm_init(CONFIG_IS_ENABLED(OF_LIVE)));
 	dm_scan_plat(false);
diff --git a/test/test-main.c b/test/test-main.c
index 139fc1f6f18..16c0d13ea55 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -111,6 +111,38 @@ static bool ut_test_run_on_flattree(struct unit_test *test)
 	return !strstr(fname, "video") || strstr(test->name, "video_base");
 }
 
+/**
+ * test_matches() - Check if a test should be run
+ *
+ * This checks if the a test should be run. In the normal case of running all
+ * tests, @select_name is NULL.
+ *
+ * @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
+ * @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
+ */
+static bool test_matches(const char *prefix, const char *test_name,
+			 const char *select_name)
+{
+	if (!select_name)
+		return true;
+
+	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 (!strcmp(test_name, select_name))
+		return true;
+
+	return false;
+}
+
 /**
  * test_pre_run() - Handle any preparation needed to run a test
  *
@@ -213,8 +245,25 @@ static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 	return 0;
 }
 
-int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
-			  const char *name)
+/**
+ * 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
+ */
+static int ut_run_test_live_flat(struct unit_test_state *uts,
+				 struct unit_test *test, const char *name)
 {
 	int runs;
 
@@ -242,24 +291,39 @@ int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
 	return 0;
 }
 
-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 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
+ */
+static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
+			struct unit_test *tests, int count,
+			const char *select_name)
 {
 	struct unit_test *test;
-	int prefix_len = prefix ? strlen(prefix) : 0;
 	int found = 0;
 
 	for (test = tests; test < tests + count; test++) {
 		const char *test_name = test->name;
 		int ret;
 
-		/* Remove the prefix */
-		if (prefix && !strncmp(test_name, prefix, prefix_len))
-			test_name += prefix_len;
-
-		if (select_name && strcmp(select_name, test_name))
+		if (!test_matches(prefix, test_name, select_name))
 			continue;
-		ret = ut_run_test_live_flat(uts, test, test_name);
+		ret = ut_run_test_live_flat(uts, test, select_name);
 		found++;
 		if (ret == -EAGAIN)
 			continue;
@@ -281,6 +345,7 @@ int ut_run_list(const char *category, const char *prefix,
 	if (!select_name)
 		printf("Running %d %s tests\n", count, category);
 
+	uts.of_root = gd_of_root();
 	ret = ut_run_tests(&uts, prefix, tests, count, select_name);
 
 	if (ret == -ENOENT)
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 30/42] test: Use return values in dm_test_run()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (28 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 29/42] test: Run driver-model tests using ut_run_list() Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 31/42] test: Move the devicetree check into ut_run_list() Simon Glass
                   ` (11 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(no changes since v1)

 test/dm/test-dm.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 20af1c13b3c..54e6577b009 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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[])
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 31/42] test: Move the devicetree check into ut_run_list()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (29 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 30/42] test: Use return values in dm_test_run() Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 32/42] test: Move restoring of driver model state to ut_run_list() Simon Glass
                   ` (10 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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().

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

(no changes since v1)

 test/dm/test-dm.c | 13 -------------
 test/test-main.c  | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 54e6577b009..8cb99ed80cc 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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);
 
diff --git a/test/test-main.c b/test/test-main.c
index 16c0d13ea55..8138fb43875 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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);
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 32/42] test: Move restoring of driver model state to ut_run_list()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (30 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 31/42] test: Move the devicetree check into ut_run_list() Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 33/42] test: log: Rename log main test file to log_ut.c Simon Glass
                   ` (9 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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().

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

(no changes since v1)

 test/dm/test-dm.c | 11 -----------
 test/test-main.c  | 30 +++++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 8cb99ed80cc..cb4f99537df 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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;
 }
 
diff --git a/test/test-main.c b/test/test-main.c
index 8138fb43875..6edd49f0b61 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 33/42] test: log: Rename log main test file to log_ut.c
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (31 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 32/42] test: Move restoring of driver model state to ut_run_list() Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 17:18   ` Heinrich Schuchardt
  2021-03-04 13:51 ` [PATCH v4 34/42] test: Add a macros for finding tests in linker_lists Simon Glass
                   ` (8 subsequent siblings)
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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

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

(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%)

diff --git a/test/log/Makefile b/test/log/Makefile
index 3f09deb644b..a3dedace043 100644
--- a/test/log/Makefile
+++ b/test/log/Makefile
@@ -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
diff --git a/test/log/test-main.c b/test/log/log_ut.c
similarity index 100%
rename from test/log/test-main.c
rename to test/log/log_ut.c
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 34/42] test: Add a macros for finding tests in linker_lists
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (32 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 33/42] test: log: Rename log main test file to log_ut.c Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix Simon Glass
                   ` (7 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(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(-)

diff --git a/include/test/test.h b/include/test/test.h
index 5eeec35f525..b16c9135f2c 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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,
diff --git a/test/bloblist.c b/test/bloblist.c
index 6953d3010a6..d876b639184 100644
--- a/test/bloblist.c
+++ b/test/bloblist.c
@@ -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);
diff --git a/test/bootm.c b/test/bootm.c
index 563d6ebaa5a..8528982ae11 100644
--- a/test/bootm.c
+++ b/test/bootm.c
@@ -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);
diff --git a/test/cmd/mem.c b/test/cmd/mem.c
index fbaa8a4b3cf..d76f47cf311 100644
--- a/test/cmd/mem.c
+++ b/test/cmd/mem.c
@@ -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);
diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
index b483069ff0f..27113c083c5 100644
--- a/test/cmd/setexpr.c
+++ b/test/cmd/setexpr.c
@@ -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);
diff --git a/test/compression.c b/test/compression.c
index a2a4b9ff9e8..4cd1be564f3 100644
--- a/test/compression.c
+++ b/test/compression.c
@@ -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);
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index cb4f99537df..729becf57b2 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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);
diff --git a/test/env/cmd_ut_env.c b/test/env/cmd_ut_env.c
index a440b1bfb0e..d65a32179ce 100644
--- a/test/env/cmd_ut_env.c
+++ b/test/env/cmd_ut_env.c
@@ -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);
diff --git a/test/lib/cmd_ut_lib.c b/test/lib/cmd_ut_lib.c
index f5c7bf3d3b0..f1ac015b2c8 100644
--- a/test/lib/cmd_ut_lib.c
+++ b/test/lib/cmd_ut_lib.c
@@ -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);
 }
diff --git a/test/log/log_ut.c b/test/log/log_ut.c
index c534add493e..5aa3a184004 100644
--- a/test/log/log_ut.c
+++ b/test/log/log_ut.c
@@ -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);
diff --git a/test/optee/cmd_ut_optee.c b/test/optee/cmd_ut_optee.c
index 9fa4c91e0dd..c3887ab11d9 100644
--- a/test/optee/cmd_ut_optee.c
+++ b/test/optee/cmd_ut_optee.c
@@ -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;
diff --git a/test/overlay/cmd_ut_overlay.c b/test/overlay/cmd_ut_overlay.c
index c001fb183fe..56a3df17138 100644
--- a/test/overlay/cmd_ut_overlay.c
+++ b/test/overlay/cmd_ut_overlay.c
@@ -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;
diff --git a/test/str_ut.c b/test/str_ut.c
index cd5045516d1..359d7d4ea1f 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -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);
 }
diff --git a/test/unicode_ut.c b/test/unicode_ut.c
index 6130ef0b549..7b9c020eff9 100644
--- a/test/unicode_ut.c
+++ b/test/unicode_ut.c
@@ -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);
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (33 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 34/42] test: Add a macros for finding tests in linker_lists Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 15:28   ` Heinrich Schuchardt
  2021-03-04 13:51 ` [PATCH v4 36/42] test: Allow SPL to run any available test Simon Glass
                   ` (6 subsequent siblings)
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(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(-)

diff --git a/include/test/test.h b/include/test/test.h
index b16c9135f2c..3330dcc72d3 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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 {
diff --git a/test/py/conftest.py b/test/py/conftest.py
index 9bfd9263455..1b909cde9d3 100644
--- a/test/py/conftest.py
+++ b/test/py/conftest.py
@@ -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.
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 36/42] test: Allow SPL to run any available test
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (34 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 37/42] sandbox: Update os_find_u_boot() to find the .img file Simon Glass
                   ` (5 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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

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

(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(-)

diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index 3779d58c3fe..ce5aae87fb6 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -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 */
 	}
 }
diff --git a/include/test/test.h b/include/test/test.h
index 3330dcc72d3..0b124edd601 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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 */
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 729becf57b2..9ba2ba23d5d 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -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);
diff --git a/test/test-main.c b/test/test-main.c
index 6edd49f0b61..e1b49e091ab 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -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;
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 37/42] sandbox: Update os_find_u_boot() to find the .img file
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (35 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 36/42] test: Allow SPL to run any available test Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 38/42] spl: Convert spl_fit to work with sandbox Simon Glass
                   ` (4 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(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(-)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index f5000e64966..2d9583c17ca 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -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);
diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index ce5aae87fb6..f82b0d3de16 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -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;
diff --git a/include/os.h b/include/os.h
index d2a4afeca0f..77d8bd89d0f 100644
--- a/include/os.h
+++ b/include/os.h
@@ -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
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 38/42] spl: Convert spl_fit to work with sandbox
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (36 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 37/42] sandbox: Update os_find_u_boot() to find the .img file Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 39/42] doc: Move coccinelle into its own section Simon Glass
                   ` (3 subsequent siblings)
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(no changes since v1)

 common/spl/spl.c     |  3 ++-
 common/spl/spl_fit.c | 27 +++++++++++++++------------
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/common/spl/spl.c b/common/spl/spl.c
index bb91b761fc7..5f51098a88f 100644
--- a/common/spl/spl.c
+++ b/common/spl/spl.c
@@ -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)
diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c
index 75c8ff065bb..49508fc5180 100644
--- a/common/spl/spl_fit.c
+++ b/common/spl/spl_fit.c
@@ -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;
 
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 39/42] doc: Move coccinelle into its own section
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (37 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 38/42] spl: Convert spl_fit to work with sandbox Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 17:23   ` Heinrich Schuchardt
  2021-03-04 13:51 ` [PATCH v4 40/42] spl: test: Add a test for spl_load_simple_fit() Simon Glass
                   ` (2 subsequent siblings)
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

This tool has nothing to do with testing. Create a new section for
'refactoring' and move it into there. It is likely that other topics may
fall under the same heading, such as using moveconfig and search/replace
tools.

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

(no changes since v1)

 doc/develop/index.rst | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/doc/develop/index.rst b/doc/develop/index.rst
index 50b1de3bdff..9208668a2d4 100644
--- a/doc/develop/index.rst
+++ b/doc/develop/index.rst
@@ -30,7 +30,14 @@ Testing
 .. toctree::
    :maxdepth: 1
 
-   coccinelle
    testing
    py_testing
    tests_sandbox
+
+Refactoring
+-----------
+
+.. toctree::
+   :maxdepth: 1
+
+   coccinelle
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 40/42] spl: test: Add a test for spl_load_simple_fit()
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (38 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 39/42] doc: Move coccinelle into its own section Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 41/42] test: sandbox: Move sandbox test docs into doc/develop Simon Glass
  2021-03-04 13:51 ` [PATCH v4 42/42] doc: Explain briefly how to write new tests Simon Glass
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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/


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

(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

diff --git a/configs/sandbox_spl_defconfig b/configs/sandbox_spl_defconfig
index c0118702a81..2696d0b6cdd 100644
--- a/configs/sandbox_spl_defconfig
+++ b/configs/sandbox_spl_defconfig
@@ -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
diff --git a/doc/arch/sandbox.rst b/doc/arch/sandbox.rst
index 60ee1e0741a..1ccb5344ac6 100644
--- a/doc/arch/sandbox.rst
+++ b/doc/arch/sandbox.rst
@@ -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)
 =======   ========================   ===============================
diff --git a/include/spl.h b/include/spl.h
index 0d134587de2..4f6e0e53f5d 100644
--- a/include/spl.h
+++ b/include/spl.h
@@ -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);
 };
diff --git a/test/Makefile b/test/Makefile
index 5cd284e322e..a26e915e050 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -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
diff --git a/test/image/Makefile b/test/image/Makefile
new file mode 100644
index 00000000000..c4039df707f
--- /dev/null
+++ b/test/image/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2021 Google LLC
+
+obj-$(CONFIG_SPL_BUILD) += spl_load.o
diff --git a/test/image/spl_load.c b/test/image/spl_load.c
new file mode 100644
index 00000000000..851603ddd75
--- /dev/null
+++ b/test/image/spl_load.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <image.h>
+#include <mapmem.h>
+#include <os.h>
+#include <spl.h>
+#include <test/ut.h>
+
+/* Declare a new SPL test */
+#define SPL_TEST(_name, _flags)		UNIT_TEST(_name, _flags, spl_test)
+
+/* Context used for this test */
+struct text_ctx {
+	int fd;
+};
+
+static ulong read_fit_image(struct spl_load_info *load, ulong sector,
+			    ulong count, void *buf)
+{
+	struct text_ctx *text_ctx = load->priv;
+	off_t offset, ret;
+	ssize_t res;
+
+	offset = sector * load->bl_len;
+	ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
+	if (ret != offset) {
+		printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
+		       ret, errno);
+		return 0;
+	}
+
+	res = os_read(text_ctx->fd, buf, count * load->bl_len);
+	if (res == -1) {
+		printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
+		       count * load->bl_len, res, errno);
+		return 0;
+	}
+
+	return count;
+}
+
+int board_fit_config_name_match(const char *name)
+{
+	return 0;
+}
+
+struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
+{
+	return map_sysmem(0x100000, 0);
+}
+
+static int spl_test_load(struct unit_test_state *uts)
+{
+	struct spl_image_info image;
+	struct image_header *header;
+	struct text_ctx text_ctx;
+	struct spl_load_info load;
+	char fname[256];
+	int ret;
+	int fd;
+
+	memset(&load, '\0', sizeof(load));
+	load.bl_len = 512;
+	load.read = read_fit_image;
+
+	ret = os_find_u_boot(fname, sizeof(fname), true);
+	if (ret) {
+		printf("(%s not found, error %d)\n", fname, ret);
+		return ret;
+	}
+	load.filename = fname;
+
+	header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
+
+	fd = os_open(fname, OS_O_RDONLY);
+	ut_assert(fd >= 0);
+	ut_asserteq(512, os_read(fd, header, 512));
+	text_ctx.fd = fd;
+
+	load.priv = &text_ctx;
+
+	ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
+
+	return 0;
+}
+SPL_TEST(spl_test_load, 0);
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 41/42] test: sandbox: Move sandbox test docs into doc/develop
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (39 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 40/42] spl: test: Add a test for spl_load_simple_fit() Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 13:51 ` [PATCH v4 42/42] doc: Explain briefly how to write new tests Simon Glass
  41 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

(no changes since v1)

 doc/arch/sandbox.rst          | 44 +++++++----------------------------
 doc/develop/tests_sandbox.rst | 44 +++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 37 deletions(-)

diff --git a/doc/arch/sandbox.rst b/doc/arch/sandbox.rst
index 1ccb5344ac6..1638b050002 100644
--- a/doc/arch/sandbox.rst
+++ b/doc/arch/sandbox.rst
@@ -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
diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
index daf87e53a99..8c2eac4c5f4 100644
--- a/doc/develop/tests_sandbox.rst
+++ b/doc/develop/tests_sandbox.rst
@@ -1,7 +1,47 @@
 .. SPDX-License-Identifier: GPL-2.0+
 
-Tests Under the Hood
-====================
+Sandbox tests
+=============
+
+Test Design
+-----------
+
+Most uclasses and many functions of U-Boot have sandbox tests. This allows much
+of the code to be checked in an developer-friendly environment.
+
+Sandbox provides a way to write and run unit tests. The traditional approach to
+unit tests is to build lots of little executables, one for each test or
+category of tests. With sandbox, so far as possible, all the tests share a
+small number of executables (e.g. 'u-boot' for sandbox, 'u-boot-spl' and
+'u-boot' for sandbox_spl) and can be run very quickly. The vast majority of
+tests can run on the 'sandbox' build,
+
+Available tests
+---------------
+
+Some of the available tests are:
+
+  - 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.
+  - image: Unit tests for images:
+
+     - test/image/test-imagetools.sh - multi-file images
+     - test/py/tests/test-fit.py     - FIT images
+  - tracing: test/trace/test-trace.sh tests the tracing system (see
+      README.trace)
+  - verified boot: test/py/tests/test_vboot.py
+
+If you change or enhance any U-Boot subsystem, you should write or expand a
+test and include it with your patch series submission. Test coverage in some
+older areas of U-Boot is still somewhat limited and 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'.
+
 
 Running sandbox tests directly
 ------------------------------
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 42/42] doc: Explain briefly how to write new tests
  2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
                   ` (40 preceding siblings ...)
  2021-03-04 13:51 ` [PATCH v4 41/42] test: sandbox: Move sandbox test docs into doc/develop Simon Glass
@ 2021-03-04 13:51 ` Simon Glass
  2021-03-04 18:14   ` Heinrich Schuchardt
  41 siblings, 1 reply; 54+ messages in thread
From: Simon Glass @ 2021-03-04 13:51 UTC (permalink / raw)
  To: u-boot

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.

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

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 | 339 ++++++++++++++++++++++++++++++++++
 5 files changed, 351 insertions(+), 1 deletion(-)
 create mode 100644 doc/develop/tests_writing.rst

diff --git a/doc/develop/index.rst b/doc/develop/index.rst
index 9208668a2d4..8f0a598b5f2 100644
--- a/doc/develop/index.rst
+++ b/doc/develop/index.rst
@@ -32,6 +32,7 @@ Testing
 
    testing
    py_testing
+   tests_writing
    tests_sandbox
 
 Refactoring
diff --git a/doc/develop/py_testing.rst b/doc/develop/py_testing.rst
index 7f01858cfda..c4cecc0a01b 100644
--- a/doc/develop/py_testing.rst
+++ b/doc/develop/py_testing.rst
@@ -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
diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
index b181c2e2e41..ced13ac8bb4 100644
--- a/doc/develop/testing.rst
+++ b/doc/develop/testing.rst
@@ -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
 -----------
diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
index 8c2eac4c5f4..7e5cc84e43c 100644
--- a/doc/develop/tests_sandbox.rst
+++ b/doc/develop/tests_sandbox.rst
@@ -199,3 +199,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.
+
diff --git a/doc/develop/tests_writing.rst b/doc/develop/tests_writing.rst
new file mode 100644
index 00000000000..d926c00f6e1
--- /dev/null
+++ b/doc/develop/tests_writing.rst
@@ -0,0 +1,339 @@
+.. SPDX-License-Identifier: GPL-2.0+
+.. Copyright 2021 Google LLC
+.. sectionauthor:: Simon Glass <sjg@chromium.org>
+
+Writing Tests
+=============
+
+This describes how to write tests in U-Boot and describes the possible options.
+
+Test types
+----------
+
+There are two basic types of test in U-Boot:
+
+  - Python tests, in test/py/tests
+  - C tests, in test/ and its subdirectories
+
+Python tests talk to U-Boot via the command line. They support both sandbox and
+real hardware. They typically do not require building test code into U-Boot
+itself. They are fairly slow to run, due to the command-line interface and there
+being two separate processes. Python tests are fairly easy to write. They can
+be a little tricky to debug sometimes due to the voluminous output of pytest.
+
+C tests are written directly in U-Boot. While they can be used on boards, they
+are more commonly used with sandbox, as they obviously add to U-Boot code size.
+C tests are easy to write so long as the required facilities exist. Where they
+do not it can involve refactoring or adding new features to sandbox. They are
+fast to run and easy to debug.
+
+Regardless of which test type is used, all tests are collected and run by the
+pytest framework, so there is typically no need to run them separately. This
+means that C tests can be used when it makes sense, and Python tests when it
+doesn't.
+
+
+This table shows how to decide whether to write a C or Python test:
+
+=====================  ===========================  =============================
+Attribute              C test                       Python test
+=====================  ===========================  =============================
+Fast to run?           Yes                          No (two separate processes)
+Easy to write?         Yes, if required test        Yes
+                       features exist in sandbox
+Needs code in U-Boot?  Yes                          No, provided the test can be
+                                                    executed and the result
+                                                    determined using the command
+                                                    line
+Easy to debug?         Yes                          Yes, but the amount of output can
+                                                    sometimes require a bit of digging
+Can use gdb?           Yes, directly                Yes, with --gdbserver
+Can run on boards?     Some can, but only if        Yes
+                       compiled in and not
+                       dependent on sandboxau
+=====================  ===========================  =============================
+
+
+Python or C
+-----------
+
+Typically in U-Boot we encourage C test using sandbox for all features. This
+allows fast testing, easy development and allows contributors to make changes
+without needing dozens of boards to test with.
+
+When a test requires setup or interaction with the running host (such as to
+generate images and then running U-Boot to check that they can be loaded), or
+cannot be run on sandbox, Python tests should be used. These should typically
+NOT rely on running with sandbox, but instead should function correctly on any
+board supported by U-Boot.
+
+
+How slow are Python tests?
+--------------------------
+
+Under the hood, Python tests work by starting a sandbox test and connecting to
+it via a pipe. Each interaction with the U-Boot process requires at least a
+context switch to handle the pipe interaction. The test sends a command to
+U-Boot, which then reacts and shows some output, then the test sees that and
+continues. Of course on real hardware, communications delays (e.g. with a serial
+console) make this slower.
+
+For comparison, consider a test that checks the 'md' (memory dump). All times
+below are approximate, as measured on an AMD 2950X system. Here is is the test
+in Python::
+
+   @pytest.mark.buildconfigspec('cmd_memory')
+   def test_md(u_boot_console):
+       """Test that md reads memory as expected, and that memory can be modified
+       using the mw command."""
+
+       ram_base = u_boot_utils.find_ram_base(u_boot_console)
+       addr = '%08x' % ram_base
+       val = 'a5f09876'
+       expected_response = addr + ': ' + val
+       u_boot_console.run_command('mw ' + addr + ' 0 10')
+       response = u_boot_console.run_command('md ' + addr + ' 10')
+       assert(not (expected_response in response))
+       u_boot_console.run_command('mw ' + addr + ' ' + val)
+       response = u_boot_console.run_command('md ' + addr + ' 10')
+       assert(expected_response in response)
+
+This runs a few commands and checks the output. Note that it runs a command,
+waits for the response and then checks it agains what is expected. If run by
+itself it takes around 800ms, including test collection. For 1000 runs it takes
+19 seconds, or 19ms per run. Of course 1000 runs it not that useful since we
+only want to run it once.
+
+There is no exactly equivalent C test, but here is a similar one that tests 'ms'
+(memory search)::
+
+   /* Test 'ms' command with bytes */
+   static int mem_test_ms_b(struct unit_test_state *uts)
+   {
+      u8 *buf;
+
+      buf = map_sysmem(0, BUF_SIZE + 1);
+      memset(buf, '\0', BUF_SIZE);
+      buf[0x0] = 0x12;
+      buf[0x31] = 0x12;
+      buf[0xff] = 0x12;
+      buf[0x100] = 0x12;
+      ut_assertok(console_record_reset_enable());
+      run_command("ms.b 1 ff 12", 0);
+      ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................");
+      ut_assert_nextline("--");
+      ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12    ................");
+      ut_assert_nextline("2 matches");
+      ut_assert_console_end();
+
+      ut_asserteq(2, env_get_hex("memmatches", 0));
+      ut_asserteq(0xff, env_get_hex("memaddr", 0));
+      ut_asserteq(0xfe, env_get_hex("mempos", 0));
+
+      unmap_sysmem(buf);
+
+      return 0;
+   }
+   MEM_TEST(mem_test_ms_b, UT_TESTF_CONSOLE_REC);
+
+This runs the command directly in U-Boot, then checks the console output, also
+directly in U-Boot. If run by itself this takes 100ms. For 1000 runs it takes
+660ms, or 0.66ms per run.
+
+So overall running a C test is perhaps 8 times faster individually and the
+interactions are perhaps 25 times faster.
+
+It should also be noted that the C test is fairly easy to debug. You can set a
+breakpoint on do_mem_search(), which is what implements the 'ms' command,
+single step to see what might be wrong, etc. That is also possible with the
+pytest, but requires two terminals and --gdbserver.
+
+
+Why does speed matter?
+----------------------
+
+Many development activities rely on running tests:
+
+  - 'git bisect run make qcheck' can be used to find a failing commit
+  - test-driven development relies on quick iteration of build/test
+  - U-Boot's continuous integration (CI) systems make use of tests. Running
+      all sandbox tests typically takes 90 seconds and running each qemu test
+      takes about 30 seconds. This is currently dwarfed by the time taken to
+      build all boards
+
+As U-Boot continues to grow its feature set, fast and reliable tests are a
+critical factor factor in developer productivity and happiness.
+
+
+Writing C tests
+---------------
+
+C tests are arranged into suites which are typically executed by the 'ut'
+command. Each suite is in its own file. This section describes how to accomplish
+some common test tasks.
+
+
+Add a new driver model test
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Use this when adding a test for a new or existing uclass, adding new operations
+or features to a uclass, adding new ofnode or dev_read_() functions, or anything
+else related to driver model.
+
+Find a suitable place for your test, perhaps near other test functions in
+existing code, or in a new file. Each uclass should have its own test file.
+
+Declare the test with::
+
+   /* Test that ... */
+   static int dm_test_uclassname_what(struct unit_test_state *uts)
+   {
+      /* test code here */
+
+      return 0;
+   }
+   DM_TEST(dm_test_uclassname_what, UT_TESTF_SCAN_FDT);
+
+Replace 'uclassname' with the name of your uclass, if applicable. Replace 'what'
+with what you are testing.
+
+The flags for DM_TEST() are defined in test/test.h and you typically want
+UT_TESTF_SCAN_FDT so that the devicetree is scanned and all devices are bound
+and ready for use. The DM_TEST macro adds UT_TESTF_DM automatically so that
+the test runner knows it is a driver model test.
+
+Driver model tests are special in that the entire driver model state is
+recreated anew for each test. This ensures that if a previous test deletes a
+device, for example, it does not affect subsequent tests. Driver model tests
+also run both with livetree and flattree, to ensure that both devicetree
+implementations work as expected.
+
+Example commit: c48cb7ebfb4 ("sandbox: add ADC unit tests") [1]
+
+[1] https://gitlab.denx.de/u-boot/u-boot/-/commit/c48cb7ebfb4
+
+
+Add a C test to an existing suite
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Use this when you are adding to or modifying an existing feature outside driver
+model. An example is bloblist.
+
+Add a new function in the same file as the rest of the suite and register it
+with the suite. For example, to add a new mem_search test::
+
+   /* Test 'ms' command with 32-bit values */
+   static int mem_test_ms_new_thing(struct unit_test_state *uts)
+   {
+         /* test code here*/
+
+         return 0;
+   }
+   MEM_TEST(mem_test_ms_new_thing, UT_TESTF_CONSOLE_REC);
+
+Note that the MEM_TEST() macros is defined at the top of the file.
+
+Example commit: 9fe064646d2 ("bloblist: Support relocating to a larger space") [1]
+
+[1] https://gitlab.denx.de/u-boot/u-boot/-/commit/9fe064646d2
+
+
+Add a new test suite
+~~~~~~~~~~~~~~~~~~~~
+
+Each suite should focus on one feature or subsystem, so if you are writing a
+new one of those, you should add a new suite.
+
+Create a new file in test/ or a subdirectory and define a macro to register the
+suite. For example::
+
+   #include <common.h>
+   #include <console.h>
+   #include <mapmem.h>
+   #include <dm/test.h>
+   #include <test/ut.h>
+
+   /* Declare a new wibble test */
+   #define WIBBLE_TEST(_name, _flags)   UNIT_TEST(_name, _flags, wibble_test)
+
+   /* Tetss go here */
+
+   /* At the bottom of the file: */
+
+   int do_ut_wibble(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+   {
+     struct unit_test *tests = UNIT_TEST_SUITE_START(wibble_test);
+     const int n_ents = UNIT_TEST_SUITE_COUNT(wibble_test);
+
+     return cmd_ut_category("cmd_wibble", "wibble_test_", tests, n_ents, argc, argv);
+   }
+
+Then add new tests to it as above.
+
+Register this new suite in test/cmd_ut.c by adding to cmd_ut_sub[]::
+
+  /* Within cmd_ut_sub[]... */
+
+  U_BOOT_CMD_MKENT(wibble, CONFIG_SYS_MAXARGS, 1, do_ut_wibble, "", ""),
+
+and adding new help to ut_help_text[]::
+
+  "ut wibble - Test the wibble feature\n"
+
+If your feature is conditional on a particular Kconfig, then you can use #ifdef
+to control that.
+
+Finally, add the test to the build by adding to the Makefile in the same
+directory::
+
+  obj-$(CONFIG_$(SPL_)CMDLINE) += wibble.o
+
+Note that CMDLINE is never enabled in SPL, so this test will only be present in
+U-Boot proper. See below for how to do SPL tests.
+
+As before, you can add an extra Kconfig check if needed::
+
+  ifneq ($(CONFIG_$(SPL_)WIBBLE),)
+  obj-$(CONFIG_$(SPL_)CMDLINE) += wibble.o
+  endif
+
+
+Example commit: 919e7a8fb64 ("test: Add a simple test for bloblist") [1]
+
+[1] https://gitlab.denx.de/u-boot/u-boot/-/commit/919e7a8fb64
+
+
+Making the test run from pytest
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+All C tests must run from pytest. Typically this is automatic, since pytest
+scans the U-Boot executable for available tests to run. So long as you have a
+'ut' subcommand for your test suite, it will run. The same applies for driver
+model tests since they use the 'ut dm' subcommand.
+
+See test/py/tests/test_ut.py for how unit tests are run.
+
+
+Add a C test for SPL
+~~~~~~~~~~~~~~~~~~~~
+
+Note: C tests are only available for sandbox_spl at present. There is currently
+no mechanism in other boards to existing SPL tests even if they are built into
+the image.
+
+SPL tests cannot be run from the 'ut' command since there are no commands
+available in SPL. Instead, sandbox (only) calls ut_run_list() on start-up, when
+the -u flag is given. This runs the available unit tests, no matter what suite
+they are in.
+
+To create a new SPL test, follow the same rules as above, either adding to an
+existing suite or creating a new one.
+
+An example SPL test is spl_test_load().
+
+
+Writing Python tests
+--------------------
+
+See :doc:`py_testing` for brief notes how to write Python tests. You
+should be able to use the existing tests in test/py/tests as examples.
-- 
2.30.1.766.gb4fecdf3b7-goog

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix
  2021-03-04 13:51 ` [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix Simon Glass
@ 2021-03-04 15:28   ` Heinrich Schuchardt
  2021-03-05  4:09     ` Simon Glass
  0 siblings, 1 reply; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 15:28 UTC (permalink / raw)
  To: u-boot

On 3/4/21 2:51 PM, Simon Glass wrote:
> 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.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (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(-)
>
> diff --git a/include/test/test.h b/include/test/test.h
> index b16c9135f2c..3330dcc72d3 100644
> --- a/include/test/test.h
> +++ b/include/test/test.h
> @@ -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 {
> diff --git a/test/py/conftest.py b/test/py/conftest.py
> index 9bfd9263455..1b909cde9d3 100644
> --- a/test/py/conftest.py
> +++ b/test/py/conftest.py
> @@ -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*$')


We should try to minimize the size of the U-Boot binary. Why do we need
a list that contain both "_ut_" and "_test_"? A single "_ut_" should be
enough to generate a unique section name.

Best regards

Heinrich

>   def generate_ut_subtest(metafunc, fixture_name, sym_path):
>       """Provide parametrization for a ut_subtest fixture.
>
>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 02/42] doc: Tidy up testing section
  2021-03-04 13:50 ` [PATCH v4 02/42] doc: Tidy up testing section Simon Glass
@ 2021-03-04 15:32   ` Heinrich Schuchardt
  0 siblings, 0 replies; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 15:32 UTC (permalink / raw)
  To: u-boot

On 3/4/21 2:50 PM, Simon Glass wrote:
> Tweak this so the output looks a little better.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

> ---
>
> (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(-)
>
> diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
> index 4bc9ca3a6ae..bc74eb53e35 100644
> --- a/doc/develop/testing.rst
> +++ b/doc/develop/testing.rst
> @@ -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.
>
>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 04/42] sandbox: Drop the 'starting...' message unless testing
  2021-03-04 13:50 ` [PATCH v4 04/42] sandbox: Drop the 'starting...' message unless testing Simon Glass
@ 2021-03-04 15:38   ` Heinrich Schuchardt
  0 siblings, 0 replies; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 15:38 UTC (permalink / raw)
  To: u-boot

On 3/4/21 2:50 PM, Simon Glass wrote:
> This message is annoying since it is only useful for testing. Add a flag
> to control whether it is emitted or not, so it can be supressed by
> default.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v1)
>
>   arch/sandbox/cpu/start.c         | 15 +++++++++++++--
>   arch/sandbox/include/asm/state.h |  1 +
>   test/py/tests/test_log.py        |  2 +-
>   3 files changed, 15 insertions(+), 3 deletions(-)
>
> diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
> index c4c4128d465..3dcab6a0cc8 100644
> --- a/arch/sandbox/cpu/start.c
> +++ b/arch/sandbox/cpu/start.c
> @@ -389,6 +389,15 @@ static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
>   }
>   SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
>
> +static int sandbox_cmdline_cb_log_test(struct sandbox_state *state,
> +				       const char *arg)
> +{
> +	state->log_test = true;
> +
> +	return 0;
> +}
> +SANDBOX_CMDLINE_OPT(log_test, 0, "Show logging info on startup (for testing)");
> +
>   static void setup_ram_buf(struct sandbox_state *state)
>   {
>   	/* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
> @@ -491,8 +500,10 @@ 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__);
> +	if (state->log_test) {
> +		log_info("sandbox: starting...\n");
> +		log_debug("debug: %s\n", __func__);
> +	}

Why don't you drop the message entirely?

test_log_dropped() could live well without this message:

Just check that you after restarting you reach a prompt and 'debug:
main' is not in the output.

Best regards

Heinrich

>
>   	/* Do pre- and post-relocation init */
>   	board_init_f(0);
> diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
> index bca13069824..d0e7fdbd5b0 100644
> --- a/arch/sandbox/include/asm/state.h
> +++ b/arch/sandbox/include/asm/state.h
> @@ -105,6 +105,7 @@ struct sandbox_state {
>   	struct list_head mapmem_head;	/* struct sandbox_mapmem_entry */
>   	bool hwspinlock;		/* Hardware Spinlock status */
>   	bool allow_memio;		/* Allow readl() etc. to work */
> +	bool log_test;			/* Output some log info for testing */
>
>   	/*
>   	 * This struct is getting large.
> diff --git a/test/py/tests/test_log.py b/test/py/tests/test_log.py
> index f889120f2b3..e2955ab0770 100644
> --- a/test/py/tests/test_log.py
> +++ b/test/py/tests/test_log.py
> @@ -43,7 +43,7 @@ def test_log_dropped(u_boot_console):
>       """Test dropped 'log' message when debug_uart is activated"""
>
>       cons = u_boot_console
> -    cons.restart_uboot()
> +    cons.restart_uboot_with_flags(['--log_test'])
>       output = cons.get_spawn_output().replace('\r', '')
>       assert 'sandbox: starting...' in output
>       assert (not 'debug: main' in output)
>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 06/42] doc: Explain how to run tests without pytest
  2021-03-04 13:50 ` [PATCH v4 06/42] doc: Explain how to run tests without pytest Simon Glass
@ 2021-03-04 16:13   ` Heinrich Schuchardt
  2021-03-07 22:49     ` Simon Glass
  0 siblings, 1 reply; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 16:13 UTC (permalink / raw)
  To: u-boot

On 3/4/21 2:50 PM, Simon Glass wrote:
> 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.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v2)
>
> 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 | 79 +++++++++++++++++++++++++++++++++++

Thank you for adding this documentation.

tests_sandbox.rst seems to be the wrong file name:

We have three types of tests:

* Python tests
* library tests started using the 'ut' shell commad
* UEFI related tests started using the 'bootefi selftest' command

All three tpyes of tests are run both on real hardware and on the sandbox.

Unfortunately a few Python tests require the sandbox.

Some tests are not run on the sandbox by default though they could, e.g.
the UEFI tests run only on QEMU because you decided not to activate
CONFIG_EFI_SELFTEST on sandbox_defconfig.

Some tests can only be used interactively, e.g.

     setenv efi_selftest block image transfer
     bootefi selftest

Some tests cannot be run on the sandbox at all, e.g.
test/py/tests/test_fpga.py

>   3 files changed, 89 insertions(+)
>   create mode 100644 doc/develop/tests_sandbox.rst
>
> diff --git a/doc/develop/index.rst b/doc/develop/index.rst
> index ac57fdb8f30..50b1de3bdff 100644
> --- a/doc/develop/index.rst
> +++ b/doc/develop/index.rst
> @@ -33,3 +33,4 @@ Testing
>      coccinelle
>      testing
>      py_testing
> +   tests_sandbox
> diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
> index f01ca4dc408..87c90eee271 100644
> --- a/doc/develop/testing.rst
> +++ b/doc/develop/testing.rst
> @@ -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
>   ----
> diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
> new file mode 100644
> index 00000000000..85bbd4f6734
> --- /dev/null
> +++ b/doc/develop/tests_sandbox.rst
> @@ -0,0 +1,79 @@
> +.. SPDX-License-Identifier: GPL-2.0+
> +
> +Tests Under the Hood
> +====================
> +
> +Running sandbox tests directly
> +------------------------------
> +
> +Typically tests are run using the pytest suite. This is easy and always gets
> +things right.

No, in the contrary:

It is difficult to set up Python testing for real hardware. Running
non-Python tests from the console is trivial both on the sandbox and on
real hardware.

> +
> +But it is also possible to run some sandbox tests directly. For example, this
> +runs the dm_test_gpio() test which you can find in test/dm/gpio.c::
> +
> +   $ ./u-boot -T -c "ut dm gpio"
This works only on the sandbox. To create examples assume that the shell
is already started:

     => ut dm gpio

and

     => bootefi selftest

We should add a man-page for the ut command in /doc/usage/.

> +
> +
> +   U-Boot 2021.01
> +
> +   Model: sandbox
> +   DRAM:  128 MiB
> +   WDT:   Started with servicing (60s timeout)
> +   MMC:   mmc2: 2 (SD), mmc1: 1 (SD), mmc0: 0 (SD)
> +   In:    serial
> +   Out:   vidconsole
> +   Err:   vidconsole
> +   Model: sandbox
> +   SCSI:
> +   Net:   eth0: eth at 10002000, eth5: eth at 10003000, eth3: sbe5, eth6: eth at 10004000
> +   Test: dm_test_gpio: gpio.c
> +   Test: dm_test_gpio: gpio.c (flat tree)
> +   Failures: 0
> +

Only the text below is sandbox specific.

> +The -T option tells U-Boot to run with the 'test' devicetree (test.dts) instead

%s/U-Boot/the U-Boot sandbox/g

> +of -D which selects the normal sandbox.dts - this is necessary because many
> +tests rely on things in the test devicetree. If you try running tests without

%s/things/nodes/ ?

> +-T then you may see failures, like::
> +
> +   $ ./u-boot -c "ut dm gpio"
> +
> +
> +   U-Boot 2021.01
> +
> +   DRAM:  128 MiB
> +   WDT:   Not found!
> +   MMC:
> +   In:    serial
> +   Out:   serial
> +   Err:   serial
> +   SCSI:
> +   Net:   No ethernet found.
> +   Please run with test device tree:
> +       ./u-boot -d arch/sandbox/dts/test.dtb
> +   Test: dm_test_gpio: gpio.c
> +   test/dm/gpio.c:37, dm_test_gpio(): 0 == gpio_lookup_name("b4", &dev, &offset, &gpio): Expected 0x0 (0), got 0xffffffea (-22)
> +   Test: dm_test_gpio: gpio.c (flat tree)
> +   test/dm/gpio.c:37, dm_test_gpio(): 0 == gpio_lookup_name("b4", &dev, &offset, &gpio): Expected 0x0 (0), got 0xffffffea (-22)
> +   Failures: 2
> +
> +The message above should provide a hint if you forget. Even running with -D

If I forget what? Please, complete the sentence.

Best regards

Heinrich

> +will produce different results.
> +
> +You can easily use gdb on these tests, without needing --gdbserver::
> +
> +   $ gdb u-boot --args -T -c "ut dm gpio"
> +   ...
> +   (gdb) break dm_test_gpio
> +   Breakpoint 1 at 0x1415bd: file test/dm/gpio.c, line 37.
> +   (gdb) run -T -c "ut dm gpio"
> +   Starting program: u-boot -T -c "ut dm gpio"
> +   Test: dm_test_gpio: gpio.c
> +
> +   Breakpoint 1, dm_test_gpio (uts=0x5555558029a0 <global_dm_test_state>)
> +       at files/test/dm/gpio.c:37
> +   37		ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
> +   (gdb)
> +
> +You can then single-step and look at variables as needed.
> +
>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 33/42] test: log: Rename log main test file to log_ut.c
  2021-03-04 13:51 ` [PATCH v4 33/42] test: log: Rename log main test file to log_ut.c Simon Glass
@ 2021-03-04 17:18   ` Heinrich Schuchardt
  0 siblings, 0 replies; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 17:18 UTC (permalink / raw)
  To: u-boot

On 04.03.21 14:51, Simon Glass wrote:
> The current name is the same as the main test runner file. Rename it to
> avoid confusion.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 39/42] doc: Move coccinelle into its own section
  2021-03-04 13:51 ` [PATCH v4 39/42] doc: Move coccinelle into its own section Simon Glass
@ 2021-03-04 17:23   ` Heinrich Schuchardt
  2021-03-07 22:18     ` Simon Glass
  0 siblings, 1 reply; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 17:23 UTC (permalink / raw)
  To: u-boot

On 04.03.21 14:51, Simon Glass wrote:
> This tool has nothing to do with testing. Create a new section for
> 'refactoring' and move it into there. It is likely that other topics may
> fall under the same heading, such as using moveconfig and search/replace
> tools.

Coccinelle makes static code analysis. This has nothing to do with
refactoring. I saw it as static testing compared to the dynamic testing
done for instance by pytest.

You might put it into one chapter with our continuous integration.

Best regards

Heinrich

>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v1)
>
>  doc/develop/index.rst | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/doc/develop/index.rst b/doc/develop/index.rst
> index 50b1de3bdff..9208668a2d4 100644
> --- a/doc/develop/index.rst
> +++ b/doc/develop/index.rst
> @@ -30,7 +30,14 @@ Testing
>  .. toctree::
>     :maxdepth: 1
>
> -   coccinelle
>     testing
>     py_testing
>     tests_sandbox
> +
> +Refactoring
> +-----------
> +
> +.. toctree::
> +   :maxdepth: 1
> +
> +   coccinelle
>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 42/42] doc: Explain briefly how to write new tests
  2021-03-04 13:51 ` [PATCH v4 42/42] doc: Explain briefly how to write new tests Simon Glass
@ 2021-03-04 18:14   ` Heinrich Schuchardt
  2021-03-07 23:21     ` Simon Glass
  0 siblings, 1 reply; 54+ messages in thread
From: Heinrich Schuchardt @ 2021-03-04 18:14 UTC (permalink / raw)
  To: u-boot

On 04.03.21 14:51, Simon Glass wrote:
> 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.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> 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 | 339 ++++++++++++++++++++++++++++++++++
>  5 files changed, 351 insertions(+), 1 deletion(-)
>  create mode 100644 doc/develop/tests_writing.rst
>
> diff --git a/doc/develop/index.rst b/doc/develop/index.rst
> index 9208668a2d4..8f0a598b5f2 100644
> --- a/doc/develop/index.rst
> +++ b/doc/develop/index.rst
> @@ -32,6 +32,7 @@ Testing
>
>     testing
>     py_testing
> +   tests_writing
>     tests_sandbox
>
>  Refactoring
> diff --git a/doc/develop/py_testing.rst b/doc/develop/py_testing.rst
> index 7f01858cfda..c4cecc0a01b 100644
> --- a/doc/develop/py_testing.rst
> +++ b/doc/develop/py_testing.rst
> @@ -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
> diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
> index b181c2e2e41..ced13ac8bb4 100644
> --- a/doc/develop/testing.rst
> +++ b/doc/develop/testing.rst
> @@ -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
>  -----------
> diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
> index 8c2eac4c5f4..7e5cc84e43c 100644
> --- a/doc/develop/tests_sandbox.rst
> +++ b/doc/develop/tests_sandbox.rst
> @@ -199,3 +199,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.
> +
> diff --git a/doc/develop/tests_writing.rst b/doc/develop/tests_writing.rst
> new file mode 100644
> index 00000000000..d926c00f6e1
> --- /dev/null
> +++ b/doc/develop/tests_writing.rst
> @@ -0,0 +1,339 @@
> +.. SPDX-License-Identifier: GPL-2.0+
> +.. Copyright 2021 Google LLC
> +.. sectionauthor:: Simon Glass <sjg@chromium.org>
> +
> +Writing Tests
> +=============
> +
> +This describes how to write tests in U-Boot and describes the possible options.
> +
> +Test types
> +----------
> +
> +There are two basic types of test in U-Boot:
> +
> +  - Python tests, in test/py/tests
> +  - C tests, in test/ and its subdirectories

You missed lib/efi_selftest/ for the UEFI tests.

> +
> +Python tests talk to U-Boot via the command line. They support both sandbox and
> +real hardware. They typically do not require building test code into U-Boot
> +itself. They are fairly slow to run, due to the command-line interface and there
> +being two separate processes. Python tests are fairly easy to write. They can
> +be a little tricky to debug sometimes due to the voluminous output of pytest.
> +
> +C tests are written directly in U-Boot. While they can be used on boards, they
> +are more commonly used with sandbox, as they obviously add to U-Boot code size.
> +C tests are easy to write so long as the required facilities exist. Where they
> +do not it can involve refactoring or adding new features to sandbox. They are
> +fast to run and easy to debug.
> +
> +Regardless of which test type is used, all tests are collected and run by the
> +pytest framework, so there is typically no need to run them separately. This
> +means that C tests can be used when it makes sense, and Python tests when it
> +doesn't.
> +
> +
> +This table shows how to decide whether to write a C or Python test:
> +
> +=====================  ===========================  =============================
> +Attribute              C test                       Python test
> +=====================  ===========================  =============================
> +Fast to run?           Yes                          No (two separate processes)
> +Easy to write?         Yes, if required test        Yes
> +                       features exist in sandbox

"in sandbox"? Better "on the target system" which might be the sandbox.

You cannot test architecture specific features on the sandbox, e.g.

* hardware drivers
* communication with firmware like TF-A and SBI
* FPGA code
* exception handling

These can be tested on real hardware using C tests.

We still have not succeeded in running file system tests in Gitlab.

For both C and Python there are trivial cases and hard ones.

> +Needs code in U-Boot?  Yes                          No, provided the test can be
> +                                                    executed and the result
> +                                                    determined using the command
> +                                                    line
> +Easy to debug?         Yes                          Yes, but the amount of output can
> +                                                    sometimes require a bit of digging

Debugging the Python tests is non-trivial. Just try to find out why some
FAT tests fail. Though Python calls sync in the test preparation the
Python test does not see the full generated file.

The problem is not the output but the non-output.

> +Can use gdb?           Yes, directly                Yes, with --gdbserver
> +Can run on boards?     Some can, but only if        Yes
> +                       compiled in and not
> +                       dependent on sandbox

Many Python tests cannot be run on boards.

> +=====================  ===========================  =============================
> +
> +
> +Python or C
> +-----------
> +
> +Typically in U-Boot we encourage C test using sandbox for all features. This
> +allows fast testing, easy development and allows contributors to make changes
> +without needing dozens of boards to test with.
> +
> +When a test requires setup or interaction with the running host (such as to
> +generate images and then running U-Boot to check that they can be loaded), or
> +cannot be run on sandbox, Python tests should be used. These should typically

Why should I use Python if the sandbox cannot run it? I could still
write a C test invoked by the "ut" command.

> +NOT rely on running with sandbox, but instead should function correctly on any
> +board supported by U-Boot.
> +
> +
> +How slow are Python tests?
> +--------------------------
> +
> +Under the hood, Python tests work by starting a sandbox test and connecting to

Above you said that they should run on any hardware. So why do you refer
to the sandbox here?

The tests start by invoking U-Boot, be it on physical hardware or on the
sandbox.

> +it via a pipe. Each interaction with the U-Boot process requires at least a
> +context switch to handle the pipe interaction. The test sends a command to
> +U-Boot, which then reacts and shows some output, then the test sees that and
> +continues. Of course on real hardware, communications delays (e.g. with a serial
> +console) make this slower.
> +
> +For comparison, consider a test that checks the 'md' (memory dump). All times
> +below are approximate, as measured on an AMD 2950X system. Here is is the test
> +in Python::
> +
> +   @pytest.mark.buildconfigspec('cmd_memory')
> +   def test_md(u_boot_console):
> +       """Test that md reads memory as expected, and that memory can be modified
> +       using the mw command."""
> +
> +       ram_base = u_boot_utils.find_ram_base(u_boot_console)
> +       addr = '%08x' % ram_base
> +       val = 'a5f09876'
> +       expected_response = addr + ': ' + val
> +       u_boot_console.run_command('mw ' + addr + ' 0 10')
> +       response = u_boot_console.run_command('md ' + addr + ' 10')
> +       assert(not (expected_response in response))
> +       u_boot_console.run_command('mw ' + addr + ' ' + val)
> +       response = u_boot_console.run_command('md ' + addr + ' 10')
> +       assert(expected_response in response)
> +
> +This runs a few commands and checks the output. Note that it runs a command,
> +waits for the response and then checks it agains what is expected. If run by
> +itself it takes around 800ms, including test collection. For 1000 runs it takes
> +19 seconds, or 19ms per run. Of course 1000 runs it not that useful since we
> +only want to run it once.
> +
> +There is no exactly equivalent C test, but here is a similar one that tests 'ms'
> +(memory search)::
> +
> +   /* Test 'ms' command with bytes */
> +   static int mem_test_ms_b(struct unit_test_state *uts)
> +   {
> +      u8 *buf;
> +
> +      buf = map_sysmem(0, BUF_SIZE + 1);
> +      memset(buf, '\0', BUF_SIZE);
> +      buf[0x0] = 0x12;
> +      buf[0x31] = 0x12;
> +      buf[0xff] = 0x12;
> +      buf[0x100] = 0x12;
> +      ut_assertok(console_record_reset_enable());
> +      run_command("ms.b 1 ff 12", 0);
> +      ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................");
> +      ut_assert_nextline("--");
> +      ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12    ................");
> +      ut_assert_nextline("2 matches");
> +      ut_assert_console_end();
> +
> +      ut_asserteq(2, env_get_hex("memmatches", 0));
> +      ut_asserteq(0xff, env_get_hex("memaddr", 0));
> +      ut_asserteq(0xfe, env_get_hex("mempos", 0));
> +
> +      unmap_sysmem(buf);
> +
> +      return 0;
> +   }
> +   MEM_TEST(mem_test_ms_b, UT_TESTF_CONSOLE_REC);
> +
> +This runs the command directly in U-Boot, then checks the console output, also
> +directly in U-Boot. If run by itself this takes 100ms. For 1000 runs it takes
> +660ms, or 0.66ms per run.
> +
> +So overall running a C test is perhaps 8 times faster individually and the
> +interactions are perhaps 25 times faster.
> +
> +It should also be noted that the C test is fairly easy to debug. You can set a
> +breakpoint on do_mem_search(), which is what implements the 'ms' command,
> +single step to see what might be wrong, etc. That is also possible with the
> +pytest, but requires two terminals and --gdbserver.
> +
> +
> +Why does speed matter?
> +----------------------
> +
> +Many development activities rely on running tests:
> +
> +  - 'git bisect run make qcheck' can be used to find a failing commit
> +  - test-driven development relies on quick iteration of build/test
> +  - U-Boot's continuous integration (CI) systems make use of tests. Running
> +      all sandbox tests typically takes 90 seconds and running each qemu test
> +      takes about 30 seconds. This is currently dwarfed by the time taken to
> +      build all boards
> +
> +As U-Boot continues to grow its feature set, fast and reliable tests are a
> +critical factor factor in developer productivity and happiness.
> +
> +
> +Writing C tests
> +---------------
> +
> +C tests are arranged into suites which are typically executed by the 'ut'
> +command. Each suite is in its own file. This section describes how to accomplish
> +some common test tasks.

UEFI tests are written in C, started with 'bootefi selftest', and are in
non of the suites you refer to.

Best regards

Heinrich

> +
> +
> +Add a new driver model test
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +Use this when adding a test for a new or existing uclass, adding new operations
> +or features to a uclass, adding new ofnode or dev_read_() functions, or anything
> +else related to driver model.
> +
> +Find a suitable place for your test, perhaps near other test functions in
> +existing code, or in a new file. Each uclass should have its own test file.
> +
> +Declare the test with::
> +
> +   /* Test that ... */
> +   static int dm_test_uclassname_what(struct unit_test_state *uts)
> +   {
> +      /* test code here */
> +
> +      return 0;
> +   }
> +   DM_TEST(dm_test_uclassname_what, UT_TESTF_SCAN_FDT);
> +
> +Replace 'uclassname' with the name of your uclass, if applicable. Replace 'what'
> +with what you are testing.
> +
> +The flags for DM_TEST() are defined in test/test.h and you typically want
> +UT_TESTF_SCAN_FDT so that the devicetree is scanned and all devices are bound
> +and ready for use. The DM_TEST macro adds UT_TESTF_DM automatically so that
> +the test runner knows it is a driver model test.
> +
> +Driver model tests are special in that the entire driver model state is
> +recreated anew for each test. This ensures that if a previous test deletes a
> +device, for example, it does not affect subsequent tests. Driver model tests
> +also run both with livetree and flattree, to ensure that both devicetree
> +implementations work as expected.
> +
> +Example commit: c48cb7ebfb4 ("sandbox: add ADC unit tests") [1]
> +
> +[1] https://gitlab.denx.de/u-boot/u-boot/-/commit/c48cb7ebfb4
> +
> +
> +Add a C test to an existing suite
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +Use this when you are adding to or modifying an existing feature outside driver
> +model. An example is bloblist.
> +
> +Add a new function in the same file as the rest of the suite and register it
> +with the suite. For example, to add a new mem_search test::
> +
> +   /* Test 'ms' command with 32-bit values */
> +   static int mem_test_ms_new_thing(struct unit_test_state *uts)
> +   {
> +         /* test code here*/
> +
> +         return 0;
> +   }
> +   MEM_TEST(mem_test_ms_new_thing, UT_TESTF_CONSOLE_REC);
> +
> +Note that the MEM_TEST() macros is defined at the top of the file.
> +
> +Example commit: 9fe064646d2 ("bloblist: Support relocating to a larger space") [1]
> +
> +[1] https://gitlab.denx.de/u-boot/u-boot/-/commit/9fe064646d2
> +
> +
> +Add a new test suite
> +~~~~~~~~~~~~~~~~~~~~
> +
> +Each suite should focus on one feature or subsystem, so if you are writing a
> +new one of those, you should add a new suite.
> +
> +Create a new file in test/ or a subdirectory and define a macro to register the
> +suite. For example::
> +
> +   #include <common.h>
> +   #include <console.h>
> +   #include <mapmem.h>
> +   #include <dm/test.h>
> +   #include <test/ut.h>
> +
> +   /* Declare a new wibble test */
> +   #define WIBBLE_TEST(_name, _flags)   UNIT_TEST(_name, _flags, wibble_test)
> +
> +   /* Tetss go here */
> +
> +   /* At the bottom of the file: */
> +
> +   int do_ut_wibble(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
> +   {
> +     struct unit_test *tests = UNIT_TEST_SUITE_START(wibble_test);
> +     const int n_ents = UNIT_TEST_SUITE_COUNT(wibble_test);
> +
> +     return cmd_ut_category("cmd_wibble", "wibble_test_", tests, n_ents, argc, argv);
> +   }
> +
> +Then add new tests to it as above.
> +
> +Register this new suite in test/cmd_ut.c by adding to cmd_ut_sub[]::
> +
> +  /* Within cmd_ut_sub[]... */
> +
> +  U_BOOT_CMD_MKENT(wibble, CONFIG_SYS_MAXARGS, 1, do_ut_wibble, "", ""),
> +
> +and adding new help to ut_help_text[]::
> +
> +  "ut wibble - Test the wibble feature\n"
> +
> +If your feature is conditional on a particular Kconfig, then you can use #ifdef
> +to control that.
> +
> +Finally, add the test to the build by adding to the Makefile in the same
> +directory::
> +
> +  obj-$(CONFIG_$(SPL_)CMDLINE) += wibble.o
> +
> +Note that CMDLINE is never enabled in SPL, so this test will only be present in
> +U-Boot proper. See below for how to do SPL tests.
> +
> +As before, you can add an extra Kconfig check if needed::
> +
> +  ifneq ($(CONFIG_$(SPL_)WIBBLE),)
> +  obj-$(CONFIG_$(SPL_)CMDLINE) += wibble.o
> +  endif
> +
> +
> +Example commit: 919e7a8fb64 ("test: Add a simple test for bloblist") [1]
> +
> +[1] https://gitlab.denx.de/u-boot/u-boot/-/commit/919e7a8fb64
> +
> +
> +Making the test run from pytest
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +All C tests must run from pytest. Typically this is automatic, since pytest
> +scans the U-Boot executable for available tests to run. So long as you have a
> +'ut' subcommand for your test suite, it will run. The same applies for driver
> +model tests since they use the 'ut dm' subcommand.
> +
> +See test/py/tests/test_ut.py for how unit tests are run.
> +
> +
> +Add a C test for SPL
> +~~~~~~~~~~~~~~~~~~~~
> +
> +Note: C tests are only available for sandbox_spl at present. There is currently
> +no mechanism in other boards to existing SPL tests even if they are built into
> +the image.
> +
> +SPL tests cannot be run from the 'ut' command since there are no commands
> +available in SPL. Instead, sandbox (only) calls ut_run_list() on start-up, when
> +the -u flag is given. This runs the available unit tests, no matter what suite
> +they are in.
> +
> +To create a new SPL test, follow the same rules as above, either adding to an
> +existing suite or creating a new one.
> +
> +An example SPL test is spl_test_load().
> +
> +
> +Writing Python tests
> +--------------------
> +
> +See :doc:`py_testing` for brief notes how to write Python tests. You
> +should be able to use the existing tests in test/py/tests as examples.
>

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix
  2021-03-04 15:28   ` Heinrich Schuchardt
@ 2021-03-05  4:09     ` Simon Glass
  0 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-05  4:09 UTC (permalink / raw)
  To: u-boot

Hi Heinrich,

On Thu, 4 Mar 2021 at 10:28, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 3/4/21 2:51 PM, Simon Glass wrote:
> > 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.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > ---
> >
> > (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(-)
> >
> > diff --git a/include/test/test.h b/include/test/test.h
> > index b16c9135f2c..3330dcc72d3 100644
> > --- a/include/test/test.h
> > +++ b/include/test/test.h
> > @@ -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 {
> > diff --git a/test/py/conftest.py b/test/py/conftest.py
> > index 9bfd9263455..1b909cde9d3 100644
> > --- a/test/py/conftest.py
> > +++ b/test/py/conftest.py
> > @@ -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*$')
>
>
> We should try to minimize the size of the U-Boot binary. Why do we need
> a list that contain both "_ut_" and "_test_"? A single "_ut_" should be
> enough to generate a unique section name.

Possibly you are right. But that is not the subject of this patch.

I think after this series (and applying the patch to select tests with
wildcards) we might be able to do some renaming, since most things are
in macros.

Regards,
Simon

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 39/42] doc: Move coccinelle into its own section
  2021-03-04 17:23   ` Heinrich Schuchardt
@ 2021-03-07 22:18     ` Simon Glass
  0 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-07 22:18 UTC (permalink / raw)
  To: u-boot

Hi Heinrich,

On Thu, 4 Mar 2021 at 12:23, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 04.03.21 14:51, Simon Glass wrote:
> > This tool has nothing to do with testing. Create a new section for
> > 'refactoring' and move it into there. It is likely that other topics may
> > fall under the same heading, such as using moveconfig and search/replace
> > tools.
>
> Coccinelle makes static code analysis. This has nothing to do with
> refactoring. I saw it as static testing compared to the dynamic testing
> done for instance by pytest.
>
> You might put it into one chapter with our continuous integration.

I don't know where that is, so I'll drop this patch.

I have only every used spatch, and that only for refactoring.

Regards,
Simon

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 06/42] doc: Explain how to run tests without pytest
  2021-03-04 16:13   ` Heinrich Schuchardt
@ 2021-03-07 22:49     ` Simon Glass
  0 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-07 22:49 UTC (permalink / raw)
  To: u-boot

Hi Heinrich,

On Thu, 4 Mar 2021 at 11:13, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 3/4/21 2:50 PM, Simon Glass wrote:
> > 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.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > ---
> >
> > (no changes since v2)
> >
> > 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 | 79 +++++++++++++++++++++++++++++++++++
>
> Thank you for adding this documentation.
>
> tests_sandbox.rst seems to be the wrong file name:

But if you look at it, it is all about running tests using sandbox.

>
> We have three types of tests:
>
> * Python tests
> * library tests started using the 'ut' shell commad
> * UEFI related tests started using the 'bootefi selftest' command
>
> All three tpyes of tests are run both on real hardware and on the sandbox.
>
> Unfortunately a few Python tests require the sandbox.
>
> Some tests are not run on the sandbox by default though they could, e.g.
> the UEFI tests run only on QEMU because you decided not to activate
> CONFIG_EFI_SELFTEST on sandbox_defconfig.

It was a long time ago, but I recall spending a year trying to
upstream a bit of refactoring to allow it and then giving up. If you
have a way to do it, please go ahead.

>
> Some tests can only be used interactively, e.g.
>
>      setenv efi_selftest block image transfer
>      bootefi selftest
>
> Some tests cannot be run on the sandbox at all, e.g.
> test/py/tests/test_fpga.py

That is a bug :-)

>
> >   3 files changed, 89 insertions(+)
> >   create mode 100644 doc/develop/tests_sandbox.rst
> >
> > diff --git a/doc/develop/index.rst b/doc/develop/index.rst
> > index ac57fdb8f30..50b1de3bdff 100644
> > --- a/doc/develop/index.rst
> > +++ b/doc/develop/index.rst
> > @@ -33,3 +33,4 @@ Testing
> >      coccinelle
> >      testing
> >      py_testing
> > +   tests_sandbox
> > diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
> > index f01ca4dc408..87c90eee271 100644
> > --- a/doc/develop/testing.rst
> > +++ b/doc/develop/testing.rst
> > @@ -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
> >   ----
> > diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
> > new file mode 100644
> > index 00000000000..85bbd4f6734
> > --- /dev/null
> > +++ b/doc/develop/tests_sandbox.rst
> > @@ -0,0 +1,79 @@
> > +.. SPDX-License-Identifier: GPL-2.0+
> > +
> > +Tests Under the Hood
> > +====================
> > +
> > +Running sandbox tests directly
> > +------------------------------
> > +
> > +Typically tests are run using the pytest suite. This is easy and always gets
> > +things right.
>
> No, in the contrary:
>
> It is difficult to set up Python testing for real hardware. Running
> non-Python tests from the console is trivial both on the sandbox and on
> real hardware.

OK I will reword it.

>
> > +
> > +But it is also possible to run some sandbox tests directly. For example, this
> > +runs the dm_test_gpio() test which you can find in test/dm/gpio.c::
> > +
> > +   $ ./u-boot -T -c "ut dm gpio"
> This works only on the sandbox. To create examples assume that the shell
> is already started:

The whole point of this file is to describe using sandbox!

>
>      => ut dm gpio
>
> and
>
>      => bootefi selftest
>
> We should add a man-page for the ut command in /doc/usage/.

OK, please go ahead.

>
> > +
> > +
> > +   U-Boot 2021.01
> > +
> > +   Model: sandbox
> > +   DRAM:  128 MiB
> > +   WDT:   Started with servicing (60s timeout)
> > +   MMC:   mmc2: 2 (SD), mmc1: 1 (SD), mmc0: 0 (SD)
> > +   In:    serial
> > +   Out:   vidconsole
> > +   Err:   vidconsole
> > +   Model: sandbox
> > +   SCSI:
> > +   Net:   eth0: eth at 10002000, eth5: eth at 10003000, eth3: sbe5, eth6: eth at 10004000
> > +   Test: dm_test_gpio: gpio.c
> > +   Test: dm_test_gpio: gpio.c (flat tree)
> > +   Failures: 0
> > +
>
> Only the text below is sandbox specific.
>
> > +The -T option tells U-Boot to run with the 'test' devicetree (test.dts) instead
>
> %s/U-Boot/the U-Boot sandbox/g
>
> > +of -D which selects the normal sandbox.dts - this is necessary because many
> > +tests rely on things in the test devicetree. If you try running tests without
>
> %s/things/nodes/ ?

or properties

>
> > +-T then you may see failures, like::
> > +
> > +   $ ./u-boot -c "ut dm gpio"
> > +
> > +
> > +   U-Boot 2021.01
> > +
> > +   DRAM:  128 MiB
> > +   WDT:   Not found!
> > +   MMC:
> > +   In:    serial
> > +   Out:   serial
> > +   Err:   serial
> > +   SCSI:
> > +   Net:   No ethernet found.
> > +   Please run with test device tree:
> > +       ./u-boot -d arch/sandbox/dts/test.dtb
> > +   Test: dm_test_gpio: gpio.c
> > +   test/dm/gpio.c:37, dm_test_gpio(): 0 == gpio_lookup_name("b4", &dev, &offset, &gpio): Expected 0x0 (0), got 0xffffffea (-22)
> > +   Test: dm_test_gpio: gpio.c (flat tree)
> > +   test/dm/gpio.c:37, dm_test_gpio(): 0 == gpio_lookup_name("b4", &dev, &offset, &gpio): Expected 0x0 (0), got 0xffffffea (-22)
> > +   Failures: 2
> > +
> > +The message above should provide a hint if you forget. Even running with -D
>
> If I forget what? Please, complete the sentence.

OK.

Regards,
Simon

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH v4 42/42] doc: Explain briefly how to write new tests
  2021-03-04 18:14   ` Heinrich Schuchardt
@ 2021-03-07 23:21     ` Simon Glass
  0 siblings, 0 replies; 54+ messages in thread
From: Simon Glass @ 2021-03-07 23:21 UTC (permalink / raw)
  To: u-boot

Hi Heinrich,

On Thu, 4 Mar 2021 at 13:14, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 04.03.21 14:51, Simon Glass wrote:
> > 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.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > ---
> >
> > 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 | 339 ++++++++++++++++++++++++++++++++++
> >  5 files changed, 351 insertions(+), 1 deletion(-)
> >  create mode 100644 doc/develop/tests_writing.rst
> >
> > diff --git a/doc/develop/index.rst b/doc/develop/index.rst
> > index 9208668a2d4..8f0a598b5f2 100644
> > --- a/doc/develop/index.rst
> > +++ b/doc/develop/index.rst
> > @@ -32,6 +32,7 @@ Testing
> >
> >     testing
> >     py_testing
> > +   tests_writing
> >     tests_sandbox
> >
> >  Refactoring
> > diff --git a/doc/develop/py_testing.rst b/doc/develop/py_testing.rst
> > index 7f01858cfda..c4cecc0a01b 100644
> > --- a/doc/develop/py_testing.rst
> > +++ b/doc/develop/py_testing.rst
> > @@ -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
> > diff --git a/doc/develop/testing.rst b/doc/develop/testing.rst
> > index b181c2e2e41..ced13ac8bb4 100644
> > --- a/doc/develop/testing.rst
> > +++ b/doc/develop/testing.rst
> > @@ -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
> >  -----------
> > diff --git a/doc/develop/tests_sandbox.rst b/doc/develop/tests_sandbox.rst
> > index 8c2eac4c5f4..7e5cc84e43c 100644
> > --- a/doc/develop/tests_sandbox.rst
> > +++ b/doc/develop/tests_sandbox.rst
> > @@ -199,3 +199,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.
> > +
> > diff --git a/doc/develop/tests_writing.rst b/doc/develop/tests_writing.rst
> > new file mode 100644
> > index 00000000000..d926c00f6e1
> > --- /dev/null
> > +++ b/doc/develop/tests_writing.rst
> > @@ -0,0 +1,339 @@
> > +.. SPDX-License-Identifier: GPL-2.0+
> > +.. Copyright 2021 Google LLC
> > +.. sectionauthor:: Simon Glass <sjg@chromium.org>
> > +
> > +Writing Tests
> > +=============
> > +
> > +This describes how to write tests in U-Boot and describes the possible options.
> > +
> > +Test types
> > +----------
> > +
> > +There are two basic types of test in U-Boot:
> > +
> > +  - Python tests, in test/py/tests
> > +  - C tests, in test/ and its subdirectories
>
> You missed lib/efi_selftest/ for the UEFI tests.

OK I will mention that. But really it is something of a pain that they
are separate.

>
> > +
> > +Python tests talk to U-Boot via the command line. They support both sandbox and
> > +real hardware. They typically do not require building test code into U-Boot
> > +itself. They are fairly slow to run, due to the command-line interface and there
> > +being two separate processes. Python tests are fairly easy to write. They can
> > +be a little tricky to debug sometimes due to the voluminous output of pytest.
> > +
> > +C tests are written directly in U-Boot. While they can be used on boards, they
> > +are more commonly used with sandbox, as they obviously add to U-Boot code size.
> > +C tests are easy to write so long as the required facilities exist. Where they
> > +do not it can involve refactoring or adding new features to sandbox. They are
> > +fast to run and easy to debug.
> > +
> > +Regardless of which test type is used, all tests are collected and run by the
> > +pytest framework, so there is typically no need to run them separately. This
> > +means that C tests can be used when it makes sense, and Python tests when it
> > +doesn't.
> > +
> > +
> > +This table shows how to decide whether to write a C or Python test:
> > +
> > +=====================  ===========================  =============================
> > +Attribute              C test                       Python test
> > +=====================  ===========================  =============================
> > +Fast to run?           Yes                          No (two separate processes)
> > +Easy to write?         Yes, if required test        Yes
> > +                       features exist in sandbox
>
> "in sandbox"? Better "on the target system" which might be the sandbox.

OK.

>
> You cannot test architecture specific features on the sandbox, e.g.
>
> * hardware drivers
> * communication with firmware like TF-A and SBI
> * FPGA code
> * exception handling
>
> These can be tested on real hardware using C tests.

We have managed to test various arch-specific things on sandbox. We
could easily add FPGA tests just by implementing a suitable driver.

The bit that I think you are missing here is that sandbox is the
standard way of running tests. Of course we can run on real hardware,
but that requires having that hardware and having the out-of-tree test
harness stuff set up (Stephen Warren's work). The more we can do in
sandbox, the easier it is.

>
> We still have not succeeded in running file system tests in Gitlab.

I never run them as they are too slow.

>
> For both C and Python there are trivial cases and hard ones.
>
> > +Needs code in U-Boot?  Yes                          No, provided the test can be
> > +                                                    executed and the result
> > +                                                    determined using the command
> > +                                                    line
> > +Easy to debug?         Yes                          Yes, but the amount of output can
> > +                                                    sometimes require a bit of digging
>
> Debugging the Python tests is non-trivial. Just try to find out why some
> FAT tests fail. Though Python calls sync in the test preparation the
> Python test does not see the full generated file.
>
> The problem is not the output but the non-output.

OK well I'll try some different wording.

>
> > +Can use gdb?           Yes, directly                Yes, with --gdbserver
> > +Can run on boards?     Some can, but only if        Yes
> > +                       compiled in and not
> > +                       dependent on sandbox
>
> Many Python tests cannot be run on boards.

OK will update it to 'some'

>
> > +=====================  ===========================  =============================
> > +
> > +
> > +Python or C
> > +-----------
> > +
> > +Typically in U-Boot we encourage C test using sandbox for all features. This
> > +allows fast testing, easy development and allows contributors to make changes
> > +without needing dozens of boards to test with.
> > +
> > +When a test requires setup or interaction with the running host (such as to
> > +generate images and then running U-Boot to check that they can be loaded), or
> > +cannot be run on sandbox, Python tests should be used. These should typically
>
> Why should I use Python if the sandbox cannot run it? I could still
> write a C test invoked by the "ut" command.
>
> > +NOT rely on running with sandbox, but instead should function correctly on any
> > +board supported by U-Boot.
> > +
> > +
> > +How slow are Python tests?
> > +--------------------------
> > +
> > +Under the hood, Python tests work by starting a sandbox test and connecting to
>
> Above you said that they should run on any hardware. So why do you refer
> to the sandbox here?

Because I am talking about sandbox. I will update it to make that clear.

[..]

> > +Writing C tests
> > +---------------
> > +
> > +C tests are arranged into suites which are typically executed by the 'ut'
> > +command. Each suite is in its own file. This section describes how to accomplish
> > +some common test tasks.
>
> UEFI tests are written in C, started with 'bootefi selftest', and are in
> non of the suites you refer to.

OK will mention that again here.

Regards,
Simon

^ permalink raw reply	[flat|nested] 54+ messages in thread

end of thread, other threads:[~2021-03-07 23:21 UTC | newest]

Thread overview: 54+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-04 13:50 [PATCH v4 00/42] test: Refactor tests to have a single test runner Simon Glass
2021-03-04 13:50 ` [PATCH v4 01/42] dm: core: Fix allocation of empty of-platdata Simon Glass
2021-03-04 13:50 ` [PATCH v4 02/42] doc: Tidy up testing section Simon Glass
2021-03-04 15:32   ` Heinrich Schuchardt
2021-03-04 13:50 ` [PATCH v4 03/42] doc: Document make tcheck Simon Glass
2021-03-04 13:50 ` [PATCH v4 04/42] sandbox: Drop the 'starting...' message unless testing Simon Glass
2021-03-04 15:38   ` Heinrich Schuchardt
2021-03-04 13:50 ` [PATCH v4 05/42] test: Re-enable test_ofplatdata Simon Glass
2021-03-04 13:50 ` [PATCH v4 06/42] doc: Explain how to run tests without pytest Simon Glass
2021-03-04 16:13   ` Heinrich Schuchardt
2021-03-07 22:49     ` Simon Glass
2021-03-04 13:50 ` [PATCH v4 07/42] doc: Document how sandbox_spl_tests are run Simon Glass
2021-03-04 13:50 ` [PATCH v4 08/42] test: Correct setexpr test prefix Simon Glass
2021-03-04 13:50 ` [PATCH v4 09/42] test: Mark all driver model tests with a flag Simon Glass
2021-03-04 13:50 ` [PATCH v4 10/42] test: Rename test-main.c to test-dm.c Simon Glass
2021-03-04 13:50 ` [PATCH v4 11/42] test: Add an overall test runner Simon Glass
2021-03-04 13:50 ` [PATCH v4 12/42] test: Create pre/post-run functions Simon Glass
2021-03-04 13:50 ` [PATCH v4 13/42] test: Call test_pre/post_run() from driver model tests Simon Glass
2021-03-04 13:50 ` [PATCH v4 14/42] test: Move dm_extended_scan() to test_pre_run() Simon Glass
2021-03-04 13:50 ` [PATCH v4 15/42] test: Move do_autoprobe() " Simon Glass
2021-03-04 13:50 ` [PATCH v4 16/42] test: Move dm_scan_plat() " Simon Glass
2021-03-04 13:50 ` [PATCH v4 17/42] test: Drop mallinfo() work-around Simon Glass
2021-03-04 13:50 ` [PATCH v4 18/42] test: Move console silencing to test_pre_run() Simon Glass
2021-03-04 13:50 ` [PATCH v4 19/42] test: Move delay skipping " Simon Glass
2021-03-04 13:50 ` [PATCH v4 20/42] test: Handle driver model reinit in test_pre_run() Simon Glass
2021-03-04 13:50 ` [PATCH v4 21/42] test: Drop struct dm_test_state Simon Glass
2021-03-04 13:50 ` [PATCH v4 22/42] test: Move dm_test_init() into test-main.c Simon Glass
2021-03-04 13:50 ` [PATCH v4 23/42] test: Move dm_test_destroy() " Simon Glass
2021-03-04 13:51 ` [PATCH v4 24/42] test: Move test running into a separate function Simon Glass
2021-03-04 13:51 ` [PATCH v4 25/42] test: Use ut_run_test() to run driver model tests Simon Glass
2021-03-04 13:51 ` [PATCH v4 26/42] test: Drop dm_do_test() Simon Glass
2021-03-04 13:51 ` [PATCH v4 27/42] test: Add ut_run_test_live_flat() to run tests twice Simon Glass
2021-03-04 13:51 ` [PATCH v4 28/42] test: Use a local variable for test state Simon Glass
2021-03-04 13:51 ` [PATCH v4 29/42] test: Run driver-model tests using ut_run_list() Simon Glass
2021-03-04 13:51 ` [PATCH v4 30/42] test: Use return values in dm_test_run() Simon Glass
2021-03-04 13:51 ` [PATCH v4 31/42] test: Move the devicetree check into ut_run_list() Simon Glass
2021-03-04 13:51 ` [PATCH v4 32/42] test: Move restoring of driver model state to ut_run_list() Simon Glass
2021-03-04 13:51 ` [PATCH v4 33/42] test: log: Rename log main test file to log_ut.c Simon Glass
2021-03-04 17:18   ` Heinrich Schuchardt
2021-03-04 13:51 ` [PATCH v4 34/42] test: Add a macros for finding tests in linker_lists Simon Glass
2021-03-04 13:51 ` [PATCH v4 35/42] test: Rename all linker lists to have a ut_ prefix Simon Glass
2021-03-04 15:28   ` Heinrich Schuchardt
2021-03-05  4:09     ` Simon Glass
2021-03-04 13:51 ` [PATCH v4 36/42] test: Allow SPL to run any available test Simon Glass
2021-03-04 13:51 ` [PATCH v4 37/42] sandbox: Update os_find_u_boot() to find the .img file Simon Glass
2021-03-04 13:51 ` [PATCH v4 38/42] spl: Convert spl_fit to work with sandbox Simon Glass
2021-03-04 13:51 ` [PATCH v4 39/42] doc: Move coccinelle into its own section Simon Glass
2021-03-04 17:23   ` Heinrich Schuchardt
2021-03-07 22:18     ` Simon Glass
2021-03-04 13:51 ` [PATCH v4 40/42] spl: test: Add a test for spl_load_simple_fit() Simon Glass
2021-03-04 13:51 ` [PATCH v4 41/42] test: sandbox: Move sandbox test docs into doc/develop Simon Glass
2021-03-04 13:51 ` [PATCH v4 42/42] doc: Explain briefly how to write new tests Simon Glass
2021-03-04 18:14   ` Heinrich Schuchardt
2021-03-07 23:21     ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.