blob: 52c03f68c6d0c743250ae4ebb39ab26bfc62a252 [file] [log] [blame]
Jörg Krause66a7a242017-03-06 21:07:11 +01001#!/usr/bin/env python2
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07003
4# Copyright (c) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glassbf7fd502016-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 Glass2ca84682019-05-14 15:53:37 -060012from __future__ import print_function
13
Simon Glass86679ce2019-07-08 13:18:36 -060014from distutils.sysconfig import get_python_lib
Simon Glassa25ebed2017-11-12 21:52:24 -070015import glob
Simon Glass11ae93e2018-10-01 21:12:47 -060016import multiprocessing
Simon Glassbf7fd502016-11-25 20:15:51 -070017import os
Simon Glass86679ce2019-07-08 13:18:36 -060018import site
Simon Glassbf7fd502016-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 Glass11ae93e2018-10-01 21:12:47 -060025for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glass7feccfd2017-06-20 21:28:49 -060026 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glassbf7fd502016-11-25 20:15:51 -070027
Simon Glassb4360202017-05-27 07:38:22 -060028# Bring in the libfdt module
Masahiro Yamada15b97f52017-10-17 13:42:43 +090029sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glassed59e002018-10-01 21:12:40 -060030sys.path.insert(0, os.path.join(our_path,
31 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060032
Simon Glass86679ce2019-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 Glassbf7fd502016-11-25 20:15:51 -070039import cmdline
40import command
Simon Glass11ae93e2018-10-01 21:12:47 -060041use_concurrent = True
42try:
43 from concurrencytest import ConcurrentTestSuite, fork_for_tests
44except:
45 use_concurrent = False
Simon Glassbf7fd502016-11-25 20:15:51 -070046import control
Simon Glassff1fd6c2018-07-06 10:27:23 -060047import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070048
Simon Glass8acce602019-07-08 13:18:50 -060049def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass084059a2018-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 Glassee0c9a72019-07-08 13:18:48 -060054 verbosity: Verbosity level to use
Simon Glassd5164a72019-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 Glass084059a2018-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)
Simon Glass8acce602019-07-08 13:18:50 -060063 toolpath: List of paths to use for tools
Simon Glass084059a2018-06-01 09:38:18 -060064 """
Simon Glassb50e5612017-11-13 18:54:54 -070065 import elf_test
Simon Glassbf7fd502016-11-25 20:15:51 -070066 import entry_test
67 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070068 import ftest
Simon Glass19790632017-11-13 18:55:01 -070069 import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070070 import test
71 import doctest
72
73 result = unittest.TestResult()
74 for module in []:
75 suite = doctest.DocTestSuite(module)
76 suite.run(result)
77
78 sys.argv = [sys.argv[0]]
Simon Glass7fe91732017-11-13 18:55:00 -070079 if debug:
80 sys.argv.append('-D')
Simon Glassee0c9a72019-07-08 13:18:48 -060081 if verbosity:
82 sys.argv.append('-v%d' % verbosity)
Simon Glass8acce602019-07-08 13:18:50 -060083 if toolpath:
84 for path in toolpath:
85 sys.argv += ['--toolpath', path]
Simon Glass934cdcf2017-11-12 21:52:21 -070086
87 # Run the entry tests first ,since these need to be the first to import the
88 # 'entry' module.
Simon Glass084059a2018-06-01 09:38:18 -060089 test_name = args and args[0] or None
Simon Glass11ae93e2018-10-01 21:12:47 -060090 suite = unittest.TestSuite()
91 loader = unittest.TestLoader()
Simon Glass2cd01282018-07-06 10:27:18 -060092 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
93 elf_test.TestElf, image_test.TestImage):
Simon Glassd5164a72019-07-08 13:18:49 -060094 # Test the test module about our arguments, if it is interested
95 if hasattr(module, 'setup_test_args'):
96 setup_test_args = getattr(module, 'setup_test_args')
97 setup_test_args(preserve_indir=test_preserve_dirs,
Simon Glass8acce602019-07-08 13:18:50 -060098 preserve_outdirs=test_preserve_dirs and test_name is not None,
99 toolpath=toolpath)
Simon Glass084059a2018-06-01 09:38:18 -0600100 if test_name:
101 try:
Simon Glass11ae93e2018-10-01 21:12:47 -0600102 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass084059a2018-06-01 09:38:18 -0600103 except AttributeError:
104 continue
105 else:
Simon Glass11ae93e2018-10-01 21:12:47 -0600106 suite.addTests(loader.loadTestsFromTestCase(module))
107 if use_concurrent and processes != 1:
108 concurrent_suite = ConcurrentTestSuite(suite,
109 fork_for_tests(processes or multiprocessing.cpu_count()))
110 concurrent_suite.run(result)
111 else:
Simon Glassbf7fd502016-11-25 20:15:51 -0700112 suite.run(result)
113
Simon Glass35343dc2019-05-14 15:53:38 -0600114 # Remove errors which just indicate a missing test. Since Python v3.5 If an
115 # ImportError or AttributeError occurs while traversing name then a
116 # synthetic test that raises that error when run will be returned. These
117 # errors are included in the errors accumulated by result.errors.
118 if test_name:
119 errors = []
120 for test, err in result.errors:
121 if ("has no attribute '%s'" % test_name) not in err:
122 errors.append((test, err))
123 result.testsRun -= 1
124 result.errors = errors
125
Simon Glass2ca84682019-05-14 15:53:37 -0600126 print(result)
Simon Glassbf7fd502016-11-25 20:15:51 -0700127 for test, err in result.errors:
Simon Glass2ca84682019-05-14 15:53:37 -0600128 print(test.id(), err)
Simon Glassbf7fd502016-11-25 20:15:51 -0700129 for test, err in result.failures:
Simon Glass2ca84682019-05-14 15:53:37 -0600130 print(err, result.failures)
Simon Glass45cb9d82019-07-08 13:18:33 -0600131 if result.skipped:
132 print('%d binman test%s SKIPPED:' %
133 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
134 for skip_info in result.skipped:
135 print('%s: %s' % (skip_info[0], skip_info[1]))
Simon Glass9677faa2017-11-12 21:52:29 -0700136 if result.errors or result.failures:
Simon Glass45cb9d82019-07-08 13:18:33 -0600137 print('binman tests FAILED')
138 return 1
Simon Glass9677faa2017-11-12 21:52:29 -0700139 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -0700140
Simon Glassfd8d1f72018-07-17 13:25:36 -0600141def GetEntryModules(include_testing=True):
142 """Get a set of entry class implementations
143
144 Returns:
145 Set of paths to entry class filenames
146 """
147 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
148 return set([os.path.splitext(os.path.basename(item))[0]
149 for item in glob_list
150 if include_testing or '_testing' not in item])
151
Simon Glassbf7fd502016-11-25 20:15:51 -0700152def RunTestCoverage():
153 """Run the tests and check that we get 100% coverage"""
Simon Glassfd8d1f72018-07-17 13:25:36 -0600154 glob_list = GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -0600155 all_set = set([os.path.splitext(os.path.basename(item))[0]
156 for item in glob_list if '_testing' not in item])
Simon Glassff1fd6c2018-07-06 10:27:23 -0600157 test_util.RunTestCoverage('tools/binman/binman.py', None,
158 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
159 options.build_dir, all_set)
Simon Glassbf7fd502016-11-25 20:15:51 -0700160
161def RunBinman(options, args):
162 """Main entry point to binman once arguments are parsed
163
164 Args:
165 options: Command-line options
166 args: Non-option arguments
167 """
168 ret_code = 0
169
Simon Glassbf7fd502016-11-25 20:15:51 -0700170 if not options.debug:
171 sys.tracebacklimit = 0
172
173 if options.test:
Simon Glassee0c9a72019-07-08 13:18:48 -0600174 ret_code = RunTests(options.debug, options.verbosity, options.processes,
Simon Glass8acce602019-07-08 13:18:50 -0600175 options.test_preserve_dirs, args[1:],
176 options.toolpath)
Simon Glassbf7fd502016-11-25 20:15:51 -0700177
178 elif options.test_coverage:
179 RunTestCoverage()
180
Simon Glassfd8d1f72018-07-17 13:25:36 -0600181 elif options.entry_docs:
182 control.WriteEntryDocs(GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700183
184 else:
185 try:
186 ret_code = control.Binman(options, args)
187 except Exception as e:
Simon Glass2ca84682019-05-14 15:53:37 -0600188 print('binman: %s' % e)
Simon Glassbf7fd502016-11-25 20:15:51 -0700189 if options.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600190 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700191 traceback.print_exc()
192 ret_code = 1
193 return ret_code
194
195
196if __name__ == "__main__":
197 (options, args) = cmdline.ParseArgs(sys.argv)
198 ret_code = RunBinman(options, args)
199 sys.exit(ret_code)