blob: c4cecc0a01bda949970d36640054d88eb754d4b2 [file] [log] [blame]
Heinrich Schuchardt01576192021-01-18 20:24:03 +01001U-Boot pytest suite
2===================
Stephen Warrend2015062016-01-15 11:15:24 -07003
Heinrich Schuchardt01576192021-01-18 20:24:03 +01004Introduction
5------------
Stephen Warrend2015062016-01-15 11:15:24 -07006
7This tool aims to test U-Boot by executing U-Boot shell commands using the
8console interface. A single top-level script exists to execute or attach to the
9U-Boot console, run the entire script of tests against it, and summarize the
10results. Advantages of this approach are:
11
12- Testing is performed in the same way a user or script would interact with
13 U-Boot; there can be no disconnect.
14- There is no need to write or embed test-related code into U-Boot itself.
15 It is asserted that writing test-related code in Python is simpler and more
Simon Glassfc328332021-03-07 17:35:17 -070016 flexible than writing it all in C. But see :doc:`tests_writing` for caveats
17 and more discussion / analysis.
Stephen Warrend2015062016-01-15 11:15:24 -070018- It is reasonably simple to interact with U-Boot in this way.
19
Heinrich Schuchardt01576192021-01-18 20:24:03 +010020Requirements
21------------
Stephen Warrend2015062016-01-15 11:15:24 -070022
23The test suite is implemented using pytest. Interaction with the U-Boot console
24involves executing some binary and interacting with its stdin/stdout. You will
25need to implement various "hook" scripts that are called by the test suite at
26the appropriate time.
27
Heinrich Schuchardt01576192021-01-18 20:24:03 +010028In order to run the test suite at a minimum we require that both Python 3 and
29pip for Python 3 are installed. All of the required python modules are
30described in the requirements.txt file in the /test/py/ directory and can be
31installed via the command
32
33.. code-block:: bash
34
35 pip install -r requirements.txt
Stephen Warrend2015062016-01-15 11:15:24 -070036
Tom Riniddaa8be2019-10-24 11:59:26 -040037In order to execute certain tests on their supported platforms other tools
Heinrich Schuchardt01576192021-01-18 20:24:03 +010038will be required. The following is an incomplete list:
Tom Riniddaa8be2019-10-24 11:59:26 -040039
Heinrich Schuchardt01576192021-01-18 20:24:03 +010040* gdisk
41* dfu-util
42* dtc
43* openssl
44* sudo OR guestmount
45* e2fsprogs
46* util-linux
47* coreutils
48* dosfstools
49* efitools
50* mount
51* mtools
52* sbsigntool
53* udisks2
AKASHI Takahirofe6ca4d2020-04-14 11:51:49 +090054
Heinrich Schuchardt01576192021-01-18 20:24:03 +010055Please use the appropriate commands for your distribution to match these tools
Tom Riniddaa8be2019-10-24 11:59:26 -040056up with the package that provides them.
Stephen Warrend2015062016-01-15 11:15:24 -070057
58The test script supports either:
59
60- Executing a sandbox port of U-Boot on the local machine as a sub-process,
61 and interacting with it over stdin/stdout.
62- Executing an external "hook" scripts to flash a U-Boot binary onto a
63 physical board, attach to the board's console stream, and reset the board.
64 Further details are described later.
65
Heinrich Schuchardt01576192021-01-18 20:24:03 +010066Using `virtualenv` to provide requirements
67~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -070068
Tom Riniddaa8be2019-10-24 11:59:26 -040069The recommended way to run the test suite, in order to ensure reproducibility
70is to use `virtualenv` to set up the necessary environment. This can be done
71via the following commands:
Stephen Warrend2015062016-01-15 11:15:24 -070072
Stephen Warrend2015062016-01-15 11:15:24 -070073
Heinrich Schuchardt01576192021-01-18 20:24:03 +010074.. code-block:: console
Stephen Warrend2015062016-01-15 11:15:24 -070075
Heinrich Schuchardt01576192021-01-18 20:24:03 +010076 $ cd /path/to/u-boot
77 $ sudo apt-get install python3 python3-virtualenv
78 $ virtualenv -p /usr/bin/python3 venv
79 $ . ./venv/bin/activate
80 $ pip install -r test/py/requirements.txt
81
82Testing sandbox
83---------------
84
85To run the test suite on the sandbox port (U-Boot built as a native user-space
Stephen Warrend2015062016-01-15 11:15:24 -070086application), simply execute:
87
Heinrich Schuchardt01576192021-01-18 20:24:03 +010088.. code-block:: bash
89
90 ./test/py/test.py --bd sandbox --build
Stephen Warrend2015062016-01-15 11:15:24 -070091
92The `--bd` option tells the test suite which board type is being tested. This
93lets the test suite know which features the board has, and hence exactly what
94can be tested.
95
96The `--build` option tells U-Boot to compile U-Boot. Alternatively, you may
97omit this option and build U-Boot yourself, in whatever way you choose, before
98running the test script.
99
100The test script will attach to U-Boot, execute all valid tests for the board,
101then print a summary of the test process. A complete log of the test session
102will be written to `${build_dir}/test-log.html`. This is best viewed in a web
103browser, but may be read directly as plain text, perhaps with the aid of the
104`html2text` utility.
105
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100106Testing under a debugger
107~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend70facf2016-02-08 14:49:02 -0700108
109If you need to run sandbox under a debugger, you may pass the command-line
110option `--gdbserver COMM`. This causes two things to happens:
111
112- Instead of running U-Boot directly, it will be run under gdbserver, with
113 debug communication via the channel `COMM`. You can attach a debugger to the
114 sandbox process in order to debug it. See `man gdbserver` and the example
115 below for details of valid values for `COMM`.
116- All timeouts in tests are disabled, allowing U-Boot an arbitrary amount of
117 time to execute commands. This is useful if U-Boot is stopped at a breakpoint
118 during debugging.
119
120A usage example is:
121
122Window 1:
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100123
124.. code-block:: bash
125
126 ./test/py/test.py --bd sandbox --gdbserver localhost:1234
Stephen Warrend70facf2016-02-08 14:49:02 -0700127
128Window 2:
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100129
130.. code-block:: bash
131
132 gdb ./build-sandbox/u-boot -ex 'target remote localhost:1234'
Stephen Warrend70facf2016-02-08 14:49:02 -0700133
134Alternatively, you could leave off the `-ex` option and type the command
135manually into gdb once it starts.
136
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100137You can use any debugger you wish, as long as it speaks the gdb remote
Stephen Warrend70facf2016-02-08 14:49:02 -0700138protocol, or any graphical wrapper around gdb.
139
140Some tests deliberately cause the sandbox process to exit, e.g. to test the
141reset command, or sandbox's CTRL-C handling. When this happens, you will need
142to attach the debugger to the new sandbox instance. If these tests are not
143relevant to your debugging session, you can skip them using pytest's -k
144command-line option; see the next section.
145
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100146Command-line options
147--------------------
Stephen Warrend2015062016-01-15 11:15:24 -0700148
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100149--board-type, --bd, -B
150 set the type of the board to be tested. For example, `sandbox` or `seaboard`.
151
152--board-identity`, --id
153 sets the identity of the board to be tested. This allows differentiation
154 between multiple instances of the same type of physical board that are
155 attached to the same host machine. This parameter is not interpreted by th
156 test script in any way, but rather is simply passed to the hook scripts
157 described below, and may be used in any site-specific way deemed necessary.
158
159--build
160 indicates that the test script should compile U-Boot itself before running
161 the tests. If using this option, make sure that any environment variables
162 required by the build process are already set, such as `$CROSS_COMPILE`.
163
164--buildman
165 indicates that `--build` should use buildman to build U-Boot. There is no need
166 to set $CROSS_COMPILE` in this case since buildman handles it.
167
168--build-dir
169 sets the directory containing the compiled U-Boot binaries. If omitted, this
170 is `${source_dir}/build-${board_type}`.
171
172--result-dir
173 sets the directory to write results, such as log files, into.
174 If omitted, the build directory is used.
175
176--persistent-data-dir
177 sets the directory used to store persistent test data. This is test data that
178 may be re-used across test runs, such as file-system images.
Stephen Warrend2015062016-01-15 11:15:24 -0700179
Stephen Warrend70facf2016-02-08 14:49:02 -0700180`pytest` also implements a number of its own command-line options. Commonly used
181options are mentioned below. Please see `pytest` documentation for complete
182details. Execute `py.test --version` for a brief summary. Note that U-Boot's
183test.py script passes all command-line arguments directly to `pytest` for
184processing.
185
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100186-k
187 selects which tests to run. The default is to run all known tests. This
Stephen Warrend70facf2016-02-08 14:49:02 -0700188 option takes a single argument which is used to filter test names. Simple
189 logical operators are supported. For example:
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100190
191 - `'-k ums'` runs only tests with "ums" in their name.
192 - `'-k ut_dm'` runs only tests with "ut_dm" in their name. Note that in this
Stephen Warrend70facf2016-02-08 14:49:02 -0700193 case, "ut_dm" is a parameter to a test rather than the test name. The full
194 test name is e.g. "test_ut[ut_dm_leak]".
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100195 - `'-k not reset'` runs everything except tests with "reset" in their name.
196 - `'-k ut or hush'` runs only tests with "ut" or "hush" in their name.
197 - `'-k not (ut or hush)'` runs everything except tests with "ut" or "hush" in
Stephen Warrend70facf2016-02-08 14:49:02 -0700198 their name.
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100199
200-s
201 prevents pytest from hiding a test's stdout. This allows you to see
Stephen Warrend70facf2016-02-08 14:49:02 -0700202 U-Boot's console log in real time on pytest's stdout.
Stephen Warrend2015062016-01-15 11:15:24 -0700203
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100204Testing real hardware
205---------------------
Stephen Warrend2015062016-01-15 11:15:24 -0700206
207The tools and techniques used to interact with real hardware will vary
208radically between different host and target systems, and the whims of the user.
209For this reason, the test suite does not attempt to directly interact with real
210hardware in any way. Rather, it executes a standardized set of "hook" scripts
211via `$PATH`. These scripts implement certain actions on behalf of the test
212suite. This keeps the test suite simple and isolated from system variances
213unrelated to U-Boot features.
214
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100215Hook scripts
216~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -0700217
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100218Environment variables
219'''''''''''''''''''''
Stephen Warrend2015062016-01-15 11:15:24 -0700220
221The following environment variables are set when running hook scripts:
222
223- `UBOOT_BOARD_TYPE` the board type being tested.
224- `UBOOT_BOARD_IDENTITY` the board identity being tested, or `na` if none was
225 specified.
226- `UBOOT_SOURCE_DIR` the U-Boot source directory.
227- `UBOOT_TEST_PY_DIR` the full path to `test/py/` in the source directory.
228- `UBOOT_BUILD_DIR` the U-Boot build directory.
229- `UBOOT_RESULT_DIR` the test result directory.
Masahiro Yamada3aca4a42017-10-19 19:08:52 +0900230- `UBOOT_PERSISTENT_DATA_DIR` the test persistent data directory.
Stephen Warrend2015062016-01-15 11:15:24 -0700231
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100232u-boot-test-console
233'''''''''''''''''''
Stephen Warrend2015062016-01-15 11:15:24 -0700234
235This script provides access to the U-Boot console. The script's stdin/stdout
236should be connected to the board's console. This process should continue to run
237indefinitely, until killed. The test suite will run this script in parallel
238with all other hooks.
239
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100240This script may be implemented e.g. by executing `cu`, `kermit`, `conmux`, etc.
241via exec().
Stephen Warrend2015062016-01-15 11:15:24 -0700242
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100243If you are able to run U-Boot under a hardware simulator such as QEMU, then
Stephen Warrend2015062016-01-15 11:15:24 -0700244you would likely spawn that simulator from this script. However, note that
245`u-boot-test-reset` may be called multiple times per test script run, and must
246cause U-Boot to start execution from scratch each time. Hopefully your
247simulator includes a virtual reset button! If not, you can launch the
248simulator from `u-boot-test-reset` instead, while arranging for this console
249process to always communicate with the current simulator instance.
250
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100251u-boot-test-flash
252'''''''''''''''''
Stephen Warrend2015062016-01-15 11:15:24 -0700253
254Prior to running the test suite against a board, some arrangement must be made
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100255so that the board executes the particular U-Boot binary to be tested. Often
Stephen Warrend2015062016-01-15 11:15:24 -0700256this involves writing the U-Boot binary to the board's flash ROM. The test
257suite calls this hook script for that purpose.
258
259This script should perform the entire flashing process synchronously; the
260script should only exit once flashing is complete, and a board reset will
261cause the newly flashed U-Boot binary to be executed.
262
263It is conceivable that this script will do nothing. This might be useful in
264the following cases:
265
266- Some other process has already written the desired U-Boot binary into the
267 board's flash prior to running the test suite.
268- The board allows U-Boot to be downloaded directly into RAM, and executed
269 from there. Use of this feature will reduce wear on the board's flash, so
270 may be preferable if available, and if cold boot testing of U-Boot is not
271 required. If this feature is used, the `u-boot-test-reset` script should
Masahiro Yamada3aca4a42017-10-19 19:08:52 +0900272 perform this download, since the board could conceivably be reset multiple
Stephen Warrend2015062016-01-15 11:15:24 -0700273 times in a single test run.
274
275It is up to the user to determine if those situations exist, and to code this
276hook script appropriately.
277
278This script will typically be implemented by calling out to some SoC- or
279board-specific vendor flashing utility.
280
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100281u-boot-test-reset
282'''''''''''''''''
Stephen Warrend2015062016-01-15 11:15:24 -0700283
284Whenever the test suite needs to reset the target board, this script is
285executed. This is guaranteed to happen at least once, prior to executing the
286first test function. If any test fails, the test infra-structure will execute
287this script again to restore U-Boot to an operational state before running the
288next test function.
289
290This script will likely be implemented by communicating with some form of
291relay or electronic switch attached to the board's reset signal.
292
293The semantics of this script require that when it is executed, U-Boot will
294start running from scratch. If the U-Boot binary to be tested has been written
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100295to flash, pulsing the board's reset signal is likely all this script needs to
296do. However, in some scenarios, this script may perform other actions. For
Stephen Warrend2015062016-01-15 11:15:24 -0700297example, it may call out to some SoC- or board-specific vendor utility in order
298to download the U-Boot binary directly into RAM and execute it. This would
299avoid the need for `u-boot-test-flash` to actually write U-Boot to flash, thus
300saving wear on the flash chip(s).
301
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100302Examples
303''''''''
Stephen Warren5b2beab2016-04-06 11:46:59 -0600304
Tom Rini85ae52b2021-02-24 17:05:04 -0500305https://source.denx.de/u-boot/u-boot-test-hooks contains some working example hook
Stephen Warren5b2beab2016-04-06 11:46:59 -0600306scripts, and may be useful as a reference when implementing hook scripts for
307your platform. These scripts are not considered part of U-Boot itself.
308
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100309Board-type-specific configuration
310~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -0700311
312Each board has a different configuration and behaviour. Many of these
313differences can be automatically detected by parsing the `.config` file in the
314build directory. However, some differences can't yet be handled automatically.
315
316For each board, an optional Python module `u_boot_board_${board_type}` may exist
317to provide board-specific information to the test script. Any global value
318defined in these modules is available for use by any test function. The data
319contained in these scripts must be purely derived from U-Boot source code.
320Hence, these configuration files are part of the U-Boot source tree too.
321
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100322Execution environment configuration
323~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -0700324
325Each user's hardware setup may enable testing different subsets of the features
326implemented by a particular board's configuration of U-Boot. For example, a
327U-Boot configuration may support USB device mode and USB Mass Storage, but this
328can only be tested if a USB cable is connected between the board and the host
329machine running the test script.
330
331For each board, optional Python modules `u_boot_boardenv_${board_type}` and
332`u_boot_boardenv_${board_type}_${board_identity}` may exist to provide
333board-specific and board-identity-specific information to the test script. Any
334global value defined in these modules is available for use by any test
335function. The data contained in these is specific to a particular user's
336hardware configuration. Hence, these configuration files are not part of the
337U-Boot source tree, and should be installed outside of the source tree. Users
338should set `$PYTHONPATH` prior to running the test script to allow these
339modules to be loaded.
340
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100341Board module parameter usage
342~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -0700343
344The test scripts rely on the following variables being defined by the board
345module:
346
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100347- none at present
Stephen Warrend2015062016-01-15 11:15:24 -0700348
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100349U-Boot `.config` feature usage
350~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -0700351
352The test scripts rely on various U-Boot `.config` features, either directly in
353order to test those features, or indirectly in order to query information from
354the running U-Boot instance in order to test other features.
355
356One example is that testing of the `md` command requires knowledge of a RAM
357address to use for the test. This data is parsed from the output of the
358`bdinfo` command, and hence relies on CONFIG_CMD_BDI being enabled.
359
360For a complete list of dependencies, please search the test scripts for
361instances of:
362
363- `buildconfig.get(...`
364- `@pytest.mark.buildconfigspec(...`
Heinrich Schuchardt10feb302019-04-22 09:18:55 +0200365- `@pytest.mark.notbuildconfigspec(...`
Stephen Warrend2015062016-01-15 11:15:24 -0700366
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100367Complete invocation example
368~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Warrend2015062016-01-15 11:15:24 -0700369
370Assuming that you have installed the hook scripts into $HOME/ubtest/bin, and
371any required environment configuration Python modules into $HOME/ubtest/py,
372then you would likely invoke the test script as follows:
373
374If U-Boot has already been built:
375
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100376.. code-block:: bash
377
378 PATH=$HOME/ubtest/bin:$PATH \
Liam Beguinbe916912018-03-14 19:15:13 -0400379 PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \
Stephen Warrend2015062016-01-15 11:15:24 -0700380 ./test/py/test.py --bd seaboard
Stephen Warrend2015062016-01-15 11:15:24 -0700381
382If you want the test script to compile U-Boot for you too, then you likely
383need to set `$CROSS_COMPILE` to allow this, and invoke the test script as
Simon Glassf5ec7ee2020-03-18 09:43:01 -0600384follows:
Stephen Warrend2015062016-01-15 11:15:24 -0700385
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100386.. code-block:: bash
387
388 CROSS_COMPILE=arm-none-eabi- \
Stephen Warrend2015062016-01-15 11:15:24 -0700389 PATH=$HOME/ubtest/bin:$PATH \
Liam Beguinbe916912018-03-14 19:15:13 -0400390 PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \
Stephen Warrend2015062016-01-15 11:15:24 -0700391 ./test/py/test.py --bd seaboard --build
Stephen Warrend2015062016-01-15 11:15:24 -0700392
Simon Glassf5ec7ee2020-03-18 09:43:01 -0600393or, using buildman to handle it:
394
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100395.. code-block:: bash
396
Simon Glassf5ec7ee2020-03-18 09:43:01 -0600397 PATH=$HOME/ubtest/bin:$PATH \
398 PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \
399 ./test/py/test.py --bd seaboard --build --buildman
Simon Glassf5ec7ee2020-03-18 09:43:01 -0600400
Heinrich Schuchardt01576192021-01-18 20:24:03 +0100401Writing tests
402-------------
Stephen Warrend2015062016-01-15 11:15:24 -0700403
404Please refer to the pytest documentation for details of writing pytest tests.
405Details specific to the U-Boot test suite are described below.
406
407A test fixture named `u_boot_console` should be used by each test function. This
408provides the means to interact with the U-Boot console, and retrieve board and
409environment configuration information.
410
411The function `u_boot_console.run_command()` executes a shell command on the
412U-Boot console, and returns all output from that command. This allows
413validation or interpretation of the command output. This function validates
414that certain strings are not seen on the U-Boot console. These include shell
415error messages and the U-Boot sign-on message (in order to detect unexpected
416board resets). See the source of `u_boot_console_base.py` for a complete list of
417"bad" strings. Some test scenarios are expected to trigger these strings. Use
418`u_boot_console.disable_check()` to temporarily disable checking for specific
419strings. See `test_unknown_cmd.py` for an example.
420
421Board- and board-environment configuration values may be accessed as sub-fields
422of the `u_boot_console.config` object, for example
423`u_boot_console.config.ram_base`.
424
425Build configuration values (from `.config`) may be accessed via the dictionary
426`u_boot_console.config.buildconfig`, with keys equal to the Kconfig variable
427names.