1U-Boot pytest suite 2=================== 3 4Introduction 5------------ 6 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 16 flexible than writing it all in C. But see :doc:`tests_writing` for caveats 17 and more discussion / analysis. 18- It is reasonably simple to interact with U-Boot in this way. 19 20Requirements 21------------ 22 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 28In 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 36 37In order to execute certain tests on their supported platforms other tools 38will be required. The following is an incomplete list: 39 40* 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 54 55Please use the appropriate commands for your distribution to match these tools 56up with the package that provides them. 57 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 66Using `virtualenv` to provide requirements 67~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 68 69The 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: 72 73 74.. code-block:: console 75 76 $ 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 86application), simply execute: 87 88.. code-block:: bash 89 90 ./test/py/test.py --bd sandbox --build 91 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 106If sandbox crashes (e.g. with a segfault) you will see message like this:: 107 108 109 test/py/u_boot_spawn.py:171: in expect 110 c = os.read(self.fd, 1024).decode(errors='replace') 111 E ValueError: U-Boot exited with signal 11 (Signals.SIGSEGV) 112 113 114Controlling output 115~~~~~~~~~~~~~~~~~~ 116 117By default a short backtrace is reported. If you would like a longer one, 118pass ``--tb=long`` when running the test. See the pytest documentation for 119more options. 120 121Running tests in parallel 122~~~~~~~~~~~~~~~~~~~~~~~~~ 123 124Note: This does not fully work yet and is documented only so you can try to 125fix the problems. 126 127First install support for parallel tests:: 128 129 pip3 install pytest-xdist 130 131Then build sandbox in a suitable build directory. It is not possible to use 132the --build flag with xdist. 133 134Finally, run the tests in parallel using the -n flag:: 135 136 # build sandbox first, in a suitable build directory. It is not possible 137 # to use the --build flag with -n 138 test/py/test.py -B sandbox --build-dir /tmp/b/sandbox -q -k 'not slow' -n32 139 140At least the following non-slow tests are known to fail: 141 142- test_fit_ecdsa 143- test_bind_unbind_with_uclass 144- ut_dm_spi_flash 145- test_gpt_rename_partition 146- test_gpt_swap_partitions 147- test_pinmux_status 148- test_sqfs_load 149 150 151Testing under a debugger 152~~~~~~~~~~~~~~~~~~~~~~~~ 153 154If you need to run sandbox under a debugger, you may pass the command-line 155option `--gdbserver COMM`. This causes two things to happens: 156 157- Instead of running U-Boot directly, it will be run under gdbserver, with 158 debug communication via the channel `COMM`. You can attach a debugger to the 159 sandbox process in order to debug it. See `man gdbserver` and the example 160 below for details of valid values for `COMM`. 161- All timeouts in tests are disabled, allowing U-Boot an arbitrary amount of 162 time to execute commands. This is useful if U-Boot is stopped at a breakpoint 163 during debugging. 164 165A usage example is: 166 167Window 1: 168 169.. code-block:: bash 170 171 ./test/py/test.py --bd sandbox --gdbserver localhost:1234 172 173Window 2: 174 175.. code-block:: bash 176 177 gdb ./build-sandbox/u-boot -ex 'target remote localhost:1234' 178 179Alternatively, you could leave off the `-ex` option and type the command 180manually into gdb once it starts. 181 182You can use any debugger you wish, as long as it speaks the gdb remote 183protocol, or any graphical wrapper around gdb. 184 185Some tests deliberately cause the sandbox process to exit, e.g. to test the 186reset command, or sandbox's CTRL-C handling. When this happens, you will need 187to attach the debugger to the new sandbox instance. If these tests are not 188relevant to your debugging session, you can skip them using pytest's -k 189command-line option; see the next section. 190 191Command-line options 192-------------------- 193 194--board-type, --bd, -B 195 set the type of the board to be tested. For example, `sandbox` or `seaboard`. 196 197--board-identity`, --id 198 sets the identity of the board to be tested. This allows differentiation 199 between multiple instances of the same type of physical board that are 200 attached to the same host machine. This parameter is not interpreted by th 201 test script in any way, but rather is simply passed to the hook scripts 202 described below, and may be used in any site-specific way deemed necessary. 203 204--build 205 indicates that the test script should compile U-Boot itself before running 206 the tests. If using this option, make sure that any environment variables 207 required by the build process are already set, such as `$CROSS_COMPILE`. 208 209--buildman 210 indicates that `--build` should use buildman to build U-Boot. There is no need 211 to set $CROSS_COMPILE` in this case since buildman handles it. 212 213--build-dir 214 sets the directory containing the compiled U-Boot binaries. If omitted, this 215 is `${source_dir}/build-${board_type}`. 216 217--result-dir 218 sets the directory to write results, such as log files, into. 219 If omitted, the build directory is used. 220 221--persistent-data-dir 222 sets the directory used to store persistent test data. This is test data that 223 may be re-used across test runs, such as file-system images. 224 225`pytest` also implements a number of its own command-line options. Commonly used 226options are mentioned below. Please see `pytest` documentation for complete 227details. Execute `py.test --version` for a brief summary. Note that U-Boot's 228test.py script passes all command-line arguments directly to `pytest` for 229processing. 230 231-k 232 selects which tests to run. The default is to run all known tests. This 233 option takes a single argument which is used to filter test names. Simple 234 logical operators are supported. For example: 235 236 - `'-k ums'` runs only tests with "ums" in their name. 237 - `'-k ut_dm'` runs only tests with "ut_dm" in their name. Note that in this 238 case, "ut_dm" is a parameter to a test rather than the test name. The full 239 test name is e.g. "test_ut[ut_dm_leak]". 240 - `'-k not reset'` runs everything except tests with "reset" in their name. 241 - `'-k ut or hush'` runs only tests with "ut" or "hush" in their name. 242 - `'-k not (ut or hush)'` runs everything except tests with "ut" or "hush" in 243 their name. 244 245-s 246 prevents pytest from hiding a test's stdout. This allows you to see 247 U-Boot's console log in real time on pytest's stdout. 248 249Testing real hardware 250--------------------- 251 252The tools and techniques used to interact with real hardware will vary 253radically between different host and target systems, and the whims of the user. 254For this reason, the test suite does not attempt to directly interact with real 255hardware in any way. Rather, it executes a standardized set of "hook" scripts 256via `$PATH`. These scripts implement certain actions on behalf of the test 257suite. This keeps the test suite simple and isolated from system variances 258unrelated to U-Boot features. 259 260Hook scripts 261~~~~~~~~~~~~ 262 263Environment variables 264''''''''''''''''''''' 265 266The following environment variables are set when running hook scripts: 267 268- `UBOOT_BOARD_TYPE` the board type being tested. 269- `UBOOT_BOARD_IDENTITY` the board identity being tested, or `na` if none was 270 specified. 271- `UBOOT_SOURCE_DIR` the U-Boot source directory. 272- `UBOOT_TEST_PY_DIR` the full path to `test/py/` in the source directory. 273- `UBOOT_BUILD_DIR` the U-Boot build directory. 274- `UBOOT_RESULT_DIR` the test result directory. 275- `UBOOT_PERSISTENT_DATA_DIR` the test persistent data directory. 276 277u-boot-test-console 278''''''''''''''''''' 279 280This script provides access to the U-Boot console. The script's stdin/stdout 281should be connected to the board's console. This process should continue to run 282indefinitely, until killed. The test suite will run this script in parallel 283with all other hooks. 284 285This script may be implemented e.g. by executing `cu`, `kermit`, `conmux`, etc. 286via exec(). 287 288If you are able to run U-Boot under a hardware simulator such as QEMU, then 289you would likely spawn that simulator from this script. However, note that 290`u-boot-test-reset` may be called multiple times per test script run, and must 291cause U-Boot to start execution from scratch each time. Hopefully your 292simulator includes a virtual reset button! If not, you can launch the 293simulator from `u-boot-test-reset` instead, while arranging for this console 294process to always communicate with the current simulator instance. 295 296u-boot-test-flash 297''''''''''''''''' 298 299Prior to running the test suite against a board, some arrangement must be made 300so that the board executes the particular U-Boot binary to be tested. Often 301this involves writing the U-Boot binary to the board's flash ROM. The test 302suite calls this hook script for that purpose. 303 304This script should perform the entire flashing process synchronously; the 305script should only exit once flashing is complete, and a board reset will 306cause the newly flashed U-Boot binary to be executed. 307 308It is conceivable that this script will do nothing. This might be useful in 309the following cases: 310 311- Some other process has already written the desired U-Boot binary into the 312 board's flash prior to running the test suite. 313- The board allows U-Boot to be downloaded directly into RAM, and executed 314 from there. Use of this feature will reduce wear on the board's flash, so 315 may be preferable if available, and if cold boot testing of U-Boot is not 316 required. If this feature is used, the `u-boot-test-reset` script should 317 perform this download, since the board could conceivably be reset multiple 318 times in a single test run. 319 320It is up to the user to determine if those situations exist, and to code this 321hook script appropriately. 322 323This script will typically be implemented by calling out to some SoC- or 324board-specific vendor flashing utility. 325 326u-boot-test-reset 327''''''''''''''''' 328 329Whenever the test suite needs to reset the target board, this script is 330executed. This is guaranteed to happen at least once, prior to executing the 331first test function. If any test fails, the test infra-structure will execute 332this script again to restore U-Boot to an operational state before running the 333next test function. 334 335This script will likely be implemented by communicating with some form of 336relay or electronic switch attached to the board's reset signal. 337 338The semantics of this script require that when it is executed, U-Boot will 339start running from scratch. If the U-Boot binary to be tested has been written 340to flash, pulsing the board's reset signal is likely all this script needs to 341do. However, in some scenarios, this script may perform other actions. For 342example, it may call out to some SoC- or board-specific vendor utility in order 343to download the U-Boot binary directly into RAM and execute it. This would 344avoid the need for `u-boot-test-flash` to actually write U-Boot to flash, thus 345saving wear on the flash chip(s). 346 347Examples 348'''''''' 349 350https://source.denx.de/u-boot/u-boot-test-hooks contains some working example hook 351scripts, and may be useful as a reference when implementing hook scripts for 352your platform. These scripts are not considered part of U-Boot itself. 353 354Board-type-specific configuration 355~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 356 357Each board has a different configuration and behaviour. Many of these 358differences can be automatically detected by parsing the `.config` file in the 359build directory. However, some differences can't yet be handled automatically. 360 361For each board, an optional Python module `u_boot_board_${board_type}` may exist 362to provide board-specific information to the test script. Any global value 363defined in these modules is available for use by any test function. The data 364contained in these scripts must be purely derived from U-Boot source code. 365Hence, these configuration files are part of the U-Boot source tree too. 366 367Execution environment configuration 368~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 369 370Each user's hardware setup may enable testing different subsets of the features 371implemented by a particular board's configuration of U-Boot. For example, a 372U-Boot configuration may support USB device mode and USB Mass Storage, but this 373can only be tested if a USB cable is connected between the board and the host 374machine running the test script. 375 376For each board, optional Python modules `u_boot_boardenv_${board_type}` and 377`u_boot_boardenv_${board_type}_${board_identity}` may exist to provide 378board-specific and board-identity-specific information to the test script. Any 379global value defined in these modules is available for use by any test 380function. The data contained in these is specific to a particular user's 381hardware configuration. Hence, these configuration files are not part of the 382U-Boot source tree, and should be installed outside of the source tree. Users 383should set `$PYTHONPATH` prior to running the test script to allow these 384modules to be loaded. 385 386Board module parameter usage 387~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 388 389The test scripts rely on the following variables being defined by the board 390module: 391 392- none at present 393 394U-Boot `.config` feature usage 395~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 396 397The test scripts rely on various U-Boot `.config` features, either directly in 398order to test those features, or indirectly in order to query information from 399the running U-Boot instance in order to test other features. 400 401One example is that testing of the `md` command requires knowledge of a RAM 402address to use for the test. This data is parsed from the output of the 403`bdinfo` command, and hence relies on CONFIG_CMD_BDI being enabled. 404 405For a complete list of dependencies, please search the test scripts for 406instances of: 407 408- `buildconfig.get(...` 409- `@pytest.mark.buildconfigspec(...` 410- `@pytest.mark.notbuildconfigspec(...` 411 412Complete invocation example 413~~~~~~~~~~~~~~~~~~~~~~~~~~~ 414 415Assuming that you have installed the hook scripts into $HOME/ubtest/bin, and 416any required environment configuration Python modules into $HOME/ubtest/py, 417then you would likely invoke the test script as follows: 418 419If U-Boot has already been built: 420 421.. code-block:: bash 422 423 PATH=$HOME/ubtest/bin:$PATH \ 424 PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \ 425 ./test/py/test.py --bd seaboard 426 427If you want the test script to compile U-Boot for you too, then you likely 428need to set `$CROSS_COMPILE` to allow this, and invoke the test script as 429follows: 430 431.. code-block:: bash 432 433 CROSS_COMPILE=arm-none-eabi- \ 434 PATH=$HOME/ubtest/bin:$PATH \ 435 PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \ 436 ./test/py/test.py --bd seaboard --build 437 438or, using buildman to handle it: 439 440.. code-block:: bash 441 442 PATH=$HOME/ubtest/bin:$PATH \ 443 PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \ 444 ./test/py/test.py --bd seaboard --build --buildman 445 446Writing tests 447------------- 448 449Please refer to the pytest documentation for details of writing pytest tests. 450Details specific to the U-Boot test suite are described below. 451 452A test fixture named `u_boot_console` should be used by each test function. This 453provides the means to interact with the U-Boot console, and retrieve board and 454environment configuration information. 455 456The function `u_boot_console.run_command()` executes a shell command on the 457U-Boot console, and returns all output from that command. This allows 458validation or interpretation of the command output. This function validates 459that certain strings are not seen on the U-Boot console. These include shell 460error messages and the U-Boot sign-on message (in order to detect unexpected 461board resets). See the source of `u_boot_console_base.py` for a complete list of 462"bad" strings. Some test scenarios are expected to trigger these strings. Use 463`u_boot_console.disable_check()` to temporarily disable checking for specific 464strings. See `test_unknown_cmd.py` for an example. 465 466Board- and board-environment configuration values may be accessed as sub-fields 467of the `u_boot_console.config` object, for example 468`u_boot_console.config.ram_base`. 469 470Build configuration values (from `.config`) may be accessed via the dictionary 471`u_boot_console.config.buildconfig`, with keys equal to the Kconfig variable 472names. 473