blob: 61f6bb9acc8af67f31571d2f0788f93e82d62d39 [file] [log] [blame]
Gilles Peskine15c2cbf2020-06-25 18:36:28 +02001#!/usr/bin/env python3
2
3"""Analyze the test outcomes from a full CI run.
4
5This script can also run on outcomes from a partial run, but the results are
6less likely to be useful.
7"""
8
9import argparse
10import sys
11import traceback
Przemek Stekiel85c54ea2022-11-17 11:50:23 +010012import re
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020013
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020014import check_test_cases
15
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020016class Results:
17 """Process analysis results."""
18
19 def __init__(self):
20 self.error_count = 0
21 self.warning_count = 0
22
23 @staticmethod
24 def log(fmt, *args, **kwargs):
25 sys.stderr.write((fmt + '\n').format(*args, **kwargs))
26
27 def error(self, fmt, *args, **kwargs):
28 self.log('Error: ' + fmt, *args, **kwargs)
29 self.error_count += 1
30
31 def warning(self, fmt, *args, **kwargs):
32 self.log('Warning: ' + fmt, *args, **kwargs)
33 self.warning_count += 1
34
35class TestCaseOutcomes:
36 """The outcomes of one test case across many configurations."""
37 # pylint: disable=too-few-public-methods
38
39 def __init__(self):
Gilles Peskine3d863f22020-06-26 13:02:30 +020040 # Collect a list of witnesses of the test case succeeding or failing.
41 # Currently we don't do anything with witnesses except count them.
42 # The format of a witness is determined by the read_outcome_file
43 # function; it's the platform and configuration joined by ';'.
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020044 self.successes = []
45 self.failures = []
46
47 def hits(self):
48 """Return the number of times a test case has been run.
49
50 This includes passes and failures, but not skips.
51 """
52 return len(self.successes) + len(self.failures)
53
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020054def analyze_coverage(results, outcomes):
55 """Check that all available test cases are executed at least once."""
Gilles Peskine686c2922022-01-07 15:58:38 +010056 available = check_test_cases.collect_available_test_cases()
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020057 for key in available:
58 hits = outcomes[key].hits() if key in outcomes else 0
59 if hits == 0:
60 # Make this a warning, not an error, as long as we haven't
61 # fixed this branch to have full coverage of test cases.
62 results.warning('Test case not executed: {}', key)
63
Valerio Setti3002c992023-01-18 17:28:36 +010064def analyze_driver_vs_reference(outcomes, component_ref, component_driver,
65 ignored_suites, ignored_test=None):
Przemek Stekiel4e955902022-10-21 13:42:08 +020066 """Check that all tests executed in the reference component are also
67 executed in the corresponding driver component.
Valerio Setti3002c992023-01-18 17:28:36 +010068 Skip:
69 - full test suites provided in ignored_suites list
70 - only some specific test inside a test suite, for which the corresponding
71 output string is provided
Przemek Stekiel4e955902022-10-21 13:42:08 +020072 """
Przemek Stekiel4e955902022-10-21 13:42:08 +020073 available = check_test_cases.collect_available_test_cases()
74 result = True
75
76 for key in available:
Przemek Stekiel4e955902022-10-21 13:42:08 +020077 # Continue if test was not executed by any component
78 hits = outcomes[key].hits() if key in outcomes else 0
Przemek Stekielc86dedf2022-10-24 09:16:04 +020079 if hits == 0:
Przemek Stekiel4e955902022-10-21 13:42:08 +020080 continue
Valerio Setti00c1ccb2023-02-02 11:33:31 +010081 # Skip ignored test suites
82 full_test_suite = key.split(';')[0] # retrieve full test suite name
83 test_string = key.split(';')[1] # retrieve the text string of this test
84 test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
85 if test_suite in ignored_suites:
86 continue
Valerio Setti3002c992023-01-18 17:28:36 +010087 if ((full_test_suite in ignored_test) and
88 (test_string in ignored_test[full_test_suite])):
89 continue
Przemek Stekiel4e955902022-10-21 13:42:08 +020090 # Search for tests that run in reference component and not in driver component
91 driver_test_passed = False
92 reference_test_passed = False
93 for entry in outcomes[key].successes:
Przemek Stekiel51f30ff2022-11-09 12:07:29 +010094 if component_driver in entry:
Przemek Stekiel4e955902022-10-21 13:42:08 +020095 driver_test_passed = True
Przemek Stekiel51f30ff2022-11-09 12:07:29 +010096 if component_ref in entry:
Przemek Stekiel4e955902022-10-21 13:42:08 +020097 reference_test_passed = True
Manuel Pégourié-Gonnardc6967d22022-12-30 13:40:34 +010098 if(reference_test_passed and not driver_test_passed):
Valerio Setti3951d1b2023-03-13 18:37:34 +010099 Results.log(key)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200100 result = False
101 return result
102
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200103def analyze_outcomes(outcomes):
104 """Run all analyses on the given outcome collection."""
105 results = Results()
Gilles Peskine8d3c70a2020-06-25 18:37:43 +0200106 analyze_coverage(results, outcomes)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200107 return results
108
109def read_outcome_file(outcome_file):
110 """Parse an outcome file and return an outcome collection.
111
112An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects.
113The keys are the test suite name and the test case description, separated
114by a semicolon.
115"""
116 outcomes = {}
117 with open(outcome_file, 'r', encoding='utf-8') as input_file:
118 for line in input_file:
119 (platform, config, suite, case, result, _cause) = line.split(';')
120 key = ';'.join([suite, case])
121 setup = ';'.join([platform, config])
122 if key not in outcomes:
123 outcomes[key] = TestCaseOutcomes()
124 if result == 'PASS':
125 outcomes[key].successes.append(setup)
126 elif result == 'FAIL':
127 outcomes[key].failures.append(setup)
128 return outcomes
129
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200130def do_analyze_coverage(outcome_file, args):
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100131 """Perform coverage analysis."""
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200132 del args # unused
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200133 outcomes = read_outcome_file(outcome_file)
Valerio Setti3951d1b2023-03-13 18:37:34 +0100134 Results.log("\n*** Analyze coverage ***\n")
Przemek Stekiel4e955902022-10-21 13:42:08 +0200135 results = analyze_outcomes(outcomes)
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200136 return results.error_count == 0
Przemek Stekiel4e955902022-10-21 13:42:08 +0200137
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200138def do_analyze_driver_vs_reference(outcome_file, args):
Przemek Stekiel4e955902022-10-21 13:42:08 +0200139 """Perform driver vs reference analyze."""
Valerio Setti3002c992023-01-18 17:28:36 +0100140 ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100141
Przemek Stekiel4e955902022-10-21 13:42:08 +0200142 outcomes = read_outcome_file(outcome_file)
Valerio Setti3951d1b2023-03-13 18:37:34 +0100143 Results.log("\n*** Analyze driver {} vs reference {} ***\n".format(
Manuel Pégourié-Gonnardc6967d22022-12-30 13:40:34 +0100144 args['component_driver'], args['component_ref']))
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100145 return analyze_driver_vs_reference(outcomes, args['component_ref'],
Valerio Setti3002c992023-01-18 17:28:36 +0100146 args['component_driver'], ignored_suites,
147 args['ignored_tests'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200148
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100149# List of tasks with a function that can handle this task and additional arguments if required
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200150TASKS = {
151 'analyze_coverage': {
152 'test_function': do_analyze_coverage,
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100153 'args': {}
154 },
155 # How to use analyze_driver_vs_reference_xxx locally:
156 # 1. tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
157 # 2. tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200158 'analyze_driver_vs_reference_hash': {
159 'test_function': do_analyze_driver_vs_reference,
160 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100161 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
162 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100163 'ignored_suites': [
164 'shax', 'mdx', # the software implementations that are being excluded
165 'md', # the legacy abstraction layer that's being excluded
Valerio Setti3002c992023-01-18 17:28:36 +0100166 ],
167 'ignored_tests': {
168 }
169 }
170 },
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100171 'analyze_driver_vs_reference_ecdsa': {
172 'test_function': do_analyze_driver_vs_reference,
173 'args': {
174 'component_ref': 'test_psa_crypto_config_reference_ecdsa_use_psa',
175 'component_driver': 'test_psa_crypto_config_accel_ecdsa_use_psa',
176 'ignored_suites': [
177 'ecdsa', # the software implementation that's excluded
Valerio Setti3002c992023-01-18 17:28:36 +0100178 ],
179 'ignored_tests': {
Valerio Setti9cb0f7a2023-01-18 17:29:29 +0100180 'test_suite_random': [
181 'PSA classic wrapper: ECDSA signature (SECP256R1)',
182 ],
Valerio Setti3002c992023-01-18 17:28:36 +0100183 }
184 }
185 },
Manuel Pégourié-Gonnarde91bcf32023-02-21 13:07:19 +0100186 'analyze_driver_vs_reference_ecdh': {
187 'test_function': do_analyze_driver_vs_reference,
188 'args': {
189 'component_ref': 'test_psa_crypto_config_reference_ecdh_use_psa',
190 'component_driver': 'test_psa_crypto_config_accel_ecdh_use_psa',
191 'ignored_suites': [
192 'ecdh', # the software implementation that's excluded
193 ],
194 'ignored_tests': {
Manuel Pégourié-Gonnarde91bcf32023-02-21 13:07:19 +0100195 }
196 }
197 },
Valerio Settid0fffc52023-03-13 16:08:03 +0100198 'analyze_driver_vs_reference_ecjpake': {
199 'test_function': do_analyze_driver_vs_reference,
200 'args': {
201 'component_ref': 'test_psa_crypto_config_reference_ecjpake_use_psa',
202 'component_driver': 'test_psa_crypto_config_accel_ecjpake_use_psa',
203 'ignored_suites': [
204 'ecjpake', # the software implementation that's excluded
205 ],
206 'ignored_tests': {
207 }
208 }
209 },
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200210}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200211
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200212def main():
213 try:
214 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200215 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200216 help='Outcome file to analyze')
Przemek Stekiel542d9322022-11-17 09:43:34 +0100217 parser.add_argument('task', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100218 help='Analysis to be done. By default, run all tasks. '
219 'With one or more TASK, run only those. '
220 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100221 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100222 parser.add_argument('--list', action='store_true',
223 help='List all available tasks and exit.')
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200224 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200225
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100226 if options.list:
227 for task in TASKS:
Valerio Setti3951d1b2023-03-13 18:37:34 +0100228 Results.log(task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100229 sys.exit(0)
230
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200231 result = True
Przemek Stekiel4e955902022-10-21 13:42:08 +0200232
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200233 if options.task == 'all':
Przemek Stekield3068af2022-11-14 16:15:19 +0100234 tasks = TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100235 else:
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100236 tasks = re.split(r'[, ]+', options.task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100237
Przemek Stekield3068af2022-11-14 16:15:19 +0100238 for task in tasks:
239 if task not in TASKS:
Valerio Setti3951d1b2023-03-13 18:37:34 +0100240 Results.log('Error: invalid task: {}'.format(task))
Przemek Stekield3068af2022-11-14 16:15:19 +0100241 sys.exit(1)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100242
243 for task in TASKS:
244 if task in tasks:
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200245 if not TASKS[task]['test_function'](options.outcomes, TASKS[task]['args']):
246 result = False
Przemek Stekiel4e955902022-10-21 13:42:08 +0200247
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200248 if result is False:
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200249 sys.exit(1)
Valerio Setti3951d1b2023-03-13 18:37:34 +0100250 Results.log("SUCCESS :-)")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200251 except Exception: # pylint: disable=broad-except
252 # Print the backtrace and exit explicitly with our chosen status.
253 traceback.print_exc()
254 sys.exit(120)
255
256if __name__ == '__main__':
257 main()