Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 2 | # Copyright (c) 2015 Stephen Warren |
| 3 | # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 4 | |
| 5 | # Implementation of pytest run-time hook functions. These are invoked by |
| 6 | # pytest at certain points during operation, e.g. startup, for each executed |
| 7 | # test, at shutdown etc. These hooks perform functions such as: |
| 8 | # - Parsing custom command-line options. |
| 9 | # - Pullilng in user-specified board configuration. |
| 10 | # - Creating the U-Boot console test fixture. |
| 11 | # - Creating the HTML log file. |
| 12 | # - Monitoring each test's results. |
| 13 | # - Implementing custom pytest markers. |
| 14 | |
| 15 | import atexit |
Tom Rini | 6a99041 | 2019-10-24 11:59:21 -0400 | [diff] [blame] | 16 | import configparser |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 17 | import errno |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 18 | import filelock |
Tom Rini | 6a99041 | 2019-10-24 11:59:21 -0400 | [diff] [blame] | 19 | import io |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 20 | import os |
| 21 | import os.path |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 22 | from pathlib import Path |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 23 | import pytest |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 24 | import re |
Tom Rini | 6a99041 | 2019-10-24 11:59:21 -0400 | [diff] [blame] | 25 | from _pytest.runner import runtestprotocol |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 26 | import subprocess |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 27 | import sys |
Simon Glass | d834d9a | 2024-10-09 18:29:03 -0600 | [diff] [blame] | 28 | from u_boot_spawn import BootFail, Timeout, Unexpected, handle_exception |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 29 | |
| 30 | # Globals: The HTML log file, and the connection to the U-Boot console. |
| 31 | log = None |
| 32 | console = None |
| 33 | |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 34 | TEST_PY_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 35 | |
Simon Glass | b15512c | 2025-01-20 14:25:32 -0700 | [diff] [blame] | 36 | # Regex for test-function symbols |
| 37 | RE_UT_TEST_LIST = re.compile(r'[^a-zA-Z0-9_]_u_boot_list_2_ut_(.*)_2_(.*)\s*$') |
| 38 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 39 | def mkdir_p(path): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """Create a directory path. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 41 | |
| 42 | This includes creating any intermediate/parent directories. Any errors |
| 43 | caused due to already extant directories are ignored. |
| 44 | |
| 45 | Args: |
| 46 | path: The directory path to create. |
| 47 | |
| 48 | Returns: |
| 49 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 50 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 51 | |
| 52 | try: |
| 53 | os.makedirs(path) |
| 54 | except OSError as exc: |
| 55 | if exc.errno == errno.EEXIST and os.path.isdir(path): |
| 56 | pass |
| 57 | else: |
| 58 | raise |
| 59 | |
| 60 | def pytest_addoption(parser): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 61 | """pytest hook: Add custom command-line options to the cmdline parser. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 62 | |
| 63 | Args: |
| 64 | parser: The pytest command-line parser. |
| 65 | |
| 66 | Returns: |
| 67 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 68 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 69 | |
| 70 | parser.addoption('--build-dir', default=None, |
| 71 | help='U-Boot build directory (O=)') |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 72 | parser.addoption('--build-dir-extra', default=None, |
| 73 | help='U-Boot build directory for extra build (O=)') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 74 | parser.addoption('--result-dir', default=None, |
| 75 | help='U-Boot test result/tmp directory') |
| 76 | parser.addoption('--persistent-data-dir', default=None, |
| 77 | help='U-Boot test persistent generated data directory') |
| 78 | parser.addoption('--board-type', '--bd', '-B', default='sandbox', |
| 79 | help='U-Boot board type') |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 80 | parser.addoption('--board-type-extra', '--bde', default='sandbox', |
| 81 | help='U-Boot extra board type') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 82 | parser.addoption('--board-identity', '--id', default='na', |
| 83 | help='U-Boot board identity/instance') |
| 84 | parser.addoption('--build', default=False, action='store_true', |
| 85 | help='Compile U-Boot before running tests') |
Simon Glass | 6e09484 | 2020-03-18 09:43:01 -0600 | [diff] [blame] | 86 | parser.addoption('--buildman', default=False, action='store_true', |
| 87 | help='Use buildman to build U-Boot (assuming --build is given)') |
Stephen Warren | 33db1ee | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 88 | parser.addoption('--gdbserver', default=None, |
| 89 | help='Run sandbox under gdbserver. The argument is the channel '+ |
| 90 | 'over which gdbserver should communicate, e.g. localhost:1234') |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 91 | parser.addoption('--role', help='U-Boot board role (for Labgrid-sjg)') |
Simon Glass | f1b1bb8 | 2024-11-12 07:13:17 -0700 | [diff] [blame] | 92 | parser.addoption('--use-running-system', default=False, action='store_true', |
| 93 | help="Assume that U-Boot is ready and don't wait for a prompt") |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 94 | |
Simon Glass | 686fad7 | 2022-08-06 17:51:56 -0600 | [diff] [blame] | 95 | def run_build(config, source_dir, build_dir, board_type, log): |
| 96 | """run_build: Build U-Boot |
| 97 | |
| 98 | Args: |
| 99 | config: The pytest configuration. |
| 100 | soruce_dir (str): Directory containing source code |
| 101 | build_dir (str): Directory to build in |
| 102 | board_type (str): board_type parameter (e.g. 'sandbox') |
| 103 | log (Logfile): Log file to use |
| 104 | """ |
| 105 | if config.getoption('buildman'): |
| 106 | if build_dir != source_dir: |
| 107 | dest_args = ['-o', build_dir, '-w'] |
| 108 | else: |
| 109 | dest_args = ['-i'] |
| 110 | cmds = (['buildman', '--board', board_type] + dest_args,) |
| 111 | name = 'buildman' |
| 112 | else: |
| 113 | if build_dir != source_dir: |
| 114 | o_opt = 'O=%s' % build_dir |
| 115 | else: |
| 116 | o_opt = '' |
| 117 | cmds = ( |
| 118 | ['make', o_opt, '-s', board_type + '_defconfig'], |
| 119 | ['make', o_opt, '-s', '-j{}'.format(os.cpu_count())], |
| 120 | ) |
| 121 | name = 'make' |
| 122 | |
| 123 | with log.section(name): |
| 124 | runner = log.get_runner(name, sys.stdout) |
| 125 | for cmd in cmds: |
| 126 | runner.run(cmd, cwd=source_dir) |
| 127 | runner.close() |
| 128 | log.status_pass('OK') |
| 129 | |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 130 | def get_details(config): |
| 131 | """Obtain salient details about the board and directories to use |
| 132 | |
| 133 | Args: |
| 134 | config (pytest.Config): pytest configuration |
| 135 | |
| 136 | Returns: |
| 137 | tuple: |
| 138 | str: Board type (U-Boot build name) |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 139 | str: Extra board type (where two U-Boot builds are needed) |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 140 | str: Identity for the lab board |
| 141 | str: Build directory |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 142 | str: Extra build directory (where two U-Boot builds are needed) |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 143 | str: Source directory |
| 144 | """ |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 145 | role = config.getoption('role') |
| 146 | |
| 147 | # Get a few provided parameters |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 148 | build_dir = config.getoption('build_dir') |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 149 | build_dir_extra = config.getoption('build_dir_extra') |
Simon Glass | 068f6a7 | 2024-12-11 06:18:58 -0700 | [diff] [blame] | 150 | |
| 151 | # The source tree must be the current directory |
| 152 | source_dir = os.path.dirname(os.path.dirname(TEST_PY_DIR)) |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 153 | if role: |
| 154 | # When using a role, build_dir and build_dir_extra are normally not set, |
| 155 | # since they are picked up from Labgrid-sjg via the u-boot-test-getrole |
| 156 | # script |
| 157 | board_identity = role |
| 158 | cmd = ['u-boot-test-getrole', role, '--configure'] |
| 159 | env = os.environ.copy() |
| 160 | if build_dir: |
| 161 | env['U_BOOT_BUILD_DIR'] = build_dir |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 162 | if build_dir_extra: |
| 163 | env['U_BOOT_BUILD_DIR_EXTRA'] = build_dir_extra |
Simon Glass | 97fb345 | 2024-12-14 11:20:20 -0700 | [diff] [blame] | 164 | |
| 165 | # Make sure the script sees that it is being run from pytest |
| 166 | env['U_BOOT_SOURCE_DIR'] = source_dir |
| 167 | |
Simon Glass | 2873ca25 | 2024-12-14 11:20:21 -0700 | [diff] [blame] | 168 | proc = subprocess.run(cmd, stdout=subprocess.PIPE, |
| 169 | stderr=subprocess.STDOUT, encoding='utf-8', |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 170 | env=env) |
| 171 | if proc.returncode: |
Simon Glass | 2873ca25 | 2024-12-14 11:20:21 -0700 | [diff] [blame] | 172 | raise ValueError(f"Error {proc.returncode} running {cmd}: '{proc.stderr} '{proc.stdout}'") |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 173 | # For debugging |
| 174 | # print('conftest: lab:', proc.stdout) |
| 175 | vals = {} |
| 176 | for line in proc.stdout.splitlines(): |
| 177 | item, value = line.split(' ', maxsplit=1) |
| 178 | k = item.split(':')[-1] |
| 179 | vals[k] = value |
| 180 | # For debugging |
| 181 | # print('conftest: lab info:', vals) |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 182 | |
| 183 | # Read the build directories here, in case none were provided in the |
| 184 | # command-line arguments |
| 185 | (board_type, board_type_extra, default_build_dir, |
Simon Glass | 068f6a7 | 2024-12-11 06:18:58 -0700 | [diff] [blame] | 186 | default_build_dir_extra) = (vals['board'], |
| 187 | vals['board_extra'], vals['build_dir'], vals['build_dir_extra']) |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 188 | else: |
| 189 | board_type = config.getoption('board_type') |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 190 | board_type_extra = config.getoption('board_type_extra') |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 191 | board_identity = config.getoption('board_identity') |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 192 | |
Simon Glass | f6dbc36 | 2024-11-12 07:13:18 -0700 | [diff] [blame] | 193 | default_build_dir = source_dir + '/build-' + board_type |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 194 | default_build_dir_extra = source_dir + '/build-' + board_type_extra |
| 195 | |
| 196 | # Use the provided command-line arguments if present, else fall back to |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 197 | if not build_dir: |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 198 | build_dir = default_build_dir |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 199 | if not build_dir_extra: |
| 200 | build_dir_extra = default_build_dir_extra |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 201 | |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 202 | return (board_type, board_type_extra, board_identity, build_dir, |
| 203 | build_dir_extra, source_dir) |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 204 | |
| 205 | def pytest_xdist_setupnodes(config, specs): |
| 206 | """Clear out any 'done' file from a previous build""" |
| 207 | global build_done_file |
| 208 | |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 209 | build_dir = get_details(config)[3] |
Simon Glass | 35ad432 | 2024-10-09 18:29:00 -0600 | [diff] [blame] | 210 | |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 211 | build_done_file = Path(build_dir) / 'build.done' |
| 212 | if build_done_file.exists(): |
| 213 | os.remove(build_done_file) |
| 214 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 215 | def pytest_configure(config): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 216 | """pytest hook: Perform custom initialization at startup time. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 217 | |
| 218 | Args: |
| 219 | config: The pytest configuration. |
| 220 | |
| 221 | Returns: |
| 222 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 223 | """ |
Simon Glass | de8e25b | 2019-12-01 19:34:18 -0700 | [diff] [blame] | 224 | def parse_config(conf_file): |
| 225 | """Parse a config file, loading it into the ubconfig container |
| 226 | |
| 227 | Args: |
| 228 | conf_file: Filename to load (within build_dir) |
| 229 | |
| 230 | Raises |
| 231 | Exception if the file does not exist |
| 232 | """ |
| 233 | dot_config = build_dir + '/' + conf_file |
| 234 | if not os.path.exists(dot_config): |
| 235 | raise Exception(conf_file + ' does not exist; ' + |
| 236 | 'try passing --build option?') |
| 237 | |
| 238 | with open(dot_config, 'rt') as f: |
| 239 | ini_str = '[root]\n' + f.read() |
| 240 | ini_sio = io.StringIO(ini_str) |
| 241 | parser = configparser.RawConfigParser() |
| 242 | parser.read_file(ini_sio) |
| 243 | ubconfig.buildconfig.update(parser.items('root')) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 244 | |
| 245 | global log |
| 246 | global console |
| 247 | global ubconfig |
| 248 | |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 249 | (board_type, board_type_extra, board_identity, build_dir, build_dir_extra, |
| 250 | source_dir) = get_details(config) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 251 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 252 | board_type_filename = board_type.replace('-', '_') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 253 | board_identity_filename = board_identity.replace('-', '_') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 254 | mkdir_p(build_dir) |
| 255 | |
| 256 | result_dir = config.getoption('result_dir') |
| 257 | if not result_dir: |
| 258 | result_dir = build_dir |
| 259 | mkdir_p(result_dir) |
| 260 | |
| 261 | persistent_data_dir = config.getoption('persistent_data_dir') |
| 262 | if not persistent_data_dir: |
| 263 | persistent_data_dir = build_dir + '/persistent-data' |
| 264 | mkdir_p(persistent_data_dir) |
| 265 | |
Stephen Warren | 33db1ee | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 266 | gdbserver = config.getoption('gdbserver') |
Igor Opaniuk | ea5f17d | 2019-02-12 16:18:14 +0200 | [diff] [blame] | 267 | if gdbserver and not board_type.startswith('sandbox'): |
| 268 | raise Exception('--gdbserver only supported with sandbox targets') |
Stephen Warren | 33db1ee | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 269 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 270 | import multiplexed_log |
| 271 | log = multiplexed_log.Logfile(result_dir + '/test-log.html') |
| 272 | |
| 273 | if config.getoption('build'): |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 274 | worker_id = os.environ.get("PYTEST_XDIST_WORKER") |
| 275 | with filelock.FileLock(os.path.join(build_dir, 'build.lock')): |
| 276 | build_done_file = Path(build_dir) / 'build.done' |
| 277 | if (not worker_id or worker_id == 'master' or |
| 278 | not build_done_file.exists()): |
| 279 | run_build(config, source_dir, build_dir, board_type, log) |
| 280 | build_done_file.touch() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 281 | |
| 282 | class ArbitraryAttributeContainer(object): |
| 283 | pass |
| 284 | |
| 285 | ubconfig = ArbitraryAttributeContainer() |
| 286 | ubconfig.brd = dict() |
| 287 | ubconfig.env = dict() |
| 288 | |
| 289 | modules = [ |
| 290 | (ubconfig.brd, 'u_boot_board_' + board_type_filename), |
| 291 | (ubconfig.env, 'u_boot_boardenv_' + board_type_filename), |
| 292 | (ubconfig.env, 'u_boot_boardenv_' + board_type_filename + '_' + |
| 293 | board_identity_filename), |
| 294 | ] |
| 295 | for (dict_to_fill, module_name) in modules: |
| 296 | try: |
| 297 | module = __import__(module_name) |
| 298 | except ImportError: |
| 299 | continue |
| 300 | dict_to_fill.update(module.__dict__) |
| 301 | |
| 302 | ubconfig.buildconfig = dict() |
| 303 | |
Simon Glass | de8e25b | 2019-12-01 19:34:18 -0700 | [diff] [blame] | 304 | # buildman -k puts autoconf.mk in the rootdir, so handle this as well |
| 305 | # as the standard U-Boot build which leaves it in include/autoconf.mk |
| 306 | parse_config('.config') |
| 307 | if os.path.exists(build_dir + '/' + 'autoconf.mk'): |
| 308 | parse_config('autoconf.mk') |
| 309 | else: |
| 310 | parse_config('include/autoconf.mk') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 311 | |
Simon Glass | 62b92f8 | 2022-08-06 17:51:57 -0600 | [diff] [blame] | 312 | ubconfig.test_py_dir = TEST_PY_DIR |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 313 | ubconfig.source_dir = source_dir |
| 314 | ubconfig.build_dir = build_dir |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 315 | ubconfig.build_dir_extra = build_dir_extra |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 316 | ubconfig.result_dir = result_dir |
| 317 | ubconfig.persistent_data_dir = persistent_data_dir |
| 318 | ubconfig.board_type = board_type |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 319 | ubconfig.board_type_extra = board_type_extra |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 320 | ubconfig.board_identity = board_identity |
Stephen Warren | 33db1ee | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 321 | ubconfig.gdbserver = gdbserver |
Simon Glass | f1b1bb8 | 2024-11-12 07:13:17 -0700 | [diff] [blame] | 322 | ubconfig.use_running_system = config.getoption('use_running_system') |
Simon Glass | 3b09787 | 2016-07-03 09:40:36 -0600 | [diff] [blame] | 323 | ubconfig.dtb = build_dir + '/arch/sandbox/dts/test.dtb' |
Simon Glass | d834d9a | 2024-10-09 18:29:03 -0600 | [diff] [blame] | 324 | ubconfig.connection_ok = True |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 325 | |
| 326 | env_vars = ( |
| 327 | 'board_type', |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 328 | 'board_type_extra', |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 329 | 'board_identity', |
| 330 | 'source_dir', |
| 331 | 'test_py_dir', |
| 332 | 'build_dir', |
Simon Glass | 5a63a4b | 2024-11-12 07:13:24 -0700 | [diff] [blame] | 333 | 'build_dir_extra', |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 334 | 'result_dir', |
| 335 | 'persistent_data_dir', |
| 336 | ) |
| 337 | for v in env_vars: |
| 338 | os.environ['U_BOOT_' + v.upper()] = getattr(ubconfig, v) |
| 339 | |
Simon Glass | 13f422e | 2016-07-04 11:58:37 -0600 | [diff] [blame] | 340 | if board_type.startswith('sandbox'): |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 341 | import u_boot_console_sandbox |
| 342 | console = u_boot_console_sandbox.ConsoleSandbox(log, ubconfig) |
| 343 | else: |
| 344 | import u_boot_console_exec_attach |
| 345 | console = u_boot_console_exec_attach.ConsoleExecAttach(log, ubconfig) |
| 346 | |
Simon Glass | b15512c | 2025-01-20 14:25:32 -0700 | [diff] [blame] | 347 | |
Simon Glass | ed298be | 2020-10-25 20:38:31 -0600 | [diff] [blame] | 348 | def generate_ut_subtest(metafunc, fixture_name, sym_path): |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 349 | """Provide parametrization for a ut_subtest fixture. |
| 350 | |
| 351 | Determines the set of unit tests built into a U-Boot binary by parsing the |
| 352 | list of symbols generated by the build process. Provides this information |
| 353 | to test functions by parameterizing their ut_subtest fixture parameter. |
| 354 | |
| 355 | Args: |
| 356 | metafunc: The pytest test function. |
| 357 | fixture_name: The fixture name to test. |
Simon Glass | ed298be | 2020-10-25 20:38:31 -0600 | [diff] [blame] | 358 | sym_path: Relative path to the symbol file with preceding '/' |
| 359 | (e.g. '/u-boot.sym') |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 360 | |
| 361 | Returns: |
| 362 | Nothing. |
| 363 | """ |
Simon Glass | ed298be | 2020-10-25 20:38:31 -0600 | [diff] [blame] | 364 | fn = console.config.build_dir + sym_path |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 365 | try: |
| 366 | with open(fn, 'rt') as f: |
| 367 | lines = f.readlines() |
| 368 | except: |
| 369 | lines = [] |
| 370 | lines.sort() |
| 371 | |
| 372 | vals = [] |
| 373 | for l in lines: |
Simon Glass | b15512c | 2025-01-20 14:25:32 -0700 | [diff] [blame] | 374 | m = RE_UT_TEST_LIST.search(l) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 375 | if not m: |
| 376 | continue |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 377 | suite, name = m.groups() |
| 378 | |
| 379 | # Tests marked with _norun should only be run manually using 'ut -f' |
| 380 | if name.endswith('_norun'): |
| 381 | continue |
| 382 | |
| 383 | vals.append(f'{suite} {name}') |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 384 | |
| 385 | ids = ['ut_' + s.replace(' ', '_') for s in vals] |
| 386 | metafunc.parametrize(fixture_name, vals, ids=ids) |
| 387 | |
| 388 | def generate_config(metafunc, fixture_name): |
| 389 | """Provide parametrization for {env,brd}__ fixtures. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 390 | |
| 391 | If a test function takes parameter(s) (fixture names) of the form brd__xxx |
| 392 | or env__xxx, the brd and env configuration dictionaries are consulted to |
| 393 | find the list of values to use for those parameters, and the test is |
| 394 | parametrized so that it runs once for each combination of values. |
| 395 | |
| 396 | Args: |
| 397 | metafunc: The pytest test function. |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 398 | fixture_name: The fixture name to test. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 399 | |
| 400 | Returns: |
| 401 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 402 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 403 | |
| 404 | subconfigs = { |
| 405 | 'brd': console.config.brd, |
| 406 | 'env': console.config.env, |
| 407 | } |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 408 | parts = fixture_name.split('__') |
| 409 | if len(parts) < 2: |
| 410 | return |
| 411 | if parts[0] not in subconfigs: |
| 412 | return |
| 413 | subconfig = subconfigs[parts[0]] |
| 414 | vals = [] |
| 415 | val = subconfig.get(fixture_name, []) |
| 416 | # If that exact name is a key in the data source: |
| 417 | if val: |
| 418 | # ... use the dict value as a single parameter value. |
| 419 | vals = (val, ) |
| 420 | else: |
| 421 | # ... otherwise, see if there's a key that contains a list of |
| 422 | # values to use instead. |
| 423 | vals = subconfig.get(fixture_name+ 's', []) |
| 424 | def fixture_id(index, val): |
| 425 | try: |
| 426 | return val['fixture_id'] |
| 427 | except: |
| 428 | return fixture_name + str(index) |
| 429 | ids = [fixture_id(index, val) for (index, val) in enumerate(vals)] |
| 430 | metafunc.parametrize(fixture_name, vals, ids=ids) |
| 431 | |
| 432 | def pytest_generate_tests(metafunc): |
| 433 | """pytest hook: parameterize test functions based on custom rules. |
| 434 | |
| 435 | Check each test function parameter (fixture name) to see if it is one of |
| 436 | our custom names, and if so, provide the correct parametrization for that |
| 437 | parameter. |
| 438 | |
| 439 | Args: |
| 440 | metafunc: The pytest test function. |
| 441 | |
| 442 | Returns: |
| 443 | Nothing. |
| 444 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 445 | for fn in metafunc.fixturenames: |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 446 | if fn == 'ut_subtest': |
Simon Glass | ed298be | 2020-10-25 20:38:31 -0600 | [diff] [blame] | 447 | generate_ut_subtest(metafunc, fn, '/u-boot.sym') |
| 448 | continue |
Simon Glass | b6c665f | 2022-04-30 00:56:55 -0600 | [diff] [blame] | 449 | m_subtest = re.match('ut_(.)pl_subtest', fn) |
| 450 | if m_subtest: |
| 451 | spl_name = m_subtest.group(1) |
| 452 | generate_ut_subtest( |
| 453 | metafunc, fn, f'/{spl_name}pl/u-boot-{spl_name}pl.sym') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 454 | continue |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 455 | generate_config(metafunc, fn) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 456 | |
Stefan Brüns | 364ea87 | 2016-11-05 17:45:32 +0100 | [diff] [blame] | 457 | @pytest.fixture(scope='session') |
| 458 | def u_boot_log(request): |
| 459 | """Generate the value of a test's log fixture. |
| 460 | |
| 461 | Args: |
| 462 | request: The pytest request. |
| 463 | |
| 464 | Returns: |
| 465 | The fixture value. |
| 466 | """ |
| 467 | |
| 468 | return console.log |
| 469 | |
| 470 | @pytest.fixture(scope='session') |
| 471 | def u_boot_config(request): |
| 472 | """Generate the value of a test's u_boot_config fixture. |
| 473 | |
| 474 | Args: |
| 475 | request: The pytest request. |
| 476 | |
| 477 | Returns: |
| 478 | The fixture value. |
| 479 | """ |
| 480 | |
| 481 | return console.config |
| 482 | |
Stephen Warren | e1d24d0 | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 483 | @pytest.fixture(scope='function') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 484 | def u_boot_console(request): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 485 | """Generate the value of a test's u_boot_console fixture. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 486 | |
| 487 | Args: |
| 488 | request: The pytest request. |
| 489 | |
| 490 | Returns: |
| 491 | The fixture value. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 492 | """ |
Simon Glass | d834d9a | 2024-10-09 18:29:03 -0600 | [diff] [blame] | 493 | if not ubconfig.connection_ok: |
| 494 | pytest.skip('Cannot get target connection') |
| 495 | return None |
| 496 | try: |
| 497 | console.ensure_spawned() |
| 498 | except OSError as err: |
| 499 | handle_exception(ubconfig, console, log, err, 'Lab failure', True) |
| 500 | except Timeout as err: |
| 501 | handle_exception(ubconfig, console, log, err, 'Lab timeout', True) |
| 502 | except BootFail as err: |
| 503 | handle_exception(ubconfig, console, log, err, 'Boot fail', True, |
| 504 | console.get_spawn_output()) |
| 505 | except Unexpected: |
| 506 | handle_exception(ubconfig, console, log, err, 'Unexpected test output', |
| 507 | False) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 508 | return console |
| 509 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 510 | anchors = {} |
Stephen Warren | aaf4e91 | 2016-02-10 13:47:37 -0700 | [diff] [blame] | 511 | tests_not_run = [] |
| 512 | tests_failed = [] |
| 513 | tests_xpassed = [] |
| 514 | tests_xfailed = [] |
| 515 | tests_skipped = [] |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 516 | tests_warning = [] |
Stephen Warren | aaf4e91 | 2016-02-10 13:47:37 -0700 | [diff] [blame] | 517 | tests_passed = [] |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 518 | |
| 519 | def pytest_itemcollected(item): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 520 | """pytest hook: Called once for each test found during collection. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 521 | |
| 522 | This enables our custom result analysis code to see the list of all tests |
| 523 | that should eventually be run. |
| 524 | |
| 525 | Args: |
| 526 | item: The item that was collected. |
| 527 | |
| 528 | Returns: |
| 529 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 530 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 531 | |
Stephen Warren | aaf4e91 | 2016-02-10 13:47:37 -0700 | [diff] [blame] | 532 | tests_not_run.append(item.name) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 533 | |
| 534 | def cleanup(): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 535 | """Clean up all global state. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 536 | |
| 537 | Executed (via atexit) once the entire test process is complete. This |
| 538 | includes logging the status of all tests, and the identity of any failed |
| 539 | or skipped tests. |
| 540 | |
| 541 | Args: |
| 542 | None. |
| 543 | |
| 544 | Returns: |
| 545 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 546 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 547 | |
| 548 | if console: |
| 549 | console.close() |
| 550 | if log: |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 551 | with log.section('Status Report', 'status_report'): |
| 552 | log.status_pass('%d passed' % len(tests_passed)) |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 553 | if tests_warning: |
| 554 | log.status_warning('%d passed with warning' % len(tests_warning)) |
| 555 | for test in tests_warning: |
| 556 | anchor = anchors.get(test, None) |
| 557 | log.status_warning('... ' + test, anchor) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 558 | if tests_skipped: |
| 559 | log.status_skipped('%d skipped' % len(tests_skipped)) |
| 560 | for test in tests_skipped: |
| 561 | anchor = anchors.get(test, None) |
| 562 | log.status_skipped('... ' + test, anchor) |
| 563 | if tests_xpassed: |
| 564 | log.status_xpass('%d xpass' % len(tests_xpassed)) |
| 565 | for test in tests_xpassed: |
| 566 | anchor = anchors.get(test, None) |
| 567 | log.status_xpass('... ' + test, anchor) |
| 568 | if tests_xfailed: |
| 569 | log.status_xfail('%d xfail' % len(tests_xfailed)) |
| 570 | for test in tests_xfailed: |
| 571 | anchor = anchors.get(test, None) |
| 572 | log.status_xfail('... ' + test, anchor) |
| 573 | if tests_failed: |
| 574 | log.status_fail('%d failed' % len(tests_failed)) |
| 575 | for test in tests_failed: |
| 576 | anchor = anchors.get(test, None) |
| 577 | log.status_fail('... ' + test, anchor) |
| 578 | if tests_not_run: |
| 579 | log.status_fail('%d not run' % len(tests_not_run)) |
| 580 | for test in tests_not_run: |
| 581 | anchor = anchors.get(test, None) |
| 582 | log.status_fail('... ' + test, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 583 | log.close() |
| 584 | atexit.register(cleanup) |
| 585 | |
| 586 | def setup_boardspec(item): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 587 | """Process any 'boardspec' marker for a test. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 588 | |
| 589 | Such a marker lists the set of board types that a test does/doesn't |
| 590 | support. If tests are being executed on an unsupported board, the test is |
| 591 | marked to be skipped. |
| 592 | |
| 593 | Args: |
| 594 | item: The pytest test item. |
| 595 | |
| 596 | Returns: |
| 597 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 598 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 599 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 600 | required_boards = [] |
Marek Vasut | 9dfdf6e | 2019-10-24 11:59:19 -0400 | [diff] [blame] | 601 | for boards in item.iter_markers('boardspec'): |
| 602 | board = boards.args[0] |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 603 | if board.startswith('!'): |
| 604 | if ubconfig.board_type == board[1:]: |
Stephen Warren | 0f0eeac | 2017-09-18 11:11:48 -0600 | [diff] [blame] | 605 | pytest.skip('board "%s" not supported' % ubconfig.board_type) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 606 | return |
| 607 | else: |
| 608 | required_boards.append(board) |
| 609 | if required_boards and ubconfig.board_type not in required_boards: |
Stephen Warren | 0f0eeac | 2017-09-18 11:11:48 -0600 | [diff] [blame] | 610 | pytest.skip('board "%s" not supported' % ubconfig.board_type) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 611 | |
| 612 | def setup_buildconfigspec(item): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 613 | """Process any 'buildconfigspec' marker for a test. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 614 | |
| 615 | Such a marker lists some U-Boot configuration feature that the test |
| 616 | requires. If tests are being executed on an U-Boot build that doesn't |
| 617 | have the required feature, the test is marked to be skipped. |
| 618 | |
| 619 | Args: |
| 620 | item: The pytest test item. |
| 621 | |
| 622 | Returns: |
| 623 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 624 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 625 | |
Marek Vasut | 9dfdf6e | 2019-10-24 11:59:19 -0400 | [diff] [blame] | 626 | for options in item.iter_markers('buildconfigspec'): |
| 627 | option = options.args[0] |
| 628 | if not ubconfig.buildconfig.get('config_' + option.lower(), None): |
| 629 | pytest.skip('.config feature "%s" not enabled' % option.lower()) |
Cristian Ciocaltea | 6c6c807 | 2019-12-24 17:19:12 +0200 | [diff] [blame] | 630 | for options in item.iter_markers('notbuildconfigspec'): |
Marek Vasut | 9dfdf6e | 2019-10-24 11:59:19 -0400 | [diff] [blame] | 631 | option = options.args[0] |
| 632 | if ubconfig.buildconfig.get('config_' + option.lower(), None): |
| 633 | pytest.skip('.config feature "%s" enabled' % option.lower()) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 634 | |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 635 | def tool_is_in_path(tool): |
| 636 | for path in os.environ["PATH"].split(os.pathsep): |
| 637 | fn = os.path.join(path, tool) |
| 638 | if os.path.isfile(fn) and os.access(fn, os.X_OK): |
| 639 | return True |
| 640 | return False |
| 641 | |
| 642 | def setup_requiredtool(item): |
| 643 | """Process any 'requiredtool' marker for a test. |
| 644 | |
| 645 | Such a marker lists some external tool (binary, executable, application) |
| 646 | that the test requires. If tests are being executed on a system that |
| 647 | doesn't have the required tool, the test is marked to be skipped. |
| 648 | |
| 649 | Args: |
| 650 | item: The pytest test item. |
| 651 | |
| 652 | Returns: |
| 653 | Nothing. |
| 654 | """ |
| 655 | |
Marek Vasut | 9dfdf6e | 2019-10-24 11:59:19 -0400 | [diff] [blame] | 656 | for tools in item.iter_markers('requiredtool'): |
| 657 | tool = tools.args[0] |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 658 | if not tool_is_in_path(tool): |
| 659 | pytest.skip('tool "%s" not in $PATH' % tool) |
| 660 | |
Simon Glass | 54ef2ca | 2022-08-06 17:51:47 -0600 | [diff] [blame] | 661 | def setup_singlethread(item): |
| 662 | """Process any 'singlethread' marker for a test. |
| 663 | |
| 664 | Skip this test if running in parallel. |
| 665 | |
| 666 | Args: |
| 667 | item: The pytest test item. |
| 668 | |
| 669 | Returns: |
| 670 | Nothing. |
| 671 | """ |
| 672 | for single in item.iter_markers('singlethread'): |
| 673 | worker_id = os.environ.get("PYTEST_XDIST_WORKER") |
| 674 | if worker_id and worker_id != 'master': |
| 675 | pytest.skip('must run single-threaded') |
| 676 | |
Stephen Warren | 3e3d143 | 2016-10-17 17:25:52 -0600 | [diff] [blame] | 677 | def start_test_section(item): |
| 678 | anchors[item.name] = log.start_section(item.name) |
| 679 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 680 | def pytest_runtest_setup(item): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 681 | """pytest hook: Configure (set up) a test item. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 682 | |
| 683 | Called once for each test to perform any custom configuration. This hook |
| 684 | is used to skip the test if certain conditions apply. |
| 685 | |
| 686 | Args: |
| 687 | item: The pytest test item. |
| 688 | |
| 689 | Returns: |
| 690 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 691 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 692 | |
Stephen Warren | 3e3d143 | 2016-10-17 17:25:52 -0600 | [diff] [blame] | 693 | start_test_section(item) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 694 | setup_boardspec(item) |
| 695 | setup_buildconfigspec(item) |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 696 | setup_requiredtool(item) |
Simon Glass | 54ef2ca | 2022-08-06 17:51:47 -0600 | [diff] [blame] | 697 | setup_singlethread(item) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 698 | |
| 699 | def pytest_runtest_protocol(item, nextitem): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 700 | """pytest hook: Called to execute a test. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 701 | |
| 702 | This hook wraps the standard pytest runtestprotocol() function in order |
| 703 | to acquire visibility into, and record, each test function's result. |
| 704 | |
| 705 | Args: |
| 706 | item: The pytest test item to execute. |
| 707 | nextitem: The pytest test item that will be executed after this one. |
| 708 | |
| 709 | Returns: |
| 710 | A list of pytest reports (test result data). |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 711 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 712 | |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 713 | log.get_and_reset_warning() |
Stephen Warren | 76e6a9e | 2021-01-30 20:12:18 -0700 | [diff] [blame] | 714 | ihook = item.ihook |
| 715 | ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 716 | reports = runtestprotocol(item, nextitem=nextitem) |
Stephen Warren | 76e6a9e | 2021-01-30 20:12:18 -0700 | [diff] [blame] | 717 | ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location) |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 718 | was_warning = log.get_and_reset_warning() |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 719 | |
Stephen Warren | 3e3d143 | 2016-10-17 17:25:52 -0600 | [diff] [blame] | 720 | # In pytest 3, runtestprotocol() may not call pytest_runtest_setup() if |
| 721 | # the test is skipped. That call is required to create the test's section |
| 722 | # in the log file. The call to log.end_section() requires that the log |
| 723 | # contain a section for this test. Create a section for the test if it |
| 724 | # doesn't already exist. |
| 725 | if not item.name in anchors: |
| 726 | start_test_section(item) |
| 727 | |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 728 | failure_cleanup = False |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 729 | if not was_warning: |
| 730 | test_list = tests_passed |
| 731 | msg = 'OK' |
| 732 | msg_log = log.status_pass |
| 733 | else: |
| 734 | test_list = tests_warning |
| 735 | msg = 'OK (with warning)' |
| 736 | msg_log = log.status_warning |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 737 | for report in reports: |
| 738 | if report.outcome == 'failed': |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 739 | if hasattr(report, 'wasxfail'): |
| 740 | test_list = tests_xpassed |
| 741 | msg = 'XPASSED' |
| 742 | msg_log = log.status_xpass |
| 743 | else: |
| 744 | failure_cleanup = True |
| 745 | test_list = tests_failed |
| 746 | msg = 'FAILED:\n' + str(report.longrepr) |
| 747 | msg_log = log.status_fail |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 748 | break |
| 749 | if report.outcome == 'skipped': |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 750 | if hasattr(report, 'wasxfail'): |
| 751 | failure_cleanup = True |
| 752 | test_list = tests_xfailed |
| 753 | msg = 'XFAILED:\n' + str(report.longrepr) |
| 754 | msg_log = log.status_xfail |
| 755 | break |
| 756 | test_list = tests_skipped |
| 757 | msg = 'SKIPPED:\n' + str(report.longrepr) |
| 758 | msg_log = log.status_skipped |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 759 | |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 760 | if failure_cleanup: |
Stephen Warren | 97a5466 | 2016-01-22 12:30:09 -0700 | [diff] [blame] | 761 | console.drain_console() |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 762 | |
Stephen Warren | aaf4e91 | 2016-02-10 13:47:37 -0700 | [diff] [blame] | 763 | test_list.append(item.name) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 764 | tests_not_run.remove(item.name) |
| 765 | |
| 766 | try: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 767 | msg_log(msg) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 768 | except: |
| 769 | # If something went wrong with logging, it's better to let the test |
| 770 | # process continue, which may report other exceptions that triggered |
| 771 | # the logging issue (e.g. console.log wasn't created). Hence, just |
| 772 | # squash the exception. If the test setup failed due to e.g. syntax |
| 773 | # error somewhere else, this won't be seen. However, once that issue |
| 774 | # is fixed, if this exception still exists, it will then be logged as |
| 775 | # part of the test's stdout. |
| 776 | import traceback |
Paul Burton | 00f2d20 | 2017-09-14 14:34:43 -0700 | [diff] [blame] | 777 | print('Exception occurred while logging runtest status:') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 778 | traceback.print_exc() |
| 779 | # FIXME: Can we force a test failure here? |
| 780 | |
| 781 | log.end_section(item.name) |
| 782 | |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 783 | if failure_cleanup: |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 784 | console.cleanup_spawn() |
| 785 | |
Stephen Warren | 76e6a9e | 2021-01-30 20:12:18 -0700 | [diff] [blame] | 786 | return True |