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