blob: aad2e9c8bc4224ab303e49e674697689105faf34 [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 Glassa25ebed2017-11-12 21:52:24 -070014import glob
Simon Glass11ae93e2018-10-01 21:12:47 -060015import multiprocessing
Simon Glassbf7fd502016-11-25 20:15:51 -070016import os
17import sys
18import traceback
19import unittest
20
21# Bring in the patman and dtoc libraries
22our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass11ae93e2018-10-01 21:12:47 -060023for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glass7feccfd2017-06-20 21:28:49 -060024 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glassbf7fd502016-11-25 20:15:51 -070025
Simon Glassb4360202017-05-27 07:38:22 -060026# Bring in the libfdt module
Masahiro Yamada15b97f52017-10-17 13:42:43 +090027sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glassed59e002018-10-01 21:12:40 -060028sys.path.insert(0, os.path.join(our_path,
29 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060030
Simon Glassbf7fd502016-11-25 20:15:51 -070031import cmdline
32import command
Simon Glass11ae93e2018-10-01 21:12:47 -060033use_concurrent = True
34try:
35 from concurrencytest import ConcurrentTestSuite, fork_for_tests
36except:
37 use_concurrent = False
Simon Glassbf7fd502016-11-25 20:15:51 -070038import control
Simon Glassff1fd6c2018-07-06 10:27:23 -060039import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070040
Simon Glass11ae93e2018-10-01 21:12:47 -060041def RunTests(debug, processes, args):
Simon Glass084059a2018-06-01 09:38:18 -060042 """Run the functional tests and any embedded doctests
43
44 Args:
45 debug: True to enable debugging, which shows a full stack trace on error
46 args: List of positional args provided to binman. This can hold a test
47 name to execute (as in 'binman -t testSections', for example)
Simon Glass11ae93e2018-10-01 21:12:47 -060048 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass084059a2018-06-01 09:38:18 -060049 """
Simon Glassb50e5612017-11-13 18:54:54 -070050 import elf_test
Simon Glassbf7fd502016-11-25 20:15:51 -070051 import entry_test
52 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070053 import ftest
Simon Glass19790632017-11-13 18:55:01 -070054 import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070055 import test
56 import doctest
57
58 result = unittest.TestResult()
59 for module in []:
60 suite = doctest.DocTestSuite(module)
61 suite.run(result)
62
63 sys.argv = [sys.argv[0]]
Simon Glass7fe91732017-11-13 18:55:00 -070064 if debug:
65 sys.argv.append('-D')
Simon Glass11ae93e2018-10-01 21:12:47 -060066 if debug:
67 sys.argv.append('-D')
Simon Glass934cdcf2017-11-12 21:52:21 -070068
69 # Run the entry tests first ,since these need to be the first to import the
70 # 'entry' module.
Simon Glass084059a2018-06-01 09:38:18 -060071 test_name = args and args[0] or None
Simon Glass11ae93e2018-10-01 21:12:47 -060072 suite = unittest.TestSuite()
73 loader = unittest.TestLoader()
Simon Glass2cd01282018-07-06 10:27:18 -060074 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
75 elf_test.TestElf, image_test.TestImage):
Simon Glass084059a2018-06-01 09:38:18 -060076 if test_name:
77 try:
Simon Glass11ae93e2018-10-01 21:12:47 -060078 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass084059a2018-06-01 09:38:18 -060079 except AttributeError:
80 continue
81 else:
Simon Glass11ae93e2018-10-01 21:12:47 -060082 suite.addTests(loader.loadTestsFromTestCase(module))
83 if use_concurrent and processes != 1:
84 concurrent_suite = ConcurrentTestSuite(suite,
85 fork_for_tests(processes or multiprocessing.cpu_count()))
86 concurrent_suite.run(result)
87 else:
Simon Glassbf7fd502016-11-25 20:15:51 -070088 suite.run(result)
89
Simon Glass35343dc2019-05-14 15:53:38 -060090 # Remove errors which just indicate a missing test. Since Python v3.5 If an
91 # ImportError or AttributeError occurs while traversing name then a
92 # synthetic test that raises that error when run will be returned. These
93 # errors are included in the errors accumulated by result.errors.
94 if test_name:
95 errors = []
96 for test, err in result.errors:
97 if ("has no attribute '%s'" % test_name) not in err:
98 errors.append((test, err))
99 result.testsRun -= 1
100 result.errors = errors
101
Simon Glass2ca84682019-05-14 15:53:37 -0600102 print(result)
Simon Glassbf7fd502016-11-25 20:15:51 -0700103 for test, err in result.errors:
Simon Glass2ca84682019-05-14 15:53:37 -0600104 print(test.id(), err)
Simon Glassbf7fd502016-11-25 20:15:51 -0700105 for test, err in result.failures:
Simon Glass2ca84682019-05-14 15:53:37 -0600106 print(err, result.failures)
Simon Glass9677faa2017-11-12 21:52:29 -0700107 if result.errors or result.failures:
Simon Glass2ca84682019-05-14 15:53:37 -0600108 print('binman tests FAILED')
Simon Glass9677faa2017-11-12 21:52:29 -0700109 return 1
110 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -0700111
Simon Glassfd8d1f72018-07-17 13:25:36 -0600112def GetEntryModules(include_testing=True):
113 """Get a set of entry class implementations
114
115 Returns:
116 Set of paths to entry class filenames
117 """
118 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
119 return set([os.path.splitext(os.path.basename(item))[0]
120 for item in glob_list
121 if include_testing or '_testing' not in item])
122
Simon Glassbf7fd502016-11-25 20:15:51 -0700123def RunTestCoverage():
124 """Run the tests and check that we get 100% coverage"""
Simon Glassfd8d1f72018-07-17 13:25:36 -0600125 glob_list = GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -0600126 all_set = set([os.path.splitext(os.path.basename(item))[0]
127 for item in glob_list if '_testing' not in item])
Simon Glassff1fd6c2018-07-06 10:27:23 -0600128 test_util.RunTestCoverage('tools/binman/binman.py', None,
129 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
130 options.build_dir, all_set)
Simon Glassbf7fd502016-11-25 20:15:51 -0700131
132def RunBinman(options, args):
133 """Main entry point to binman once arguments are parsed
134
135 Args:
136 options: Command-line options
137 args: Non-option arguments
138 """
139 ret_code = 0
140
141 # For testing: This enables full exception traces.
142 #options.debug = True
143
144 if not options.debug:
145 sys.tracebacklimit = 0
146
147 if options.test:
Simon Glass11ae93e2018-10-01 21:12:47 -0600148 ret_code = RunTests(options.debug, options.processes, args[1:])
Simon Glassbf7fd502016-11-25 20:15:51 -0700149
150 elif options.test_coverage:
151 RunTestCoverage()
152
Simon Glassfd8d1f72018-07-17 13:25:36 -0600153 elif options.entry_docs:
154 control.WriteEntryDocs(GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700155
156 else:
157 try:
158 ret_code = control.Binman(options, args)
159 except Exception as e:
Simon Glass2ca84682019-05-14 15:53:37 -0600160 print('binman: %s' % e)
Simon Glassbf7fd502016-11-25 20:15:51 -0700161 if options.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600162 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700163 traceback.print_exc()
164 ret_code = 1
165 return ret_code
166
167
168if __name__ == "__main__":
169 (options, args) = cmdline.ParseArgs(sys.argv)
170 ret_code = RunBinman(options, args)
171 sys.exit(ret_code)