blob: 9878eb86d4faf0b1faa14401c8a1566695d8b49c [file] [log] [blame]
Jörg Krausecd6d46e2017-03-06 21:07:11 +01001#!/usr/bin/env python2
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07003
4# Copyright (c) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glass2574ef62016-11-25 20:15:51 -07007# Creates binary images from input files controlled by a description
8#
9
10"""See README for more information"""
11
Simon Glass7cca27d2019-05-14 15:53:37 -060012from __future__ import print_function
13
Simon Glass40778d72019-07-08 13:18:36 -060014from distutils.sysconfig import get_python_lib
Simon Glass2c3cf452017-11-12 21:52:24 -070015import glob
Simon Glass7057d022018-10-01 21:12:47 -060016import multiprocessing
Simon Glass2574ef62016-11-25 20:15:51 -070017import os
Simon Glass40778d72019-07-08 13:18:36 -060018import site
Simon Glass2574ef62016-11-25 20:15:51 -070019import sys
20import traceback
21import unittest
22
23# Bring in the patman and dtoc libraries
24our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass7057d022018-10-01 21:12:47 -060025for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glassdde3e712017-06-20 21:28:49 -060026 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glass2574ef62016-11-25 20:15:51 -070027
Simon Glass55901ff2017-05-27 07:38:22 -060028# Bring in the libfdt module
Masahiro Yamada47ae5392017-10-17 13:42:43 +090029sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glass5656ca22018-10-01 21:12:40 -060030sys.path.insert(0, os.path.join(our_path,
31 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060032
Simon Glass40778d72019-07-08 13:18:36 -060033# 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.
37sys.path.append(get_python_lib())
38
Simon Glass2574ef62016-11-25 20:15:51 -070039import cmdline
40import command
Simon Glass7057d022018-10-01 21:12:47 -060041use_concurrent = True
42try:
43 from concurrencytest import ConcurrentTestSuite, fork_for_tests
44except:
45 use_concurrent = False
Simon Glass2574ef62016-11-25 20:15:51 -070046import control
Simon Glass132be852018-07-06 10:27:23 -060047import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070048
Simon Glass1c420c92019-07-08 13:18:49 -060049def RunTests(debug, verbosity, processes, test_preserve_dirs, args):
Simon Glass5666f9a2018-06-01 09:38:18 -060050 """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 Glass8a50b4a2019-07-08 13:18:48 -060054 verbosity: Verbosity level to use
Simon Glass1c420c92019-07-08 13:18:49 -060055 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 Glass5666f9a2018-06-01 09:38:18 -060061 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 Glass24ad3652017-11-13 18:54:54 -070064 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070065 import entry_test
66 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070067 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070068 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070069 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 Glass075a45c2017-11-13 18:55:00 -070078 if debug:
79 sys.argv.append('-D')
Simon Glass8a50b4a2019-07-08 13:18:48 -060080 if verbosity:
81 sys.argv.append('-v%d' % verbosity)
Simon Glass8f521362017-11-12 21:52:21 -070082
83 # Run the entry tests first ,since these need to be the first to import the
84 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060085 test_name = args and args[0] or None
Simon Glass7057d022018-10-01 21:12:47 -060086 suite = unittest.TestSuite()
87 loader = unittest.TestLoader()
Simon Glass02e0fc62018-07-06 10:27:18 -060088 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
89 elf_test.TestElf, image_test.TestImage):
Simon Glass1c420c92019-07-08 13:18:49 -060090 # 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 Glass5666f9a2018-06-01 09:38:18 -060095 if test_name:
96 try:
Simon Glass7057d022018-10-01 21:12:47 -060097 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass5666f9a2018-06-01 09:38:18 -060098 except AttributeError:
99 continue
100 else:
Simon Glass7057d022018-10-01 21:12:47 -0600101 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 Glass2574ef62016-11-25 20:15:51 -0700107 suite.run(result)
108
Simon Glassd1ba61c2019-05-14 15:53:38 -0600109 # 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 Glass7cca27d2019-05-14 15:53:37 -0600121 print(result)
Simon Glass2574ef62016-11-25 20:15:51 -0700122 for test, err in result.errors:
Simon Glass7cca27d2019-05-14 15:53:37 -0600123 print(test.id(), err)
Simon Glass2574ef62016-11-25 20:15:51 -0700124 for test, err in result.failures:
Simon Glass7cca27d2019-05-14 15:53:37 -0600125 print(err, result.failures)
Simon Glass231976a2019-07-08 13:18:33 -0600126 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 Glass9ba021c2017-11-12 21:52:29 -0700131 if result.errors or result.failures:
Simon Glass231976a2019-07-08 13:18:33 -0600132 print('binman tests FAILED')
133 return 1
Simon Glass9ba021c2017-11-12 21:52:29 -0700134 return 0
Simon Glass2574ef62016-11-25 20:15:51 -0700135
Simon Glass969616c2018-07-17 13:25:36 -0600136def 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 Glass2574ef62016-11-25 20:15:51 -0700147def RunTestCoverage():
148 """Run the tests and check that we get 100% coverage"""
Simon Glass969616c2018-07-17 13:25:36 -0600149 glob_list = GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -0600150 all_set = set([os.path.splitext(os.path.basename(item))[0]
151 for item in glob_list if '_testing' not in item])
Simon Glass132be852018-07-06 10:27:23 -0600152 test_util.RunTestCoverage('tools/binman/binman.py', None,
153 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
154 options.build_dir, all_set)
Simon Glass2574ef62016-11-25 20:15:51 -0700155
156def 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 Glass2574ef62016-11-25 20:15:51 -0700165 if not options.debug:
166 sys.tracebacklimit = 0
167
168 if options.test:
Simon Glass8a50b4a2019-07-08 13:18:48 -0600169 ret_code = RunTests(options.debug, options.verbosity, options.processes,
Simon Glass1c420c92019-07-08 13:18:49 -0600170 options.test_preserve_dirs, args[1:])
Simon Glass2574ef62016-11-25 20:15:51 -0700171
172 elif options.test_coverage:
173 RunTestCoverage()
174
Simon Glass969616c2018-07-17 13:25:36 -0600175 elif options.entry_docs:
176 control.WriteEntryDocs(GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700177
178 else:
179 try:
180 ret_code = control.Binman(options, args)
181 except Exception as e:
Simon Glass7cca27d2019-05-14 15:53:37 -0600182 print('binman: %s' % e)
Simon Glass2574ef62016-11-25 20:15:51 -0700183 if options.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600184 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700185 traceback.print_exc()
186 ret_code = 1
187 return ret_code
188
189
190if __name__ == "__main__":
191 (options, args) = cmdline.ParseArgs(sys.argv)
192 ret_code = RunBinman(options, args)
193 sys.exit(ret_code)