blob: 7c1dcfb65fcdd42a7301a725c6e91c15aa5b5ff4 [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 Glassee0c9a72019-07-08 13:18:48 -060049def RunTests(debug, verbosity, processes, args):
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 Glass084059a2018-06-01 09:38:18 -060055 args: List of positional args provided to binman. This can hold a test
56 name to execute (as in 'binman -t testSections', for example)
Simon Glass11ae93e2018-10-01 21:12:47 -060057 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass084059a2018-06-01 09:38:18 -060058 """
Simon Glassb50e5612017-11-13 18:54:54 -070059 import elf_test
Simon Glassbf7fd502016-11-25 20:15:51 -070060 import entry_test
61 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070062 import ftest
Simon Glass19790632017-11-13 18:55:01 -070063 import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070064 import test
65 import doctest
66
67 result = unittest.TestResult()
68 for module in []:
69 suite = doctest.DocTestSuite(module)
70 suite.run(result)
71
72 sys.argv = [sys.argv[0]]
Simon Glass7fe91732017-11-13 18:55:00 -070073 if debug:
74 sys.argv.append('-D')
Simon Glassee0c9a72019-07-08 13:18:48 -060075 if verbosity:
76 sys.argv.append('-v%d' % verbosity)
Simon Glass934cdcf2017-11-12 21:52:21 -070077
78 # Run the entry tests first ,since these need to be the first to import the
79 # 'entry' module.
Simon Glass084059a2018-06-01 09:38:18 -060080 test_name = args and args[0] or None
Simon Glass11ae93e2018-10-01 21:12:47 -060081 suite = unittest.TestSuite()
82 loader = unittest.TestLoader()
Simon Glass2cd01282018-07-06 10:27:18 -060083 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
84 elf_test.TestElf, image_test.TestImage):
Simon Glass084059a2018-06-01 09:38:18 -060085 if test_name:
86 try:
Simon Glass11ae93e2018-10-01 21:12:47 -060087 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass084059a2018-06-01 09:38:18 -060088 except AttributeError:
89 continue
90 else:
Simon Glass11ae93e2018-10-01 21:12:47 -060091 suite.addTests(loader.loadTestsFromTestCase(module))
92 if use_concurrent and processes != 1:
93 concurrent_suite = ConcurrentTestSuite(suite,
94 fork_for_tests(processes or multiprocessing.cpu_count()))
95 concurrent_suite.run(result)
96 else:
Simon Glassbf7fd502016-11-25 20:15:51 -070097 suite.run(result)
98
Simon Glass35343dc2019-05-14 15:53:38 -060099 # Remove errors which just indicate a missing test. Since Python v3.5 If an
100 # ImportError or AttributeError occurs while traversing name then a
101 # synthetic test that raises that error when run will be returned. These
102 # errors are included in the errors accumulated by result.errors.
103 if test_name:
104 errors = []
105 for test, err in result.errors:
106 if ("has no attribute '%s'" % test_name) not in err:
107 errors.append((test, err))
108 result.testsRun -= 1
109 result.errors = errors
110
Simon Glass2ca84682019-05-14 15:53:37 -0600111 print(result)
Simon Glassbf7fd502016-11-25 20:15:51 -0700112 for test, err in result.errors:
Simon Glass2ca84682019-05-14 15:53:37 -0600113 print(test.id(), err)
Simon Glassbf7fd502016-11-25 20:15:51 -0700114 for test, err in result.failures:
Simon Glass2ca84682019-05-14 15:53:37 -0600115 print(err, result.failures)
Simon Glass45cb9d82019-07-08 13:18:33 -0600116 if result.skipped:
117 print('%d binman test%s SKIPPED:' %
118 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
119 for skip_info in result.skipped:
120 print('%s: %s' % (skip_info[0], skip_info[1]))
Simon Glass9677faa2017-11-12 21:52:29 -0700121 if result.errors or result.failures:
Simon Glass45cb9d82019-07-08 13:18:33 -0600122 print('binman tests FAILED')
123 return 1
Simon Glass9677faa2017-11-12 21:52:29 -0700124 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -0700125
Simon Glassfd8d1f72018-07-17 13:25:36 -0600126def GetEntryModules(include_testing=True):
127 """Get a set of entry class implementations
128
129 Returns:
130 Set of paths to entry class filenames
131 """
132 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
133 return set([os.path.splitext(os.path.basename(item))[0]
134 for item in glob_list
135 if include_testing or '_testing' not in item])
136
Simon Glassbf7fd502016-11-25 20:15:51 -0700137def RunTestCoverage():
138 """Run the tests and check that we get 100% coverage"""
Simon Glassfd8d1f72018-07-17 13:25:36 -0600139 glob_list = GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -0600140 all_set = set([os.path.splitext(os.path.basename(item))[0]
141 for item in glob_list if '_testing' not in item])
Simon Glassff1fd6c2018-07-06 10:27:23 -0600142 test_util.RunTestCoverage('tools/binman/binman.py', None,
143 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
144 options.build_dir, all_set)
Simon Glassbf7fd502016-11-25 20:15:51 -0700145
146def RunBinman(options, args):
147 """Main entry point to binman once arguments are parsed
148
149 Args:
150 options: Command-line options
151 args: Non-option arguments
152 """
153 ret_code = 0
154
Simon Glassbf7fd502016-11-25 20:15:51 -0700155 if not options.debug:
156 sys.tracebacklimit = 0
157
158 if options.test:
Simon Glassee0c9a72019-07-08 13:18:48 -0600159 ret_code = RunTests(options.debug, options.verbosity, options.processes,
160 args[1:])
Simon Glassbf7fd502016-11-25 20:15:51 -0700161
162 elif options.test_coverage:
163 RunTestCoverage()
164
Simon Glassfd8d1f72018-07-17 13:25:36 -0600165 elif options.entry_docs:
166 control.WriteEntryDocs(GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700167
168 else:
169 try:
170 ret_code = control.Binman(options, args)
171 except Exception as e:
Simon Glass2ca84682019-05-14 15:53:37 -0600172 print('binman: %s' % e)
Simon Glassbf7fd502016-11-25 20:15:51 -0700173 if options.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600174 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700175 traceback.print_exc()
176 ret_code = 1
177 return ret_code
178
179
180if __name__ == "__main__":
181 (options, args) = cmdline.ParseArgs(sys.argv)
182 ret_code = RunBinman(options, args)
183 sys.exit(ret_code)