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