blob: bab98826dc60495e701e331a5f7c1155412a1f2b [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 Glass8f521362017-11-12 21:52:21 -070074
75 # Run the entry tests first ,since these need to be the first to import the
76 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060077 test_name = args and args[0] or None
Simon Glass7057d022018-10-01 21:12:47 -060078 suite = unittest.TestSuite()
79 loader = unittest.TestLoader()
Simon Glass02e0fc62018-07-06 10:27:18 -060080 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
81 elf_test.TestElf, image_test.TestImage):
Simon Glass5666f9a2018-06-01 09:38:18 -060082 if test_name:
83 try:
Simon Glass7057d022018-10-01 21:12:47 -060084 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass5666f9a2018-06-01 09:38:18 -060085 except AttributeError:
86 continue
87 else:
Simon Glass7057d022018-10-01 21:12:47 -060088 suite.addTests(loader.loadTestsFromTestCase(module))
89 if use_concurrent and processes != 1:
90 concurrent_suite = ConcurrentTestSuite(suite,
91 fork_for_tests(processes or multiprocessing.cpu_count()))
92 concurrent_suite.run(result)
93 else:
Simon Glass2574ef62016-11-25 20:15:51 -070094 suite.run(result)
95
Simon Glassd1ba61c2019-05-14 15:53:38 -060096 # Remove errors which just indicate a missing test. Since Python v3.5 If an
97 # ImportError or AttributeError occurs while traversing name then a
98 # synthetic test that raises that error when run will be returned. These
99 # errors are included in the errors accumulated by result.errors.
100 if test_name:
101 errors = []
102 for test, err in result.errors:
103 if ("has no attribute '%s'" % test_name) not in err:
104 errors.append((test, err))
105 result.testsRun -= 1
106 result.errors = errors
107
Simon Glass7cca27d2019-05-14 15:53:37 -0600108 print(result)
Simon Glass2574ef62016-11-25 20:15:51 -0700109 for test, err in result.errors:
Simon Glass7cca27d2019-05-14 15:53:37 -0600110 print(test.id(), err)
Simon Glass2574ef62016-11-25 20:15:51 -0700111 for test, err in result.failures:
Simon Glass7cca27d2019-05-14 15:53:37 -0600112 print(err, result.failures)
Simon Glass231976a2019-07-08 13:18:33 -0600113 if result.skipped:
114 print('%d binman test%s SKIPPED:' %
115 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
116 for skip_info in result.skipped:
117 print('%s: %s' % (skip_info[0], skip_info[1]))
Simon Glass9ba021c2017-11-12 21:52:29 -0700118 if result.errors or result.failures:
Simon Glass231976a2019-07-08 13:18:33 -0600119 print('binman tests FAILED')
120 return 1
Simon Glass9ba021c2017-11-12 21:52:29 -0700121 return 0
Simon Glass2574ef62016-11-25 20:15:51 -0700122
Simon Glass969616c2018-07-17 13:25:36 -0600123def GetEntryModules(include_testing=True):
124 """Get a set of entry class implementations
125
126 Returns:
127 Set of paths to entry class filenames
128 """
129 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
130 return set([os.path.splitext(os.path.basename(item))[0]
131 for item in glob_list
132 if include_testing or '_testing' not in item])
133
Simon Glass2574ef62016-11-25 20:15:51 -0700134def RunTestCoverage():
135 """Run the tests and check that we get 100% coverage"""
Simon Glass969616c2018-07-17 13:25:36 -0600136 glob_list = GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -0600137 all_set = set([os.path.splitext(os.path.basename(item))[0]
138 for item in glob_list if '_testing' not in item])
Simon Glass132be852018-07-06 10:27:23 -0600139 test_util.RunTestCoverage('tools/binman/binman.py', None,
140 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
141 options.build_dir, all_set)
Simon Glass2574ef62016-11-25 20:15:51 -0700142
143def RunBinman(options, args):
144 """Main entry point to binman once arguments are parsed
145
146 Args:
147 options: Command-line options
148 args: Non-option arguments
149 """
150 ret_code = 0
151
Simon Glass2574ef62016-11-25 20:15:51 -0700152 if not options.debug:
153 sys.tracebacklimit = 0
154
155 if options.test:
Simon Glass7057d022018-10-01 21:12:47 -0600156 ret_code = RunTests(options.debug, options.processes, args[1:])
Simon Glass2574ef62016-11-25 20:15:51 -0700157
158 elif options.test_coverage:
159 RunTestCoverage()
160
Simon Glass969616c2018-07-17 13:25:36 -0600161 elif options.entry_docs:
162 control.WriteEntryDocs(GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700163
164 else:
165 try:
166 ret_code = control.Binman(options, args)
167 except Exception as e:
Simon Glass7cca27d2019-05-14 15:53:37 -0600168 print('binman: %s' % e)
Simon Glass2574ef62016-11-25 20:15:51 -0700169 if options.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600170 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700171 traceback.print_exc()
172 ret_code = 1
173 return ret_code
174
175
176if __name__ == "__main__":
177 (options, args) = cmdline.ParseArgs(sys.argv)
178 ret_code = RunBinman(options, args)
179 sys.exit(ret_code)