blob: 05aeaecd8f36b8da84e35d783af07c5b0449696b [file] [log] [blame]
Jörg Krausecd6d46e2017-03-06 21:07:11 +01001#!/usr/bin/env python2
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
23# Bring in the patman and dtoc libraries
24our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass7057d022018-10-01 21:12:47 -060025for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glassdde3e712017-06-20 21:28:49 -060026 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glass2574ef62016-11-25 20:15:51 -070027
Simon Glass55901ff2017-05-27 07:38:22 -060028# Bring in the libfdt module
Masahiro Yamada47ae5392017-10-17 13:42:43 +090029sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glass5656ca22018-10-01 21:12:40 -060030sys.path.insert(0, os.path.join(our_path,
31 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060032
Simon Glass40778d72019-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 Glass2574ef62016-11-25 20:15:51 -070039import cmdline
40import command
Simon Glass7057d022018-10-01 21:12:47 -060041use_concurrent = True
42try:
43 from concurrencytest import ConcurrentTestSuite, fork_for_tests
44except:
45 use_concurrent = False
Simon Glass2574ef62016-11-25 20:15:51 -070046import control
Simon Glass132be852018-07-06 10:27:23 -060047import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070048
Simon Glass7057d022018-10-01 21:12:47 -060049def RunTests(debug, processes, args):
Simon Glass5666f9a2018-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
54 args: List of positional args provided to binman. This can hold a test
55 name to execute (as in 'binman -t testSections', for example)
Simon Glass7057d022018-10-01 21:12:47 -060056 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass5666f9a2018-06-01 09:38:18 -060057 """
Simon Glass24ad3652017-11-13 18:54:54 -070058 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070059 import entry_test
60 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070061 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070062 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070063 import test
64 import doctest
65
66 result = unittest.TestResult()
67 for module in []:
68 suite = doctest.DocTestSuite(module)
69 suite.run(result)
70
71 sys.argv = [sys.argv[0]]
Simon Glass075a45c2017-11-13 18:55:00 -070072 if debug:
73 sys.argv.append('-D')
Simon Glass7057d022018-10-01 21:12:47 -060074 if debug:
75 sys.argv.append('-D')
Simon Glass8f521362017-11-12 21:52:21 -070076
77 # Run the entry tests first ,since these need to be the first to import the
78 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060079 test_name = args and args[0] or None
Simon Glass7057d022018-10-01 21:12:47 -060080 suite = unittest.TestSuite()
81 loader = unittest.TestLoader()
Simon Glass02e0fc62018-07-06 10:27:18 -060082 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
83 elf_test.TestElf, image_test.TestImage):
Simon Glass5666f9a2018-06-01 09:38:18 -060084 if test_name:
85 try:
Simon Glass7057d022018-10-01 21:12:47 -060086 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass5666f9a2018-06-01 09:38:18 -060087 except AttributeError:
88 continue
89 else:
Simon Glass7057d022018-10-01 21:12:47 -060090 suite.addTests(loader.loadTestsFromTestCase(module))
91 if use_concurrent and processes != 1:
92 concurrent_suite = ConcurrentTestSuite(suite,
93 fork_for_tests(processes or multiprocessing.cpu_count()))
94 concurrent_suite.run(result)
95 else:
Simon Glass2574ef62016-11-25 20:15:51 -070096 suite.run(result)
97
Simon Glassd1ba61c2019-05-14 15:53:38 -060098 # Remove errors which just indicate a missing test. Since Python v3.5 If an
99 # ImportError or AttributeError occurs while traversing name then a
100 # synthetic test that raises that error when run will be returned. These
101 # errors are included in the errors accumulated by result.errors.
102 if test_name:
103 errors = []
104 for test, err in result.errors:
105 if ("has no attribute '%s'" % test_name) not in err:
106 errors.append((test, err))
107 result.testsRun -= 1
108 result.errors = errors
109
Simon Glass7cca27d2019-05-14 15:53:37 -0600110 print(result)
Simon Glass2574ef62016-11-25 20:15:51 -0700111 for test, err in result.errors:
Simon Glass7cca27d2019-05-14 15:53:37 -0600112 print(test.id(), err)
Simon Glass2574ef62016-11-25 20:15:51 -0700113 for test, err in result.failures:
Simon Glass7cca27d2019-05-14 15:53:37 -0600114 print(err, result.failures)
Simon Glass231976a2019-07-08 13:18:33 -0600115 if result.skipped:
116 print('%d binman test%s SKIPPED:' %
117 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
118 for skip_info in result.skipped:
119 print('%s: %s' % (skip_info[0], skip_info[1]))
Simon Glass9ba021c2017-11-12 21:52:29 -0700120 if result.errors or result.failures:
Simon Glass231976a2019-07-08 13:18:33 -0600121 print('binman tests FAILED')
122 return 1
Simon Glass9ba021c2017-11-12 21:52:29 -0700123 return 0
Simon Glass2574ef62016-11-25 20:15:51 -0700124
Simon Glass969616c2018-07-17 13:25:36 -0600125def GetEntryModules(include_testing=True):
126 """Get a set of entry class implementations
127
128 Returns:
129 Set of paths to entry class filenames
130 """
131 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
132 return set([os.path.splitext(os.path.basename(item))[0]
133 for item in glob_list
134 if include_testing or '_testing' not in item])
135
Simon Glass2574ef62016-11-25 20:15:51 -0700136def RunTestCoverage():
137 """Run the tests and check that we get 100% coverage"""
Simon Glass969616c2018-07-17 13:25:36 -0600138 glob_list = GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -0600139 all_set = set([os.path.splitext(os.path.basename(item))[0]
140 for item in glob_list if '_testing' not in item])
Simon Glass132be852018-07-06 10:27:23 -0600141 test_util.RunTestCoverage('tools/binman/binman.py', None,
142 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
143 options.build_dir, all_set)
Simon Glass2574ef62016-11-25 20:15:51 -0700144
145def RunBinman(options, args):
146 """Main entry point to binman once arguments are parsed
147
148 Args:
149 options: Command-line options
150 args: Non-option arguments
151 """
152 ret_code = 0
153
154 # For testing: This enables full exception traces.
155 #options.debug = True
156
157 if not options.debug:
158 sys.tracebacklimit = 0
159
160 if options.test:
Simon Glass7057d022018-10-01 21:12:47 -0600161 ret_code = RunTests(options.debug, options.processes, args[1:])
Simon Glass2574ef62016-11-25 20:15:51 -0700162
163 elif options.test_coverage:
164 RunTestCoverage()
165
Simon Glass969616c2018-07-17 13:25:36 -0600166 elif options.entry_docs:
167 control.WriteEntryDocs(GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700168
169 else:
170 try:
171 ret_code = control.Binman(options, args)
172 except Exception as e:
Simon Glass7cca27d2019-05-14 15:53:37 -0600173 print('binman: %s' % e)
Simon Glass2574ef62016-11-25 20:15:51 -0700174 if options.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600175 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700176 traceback.print_exc()
177 ret_code = 1
178 return ret_code
179
180
181if __name__ == "__main__":
182 (options, args) = cmdline.ParseArgs(sys.argv)
183 ret_code = RunBinman(options, args)
184 sys.exit(ret_code)