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 | 2c3cf45 | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 14 | import glob |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 15 | import multiprocessing |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 16 | import os |
| 17 | import sys |
| 18 | import traceback |
| 19 | import unittest |
| 20 | |
| 21 | # Bring in the patman and dtoc libraries |
| 22 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 23 | for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']: |
Simon Glass | dde3e71 | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 24 | sys.path.insert(0, os.path.join(our_path, dirname)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 25 | |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 26 | # Bring in the libfdt module |
Masahiro Yamada | 47ae539 | 2017-10-17 13:42:43 +0900 | [diff] [blame] | 27 | sys.path.insert(0, 'scripts/dtc/pylibfdt') |
Simon Glass | 5656ca2 | 2018-10-01 21:12:40 -0600 | [diff] [blame] | 28 | sys.path.insert(0, os.path.join(our_path, |
| 29 | '../../build-sandbox_spl/scripts/dtc/pylibfdt')) |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 30 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 31 | import cmdline |
| 32 | import command |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 33 | use_concurrent = True |
| 34 | try: |
| 35 | from concurrencytest import ConcurrentTestSuite, fork_for_tests |
| 36 | except: |
| 37 | use_concurrent = False |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 38 | import control |
Simon Glass | 132be85 | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 39 | import test_util |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 41 | def RunTests(debug, processes, args): |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 42 | """Run the functional tests and any embedded doctests |
| 43 | |
| 44 | Args: |
| 45 | debug: True to enable debugging, which shows a full stack trace on error |
| 46 | args: List of positional args provided to binman. This can hold a test |
| 47 | name to execute (as in 'binman -t testSections', for example) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 48 | 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] | 49 | """ |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 50 | import elf_test |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 51 | import entry_test |
| 52 | import fdt_test |
Simon Glass | 076e63b | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 53 | import ftest |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 54 | import image_test |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 55 | import test |
| 56 | import doctest |
| 57 | |
| 58 | result = unittest.TestResult() |
| 59 | for module in []: |
| 60 | suite = doctest.DocTestSuite(module) |
| 61 | suite.run(result) |
| 62 | |
| 63 | sys.argv = [sys.argv[0]] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 64 | if debug: |
| 65 | sys.argv.append('-D') |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 66 | if debug: |
| 67 | sys.argv.append('-D') |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 68 | |
| 69 | # Run the entry tests first ,since these need to be the first to import the |
| 70 | # 'entry' module. |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 71 | test_name = args and args[0] or None |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 72 | suite = unittest.TestSuite() |
| 73 | loader = unittest.TestLoader() |
Simon Glass | 02e0fc6 | 2018-07-06 10:27:18 -0600 | [diff] [blame] | 74 | for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt, |
| 75 | elf_test.TestElf, image_test.TestImage): |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 76 | if test_name: |
| 77 | try: |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 78 | suite.addTests(loader.loadTestsFromName(test_name, module)) |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 79 | except AttributeError: |
| 80 | continue |
| 81 | else: |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 82 | suite.addTests(loader.loadTestsFromTestCase(module)) |
| 83 | if use_concurrent and processes != 1: |
| 84 | concurrent_suite = ConcurrentTestSuite(suite, |
| 85 | fork_for_tests(processes or multiprocessing.cpu_count())) |
| 86 | concurrent_suite.run(result) |
| 87 | else: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 88 | suite.run(result) |
| 89 | |
Simon Glass | d1ba61c | 2019-05-14 15:53:38 -0600 | [diff] [blame] | 90 | # Remove errors which just indicate a missing test. Since Python v3.5 If an |
| 91 | # ImportError or AttributeError occurs while traversing name then a |
| 92 | # synthetic test that raises that error when run will be returned. These |
| 93 | # errors are included in the errors accumulated by result.errors. |
| 94 | if test_name: |
| 95 | errors = [] |
| 96 | for test, err in result.errors: |
| 97 | if ("has no attribute '%s'" % test_name) not in err: |
| 98 | errors.append((test, err)) |
| 99 | result.testsRun -= 1 |
| 100 | result.errors = errors |
| 101 | |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 102 | print(result) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 103 | for test, err in result.errors: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 104 | print(test.id(), err) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 105 | for test, err in result.failures: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 106 | print(err, result.failures) |
Simon Glass | 9ba021c | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 107 | if result.errors or result.failures: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 108 | print('binman tests FAILED') |
Simon Glass | 9ba021c | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 109 | return 1 |
| 110 | return 0 |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 111 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 112 | def GetEntryModules(include_testing=True): |
| 113 | """Get a set of entry class implementations |
| 114 | |
| 115 | Returns: |
| 116 | Set of paths to entry class filenames |
| 117 | """ |
| 118 | glob_list = glob.glob(os.path.join(our_path, 'etype/*.py')) |
| 119 | return set([os.path.splitext(os.path.basename(item))[0] |
| 120 | for item in glob_list |
| 121 | if include_testing or '_testing' not in item]) |
| 122 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 123 | def RunTestCoverage(): |
| 124 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 125 | glob_list = GetEntryModules(False) |
Tom Rini | c2a849d | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 126 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 127 | for item in glob_list if '_testing' not in item]) |
Simon Glass | 132be85 | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 128 | test_util.RunTestCoverage('tools/binman/binman.py', None, |
| 129 | ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'], |
| 130 | options.build_dir, all_set) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 131 | |
| 132 | def RunBinman(options, args): |
| 133 | """Main entry point to binman once arguments are parsed |
| 134 | |
| 135 | Args: |
| 136 | options: Command-line options |
| 137 | args: Non-option arguments |
| 138 | """ |
| 139 | ret_code = 0 |
| 140 | |
| 141 | # For testing: This enables full exception traces. |
| 142 | #options.debug = True |
| 143 | |
| 144 | if not options.debug: |
| 145 | sys.tracebacklimit = 0 |
| 146 | |
| 147 | if options.test: |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 148 | ret_code = RunTests(options.debug, options.processes, args[1:]) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 149 | |
| 150 | elif options.test_coverage: |
| 151 | RunTestCoverage() |
| 152 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 153 | elif options.entry_docs: |
| 154 | control.WriteEntryDocs(GetEntryModules()) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 155 | |
| 156 | else: |
| 157 | try: |
| 158 | ret_code = control.Binman(options, args) |
| 159 | except Exception as e: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 160 | print('binman: %s' % e) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 161 | if options.debug: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 162 | print() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 163 | traceback.print_exc() |
| 164 | ret_code = 1 |
| 165 | return ret_code |
| 166 | |
| 167 | |
| 168 | if __name__ == "__main__": |
| 169 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 170 | ret_code = RunBinman(options, args) |
| 171 | sys.exit(ret_code) |