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