blob: 9e6fd7211754cd192718a566918e6072f7e93106 [file] [log] [blame]
Simon Glass377bca82019-10-31 07:43:05 -06001#!/usr/bin/env python3
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
Simon Glassf46732a2019-07-08 14:25:29 -060023# Bring in the patman and dtoc libraries (but don't override the first path
24# in PYTHONPATH)
Simon Glass2574ef62016-11-25 20:15:51 -070025our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass7057d022018-10-01 21:12:47 -060026for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glassf46732a2019-07-08 14:25:29 -060027 sys.path.insert(2, os.path.join(our_path, dirname))
Simon Glass2574ef62016-11-25 20:15:51 -070028
Simon Glass55901ff2017-05-27 07:38:22 -060029# Bring in the libfdt module
Simon Glassf46732a2019-07-08 14:25:29 -060030sys.path.insert(2, 'scripts/dtc/pylibfdt')
31sys.path.insert(2, os.path.join(our_path,
Simon Glass5656ca22018-10-01 21:12:40 -060032 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060033
Simon Glass40778d72019-07-08 13:18:36 -060034# When running under python-coverage on Ubuntu 16.04, the dist-packages
35# directories are dropped from the python path. Add them in so that we can find
36# the elffile module. We could use site.getsitepackages() here but unfortunately
37# that is not available in a virtualenv.
38sys.path.append(get_python_lib())
39
Simon Glass2574ef62016-11-25 20:15:51 -070040import cmdline
41import command
Simon Glass7057d022018-10-01 21:12:47 -060042use_concurrent = True
43try:
44 from concurrencytest import ConcurrentTestSuite, fork_for_tests
45except:
46 use_concurrent = False
Simon Glass2574ef62016-11-25 20:15:51 -070047import control
Simon Glass132be852018-07-06 10:27:23 -060048import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070049
Simon Glasscebfab22019-07-08 13:18:50 -060050def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass5666f9a2018-06-01 09:38:18 -060051 """Run the functional tests and any embedded doctests
52
53 Args:
54 debug: True to enable debugging, which shows a full stack trace on error
Simon Glass8a50b4a2019-07-08 13:18:48 -060055 verbosity: Verbosity level to use
Simon Glass1c420c92019-07-08 13:18:49 -060056 test_preserve_dirs: True to preserve the input directory used by tests
57 so that it can be examined afterwards (only useful for debugging
58 tests). If a single test is selected (in args[0]) it also preserves
59 the output directory for this test. Both directories are displayed
60 on the command line.
61 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass5666f9a2018-06-01 09:38:18 -060062 args: List of positional args provided to binman. This can hold a test
Simon Glassf46732a2019-07-08 14:25:29 -060063 name to execute (as in 'binman test testSections', for example)
Simon Glasscebfab22019-07-08 13:18:50 -060064 toolpath: List of paths to use for tools
Simon Glass5666f9a2018-06-01 09:38:18 -060065 """
Simon Glass96a62962019-07-08 13:18:52 -060066 import cbfs_util_test
Simon Glass24ad3652017-11-13 18:54:54 -070067 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070068 import entry_test
69 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070070 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070071 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070072 import test
73 import doctest
74
75 result = unittest.TestResult()
76 for module in []:
77 suite = doctest.DocTestSuite(module)
78 suite.run(result)
79
80 sys.argv = [sys.argv[0]]
Simon Glass075a45c2017-11-13 18:55:00 -070081 if debug:
82 sys.argv.append('-D')
Simon Glass8a50b4a2019-07-08 13:18:48 -060083 if verbosity:
84 sys.argv.append('-v%d' % verbosity)
Simon Glasscebfab22019-07-08 13:18:50 -060085 if toolpath:
86 for path in toolpath:
87 sys.argv += ['--toolpath', path]
Simon Glass8f521362017-11-12 21:52:21 -070088
89 # Run the entry tests first ,since these need to be the first to import the
90 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060091 test_name = args and args[0] or None
Simon Glass7057d022018-10-01 21:12:47 -060092 suite = unittest.TestSuite()
93 loader = unittest.TestLoader()
Simon Glass02e0fc62018-07-06 10:27:18 -060094 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
Simon Glass96a62962019-07-08 13:18:52 -060095 elf_test.TestElf, image_test.TestImage,
96 cbfs_util_test.TestCbfs):
Simon Glass1c420c92019-07-08 13:18:49 -060097 # Test the test module about our arguments, if it is interested
98 if hasattr(module, 'setup_test_args'):
99 setup_test_args = getattr(module, 'setup_test_args')
100 setup_test_args(preserve_indir=test_preserve_dirs,
Simon Glasscebfab22019-07-08 13:18:50 -0600101 preserve_outdirs=test_preserve_dirs and test_name is not None,
Simon Glassf46732a2019-07-08 14:25:29 -0600102 toolpath=toolpath, verbosity=verbosity)
Simon Glass5666f9a2018-06-01 09:38:18 -0600103 if test_name:
104 try:
Simon Glass7057d022018-10-01 21:12:47 -0600105 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass5666f9a2018-06-01 09:38:18 -0600106 except AttributeError:
107 continue
108 else:
Simon Glass7057d022018-10-01 21:12:47 -0600109 suite.addTests(loader.loadTestsFromTestCase(module))
110 if use_concurrent and processes != 1:
111 concurrent_suite = ConcurrentTestSuite(suite,
112 fork_for_tests(processes or multiprocessing.cpu_count()))
113 concurrent_suite.run(result)
114 else:
Simon Glass2574ef62016-11-25 20:15:51 -0700115 suite.run(result)
116
Simon Glassd1ba61c2019-05-14 15:53:38 -0600117 # Remove errors which just indicate a missing test. Since Python v3.5 If an
118 # ImportError or AttributeError occurs while traversing name then a
119 # synthetic test that raises that error when run will be returned. These
120 # errors are included in the errors accumulated by result.errors.
121 if test_name:
122 errors = []
123 for test, err in result.errors:
124 if ("has no attribute '%s'" % test_name) not in err:
125 errors.append((test, err))
126 result.testsRun -= 1
127 result.errors = errors
128
Simon Glass7cca27d2019-05-14 15:53:37 -0600129 print(result)
Simon Glass2574ef62016-11-25 20:15:51 -0700130 for test, err in result.errors:
Simon Glass7cca27d2019-05-14 15:53:37 -0600131 print(test.id(), err)
Simon Glass2574ef62016-11-25 20:15:51 -0700132 for test, err in result.failures:
Simon Glass7cca27d2019-05-14 15:53:37 -0600133 print(err, result.failures)
Simon Glass231976a2019-07-08 13:18:33 -0600134 if result.skipped:
135 print('%d binman test%s SKIPPED:' %
136 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
137 for skip_info in result.skipped:
138 print('%s: %s' % (skip_info[0], skip_info[1]))
Simon Glass9ba021c2017-11-12 21:52:29 -0700139 if result.errors or result.failures:
Simon Glass231976a2019-07-08 13:18:33 -0600140 print('binman tests FAILED')
141 return 1
Simon Glass9ba021c2017-11-12 21:52:29 -0700142 return 0
Simon Glass2574ef62016-11-25 20:15:51 -0700143
Simon Glass969616c2018-07-17 13:25:36 -0600144def GetEntryModules(include_testing=True):
145 """Get a set of entry class implementations
146
147 Returns:
148 Set of paths to entry class filenames
149 """
150 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
151 return set([os.path.splitext(os.path.basename(item))[0]
152 for item in glob_list
153 if include_testing or '_testing' not in item])
154
Simon Glass2574ef62016-11-25 20:15:51 -0700155def RunTestCoverage():
156 """Run the tests and check that we get 100% coverage"""
Simon Glass969616c2018-07-17 13:25:36 -0600157 glob_list = GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -0600158 all_set = set([os.path.splitext(os.path.basename(item))[0]
159 for item in glob_list if '_testing' not in item])
Simon Glass132be852018-07-06 10:27:23 -0600160 test_util.RunTestCoverage('tools/binman/binman.py', None,
161 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glassf46732a2019-07-08 14:25:29 -0600162 args.build_dir, all_set)
Simon Glass2574ef62016-11-25 20:15:51 -0700163
Simon Glassf46732a2019-07-08 14:25:29 -0600164def RunBinman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700165 """Main entry point to binman once arguments are parsed
166
167 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600168 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700169 """
170 ret_code = 0
171
Simon Glassf46732a2019-07-08 14:25:29 -0600172 if not args.debug:
Simon Glass2574ef62016-11-25 20:15:51 -0700173 sys.tracebacklimit = 0
174
Simon Glassf46732a2019-07-08 14:25:29 -0600175 if args.cmd == 'test':
176 if args.test_coverage:
177 RunTestCoverage()
178 else:
179 ret_code = RunTests(args.debug, args.verbosity, args.processes,
180 args.test_preserve_dirs, args.tests,
181 args.toolpath)
Simon Glass2574ef62016-11-25 20:15:51 -0700182
Simon Glassf46732a2019-07-08 14:25:29 -0600183 elif args.cmd == 'entry-docs':
Simon Glass969616c2018-07-17 13:25:36 -0600184 control.WriteEntryDocs(GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700185
186 else:
187 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600188 ret_code = control.Binman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700189 except Exception as e:
Simon Glass7cca27d2019-05-14 15:53:37 -0600190 print('binman: %s' % e)
Simon Glassf46732a2019-07-08 14:25:29 -0600191 if args.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600192 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700193 traceback.print_exc()
194 ret_code = 1
195 return ret_code
196
197
198if __name__ == "__main__":
Simon Glassf46732a2019-07-08 14:25:29 -0600199 args = cmdline.ParseArgs(sys.argv[1:])
200
201 ret_code = RunBinman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700202 sys.exit(ret_code)