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