analyze_outcomes.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python3
  2. """Analyze the test outcomes from a full CI run.
  3. This script can also run on outcomes from a partial run, but the results are
  4. less likely to be useful.
  5. """
  6. import argparse
  7. import sys
  8. import traceback
  9. import re
  10. import check_test_cases
  11. class Results:
  12. """Process analysis results."""
  13. def __init__(self):
  14. self.error_count = 0
  15. self.warning_count = 0
  16. @staticmethod
  17. def log(fmt, *args, **kwargs):
  18. sys.stderr.write((fmt + '\n').format(*args, **kwargs))
  19. def error(self, fmt, *args, **kwargs):
  20. self.log('Error: ' + fmt, *args, **kwargs)
  21. self.error_count += 1
  22. def warning(self, fmt, *args, **kwargs):
  23. self.log('Warning: ' + fmt, *args, **kwargs)
  24. self.warning_count += 1
  25. class TestCaseOutcomes:
  26. """The outcomes of one test case across many configurations."""
  27. # pylint: disable=too-few-public-methods
  28. def __init__(self):
  29. # Collect a list of witnesses of the test case succeeding or failing.
  30. # Currently we don't do anything with witnesses except count them.
  31. # The format of a witness is determined by the read_outcome_file
  32. # function; it's the platform and configuration joined by ';'.
  33. self.successes = []
  34. self.failures = []
  35. def hits(self):
  36. """Return the number of times a test case has been run.
  37. This includes passes and failures, but not skips.
  38. """
  39. return len(self.successes) + len(self.failures)
  40. def analyze_coverage(results, outcomes):
  41. """Check that all available test cases are executed at least once."""
  42. available = check_test_cases.collect_available_test_cases()
  43. for key in available:
  44. hits = outcomes[key].hits() if key in outcomes else 0
  45. if hits == 0:
  46. # Make this a warning, not an error, as long as we haven't
  47. # fixed this branch to have full coverage of test cases.
  48. results.warning('Test case not executed: {}', key)
  49. def analyze_driver_vs_reference(outcomes, component_ref, component_driver,
  50. ignored_suites, ignored_test=None):
  51. """Check that all tests executed in the reference component are also
  52. executed in the corresponding driver component.
  53. Skip:
  54. - full test suites provided in ignored_suites list
  55. - only some specific test inside a test suite, for which the corresponding
  56. output string is provided
  57. """
  58. available = check_test_cases.collect_available_test_cases()
  59. result = True
  60. for key in available:
  61. # Continue if test was not executed by any component
  62. hits = outcomes[key].hits() if key in outcomes else 0
  63. if hits == 0:
  64. continue
  65. # Skip ignored test suites
  66. full_test_suite = key.split(';')[0] # retrieve full test suite name
  67. test_string = key.split(';')[1] # retrieve the text string of this test
  68. test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
  69. if test_suite in ignored_suites:
  70. continue
  71. if ((full_test_suite in ignored_test) and
  72. (test_string in ignored_test[full_test_suite])):
  73. continue
  74. # Search for tests that run in reference component and not in driver component
  75. driver_test_passed = False
  76. reference_test_passed = False
  77. for entry in outcomes[key].successes:
  78. if component_driver in entry:
  79. driver_test_passed = True
  80. if component_ref in entry:
  81. reference_test_passed = True
  82. if(reference_test_passed and not driver_test_passed):
  83. Results.log(key)
  84. result = False
  85. return result
  86. def analyze_outcomes(outcomes):
  87. """Run all analyses on the given outcome collection."""
  88. results = Results()
  89. analyze_coverage(results, outcomes)
  90. return results
  91. def read_outcome_file(outcome_file):
  92. """Parse an outcome file and return an outcome collection.
  93. An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects.
  94. The keys are the test suite name and the test case description, separated
  95. by a semicolon.
  96. """
  97. outcomes = {}
  98. with open(outcome_file, 'r', encoding='utf-8') as input_file:
  99. for line in input_file:
  100. (platform, config, suite, case, result, _cause) = line.split(';')
  101. key = ';'.join([suite, case])
  102. setup = ';'.join([platform, config])
  103. if key not in outcomes:
  104. outcomes[key] = TestCaseOutcomes()
  105. if result == 'PASS':
  106. outcomes[key].successes.append(setup)
  107. elif result == 'FAIL':
  108. outcomes[key].failures.append(setup)
  109. return outcomes
  110. def do_analyze_coverage(outcome_file, args):
  111. """Perform coverage analysis."""
  112. del args # unused
  113. outcomes = read_outcome_file(outcome_file)
  114. Results.log("\n*** Analyze coverage ***\n")
  115. results = analyze_outcomes(outcomes)
  116. return results.error_count == 0
  117. def do_analyze_driver_vs_reference(outcome_file, args):
  118. """Perform driver vs reference analyze."""
  119. ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
  120. outcomes = read_outcome_file(outcome_file)
  121. Results.log("\n*** Analyze driver {} vs reference {} ***\n".format(
  122. args['component_driver'], args['component_ref']))
  123. return analyze_driver_vs_reference(outcomes, args['component_ref'],
  124. args['component_driver'], ignored_suites,
  125. args['ignored_tests'])
  126. # List of tasks with a function that can handle this task and additional arguments if required
  127. TASKS = {
  128. 'analyze_coverage': {
  129. 'test_function': do_analyze_coverage,
  130. 'args': {}
  131. },
  132. # How to use analyze_driver_vs_reference_xxx locally:
  133. # 1. tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
  134. # 2. tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
  135. 'analyze_driver_vs_reference_hash': {
  136. 'test_function': do_analyze_driver_vs_reference,
  137. 'args': {
  138. 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
  139. 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
  140. 'ignored_suites': [
  141. 'shax', 'mdx', # the software implementations that are being excluded
  142. 'md', # the legacy abstraction layer that's being excluded
  143. ],
  144. 'ignored_tests': {
  145. }
  146. }
  147. },
  148. 'analyze_driver_vs_reference_ecdsa': {
  149. 'test_function': do_analyze_driver_vs_reference,
  150. 'args': {
  151. 'component_ref': 'test_psa_crypto_config_reference_ecdsa_use_psa',
  152. 'component_driver': 'test_psa_crypto_config_accel_ecdsa_use_psa',
  153. 'ignored_suites': [
  154. 'ecdsa', # the software implementation that's excluded
  155. ],
  156. 'ignored_tests': {
  157. 'test_suite_random': [
  158. 'PSA classic wrapper: ECDSA signature (SECP256R1)',
  159. ],
  160. }
  161. }
  162. },
  163. 'analyze_driver_vs_reference_ecdh': {
  164. 'test_function': do_analyze_driver_vs_reference,
  165. 'args': {
  166. 'component_ref': 'test_psa_crypto_config_reference_ecdh_use_psa',
  167. 'component_driver': 'test_psa_crypto_config_accel_ecdh_use_psa',
  168. 'ignored_suites': [
  169. 'ecdh', # the software implementation that's excluded
  170. ],
  171. 'ignored_tests': {
  172. }
  173. }
  174. },
  175. 'analyze_driver_vs_reference_ecjpake': {
  176. 'test_function': do_analyze_driver_vs_reference,
  177. 'args': {
  178. 'component_ref': 'test_psa_crypto_config_reference_ecjpake_use_psa',
  179. 'component_driver': 'test_psa_crypto_config_accel_ecjpake_use_psa',
  180. 'ignored_suites': [
  181. 'ecjpake', # the software implementation that's excluded
  182. ],
  183. 'ignored_tests': {
  184. }
  185. }
  186. },
  187. }
  188. def main():
  189. try:
  190. parser = argparse.ArgumentParser(description=__doc__)
  191. parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
  192. help='Outcome file to analyze')
  193. parser.add_argument('task', default='all', nargs='?',
  194. help='Analysis to be done. By default, run all tasks. '
  195. 'With one or more TASK, run only those. '
  196. 'TASK can be the name of a single task or '
  197. 'comma/space-separated list of tasks. ')
  198. parser.add_argument('--list', action='store_true',
  199. help='List all available tasks and exit.')
  200. options = parser.parse_args()
  201. if options.list:
  202. for task in TASKS:
  203. Results.log(task)
  204. sys.exit(0)
  205. result = True
  206. if options.task == 'all':
  207. tasks = TASKS.keys()
  208. else:
  209. tasks = re.split(r'[, ]+', options.task)
  210. for task in tasks:
  211. if task not in TASKS:
  212. Results.log('Error: invalid task: {}'.format(task))
  213. sys.exit(1)
  214. for task in TASKS:
  215. if task in tasks:
  216. if not TASKS[task]['test_function'](options.outcomes, TASKS[task]['args']):
  217. result = False
  218. if result is False:
  219. sys.exit(1)
  220. Results.log("SUCCESS :-)")
  221. except Exception: # pylint: disable=broad-except
  222. # Print the backtrace and exit explicitly with our chosen status.
  223. traceback.print_exc()
  224. sys.exit(120)
  225. if __name__ == '__main__':
  226. main()