blob: daff7ae4d3fe446ccd7833cfdbac15929c32168b [file] [log] [blame]
Simon Glass38856012019-10-31 07:43:05 -06001#!/usr/bin/env python3
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 Glass86679ce2019-07-08 13:18:36 -060012from distutils.sysconfig import get_python_lib
Simon Glassa25ebed2017-11-12 21:52:24 -070013import glob
Simon Glass11ae93e2018-10-01 21:12:47 -060014import multiprocessing
Simon Glassbf7fd502016-11-25 20:15:51 -070015import os
Simon Glass86679ce2019-07-08 13:18:36 -060016import site
Simon Glassbf7fd502016-11-25 20:15:51 -070017import sys
18import traceback
19import unittest
20
Simon Glass53cd5d92019-07-08 14:25:29 -060021# Bring in the patman and dtoc libraries (but don't override the first path
22# in PYTHONPATH)
Simon Glassbf7fd502016-11-25 20:15:51 -070023our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass11ae93e2018-10-01 21:12:47 -060024for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glass53cd5d92019-07-08 14:25:29 -060025 sys.path.insert(2, os.path.join(our_path, dirname))
Simon Glassbf7fd502016-11-25 20:15:51 -070026
Simon Glassb4360202017-05-27 07:38:22 -060027# Bring in the libfdt module
Simon Glass53cd5d92019-07-08 14:25:29 -060028sys.path.insert(2, 'scripts/dtc/pylibfdt')
29sys.path.insert(2, os.path.join(our_path,
Simon Glassed59e002018-10-01 21:12:40 -060030 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060031
Simon Glass86679ce2019-07-08 13:18:36 -060032# When running under python-coverage on Ubuntu 16.04, the dist-packages
33# directories are dropped from the python path. Add them in so that we can find
34# the elffile module. We could use site.getsitepackages() here but unfortunately
35# that is not available in a virtualenv.
36sys.path.append(get_python_lib())
37
Simon Glassbf7fd502016-11-25 20:15:51 -070038import cmdline
39import command
Simon Glass11ae93e2018-10-01 21:12:47 -060040use_concurrent = True
41try:
42 from concurrencytest import ConcurrentTestSuite, fork_for_tests
43except:
44 use_concurrent = False
Simon Glassbf7fd502016-11-25 20:15:51 -070045import control
Simon Glassff1fd6c2018-07-06 10:27:23 -060046import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070047
Simon Glass8acce602019-07-08 13:18:50 -060048def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass084059a2018-06-01 09:38:18 -060049 """Run the functional tests and any embedded doctests
50
51 Args:
52 debug: True to enable debugging, which shows a full stack trace on error
Simon Glassee0c9a72019-07-08 13:18:48 -060053 verbosity: Verbosity level to use
Simon Glassd5164a72019-07-08 13:18:49 -060054 test_preserve_dirs: True to preserve the input directory used by tests
55 so that it can be examined afterwards (only useful for debugging
56 tests). If a single test is selected (in args[0]) it also preserves
57 the output directory for this test. Both directories are displayed
58 on the command line.
59 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass084059a2018-06-01 09:38:18 -060060 args: List of positional args provided to binman. This can hold a test
Simon Glass53cd5d92019-07-08 14:25:29 -060061 name to execute (as in 'binman test testSections', for example)
Simon Glass8acce602019-07-08 13:18:50 -060062 toolpath: List of paths to use for tools
Simon Glass084059a2018-06-01 09:38:18 -060063 """
Simon Glass4997a7e2019-07-08 13:18:52 -060064 import cbfs_util_test
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,
Simon Glass4997a7e2019-07-08 13:18:52 -060093 elf_test.TestElf, image_test.TestImage,
94 cbfs_util_test.TestCbfs):
Simon Glassd5164a72019-07-08 13:18:49 -060095 # Test the test module about our arguments, if it is interested
96 if hasattr(module, 'setup_test_args'):
97 setup_test_args = getattr(module, 'setup_test_args')
98 setup_test_args(preserve_indir=test_preserve_dirs,
Simon Glass8acce602019-07-08 13:18:50 -060099 preserve_outdirs=test_preserve_dirs and test_name is not None,
Simon Glass53cd5d92019-07-08 14:25:29 -0600100 toolpath=toolpath, verbosity=verbosity)
Simon Glass084059a2018-06-01 09:38:18 -0600101 if test_name:
102 try:
Simon Glass11ae93e2018-10-01 21:12:47 -0600103 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass084059a2018-06-01 09:38:18 -0600104 except AttributeError:
105 continue
106 else:
Simon Glass11ae93e2018-10-01 21:12:47 -0600107 suite.addTests(loader.loadTestsFromTestCase(module))
108 if use_concurrent and processes != 1:
109 concurrent_suite = ConcurrentTestSuite(suite,
110 fork_for_tests(processes or multiprocessing.cpu_count()))
111 concurrent_suite.run(result)
112 else:
Simon Glassbf7fd502016-11-25 20:15:51 -0700113 suite.run(result)
114
Simon Glass35343dc2019-05-14 15:53:38 -0600115 # Remove errors which just indicate a missing test. Since Python v3.5 If an
116 # ImportError or AttributeError occurs while traversing name then a
117 # synthetic test that raises that error when run will be returned. These
118 # errors are included in the errors accumulated by result.errors.
119 if test_name:
120 errors = []
121 for test, err in result.errors:
122 if ("has no attribute '%s'" % test_name) not in err:
123 errors.append((test, err))
124 result.testsRun -= 1
125 result.errors = errors
126
Simon Glass2ca84682019-05-14 15:53:37 -0600127 print(result)
Simon Glassbf7fd502016-11-25 20:15:51 -0700128 for test, err in result.errors:
Simon Glass2ca84682019-05-14 15:53:37 -0600129 print(test.id(), err)
Simon Glassbf7fd502016-11-25 20:15:51 -0700130 for test, err in result.failures:
Simon Glass2ca84682019-05-14 15:53:37 -0600131 print(err, result.failures)
Simon Glass45cb9d82019-07-08 13:18:33 -0600132 if result.skipped:
133 print('%d binman test%s SKIPPED:' %
134 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
135 for skip_info in result.skipped:
136 print('%s: %s' % (skip_info[0], skip_info[1]))
Simon Glass9677faa2017-11-12 21:52:29 -0700137 if result.errors or result.failures:
Simon Glass45cb9d82019-07-08 13:18:33 -0600138 print('binman tests FAILED')
139 return 1
Simon Glass9677faa2017-11-12 21:52:29 -0700140 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -0700141
Simon Glassfd8d1f72018-07-17 13:25:36 -0600142def GetEntryModules(include_testing=True):
143 """Get a set of entry class implementations
144
145 Returns:
146 Set of paths to entry class filenames
147 """
148 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
149 return set([os.path.splitext(os.path.basename(item))[0]
150 for item in glob_list
151 if include_testing or '_testing' not in item])
152
Simon Glassbf7fd502016-11-25 20:15:51 -0700153def RunTestCoverage():
154 """Run the tests and check that we get 100% coverage"""
Simon Glassfd8d1f72018-07-17 13:25:36 -0600155 glob_list = GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -0600156 all_set = set([os.path.splitext(os.path.basename(item))[0]
157 for item in glob_list if '_testing' not in item])
Simon Glassc07ab6e2020-04-17 18:08:58 -0600158 test_util.RunTestCoverage('tools/binman/binman', None,
159 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass53cd5d92019-07-08 14:25:29 -0600160 args.build_dir, all_set)
Simon Glassbf7fd502016-11-25 20:15:51 -0700161
Simon Glass53cd5d92019-07-08 14:25:29 -0600162def RunBinman(args):
Simon Glassbf7fd502016-11-25 20:15:51 -0700163 """Main entry point to binman once arguments are parsed
164
165 Args:
Simon Glass53cd5d92019-07-08 14:25:29 -0600166 args: Command line arguments Namespace object
Simon Glassbf7fd502016-11-25 20:15:51 -0700167 """
168 ret_code = 0
169
Simon Glass53cd5d92019-07-08 14:25:29 -0600170 if not args.debug:
Simon Glassbf7fd502016-11-25 20:15:51 -0700171 sys.tracebacklimit = 0
172
Simon Glass53cd5d92019-07-08 14:25:29 -0600173 if args.cmd == 'test':
174 if args.test_coverage:
175 RunTestCoverage()
176 else:
177 ret_code = RunTests(args.debug, args.verbosity, args.processes,
178 args.test_preserve_dirs, args.tests,
179 args.toolpath)
Simon Glassbf7fd502016-11-25 20:15:51 -0700180
Simon Glass53cd5d92019-07-08 14:25:29 -0600181 elif args.cmd == 'entry-docs':
Simon Glassfd8d1f72018-07-17 13:25:36 -0600182 control.WriteEntryDocs(GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700183
184 else:
185 try:
Simon Glass53cd5d92019-07-08 14:25:29 -0600186 ret_code = control.Binman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700187 except Exception as e:
Simon Glass2ca84682019-05-14 15:53:37 -0600188 print('binman: %s' % e)
Simon Glass53cd5d92019-07-08 14:25:29 -0600189 if args.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__":
Simon Glass53cd5d92019-07-08 14:25:29 -0600197 args = cmdline.ParseArgs(sys.argv[1:])
198
199 ret_code = RunBinman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700200 sys.exit(ret_code)