blob: 613aad5c4514795945b15fb7abfc37afba22e1aa [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 Glass4997a7e2019-07-08 13:18:52 -060065 import cbfs_util_test
Simon Glassb50e5612017-11-13 18:54:54 -070066 import elf_test
Simon Glassbf7fd502016-11-25 20:15:51 -070067 import entry_test
68 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070069 import ftest
Simon Glass19790632017-11-13 18:55:01 -070070 import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070071 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 Glass7fe91732017-11-13 18:55:00 -070080 if debug:
81 sys.argv.append('-D')
Simon Glassee0c9a72019-07-08 13:18:48 -060082 if verbosity:
83 sys.argv.append('-v%d' % verbosity)
Simon Glass8acce602019-07-08 13:18:50 -060084 if toolpath:
85 for path in toolpath:
86 sys.argv += ['--toolpath', path]
Simon Glass934cdcf2017-11-12 21:52:21 -070087
88 # Run the entry tests first ,since these need to be the first to import the
89 # 'entry' module.
Simon Glass084059a2018-06-01 09:38:18 -060090 test_name = args and args[0] or None
Simon Glass11ae93e2018-10-01 21:12:47 -060091 suite = unittest.TestSuite()
92 loader = unittest.TestLoader()
Simon Glass2cd01282018-07-06 10:27:18 -060093 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
Simon Glass4997a7e2019-07-08 13:18:52 -060094 elf_test.TestElf, image_test.TestImage,
95 cbfs_util_test.TestCbfs):
Simon Glassd5164a72019-07-08 13:18:49 -060096 # 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 Glass8acce602019-07-08 13:18:50 -0600100 preserve_outdirs=test_preserve_dirs and test_name is not None,
101 toolpath=toolpath)
Simon Glass084059a2018-06-01 09:38:18 -0600102 if test_name:
103 try:
Simon Glass11ae93e2018-10-01 21:12:47 -0600104 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass084059a2018-06-01 09:38:18 -0600105 except AttributeError:
106 continue
107 else:
Simon Glass11ae93e2018-10-01 21:12:47 -0600108 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 Glassbf7fd502016-11-25 20:15:51 -0700114 suite.run(result)
115
Simon Glass35343dc2019-05-14 15:53:38 -0600116 # 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 Glass2ca84682019-05-14 15:53:37 -0600128 print(result)
Simon Glassbf7fd502016-11-25 20:15:51 -0700129 for test, err in result.errors:
Simon Glass2ca84682019-05-14 15:53:37 -0600130 print(test.id(), err)
Simon Glassbf7fd502016-11-25 20:15:51 -0700131 for test, err in result.failures:
Simon Glass2ca84682019-05-14 15:53:37 -0600132 print(err, result.failures)
Simon Glass45cb9d82019-07-08 13:18:33 -0600133 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 Glass9677faa2017-11-12 21:52:29 -0700138 if result.errors or result.failures:
Simon Glass45cb9d82019-07-08 13:18:33 -0600139 print('binman tests FAILED')
140 return 1
Simon Glass9677faa2017-11-12 21:52:29 -0700141 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -0700142
Simon Glassfd8d1f72018-07-17 13:25:36 -0600143def 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 Glassbf7fd502016-11-25 20:15:51 -0700154def RunTestCoverage():
155 """Run the tests and check that we get 100% coverage"""
Simon Glassfd8d1f72018-07-17 13:25:36 -0600156 glob_list = GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -0600157 all_set = set([os.path.splitext(os.path.basename(item))[0]
158 for item in glob_list if '_testing' not in item])
Simon Glassff1fd6c2018-07-06 10:27:23 -0600159 test_util.RunTestCoverage('tools/binman/binman.py', None,
160 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
161 options.build_dir, all_set)
Simon Glassbf7fd502016-11-25 20:15:51 -0700162
163def 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 Glassbf7fd502016-11-25 20:15:51 -0700172 if not options.debug:
173 sys.tracebacklimit = 0
174
175 if options.test:
Simon Glassee0c9a72019-07-08 13:18:48 -0600176 ret_code = RunTests(options.debug, options.verbosity, options.processes,
Simon Glass8acce602019-07-08 13:18:50 -0600177 options.test_preserve_dirs, args[1:],
178 options.toolpath)
Simon Glassbf7fd502016-11-25 20:15:51 -0700179
180 elif options.test_coverage:
181 RunTestCoverage()
182
Simon Glassfd8d1f72018-07-17 13:25:36 -0600183 elif options.entry_docs:
184 control.WriteEntryDocs(GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700185
186 else:
187 try:
188 ret_code = control.Binman(options, args)
189 except Exception as e:
Simon Glass2ca84682019-05-14 15:53:37 -0600190 print('binman: %s' % e)
Simon Glassbf7fd502016-11-25 20:15:51 -0700191 if options.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600192 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700193 traceback.print_exc()
194 ret_code = 1
195 return ret_code
196
197
198if __name__ == "__main__":
199 (options, args) = cmdline.ParseArgs(sys.argv)
200 ret_code = RunBinman(options, args)
201 sys.exit(ret_code)