Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """Analyze the test outcomes from a full CI run. |
| 4 | |
| 5 | This script can also run on outcomes from a partial run, but the results are |
| 6 | less likely to be useful. |
| 7 | """ |
| 8 | |
| 9 | import argparse |
| 10 | import sys |
| 11 | import traceback |
| 12 | |
Gilles Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame] | 13 | import check_test_cases |
| 14 | |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 15 | class Results: |
| 16 | """Process analysis results.""" |
| 17 | |
| 18 | def __init__(self): |
| 19 | self.error_count = 0 |
| 20 | self.warning_count = 0 |
| 21 | |
| 22 | @staticmethod |
| 23 | def log(fmt, *args, **kwargs): |
| 24 | sys.stderr.write((fmt + '\n').format(*args, **kwargs)) |
| 25 | |
| 26 | def error(self, fmt, *args, **kwargs): |
| 27 | self.log('Error: ' + fmt, *args, **kwargs) |
| 28 | self.error_count += 1 |
| 29 | |
| 30 | def warning(self, fmt, *args, **kwargs): |
| 31 | self.log('Warning: ' + fmt, *args, **kwargs) |
| 32 | self.warning_count += 1 |
| 33 | |
| 34 | class TestCaseOutcomes: |
| 35 | """The outcomes of one test case across many configurations.""" |
| 36 | # pylint: disable=too-few-public-methods |
| 37 | |
| 38 | def __init__(self): |
Gilles Peskine | 3d863f2 | 2020-06-26 13:02:30 +0200 | [diff] [blame] | 39 | # Collect a list of witnesses of the test case succeeding or failing. |
| 40 | # Currently we don't do anything with witnesses except count them. |
| 41 | # The format of a witness is determined by the read_outcome_file |
| 42 | # function; it's the platform and configuration joined by ';'. |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 43 | self.successes = [] |
| 44 | self.failures = [] |
| 45 | |
| 46 | def hits(self): |
| 47 | """Return the number of times a test case has been run. |
| 48 | |
| 49 | This includes passes and failures, but not skips. |
| 50 | """ |
| 51 | return len(self.successes) + len(self.failures) |
| 52 | |
Gilles Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame] | 53 | def analyze_coverage(results, outcomes): |
| 54 | """Check that all available test cases are executed at least once.""" |
Gilles Peskine | 686c292 | 2022-01-07 15:58:38 +0100 | [diff] [blame] | 55 | available = check_test_cases.collect_available_test_cases() |
Gilles Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame] | 56 | for key in available: |
| 57 | hits = outcomes[key].hits() if key in outcomes else 0 |
| 58 | if hits == 0: |
| 59 | # Make this a warning, not an error, as long as we haven't |
| 60 | # fixed this branch to have full coverage of test cases. |
| 61 | results.warning('Test case not executed: {}', key) |
| 62 | |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 63 | def analyze_driver_vs_reference(outcomes, components, ignored_tests): |
| 64 | """Check that all tests executed in the reference component are also |
| 65 | executed in the corresponding driver component. |
| 66 | Skip test suits provided in ignored_tests list. |
| 67 | """ |
| 68 | driver_component = components[0] |
| 69 | reference_component = components[1] |
| 70 | available = check_test_cases.collect_available_test_cases() |
| 71 | result = True |
| 72 | |
| 73 | for key in available: |
| 74 | # Skip ignored test suites |
| 75 | test_suit = key.split(';')[0] # retrieve test suit name |
| 76 | test_suit = test_suit.split('.')[0] # retrieve main part of test suit name |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 77 | if test_suit in ignored_tests: |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 78 | continue |
| 79 | # Continue if test was not executed by any component |
| 80 | hits = outcomes[key].hits() if key in outcomes else 0 |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 81 | if hits == 0: |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 82 | continue |
| 83 | # Search for tests that run in reference component and not in driver component |
| 84 | driver_test_passed = False |
| 85 | reference_test_passed = False |
| 86 | for entry in outcomes[key].successes: |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 87 | if driver_component in entry: |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 88 | driver_test_passed = True |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 89 | if reference_component in entry: |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 90 | reference_test_passed = True |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 91 | #if(driver_test_passed is True and reference_test_passed is False): |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 92 | # print('{}: driver: passed; reference: skipped'.format(key)) |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 93 | if(driver_test_passed is False and reference_test_passed is True): |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 94 | print('{}: driver: skipped/failed; reference: passed'.format(key)) |
| 95 | result = False |
| 96 | return result |
| 97 | |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 98 | def analyze_outcomes(outcomes): |
| 99 | """Run all analyses on the given outcome collection.""" |
| 100 | results = Results() |
Gilles Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame] | 101 | analyze_coverage(results, outcomes) |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 102 | return results |
| 103 | |
| 104 | def read_outcome_file(outcome_file): |
| 105 | """Parse an outcome file and return an outcome collection. |
| 106 | |
| 107 | An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects. |
| 108 | The keys are the test suite name and the test case description, separated |
| 109 | by a semicolon. |
| 110 | """ |
| 111 | outcomes = {} |
| 112 | with open(outcome_file, 'r', encoding='utf-8') as input_file: |
| 113 | for line in input_file: |
| 114 | (platform, config, suite, case, result, _cause) = line.split(';') |
| 115 | key = ';'.join([suite, case]) |
| 116 | setup = ';'.join([platform, config]) |
| 117 | if key not in outcomes: |
| 118 | outcomes[key] = TestCaseOutcomes() |
| 119 | if result == 'PASS': |
| 120 | outcomes[key].successes.append(setup) |
| 121 | elif result == 'FAIL': |
| 122 | outcomes[key].failures.append(setup) |
| 123 | return outcomes |
| 124 | |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 125 | def do_analyze_coverage(outcome_file, args): |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 126 | """Perform coverage analyze.""" |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 127 | del args # unused |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 128 | outcomes = read_outcome_file(outcome_file) |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 129 | results = analyze_outcomes(outcomes) |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 130 | return results.error_count == 0 |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 131 | |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 132 | def do_analyze_driver_vs_reference(outcome_file, args): |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 133 | """Perform driver vs reference analyze.""" |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 134 | components = args['components'].split(',') |
| 135 | ignored_tests = args['ignored'].split(',') |
| 136 | ignored_tests = ['test_suite_' + x for x in ignored_tests] |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 137 | # We need exactly 2 components to analyze (first driver and second reference) |
| 138 | if(len(components) != 2 or "accel" not in components[0] or "reference" not in components[1]): |
| 139 | print('Error: Wrong component list. Exactly 2 components are required (driver,reference). ') |
| 140 | return False |
| 141 | outcomes = read_outcome_file(outcome_file) |
| 142 | return analyze_driver_vs_reference(outcomes, components, ignored_tests) |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 143 | |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 144 | # List of tasks with function that can handle this task and additional arguments if required |
| 145 | # pylint: disable=line-too-long |
| 146 | TASKS = { |
| 147 | 'analyze_coverage': { |
| 148 | 'test_function': do_analyze_coverage, |
| 149 | 'args': {}}, |
| 150 | 'analyze_driver_vs_reference_hash': { |
| 151 | 'test_function': do_analyze_driver_vs_reference, |
| 152 | 'args': { |
| 153 | 'components': 'test_psa_crypto_config_accel_hash_use_psa,test_psa_crypto_config_reference_hash_use_psa', |
| 154 | 'ignored': 'md,mdx,shax,entropy,hmac_drbg,random,psa_crypto_init,hkdf'}} |
| 155 | } |
| 156 | # pylint: enable=line-too-long |
| 157 | |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 158 | def main(): |
| 159 | try: |
| 160 | parser = argparse.ArgumentParser(description=__doc__) |
Przemek Stekiel | 58bbc23 | 2022-10-24 08:10:10 +0200 | [diff] [blame] | 161 | parser.add_argument('outcomes', metavar='OUTCOMES.CSV', |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 162 | help='Outcome file to analyze') |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 163 | parser.add_argument('--task', default='all', |
| 164 | help='Analyze to be done: all or analyze_coverage or ' |
| 165 | 'analyze_driver_vs_reference_hash') |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 166 | options = parser.parse_args() |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 167 | |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 168 | result = True |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 169 | |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 170 | if options.task == 'all': |
| 171 | for task in TASKS: |
| 172 | if not TASKS[task]['test_function'](options.outcomes, TASKS[task]['args']): |
| 173 | result = False |
| 174 | elif options.task in TASKS: |
| 175 | if not TASKS[options.task]['test_function'](options.outcomes, |
| 176 | TASKS[options.task]['args']): |
| 177 | result = False |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 178 | else: |
| 179 | print('Error: Unknown task: {}'.format(options.task)) |
Przemek Stekiel | 4d13c83 | 2022-10-26 16:11:26 +0200 | [diff] [blame] | 180 | result = False |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 181 | |
Przemek Stekiel | c86dedf | 2022-10-24 09:16:04 +0200 | [diff] [blame] | 182 | if result is False: |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 183 | sys.exit(1) |
Przemek Stekiel | 4e95590 | 2022-10-21 13:42:08 +0200 | [diff] [blame] | 184 | print("SUCCESS :-)") |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 185 | except Exception: # pylint: disable=broad-except |
| 186 | # Print the backtrace and exit explicitly with our chosen status. |
| 187 | traceback.print_exc() |
| 188 | sys.exit(120) |
| 189 | |
| 190 | if __name__ == '__main__': |
| 191 | main() |