binman.py 4.6 KB

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