1#!/usr/bin/env python2 2 3# Copyright (c) 2016 Google, Inc 4# Written by Simon Glass <sjg@chromium.org> 5# 6# SPDX-License-Identifier: GPL-2.0+ 7# 8# Creates binary images from input files controlled by a description 9# 10 11"""See README for more information""" 12 13import glob 14import 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__)) 21for dirname in ['../patman', '../dtoc', '..']: 22 sys.path.insert(0, os.path.join(our_path, dirname)) 23 24# Bring in the libfdt module 25sys.path.insert(0, 'scripts/dtc/pylibfdt') 26 27# Also allow entry-type modules to be brought in from the etype directory. 28sys.path.insert(0, os.path.join(our_path, 'etype')) 29 30import cmdline 31import command 32import control 33 34def RunTests(debug): 35 """Run the functional tests and any embedded doctests""" 36 import elf_test 37 import entry_test 38 import fdt_test 39 import ftest 40 import image_test 41 import test 42 import doctest 43 44 result = unittest.TestResult() 45 for module in []: 46 suite = doctest.DocTestSuite(module) 47 suite.run(result) 48 49 sys.argv = [sys.argv[0]] 50 if debug: 51 sys.argv.append('-D') 52 53 # Run the entry tests first ,since these need to be the first to import the 54 # 'entry' module. 55 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry) 56 suite.run(result) 57 for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf, 58 image_test.TestImage): 59 suite = unittest.TestLoader().loadTestsFromTestCase(module) 60 suite.run(result) 61 62 print result 63 for test, err in result.errors: 64 print test.id(), err 65 for test, err in result.failures: 66 print err, result.failures 67 if result.errors or result.failures: 68 print 'binman tests FAILED' 69 return 1 70 return 0 71 72def RunTestCoverage(): 73 """Run the tests and check that we get 100% coverage""" 74 # This uses the build output from sandbox_spl to get _libfdt.so 75 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run ' 76 '--include "tools/binman/*.py" --omit "*test*,*binman.py" ' 77 'tools/binman/binman.py -t' % options.build_dir) 78 os.system(cmd) 79 stdout = command.Output('coverage', 'report') 80 lines = stdout.splitlines() 81 82 test_set= set([os.path.basename(line.split()[0]) 83 for line in lines if '/etype/' in line]) 84 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py')) 85 all_set = set([os.path.basename(item) for item in glob_list]) 86 missing_list = all_set 87 missing_list.difference_update(test_set) 88 missing_list.remove('_testing.py') 89 coverage = lines[-1].split(' ')[-1] 90 ok = True 91 if missing_list: 92 print 'Missing tests for %s' % (', '.join(missing_list)) 93 ok = False 94 if coverage != '100%': 95 print stdout 96 print "Type 'coverage html' to get a report in htmlcov/index.html" 97 print 'Coverage error: %s, but should be 100%%' % coverage 98 ok = False 99 if not ok: 100 raise ValueError('Test coverage failure') 101 102def RunBinman(options, args): 103 """Main entry point to binman once arguments are parsed 104 105 Args: 106 options: Command-line options 107 args: Non-option arguments 108 """ 109 ret_code = 0 110 111 # For testing: This enables full exception traces. 112 #options.debug = True 113 114 if not options.debug: 115 sys.tracebacklimit = 0 116 117 if options.test: 118 ret_code = RunTests(options.debug) 119 120 elif options.test_coverage: 121 RunTestCoverage() 122 123 elif options.full_help: 124 pager = os.getenv('PAGER') 125 if not pager: 126 pager = 'more' 127 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 128 'README') 129 command.Run(pager, fname) 130 131 else: 132 try: 133 ret_code = control.Binman(options, args) 134 except Exception as e: 135 print 'binman: %s' % e 136 if options.debug: 137 print 138 traceback.print_exc() 139 ret_code = 1 140 return ret_code 141 142 143if __name__ == "__main__": 144 (options, args) = cmdline.ParseArgs(sys.argv) 145 ret_code = RunBinman(options, args) 146 sys.exit(ret_code) 147