Simon Glass | 377bca8 | 2019-10-31 07:43:05 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 3 | |
| 4 | # Copyright (c) 2016 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 7 | # Creates binary images from input files controlled by a description |
| 8 | # |
| 9 | |
| 10 | """See README for more information""" |
| 11 | |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
Simon Glass | 40778d7 | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 14 | from distutils.sysconfig import get_python_lib |
Simon Glass | 2c3cf45 | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 15 | import glob |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 16 | import multiprocessing |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | import os |
Simon Glass | 40778d7 | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 18 | import site |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 19 | import sys |
| 20 | import traceback |
| 21 | import unittest |
| 22 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 23 | # Bring in the patman and dtoc libraries (but don't override the first path |
| 24 | # in PYTHONPATH) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 25 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 26 | for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 27 | sys.path.insert(2, os.path.join(our_path, dirname)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 28 | |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 29 | # Bring in the libfdt module |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 30 | sys.path.insert(2, 'scripts/dtc/pylibfdt') |
| 31 | sys.path.insert(2, os.path.join(our_path, |
Simon Glass | 5656ca2 | 2018-10-01 21:12:40 -0600 | [diff] [blame] | 32 | '../../build-sandbox_spl/scripts/dtc/pylibfdt')) |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 33 | |
Simon Glass | 40778d7 | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 34 | # When running under python-coverage on Ubuntu 16.04, the dist-packages |
| 35 | # directories are dropped from the python path. Add them in so that we can find |
| 36 | # the elffile module. We could use site.getsitepackages() here but unfortunately |
| 37 | # that is not available in a virtualenv. |
| 38 | sys.path.append(get_python_lib()) |
| 39 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | import cmdline |
| 41 | import command |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 42 | use_concurrent = True |
| 43 | try: |
| 44 | from concurrencytest import ConcurrentTestSuite, fork_for_tests |
| 45 | except: |
| 46 | use_concurrent = False |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 47 | import control |
Simon Glass | 132be85 | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 48 | import test_util |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 49 | |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 50 | def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath): |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 51 | """Run the functional tests and any embedded doctests |
| 52 | |
| 53 | Args: |
| 54 | debug: True to enable debugging, which shows a full stack trace on error |
Simon Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 55 | verbosity: Verbosity level to use |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 56 | test_preserve_dirs: True to preserve the input directory used by tests |
| 57 | so that it can be examined afterwards (only useful for debugging |
| 58 | tests). If a single test is selected (in args[0]) it also preserves |
| 59 | the output directory for this test. Both directories are displayed |
| 60 | on the command line. |
| 61 | processes: Number of processes to use to run tests (None=same as #CPUs) |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 62 | args: List of positional args provided to binman. This can hold a test |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 63 | name to execute (as in 'binman test testSections', for example) |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 64 | toolpath: List of paths to use for tools |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 65 | """ |
Simon Glass | 96a6296 | 2019-07-08 13:18:52 -0600 | [diff] [blame] | 66 | import cbfs_util_test |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 67 | import elf_test |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 68 | import entry_test |
| 69 | import fdt_test |
Simon Glass | 076e63b | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 70 | import ftest |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 71 | import image_test |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 72 | import test |
| 73 | import doctest |
| 74 | |
| 75 | result = unittest.TestResult() |
| 76 | for module in []: |
| 77 | suite = doctest.DocTestSuite(module) |
| 78 | suite.run(result) |
| 79 | |
| 80 | sys.argv = [sys.argv[0]] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 81 | if debug: |
| 82 | sys.argv.append('-D') |
Simon Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 83 | if verbosity: |
| 84 | sys.argv.append('-v%d' % verbosity) |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 85 | if toolpath: |
| 86 | for path in toolpath: |
| 87 | sys.argv += ['--toolpath', path] |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 88 | |
| 89 | # Run the entry tests first ,since these need to be the first to import the |
| 90 | # 'entry' module. |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 91 | test_name = args and args[0] or None |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 92 | suite = unittest.TestSuite() |
| 93 | loader = unittest.TestLoader() |
Simon Glass | 02e0fc6 | 2018-07-06 10:27:18 -0600 | [diff] [blame] | 94 | for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt, |
Simon Glass | 96a6296 | 2019-07-08 13:18:52 -0600 | [diff] [blame] | 95 | elf_test.TestElf, image_test.TestImage, |
| 96 | cbfs_util_test.TestCbfs): |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 97 | # Test the test module about our arguments, if it is interested |
| 98 | if hasattr(module, 'setup_test_args'): |
| 99 | setup_test_args = getattr(module, 'setup_test_args') |
| 100 | setup_test_args(preserve_indir=test_preserve_dirs, |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 101 | preserve_outdirs=test_preserve_dirs and test_name is not None, |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 102 | toolpath=toolpath, verbosity=verbosity) |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 103 | if test_name: |
| 104 | try: |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 105 | suite.addTests(loader.loadTestsFromName(test_name, module)) |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 106 | except AttributeError: |
| 107 | continue |
| 108 | else: |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 109 | suite.addTests(loader.loadTestsFromTestCase(module)) |
| 110 | if use_concurrent and processes != 1: |
| 111 | concurrent_suite = ConcurrentTestSuite(suite, |
| 112 | fork_for_tests(processes or multiprocessing.cpu_count())) |
| 113 | concurrent_suite.run(result) |
| 114 | else: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 115 | suite.run(result) |
| 116 | |
Simon Glass | d1ba61c | 2019-05-14 15:53:38 -0600 | [diff] [blame] | 117 | # Remove errors which just indicate a missing test. Since Python v3.5 If an |
| 118 | # ImportError or AttributeError occurs while traversing name then a |
| 119 | # synthetic test that raises that error when run will be returned. These |
| 120 | # errors are included in the errors accumulated by result.errors. |
| 121 | if test_name: |
| 122 | errors = [] |
| 123 | for test, err in result.errors: |
| 124 | if ("has no attribute '%s'" % test_name) not in err: |
| 125 | errors.append((test, err)) |
| 126 | result.testsRun -= 1 |
| 127 | result.errors = errors |
| 128 | |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 129 | print(result) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 130 | for test, err in result.errors: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 131 | print(test.id(), err) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 132 | for test, err in result.failures: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 133 | print(err, result.failures) |
Simon Glass | 231976a | 2019-07-08 13:18:33 -0600 | [diff] [blame] | 134 | if result.skipped: |
| 135 | print('%d binman test%s SKIPPED:' % |
| 136 | (len(result.skipped), 's' if len(result.skipped) > 1 else '')) |
| 137 | for skip_info in result.skipped: |
| 138 | print('%s: %s' % (skip_info[0], skip_info[1])) |
Simon Glass | 9ba021c | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 139 | if result.errors or result.failures: |
Simon Glass | 231976a | 2019-07-08 13:18:33 -0600 | [diff] [blame] | 140 | print('binman tests FAILED') |
| 141 | return 1 |
Simon Glass | 9ba021c | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 142 | return 0 |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 143 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 144 | def GetEntryModules(include_testing=True): |
| 145 | """Get a set of entry class implementations |
| 146 | |
| 147 | Returns: |
| 148 | Set of paths to entry class filenames |
| 149 | """ |
| 150 | glob_list = glob.glob(os.path.join(our_path, 'etype/*.py')) |
| 151 | return set([os.path.splitext(os.path.basename(item))[0] |
| 152 | for item in glob_list |
| 153 | if include_testing or '_testing' not in item]) |
| 154 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 155 | def RunTestCoverage(): |
| 156 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 157 | glob_list = GetEntryModules(False) |
Tom Rini | c2a849d | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 158 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 159 | for item in glob_list if '_testing' not in item]) |
Simon Glass | 132be85 | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 160 | test_util.RunTestCoverage('tools/binman/binman.py', None, |
| 161 | ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'], |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 162 | args.build_dir, all_set) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 163 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 164 | def RunBinman(args): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 165 | """Main entry point to binman once arguments are parsed |
| 166 | |
| 167 | Args: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 168 | args: Command line arguments Namespace object |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 169 | """ |
| 170 | ret_code = 0 |
| 171 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 172 | if not args.debug: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 173 | sys.tracebacklimit = 0 |
| 174 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 175 | if args.cmd == 'test': |
| 176 | if args.test_coverage: |
| 177 | RunTestCoverage() |
| 178 | else: |
| 179 | ret_code = RunTests(args.debug, args.verbosity, args.processes, |
| 180 | args.test_preserve_dirs, args.tests, |
| 181 | args.toolpath) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 182 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 183 | elif args.cmd == 'entry-docs': |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 184 | control.WriteEntryDocs(GetEntryModules()) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 185 | |
| 186 | else: |
| 187 | try: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 188 | ret_code = control.Binman(args) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 189 | except Exception as e: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 190 | print('binman: %s' % e) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 191 | if args.debug: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 192 | print() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 193 | traceback.print_exc() |
| 194 | ret_code = 1 |
| 195 | return ret_code |
| 196 | |
| 197 | |
| 198 | if __name__ == "__main__": |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 199 | args = cmdline.ParseArgs(sys.argv[1:]) |
| 200 | |
| 201 | ret_code = RunBinman(args) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 202 | sys.exit(ret_code) |