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