Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 1 | # U-Boot pytest suite |
| 2 | |
| 3 | ## Introduction |
| 4 | |
| 5 | This tool aims to test U-Boot by executing U-Boot shell commands using the |
| 6 | console interface. A single top-level script exists to execute or attach to the |
| 7 | U-Boot console, run the entire script of tests against it, and summarize the |
| 8 | results. Advantages of this approach are: |
| 9 | |
| 10 | - Testing is performed in the same way a user or script would interact with |
| 11 | U-Boot; there can be no disconnect. |
| 12 | - There is no need to write or embed test-related code into U-Boot itself. |
| 13 | It is asserted that writing test-related code in Python is simpler and more |
Masahiro Yamada | c901891 | 2017-10-19 19:08:52 +0900 | [diff] [blame] | 14 | flexible than writing it all in C. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 15 | - It is reasonably simple to interact with U-Boot in this way. |
| 16 | |
| 17 | ## Requirements |
| 18 | |
| 19 | The test suite is implemented using pytest. Interaction with the U-Boot console |
| 20 | involves executing some binary and interacting with its stdin/stdout. You will |
| 21 | need to implement various "hook" scripts that are called by the test suite at |
| 22 | the appropriate time. |
| 23 | |
Tom Rini | a39e81b | 2019-10-24 11:59:26 -0400 | [diff] [blame] | 24 | In order to run the testsuite at a minimum we require that both python3 and |
| 25 | pip for python3 be installed. All of the required python modules are |
| 26 | described in the requirements.txt file in this directory and can be installed |
| 27 | with the command ```pip install -r requirements.txt``` |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 28 | |
Tom Rini | a39e81b | 2019-10-24 11:59:26 -0400 | [diff] [blame] | 29 | In order to execute certain tests on their supported platforms other tools |
| 30 | will be required. The following is an incomplete list: |
| 31 | |
| 32 | | Package | |
| 33 | | -------------- | |
| 34 | | gdisk | |
| 35 | | dfu-util | |
| 36 | | dtc | |
| 37 | | openssl | |
| 38 | | sudo OR guestmount | |
| 39 | | e2fsprogs | |
| 40 | | dosfstools | |
| 41 | |
| 42 | Please use the apporirate commands for your distribution to match these tools |
| 43 | up with the package that provides them. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 44 | |
| 45 | The test script supports either: |
| 46 | |
| 47 | - Executing a sandbox port of U-Boot on the local machine as a sub-process, |
| 48 | and interacting with it over stdin/stdout. |
| 49 | - Executing an external "hook" scripts to flash a U-Boot binary onto a |
| 50 | physical board, attach to the board's console stream, and reset the board. |
| 51 | Further details are described later. |
| 52 | |
| 53 | ### Using `virtualenv` to provide requirements |
| 54 | |
Tom Rini | a39e81b | 2019-10-24 11:59:26 -0400 | [diff] [blame] | 55 | The recommended way to run the test suite, in order to ensure reproducibility |
| 56 | is to use `virtualenv` to set up the necessary environment. This can be done |
| 57 | via the following commands: |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 58 | |
| 59 | ```bash |
| 60 | $ cd /path/to/u-boot |
Tom Rini | a39e81b | 2019-10-24 11:59:26 -0400 | [diff] [blame] | 61 | $ sudo apt-get install python3 python3-virtualenv |
| 62 | $ virtualenv -p /usr/bin/python3 venv |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 63 | $ . ./venv/bin/activate |
Tom Rini | a39e81b | 2019-10-24 11:59:26 -0400 | [diff] [blame] | 64 | $ pip install -r test/py/requirements.txt |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 65 | ``` |
| 66 | |
| 67 | ## Testing sandbox |
| 68 | |
| 69 | To run the testsuite on the sandbox port (U-Boot built as a native user-space |
| 70 | application), simply execute: |
| 71 | |
| 72 | ``` |
| 73 | ./test/py/test.py --bd sandbox --build |
| 74 | ``` |
| 75 | |
| 76 | The `--bd` option tells the test suite which board type is being tested. This |
| 77 | lets the test suite know which features the board has, and hence exactly what |
| 78 | can be tested. |
| 79 | |
| 80 | The `--build` option tells U-Boot to compile U-Boot. Alternatively, you may |
| 81 | omit this option and build U-Boot yourself, in whatever way you choose, before |
| 82 | running the test script. |
| 83 | |
| 84 | The test script will attach to U-Boot, execute all valid tests for the board, |
| 85 | then print a summary of the test process. A complete log of the test session |
| 86 | will be written to `${build_dir}/test-log.html`. This is best viewed in a web |
| 87 | browser, but may be read directly as plain text, perhaps with the aid of the |
| 88 | `html2text` utility. |
| 89 | |
Stephen Warren | c9afc50 | 2016-02-08 14:49:02 -0700 | [diff] [blame] | 90 | ### Testing under a debugger |
| 91 | |
| 92 | If you need to run sandbox under a debugger, you may pass the command-line |
| 93 | option `--gdbserver COMM`. This causes two things to happens: |
| 94 | |
| 95 | - Instead of running U-Boot directly, it will be run under gdbserver, with |
| 96 | debug communication via the channel `COMM`. You can attach a debugger to the |
| 97 | sandbox process in order to debug it. See `man gdbserver` and the example |
| 98 | below for details of valid values for `COMM`. |
| 99 | - All timeouts in tests are disabled, allowing U-Boot an arbitrary amount of |
| 100 | time to execute commands. This is useful if U-Boot is stopped at a breakpoint |
| 101 | during debugging. |
| 102 | |
| 103 | A usage example is: |
| 104 | |
| 105 | Window 1: |
| 106 | ```shell |
| 107 | ./test/py/test.py --bd sandbox --gdbserver localhost:1234 |
| 108 | ``` |
| 109 | |
| 110 | Window 2: |
| 111 | ```shell |
| 112 | gdb ./build-sandbox/u-boot -ex 'target remote localhost:1234' |
| 113 | ``` |
| 114 | |
| 115 | Alternatively, you could leave off the `-ex` option and type the command |
| 116 | manually into gdb once it starts. |
| 117 | |
| 118 | You can use any debugger you wish, so long as it speaks the gdb remote |
| 119 | protocol, or any graphical wrapper around gdb. |
| 120 | |
| 121 | Some tests deliberately cause the sandbox process to exit, e.g. to test the |
| 122 | reset command, or sandbox's CTRL-C handling. When this happens, you will need |
| 123 | to attach the debugger to the new sandbox instance. If these tests are not |
| 124 | relevant to your debugging session, you can skip them using pytest's -k |
| 125 | command-line option; see the next section. |
| 126 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 127 | ## Command-line options |
| 128 | |
| 129 | - `--board-type`, `--bd`, `-B` set the type of the board to be tested. For |
| 130 | example, `sandbox` or `seaboard`. |
| 131 | - `--board-identity`, `--id` set the identity of the board to be tested. |
| 132 | This allows differentiation between multiple instances of the same type of |
| 133 | physical board that are attached to the same host machine. This parameter is |
| 134 | not interpreted by the test script in any way, but rather is simply passed |
| 135 | to the hook scripts described below, and may be used in any site-specific |
| 136 | way deemed necessary. |
| 137 | - `--build` indicates that the test script should compile U-Boot itself |
| 138 | before running the tests. If using this option, make sure that any |
| 139 | environment variables required by the build process are already set, such as |
| 140 | `$CROSS_COMPILE`. |
Simon Glass | 6e09484 | 2020-03-18 09:43:01 -0600 | [diff] [blame^] | 141 | - `--buildman` indicates that `--build` should use buildman to build U-Boot. |
| 142 | There is no need to set $CROSS_COMPILE` in this case since buildman handles |
| 143 | it. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 144 | - `--build-dir` sets the directory containing the compiled U-Boot binaries. |
| 145 | If omitted, this is `${source_dir}/build-${board_type}`. |
| 146 | - `--result-dir` sets the directory to write results, such as log files, |
| 147 | into. If omitted, the build directory is used. |
| 148 | - `--persistent-data-dir` sets the directory used to store persistent test |
| 149 | data. This is test data that may be re-used across test runs, such as file- |
| 150 | system images. |
| 151 | |
Stephen Warren | c9afc50 | 2016-02-08 14:49:02 -0700 | [diff] [blame] | 152 | `pytest` also implements a number of its own command-line options. Commonly used |
| 153 | options are mentioned below. Please see `pytest` documentation for complete |
| 154 | details. Execute `py.test --version` for a brief summary. Note that U-Boot's |
| 155 | test.py script passes all command-line arguments directly to `pytest` for |
| 156 | processing. |
| 157 | |
| 158 | - `-k` selects which tests to run. The default is to run all known tests. This |
| 159 | option takes a single argument which is used to filter test names. Simple |
| 160 | logical operators are supported. For example: |
| 161 | - `'ums'` runs only tests with "ums" in their name. |
Liam Beguin | 7fc3558 | 2018-03-14 19:15:12 -0400 | [diff] [blame] | 162 | - `'ut_dm'` runs only tests with "ut_dm" in their name. Note that in this |
Stephen Warren | c9afc50 | 2016-02-08 14:49:02 -0700 | [diff] [blame] | 163 | case, "ut_dm" is a parameter to a test rather than the test name. The full |
| 164 | test name is e.g. "test_ut[ut_dm_leak]". |
| 165 | - `'not reset'` runs everything except tests with "reset" in their name. |
| 166 | - `'ut or hush'` runs only tests with "ut" or "hush" in their name. |
| 167 | - `'not (ut or hush)'` runs everything except tests with "ut" or "hush" in |
| 168 | their name. |
| 169 | - `-s` prevents pytest from hiding a test's stdout. This allows you to see |
| 170 | U-Boot's console log in real time on pytest's stdout. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 171 | |
| 172 | ## Testing real hardware |
| 173 | |
| 174 | The tools and techniques used to interact with real hardware will vary |
| 175 | radically between different host and target systems, and the whims of the user. |
| 176 | For this reason, the test suite does not attempt to directly interact with real |
| 177 | hardware in any way. Rather, it executes a standardized set of "hook" scripts |
| 178 | via `$PATH`. These scripts implement certain actions on behalf of the test |
| 179 | suite. This keeps the test suite simple and isolated from system variances |
| 180 | unrelated to U-Boot features. |
| 181 | |
| 182 | ### Hook scripts |
| 183 | |
| 184 | #### Environment variables |
| 185 | |
| 186 | The following environment variables are set when running hook scripts: |
| 187 | |
| 188 | - `UBOOT_BOARD_TYPE` the board type being tested. |
| 189 | - `UBOOT_BOARD_IDENTITY` the board identity being tested, or `na` if none was |
| 190 | specified. |
| 191 | - `UBOOT_SOURCE_DIR` the U-Boot source directory. |
| 192 | - `UBOOT_TEST_PY_DIR` the full path to `test/py/` in the source directory. |
| 193 | - `UBOOT_BUILD_DIR` the U-Boot build directory. |
| 194 | - `UBOOT_RESULT_DIR` the test result directory. |
Masahiro Yamada | c901891 | 2017-10-19 19:08:52 +0900 | [diff] [blame] | 195 | - `UBOOT_PERSISTENT_DATA_DIR` the test persistent data directory. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 196 | |
| 197 | #### `u-boot-test-console` |
| 198 | |
| 199 | This script provides access to the U-Boot console. The script's stdin/stdout |
| 200 | should be connected to the board's console. This process should continue to run |
| 201 | indefinitely, until killed. The test suite will run this script in parallel |
| 202 | with all other hooks. |
| 203 | |
| 204 | This script may be implemented e.g. by exec()ing `cu`, `kermit`, `conmux`, etc. |
| 205 | |
| 206 | If you are able to run U-Boot under a hardware simulator such as qemu, then |
| 207 | you would likely spawn that simulator from this script. However, note that |
| 208 | `u-boot-test-reset` may be called multiple times per test script run, and must |
| 209 | cause U-Boot to start execution from scratch each time. Hopefully your |
| 210 | simulator includes a virtual reset button! If not, you can launch the |
| 211 | simulator from `u-boot-test-reset` instead, while arranging for this console |
| 212 | process to always communicate with the current simulator instance. |
| 213 | |
| 214 | #### `u-boot-test-flash` |
| 215 | |
| 216 | Prior to running the test suite against a board, some arrangement must be made |
| 217 | so that the board executes the particular U-Boot binary to be tested. Often, |
| 218 | this involves writing the U-Boot binary to the board's flash ROM. The test |
| 219 | suite calls this hook script for that purpose. |
| 220 | |
| 221 | This script should perform the entire flashing process synchronously; the |
| 222 | script should only exit once flashing is complete, and a board reset will |
| 223 | cause the newly flashed U-Boot binary to be executed. |
| 224 | |
| 225 | It is conceivable that this script will do nothing. This might be useful in |
| 226 | the following cases: |
| 227 | |
| 228 | - Some other process has already written the desired U-Boot binary into the |
| 229 | board's flash prior to running the test suite. |
| 230 | - The board allows U-Boot to be downloaded directly into RAM, and executed |
| 231 | from there. Use of this feature will reduce wear on the board's flash, so |
| 232 | may be preferable if available, and if cold boot testing of U-Boot is not |
| 233 | required. If this feature is used, the `u-boot-test-reset` script should |
Masahiro Yamada | c901891 | 2017-10-19 19:08:52 +0900 | [diff] [blame] | 234 | perform this download, since the board could conceivably be reset multiple |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 235 | times in a single test run. |
| 236 | |
| 237 | It is up to the user to determine if those situations exist, and to code this |
| 238 | hook script appropriately. |
| 239 | |
| 240 | This script will typically be implemented by calling out to some SoC- or |
| 241 | board-specific vendor flashing utility. |
| 242 | |
| 243 | #### `u-boot-test-reset` |
| 244 | |
| 245 | Whenever the test suite needs to reset the target board, this script is |
| 246 | executed. This is guaranteed to happen at least once, prior to executing the |
| 247 | first test function. If any test fails, the test infra-structure will execute |
| 248 | this script again to restore U-Boot to an operational state before running the |
| 249 | next test function. |
| 250 | |
| 251 | This script will likely be implemented by communicating with some form of |
| 252 | relay or electronic switch attached to the board's reset signal. |
| 253 | |
| 254 | The semantics of this script require that when it is executed, U-Boot will |
| 255 | start running from scratch. If the U-Boot binary to be tested has been written |
| 256 | to flash, pulsing the board's reset signal is likely all this script need do. |
| 257 | However, in some scenarios, this script may perform other actions. For |
| 258 | example, it may call out to some SoC- or board-specific vendor utility in order |
| 259 | to download the U-Boot binary directly into RAM and execute it. This would |
| 260 | avoid the need for `u-boot-test-flash` to actually write U-Boot to flash, thus |
| 261 | saving wear on the flash chip(s). |
| 262 | |
Stephen Warren | 140d04d | 2016-04-06 11:46:59 -0600 | [diff] [blame] | 263 | #### Examples |
| 264 | |
| 265 | https://github.com/swarren/uboot-test-hooks contains some working example hook |
| 266 | scripts, and may be useful as a reference when implementing hook scripts for |
| 267 | your platform. These scripts are not considered part of U-Boot itself. |
| 268 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 269 | ### Board-type-specific configuration |
| 270 | |
| 271 | Each board has a different configuration and behaviour. Many of these |
| 272 | differences can be automatically detected by parsing the `.config` file in the |
| 273 | build directory. However, some differences can't yet be handled automatically. |
| 274 | |
| 275 | For each board, an optional Python module `u_boot_board_${board_type}` may exist |
| 276 | to provide board-specific information to the test script. Any global value |
| 277 | defined in these modules is available for use by any test function. The data |
| 278 | contained in these scripts must be purely derived from U-Boot source code. |
| 279 | Hence, these configuration files are part of the U-Boot source tree too. |
| 280 | |
| 281 | ### Execution environment configuration |
| 282 | |
| 283 | Each user's hardware setup may enable testing different subsets of the features |
| 284 | implemented by a particular board's configuration of U-Boot. For example, a |
| 285 | U-Boot configuration may support USB device mode and USB Mass Storage, but this |
| 286 | can only be tested if a USB cable is connected between the board and the host |
| 287 | machine running the test script. |
| 288 | |
| 289 | For each board, optional Python modules `u_boot_boardenv_${board_type}` and |
| 290 | `u_boot_boardenv_${board_type}_${board_identity}` may exist to provide |
| 291 | board-specific and board-identity-specific information to the test script. Any |
| 292 | global value defined in these modules is available for use by any test |
| 293 | function. The data contained in these is specific to a particular user's |
| 294 | hardware configuration. Hence, these configuration files are not part of the |
| 295 | U-Boot source tree, and should be installed outside of the source tree. Users |
| 296 | should set `$PYTHONPATH` prior to running the test script to allow these |
| 297 | modules to be loaded. |
| 298 | |
| 299 | ### Board module parameter usage |
| 300 | |
| 301 | The test scripts rely on the following variables being defined by the board |
| 302 | module: |
| 303 | |
| 304 | - None at present. |
| 305 | |
| 306 | ### U-Boot `.config` feature usage |
| 307 | |
| 308 | The test scripts rely on various U-Boot `.config` features, either directly in |
| 309 | order to test those features, or indirectly in order to query information from |
| 310 | the running U-Boot instance in order to test other features. |
| 311 | |
| 312 | One example is that testing of the `md` command requires knowledge of a RAM |
| 313 | address to use for the test. This data is parsed from the output of the |
| 314 | `bdinfo` command, and hence relies on CONFIG_CMD_BDI being enabled. |
| 315 | |
| 316 | For a complete list of dependencies, please search the test scripts for |
| 317 | instances of: |
| 318 | |
| 319 | - `buildconfig.get(...` |
| 320 | - `@pytest.mark.buildconfigspec(...` |
Heinrich Schuchardt | 59264ae | 2019-04-22 09:18:55 +0200 | [diff] [blame] | 321 | - `@pytest.mark.notbuildconfigspec(...` |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 322 | |
| 323 | ### Complete invocation example |
| 324 | |
| 325 | Assuming that you have installed the hook scripts into $HOME/ubtest/bin, and |
| 326 | any required environment configuration Python modules into $HOME/ubtest/py, |
| 327 | then you would likely invoke the test script as follows: |
| 328 | |
| 329 | If U-Boot has already been built: |
| 330 | |
| 331 | ```bash |
| 332 | PATH=$HOME/ubtest/bin:$PATH \ |
Liam Beguin | ce3f90e | 2018-03-14 19:15:13 -0400 | [diff] [blame] | 333 | PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 334 | ./test/py/test.py --bd seaboard |
| 335 | ``` |
| 336 | |
| 337 | If you want the test script to compile U-Boot for you too, then you likely |
| 338 | need to set `$CROSS_COMPILE` to allow this, and invoke the test script as |
Simon Glass | 6e09484 | 2020-03-18 09:43:01 -0600 | [diff] [blame^] | 339 | follows: |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 340 | |
| 341 | ```bash |
| 342 | CROSS_COMPILE=arm-none-eabi- \ |
| 343 | PATH=$HOME/ubtest/bin:$PATH \ |
Liam Beguin | ce3f90e | 2018-03-14 19:15:13 -0400 | [diff] [blame] | 344 | PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 345 | ./test/py/test.py --bd seaboard --build |
| 346 | ``` |
| 347 | |
Simon Glass | 6e09484 | 2020-03-18 09:43:01 -0600 | [diff] [blame^] | 348 | or, using buildman to handle it: |
| 349 | |
| 350 | ```bash |
| 351 | PATH=$HOME/ubtest/bin:$PATH \ |
| 352 | PYTHONPATH=${HOME}/ubtest/py/${HOSTNAME}:${PYTHONPATH} \ |
| 353 | ./test/py/test.py --bd seaboard --build --buildman |
| 354 | ``` |
| 355 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 356 | ## Writing tests |
| 357 | |
| 358 | Please refer to the pytest documentation for details of writing pytest tests. |
| 359 | Details specific to the U-Boot test suite are described below. |
| 360 | |
| 361 | A test fixture named `u_boot_console` should be used by each test function. This |
| 362 | provides the means to interact with the U-Boot console, and retrieve board and |
| 363 | environment configuration information. |
| 364 | |
| 365 | The function `u_boot_console.run_command()` executes a shell command on the |
| 366 | U-Boot console, and returns all output from that command. This allows |
| 367 | validation or interpretation of the command output. This function validates |
| 368 | that certain strings are not seen on the U-Boot console. These include shell |
| 369 | error messages and the U-Boot sign-on message (in order to detect unexpected |
| 370 | board resets). See the source of `u_boot_console_base.py` for a complete list of |
| 371 | "bad" strings. Some test scenarios are expected to trigger these strings. Use |
| 372 | `u_boot_console.disable_check()` to temporarily disable checking for specific |
| 373 | strings. See `test_unknown_cmd.py` for an example. |
| 374 | |
| 375 | Board- and board-environment configuration values may be accessed as sub-fields |
| 376 | of the `u_boot_console.config` object, for example |
| 377 | `u_boot_console.config.ram_base`. |
| 378 | |
| 379 | Build configuration values (from `.config`) may be accessed via the dictionary |
| 380 | `u_boot_console.config.buildconfig`, with keys equal to the Kconfig variable |
| 381 | names. |