Jörg Krause | 66a7a24 | 2017-03-06 21:07:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 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 | bf7fd50 | 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 | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
Simon Glass | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 14 | from distutils.sysconfig import get_python_lib |
Simon Glass | a25ebed | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 15 | import glob |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 16 | import multiprocessing |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | import os |
Simon Glass | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 18 | import site |
Simon Glass | bf7fd50 | 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 | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 25 | for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']: |
Simon Glass | 7feccfd | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 26 | sys.path.insert(0, os.path.join(our_path, dirname)) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 27 | |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 28 | # Bring in the libfdt module |
Masahiro Yamada | 15b97f5 | 2017-10-17 13:42:43 +0900 | [diff] [blame] | 29 | sys.path.insert(0, 'scripts/dtc/pylibfdt') |
Simon Glass | ed59e00 | 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 | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 32 | |
Simon Glass | 86679ce | 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 | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 39 | import cmdline |
| 40 | import command |
Simon Glass | 11ae93e | 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 | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 46 | import control |
Simon Glass | ff1fd6c | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 47 | import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 48 | |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 49 | def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath): |
Simon Glass | 084059a | 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 | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 54 | verbosity: Verbosity level to use |
Simon Glass | d5164a7 | 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 | 084059a | 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) |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 63 | toolpath: List of paths to use for tools |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 64 | """ |
Simon Glass | 4997a7e | 2019-07-08 13:18:52 -0600 | [diff] [blame^] | 65 | import cbfs_util_test |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 66 | import elf_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 67 | import entry_test |
| 68 | import fdt_test |
Simon Glass | 680e331 | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 69 | import ftest |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 70 | import image_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 71 | import test |
| 72 | import doctest |
| 73 | |
| 74 | result = unittest.TestResult() |
| 75 | for module in []: |
| 76 | suite = doctest.DocTestSuite(module) |
| 77 | suite.run(result) |
| 78 | |
| 79 | sys.argv = [sys.argv[0]] |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 80 | if debug: |
| 81 | sys.argv.append('-D') |
Simon Glass | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 82 | if verbosity: |
| 83 | sys.argv.append('-v%d' % verbosity) |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 84 | if toolpath: |
| 85 | for path in toolpath: |
| 86 | sys.argv += ['--toolpath', path] |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 87 | |
| 88 | # Run the entry tests first ,since these need to be the first to import the |
| 89 | # 'entry' module. |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 90 | test_name = args and args[0] or None |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 91 | suite = unittest.TestSuite() |
| 92 | loader = unittest.TestLoader() |
Simon Glass | 2cd0128 | 2018-07-06 10:27:18 -0600 | [diff] [blame] | 93 | for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt, |
Simon Glass | 4997a7e | 2019-07-08 13:18:52 -0600 | [diff] [blame^] | 94 | elf_test.TestElf, image_test.TestImage, |
| 95 | cbfs_util_test.TestCbfs): |
Simon Glass | d5164a7 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 96 | # Test the test module about our arguments, if it is interested |
| 97 | if hasattr(module, 'setup_test_args'): |
| 98 | setup_test_args = getattr(module, 'setup_test_args') |
| 99 | setup_test_args(preserve_indir=test_preserve_dirs, |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 100 | preserve_outdirs=test_preserve_dirs and test_name is not None, |
| 101 | toolpath=toolpath) |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 102 | if test_name: |
| 103 | try: |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 104 | suite.addTests(loader.loadTestsFromName(test_name, module)) |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 105 | except AttributeError: |
| 106 | continue |
| 107 | else: |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 108 | suite.addTests(loader.loadTestsFromTestCase(module)) |
| 109 | if use_concurrent and processes != 1: |
| 110 | concurrent_suite = ConcurrentTestSuite(suite, |
| 111 | fork_for_tests(processes or multiprocessing.cpu_count())) |
| 112 | concurrent_suite.run(result) |
| 113 | else: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 114 | suite.run(result) |
| 115 | |
Simon Glass | 35343dc | 2019-05-14 15:53:38 -0600 | [diff] [blame] | 116 | # Remove errors which just indicate a missing test. Since Python v3.5 If an |
| 117 | # ImportError or AttributeError occurs while traversing name then a |
| 118 | # synthetic test that raises that error when run will be returned. These |
| 119 | # errors are included in the errors accumulated by result.errors. |
| 120 | if test_name: |
| 121 | errors = [] |
| 122 | for test, err in result.errors: |
| 123 | if ("has no attribute '%s'" % test_name) not in err: |
| 124 | errors.append((test, err)) |
| 125 | result.testsRun -= 1 |
| 126 | result.errors = errors |
| 127 | |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 128 | print(result) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 129 | for test, err in result.errors: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 130 | print(test.id(), err) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 131 | for test, err in result.failures: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 132 | print(err, result.failures) |
Simon Glass | 45cb9d8 | 2019-07-08 13:18:33 -0600 | [diff] [blame] | 133 | if result.skipped: |
| 134 | print('%d binman test%s SKIPPED:' % |
| 135 | (len(result.skipped), 's' if len(result.skipped) > 1 else '')) |
| 136 | for skip_info in result.skipped: |
| 137 | print('%s: %s' % (skip_info[0], skip_info[1])) |
Simon Glass | 9677faa | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 138 | if result.errors or result.failures: |
Simon Glass | 45cb9d8 | 2019-07-08 13:18:33 -0600 | [diff] [blame] | 139 | print('binman tests FAILED') |
| 140 | return 1 |
Simon Glass | 9677faa | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 141 | return 0 |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 142 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 143 | def GetEntryModules(include_testing=True): |
| 144 | """Get a set of entry class implementations |
| 145 | |
| 146 | Returns: |
| 147 | Set of paths to entry class filenames |
| 148 | """ |
| 149 | glob_list = glob.glob(os.path.join(our_path, 'etype/*.py')) |
| 150 | return set([os.path.splitext(os.path.basename(item))[0] |
| 151 | for item in glob_list |
| 152 | if include_testing or '_testing' not in item]) |
| 153 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 154 | def RunTestCoverage(): |
| 155 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 156 | glob_list = GetEntryModules(False) |
Tom Rini | 16d836c | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 157 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 158 | for item in glob_list if '_testing' not in item]) |
Simon Glass | ff1fd6c | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 159 | test_util.RunTestCoverage('tools/binman/binman.py', None, |
| 160 | ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'], |
| 161 | options.build_dir, all_set) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 162 | |
| 163 | def RunBinman(options, args): |
| 164 | """Main entry point to binman once arguments are parsed |
| 165 | |
| 166 | Args: |
| 167 | options: Command-line options |
| 168 | args: Non-option arguments |
| 169 | """ |
| 170 | ret_code = 0 |
| 171 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 172 | if not options.debug: |
| 173 | sys.tracebacklimit = 0 |
| 174 | |
| 175 | if options.test: |
Simon Glass | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 176 | ret_code = RunTests(options.debug, options.verbosity, options.processes, |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 177 | options.test_preserve_dirs, args[1:], |
| 178 | options.toolpath) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 179 | |
| 180 | elif options.test_coverage: |
| 181 | RunTestCoverage() |
| 182 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 183 | elif options.entry_docs: |
| 184 | control.WriteEntryDocs(GetEntryModules()) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 185 | |
| 186 | else: |
| 187 | try: |
| 188 | ret_code = control.Binman(options, args) |
| 189 | except Exception as e: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 190 | print('binman: %s' % e) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 191 | if options.debug: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 192 | print() |
Simon Glass | bf7fd50 | 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__": |
| 199 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 200 | ret_code = RunBinman(options, args) |
| 201 | sys.exit(ret_code) |