blob: 439908e66508045e78563617304e3d36dffe9417 [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 Glassa25ebed2017-11-12 21:52:24 -070012import glob
Simon Glass11ae93e2018-10-01 21:12:47 -060013import multiprocessing
Simon Glassbf7fd502016-11-25 20:15:51 -070014import os
15import sys
16import traceback
17import unittest
18
19# Bring in the patman and dtoc libraries
20our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass11ae93e2018-10-01 21:12:47 -060021for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
Simon Glass7feccfd2017-06-20 21:28:49 -060022 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glassbf7fd502016-11-25 20:15:51 -070023
Simon Glassb4360202017-05-27 07:38:22 -060024# Bring in the libfdt module
Masahiro Yamada15b97f52017-10-17 13:42:43 +090025sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glassed59e002018-10-01 21:12:40 -060026sys.path.insert(0, os.path.join(our_path,
27 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060028
Simon Glassbf7fd502016-11-25 20:15:51 -070029import cmdline
30import command
Simon Glass11ae93e2018-10-01 21:12:47 -060031use_concurrent = True
32try:
33 from concurrencytest import ConcurrentTestSuite, fork_for_tests
34except:
35 use_concurrent = False
Simon Glassbf7fd502016-11-25 20:15:51 -070036import control
Simon Glassff1fd6c2018-07-06 10:27:23 -060037import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070038
Simon Glass11ae93e2018-10-01 21:12:47 -060039def RunTests(debug, processes, args):
Simon Glass084059a2018-06-01 09:38:18 -060040 """Run the functional tests and any embedded doctests
41
42 Args:
43 debug: True to enable debugging, which shows a full stack trace on error
44 args: List of positional args provided to binman. This can hold a test
45 name to execute (as in 'binman -t testSections', for example)
Simon Glass11ae93e2018-10-01 21:12:47 -060046 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass084059a2018-06-01 09:38:18 -060047 """
Simon Glassb50e5612017-11-13 18:54:54 -070048 import elf_test
Simon Glassbf7fd502016-11-25 20:15:51 -070049 import entry_test
50 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070051 import ftest
Simon Glass19790632017-11-13 18:55:01 -070052 import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070053 import test
54 import doctest
55
56 result = unittest.TestResult()
57 for module in []:
58 suite = doctest.DocTestSuite(module)
59 suite.run(result)
60
61 sys.argv = [sys.argv[0]]
Simon Glass7fe91732017-11-13 18:55:00 -070062 if debug:
63 sys.argv.append('-D')
Simon Glass11ae93e2018-10-01 21:12:47 -060064 if debug:
65 sys.argv.append('-D')
Simon Glass934cdcf2017-11-12 21:52:21 -070066
67 # Run the entry tests first ,since these need to be the first to import the
68 # 'entry' module.
Simon Glass084059a2018-06-01 09:38:18 -060069 test_name = args and args[0] or None
Simon Glass11ae93e2018-10-01 21:12:47 -060070 suite = unittest.TestSuite()
71 loader = unittest.TestLoader()
Simon Glass2cd01282018-07-06 10:27:18 -060072 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
73 elf_test.TestElf, image_test.TestImage):
Simon Glass084059a2018-06-01 09:38:18 -060074 if test_name:
75 try:
Simon Glass11ae93e2018-10-01 21:12:47 -060076 suite.addTests(loader.loadTestsFromName(test_name, module))
Simon Glass084059a2018-06-01 09:38:18 -060077 except AttributeError:
78 continue
79 else:
Simon Glass11ae93e2018-10-01 21:12:47 -060080 suite.addTests(loader.loadTestsFromTestCase(module))
81 if use_concurrent and processes != 1:
82 concurrent_suite = ConcurrentTestSuite(suite,
83 fork_for_tests(processes or multiprocessing.cpu_count()))
84 concurrent_suite.run(result)
85 else:
Simon Glassbf7fd502016-11-25 20:15:51 -070086 suite.run(result)
87
88 print result
89 for test, err in result.errors:
90 print test.id(), err
91 for test, err in result.failures:
Simon Glass9677faa2017-11-12 21:52:29 -070092 print err, result.failures
93 if result.errors or result.failures:
94 print 'binman tests FAILED'
95 return 1
96 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -070097
Simon Glassfd8d1f72018-07-17 13:25:36 -060098def GetEntryModules(include_testing=True):
99 """Get a set of entry class implementations
100
101 Returns:
102 Set of paths to entry class filenames
103 """
104 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
105 return set([os.path.splitext(os.path.basename(item))[0]
106 for item in glob_list
107 if include_testing or '_testing' not in item])
108
Simon Glassbf7fd502016-11-25 20:15:51 -0700109def RunTestCoverage():
110 """Run the tests and check that we get 100% coverage"""
Simon Glassfd8d1f72018-07-17 13:25:36 -0600111 glob_list = GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -0600112 all_set = set([os.path.splitext(os.path.basename(item))[0]
113 for item in glob_list if '_testing' not in item])
Simon Glassff1fd6c2018-07-06 10:27:23 -0600114 test_util.RunTestCoverage('tools/binman/binman.py', None,
115 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
116 options.build_dir, all_set)
Simon Glassbf7fd502016-11-25 20:15:51 -0700117
118def RunBinman(options, args):
119 """Main entry point to binman once arguments are parsed
120
121 Args:
122 options: Command-line options
123 args: Non-option arguments
124 """
125 ret_code = 0
126
127 # For testing: This enables full exception traces.
128 #options.debug = True
129
130 if not options.debug:
131 sys.tracebacklimit = 0
132
133 if options.test:
Simon Glass11ae93e2018-10-01 21:12:47 -0600134 ret_code = RunTests(options.debug, options.processes, args[1:])
Simon Glassbf7fd502016-11-25 20:15:51 -0700135
136 elif options.test_coverage:
137 RunTestCoverage()
138
Simon Glassfd8d1f72018-07-17 13:25:36 -0600139 elif options.entry_docs:
140 control.WriteEntryDocs(GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700141
142 else:
143 try:
144 ret_code = control.Binman(options, args)
145 except Exception as e:
146 print 'binman: %s' % e
147 if options.debug:
148 print
149 traceback.print_exc()
150 ret_code = 1
151 return ret_code
152
153
154if __name__ == "__main__":
155 (options, args) = cmdline.ParseArgs(sys.argv)
156 ret_code = RunBinman(options, args)
157 sys.exit(ret_code)