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