binman.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. test_name = args and args[0] or None
  47. for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
  48. elf_test.TestElf, image_test.TestImage):
  49. if test_name:
  50. try:
  51. suite = unittest.TestLoader().loadTestsFromName(test_name, module)
  52. except AttributeError:
  53. continue
  54. else:
  55. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  56. suite.run(result)
  57. print result
  58. for test, err in result.errors:
  59. print test.id(), err
  60. for test, err in result.failures:
  61. print err, result.failures
  62. if result.errors or result.failures:
  63. print 'binman tests FAILED'
  64. return 1
  65. return 0
  66. def RunTestCoverage():
  67. """Run the tests and check that we get 100% coverage"""
  68. # This uses the build output from sandbox_spl to get _libfdt.so
  69. cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools python-coverage run '
  70. '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
  71. 'tools/binman/binman.py -t' % options.build_dir)
  72. os.system(cmd)
  73. stdout = command.Output('python-coverage', 'report')
  74. lines = stdout.splitlines()
  75. test_set= set([os.path.basename(line.split()[0])
  76. for line in lines if '/etype/' in line])
  77. glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
  78. all_set = set([os.path.splitext(os.path.basename(item))[0]
  79. for item in glob_list if '_testing' not in item])
  80. missing_list = all_set
  81. missing_list.difference_update(test_set)
  82. coverage = lines[-1].split(' ')[-1]
  83. ok = True
  84. if missing_list:
  85. print 'Missing tests for %s' % (', '.join(missing_list))
  86. print stdout
  87. ok = False
  88. if coverage != '100%':
  89. print stdout
  90. print "Type 'coverage html' to get a report in htmlcov/index.html"
  91. print 'Coverage error: %s, but should be 100%%' % coverage
  92. ok = False
  93. if not ok:
  94. raise ValueError('Test coverage failure')
  95. def RunBinman(options, args):
  96. """Main entry point to binman once arguments are parsed
  97. Args:
  98. options: Command-line options
  99. args: Non-option arguments
  100. """
  101. ret_code = 0
  102. # For testing: This enables full exception traces.
  103. #options.debug = True
  104. if not options.debug:
  105. sys.tracebacklimit = 0
  106. if options.test:
  107. ret_code = RunTests(options.debug, args[1:])
  108. elif options.test_coverage:
  109. RunTestCoverage()
  110. elif options.full_help:
  111. pager = os.getenv('PAGER')
  112. if not pager:
  113. pager = 'more'
  114. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  115. 'README')
  116. command.Run(pager, fname)
  117. else:
  118. try:
  119. ret_code = control.Binman(options, args)
  120. except Exception as e:
  121. print 'binman: %s' % e
  122. if options.debug:
  123. print
  124. traceback.print_exc()
  125. ret_code = 1
  126. return ret_code
  127. if __name__ == "__main__":
  128. (options, args) = cmdline.ParseArgs(sys.argv)
  129. ret_code = RunBinman(options, args)
  130. sys.exit(ret_code)