blob: c954b7dad998de5d85a8efe3d1f23a60c938a405 [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
Valerio Settia2663322023-03-24 08:20:18 +010013import subprocess
14import os
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020015
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020016import check_test_cases
17
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020018class Results:
19 """Process analysis results."""
20
21 def __init__(self):
22 self.error_count = 0
23 self.warning_count = 0
24
25 @staticmethod
26 def log(fmt, *args, **kwargs):
27 sys.stderr.write((fmt + '\n').format(*args, **kwargs))
28
29 def error(self, fmt, *args, **kwargs):
30 self.log('Error: ' + fmt, *args, **kwargs)
31 self.error_count += 1
32
33 def warning(self, fmt, *args, **kwargs):
34 self.log('Warning: ' + fmt, *args, **kwargs)
35 self.warning_count += 1
36
37class TestCaseOutcomes:
38 """The outcomes of one test case across many configurations."""
39 # pylint: disable=too-few-public-methods
40
41 def __init__(self):
Gilles Peskine3d863f22020-06-26 13:02:30 +020042 # Collect a list of witnesses of the test case succeeding or failing.
43 # Currently we don't do anything with witnesses except count them.
44 # The format of a witness is determined by the read_outcome_file
45 # function; it's the platform and configuration joined by ';'.
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020046 self.successes = []
47 self.failures = []
48
49 def hits(self):
50 """Return the number of times a test case has been run.
51
52 This includes passes and failures, but not skips.
53 """
54 return len(self.successes) + len(self.failures)
55
Valerio Settia2663322023-03-24 08:20:18 +010056def execute_reference_driver_tests(ref_component, driver_component, outcome_file):
57 """Run the tests that will fullfill the outcome file used for the following
58 coverage analysis"""
59 # If the outcome file already exists, we assume that the user wants to
60 # perform the comparison analysis again without repeating the tests.
61 if os.path.exists(outcome_file):
62 Results.log("Outcome file (" + outcome_file + ") already exists. " + \
63 "Tests will be skipped.")
64 return
65
66 shell_command = "tests/scripts/all.sh --outcome-file " + outcome_file + \
67 " " + ref_component + " " + driver_component
68 print("Running: " + shell_command)
69 ret_val = subprocess.run(shell_command.split(), check=False).returncode
70
71 if ret_val != 0:
72 Results.log("Error: failed to run reference/driver components")
73 sys.exit(ret_val)
74
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020075def analyze_coverage(results, outcomes):
76 """Check that all available test cases are executed at least once."""
Gilles Peskine686c2922022-01-07 15:58:38 +010077 available = check_test_cases.collect_available_test_cases()
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020078 for key in available:
79 hits = outcomes[key].hits() if key in outcomes else 0
80 if hits == 0:
81 # Make this a warning, not an error, as long as we haven't
82 # fixed this branch to have full coverage of test cases.
83 results.warning('Test case not executed: {}', key)
84
Valerio Setti3002c992023-01-18 17:28:36 +010085def analyze_driver_vs_reference(outcomes, component_ref, component_driver,
86 ignored_suites, ignored_test=None):
Przemek Stekiel4e955902022-10-21 13:42:08 +020087 """Check that all tests executed in the reference component are also
88 executed in the corresponding driver component.
Valerio Setti3002c992023-01-18 17:28:36 +010089 Skip:
90 - full test suites provided in ignored_suites list
91 - only some specific test inside a test suite, for which the corresponding
92 output string is provided
Przemek Stekiel4e955902022-10-21 13:42:08 +020093 """
Przemek Stekiel4e955902022-10-21 13:42:08 +020094 available = check_test_cases.collect_available_test_cases()
95 result = True
96
97 for key in available:
Przemek Stekiel4e955902022-10-21 13:42:08 +020098 # Continue if test was not executed by any component
99 hits = outcomes[key].hits() if key in outcomes else 0
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200100 if hits == 0:
Przemek Stekiel4e955902022-10-21 13:42:08 +0200101 continue
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100102 # Skip ignored test suites
103 full_test_suite = key.split(';')[0] # retrieve full test suite name
104 test_string = key.split(';')[1] # retrieve the text string of this test
105 test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100106 if test_suite in ignored_suites or full_test_suite in ignored_suites:
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100107 continue
Valerio Setti3002c992023-01-18 17:28:36 +0100108 if ((full_test_suite in ignored_test) and
109 (test_string in ignored_test[full_test_suite])):
110 continue
Przemek Stekiel4e955902022-10-21 13:42:08 +0200111 # Search for tests that run in reference component and not in driver component
112 driver_test_passed = False
113 reference_test_passed = False
114 for entry in outcomes[key].successes:
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100115 if component_driver in entry:
Przemek Stekiel4e955902022-10-21 13:42:08 +0200116 driver_test_passed = True
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100117 if component_ref in entry:
Przemek Stekiel4e955902022-10-21 13:42:08 +0200118 reference_test_passed = True
Manuel Pégourié-Gonnardc6967d22022-12-30 13:40:34 +0100119 if(reference_test_passed and not driver_test_passed):
Valerio Setti3951d1b2023-03-13 18:37:34 +0100120 Results.log(key)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200121 result = False
122 return result
123
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200124def analyze_outcomes(outcomes):
125 """Run all analyses on the given outcome collection."""
126 results = Results()
Gilles Peskine8d3c70a2020-06-25 18:37:43 +0200127 analyze_coverage(results, outcomes)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200128 return results
129
130def read_outcome_file(outcome_file):
131 """Parse an outcome file and return an outcome collection.
132
133An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects.
134The keys are the test suite name and the test case description, separated
135by a semicolon.
136"""
137 outcomes = {}
138 with open(outcome_file, 'r', encoding='utf-8') as input_file:
139 for line in input_file:
140 (platform, config, suite, case, result, _cause) = line.split(';')
141 key = ';'.join([suite, case])
142 setup = ';'.join([platform, config])
143 if key not in outcomes:
144 outcomes[key] = TestCaseOutcomes()
145 if result == 'PASS':
146 outcomes[key].successes.append(setup)
147 elif result == 'FAIL':
148 outcomes[key].failures.append(setup)
149 return outcomes
150
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200151def do_analyze_coverage(outcome_file, args):
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100152 """Perform coverage analysis."""
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200153 del args # unused
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200154 outcomes = read_outcome_file(outcome_file)
Valerio Setti3951d1b2023-03-13 18:37:34 +0100155 Results.log("\n*** Analyze coverage ***\n")
Przemek Stekiel4e955902022-10-21 13:42:08 +0200156 results = analyze_outcomes(outcomes)
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200157 return results.error_count == 0
Przemek Stekiel4e955902022-10-21 13:42:08 +0200158
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200159def do_analyze_driver_vs_reference(outcome_file, args):
Przemek Stekiel4e955902022-10-21 13:42:08 +0200160 """Perform driver vs reference analyze."""
Valerio Settia2663322023-03-24 08:20:18 +0100161 execute_reference_driver_tests(args['component_ref'], \
162 args['component_driver'], outcome_file)
163
Valerio Setti3002c992023-01-18 17:28:36 +0100164 ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100165
Przemek Stekiel4e955902022-10-21 13:42:08 +0200166 outcomes = read_outcome_file(outcome_file)
Valerio Setti3951d1b2023-03-13 18:37:34 +0100167 Results.log("\n*** Analyze driver {} vs reference {} ***\n".format(
Manuel Pégourié-Gonnardc6967d22022-12-30 13:40:34 +0100168 args['component_driver'], args['component_ref']))
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100169 return analyze_driver_vs_reference(outcomes, args['component_ref'],
Valerio Setti3002c992023-01-18 17:28:36 +0100170 args['component_driver'], ignored_suites,
171 args['ignored_tests'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200172
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100173# List of tasks with a function that can handle this task and additional arguments if required
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200174TASKS = {
175 'analyze_coverage': {
176 'test_function': do_analyze_coverage,
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100177 'args': {}
178 },
Valerio Settia2663322023-03-24 08:20:18 +0100179 # There are 2 options to use analyze_driver_vs_reference_xxx locally:
180 # 1. Run tests and then analysis:
181 # - tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
182 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
183 # 2. Let this script run both automatically:
184 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200185 'analyze_driver_vs_reference_hash': {
186 'test_function': do_analyze_driver_vs_reference,
187 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100188 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
189 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100190 'ignored_suites': [
191 'shax', 'mdx', # the software implementations that are being excluded
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100192 'md.psa', # purposefully depends on whether drivers are present
Valerio Setti3002c992023-01-18 17:28:36 +0100193 ],
194 'ignored_tests': {
195 }
196 }
197 },
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100198 'analyze_driver_vs_reference_ecdsa': {
199 'test_function': do_analyze_driver_vs_reference,
200 'args': {
201 'component_ref': 'test_psa_crypto_config_reference_ecdsa_use_psa',
202 'component_driver': 'test_psa_crypto_config_accel_ecdsa_use_psa',
203 'ignored_suites': [
204 'ecdsa', # the software implementation that's excluded
Valerio Setti3002c992023-01-18 17:28:36 +0100205 ],
206 'ignored_tests': {
Valerio Setti9cb0f7a2023-01-18 17:29:29 +0100207 'test_suite_random': [
208 'PSA classic wrapper: ECDSA signature (SECP256R1)',
209 ],
Valerio Setti3002c992023-01-18 17:28:36 +0100210 }
211 }
212 },
Manuel Pégourié-Gonnarde91bcf32023-02-21 13:07:19 +0100213 'analyze_driver_vs_reference_ecdh': {
214 'test_function': do_analyze_driver_vs_reference,
215 'args': {
216 'component_ref': 'test_psa_crypto_config_reference_ecdh_use_psa',
217 'component_driver': 'test_psa_crypto_config_accel_ecdh_use_psa',
218 'ignored_suites': [
219 'ecdh', # the software implementation that's excluded
220 ],
221 'ignored_tests': {
Manuel Pégourié-Gonnarde91bcf32023-02-21 13:07:19 +0100222 }
223 }
224 },
Valerio Settid0fffc52023-03-13 16:08:03 +0100225 'analyze_driver_vs_reference_ecjpake': {
226 'test_function': do_analyze_driver_vs_reference,
227 'args': {
228 'component_ref': 'test_psa_crypto_config_reference_ecjpake_use_psa',
229 'component_driver': 'test_psa_crypto_config_accel_ecjpake_use_psa',
230 'ignored_suites': [
231 'ecjpake', # the software implementation that's excluded
232 ],
233 'ignored_tests': {
234 }
235 }
236 },
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200237}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200238
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200239def main():
240 try:
241 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200242 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200243 help='Outcome file to analyze')
Przemek Stekiel542d9322022-11-17 09:43:34 +0100244 parser.add_argument('task', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100245 help='Analysis to be done. By default, run all tasks. '
246 'With one or more TASK, run only those. '
247 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100248 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100249 parser.add_argument('--list', action='store_true',
250 help='List all available tasks and exit.')
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200251 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200252
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100253 if options.list:
254 for task in TASKS:
Valerio Setti3951d1b2023-03-13 18:37:34 +0100255 Results.log(task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100256 sys.exit(0)
257
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200258 result = True
Przemek Stekiel4e955902022-10-21 13:42:08 +0200259
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200260 if options.task == 'all':
Przemek Stekield3068af2022-11-14 16:15:19 +0100261 tasks = TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100262 else:
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100263 tasks = re.split(r'[, ]+', options.task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100264
Przemek Stekield3068af2022-11-14 16:15:19 +0100265 for task in tasks:
266 if task not in TASKS:
Valerio Setti3951d1b2023-03-13 18:37:34 +0100267 Results.log('Error: invalid task: {}'.format(task))
Przemek Stekield3068af2022-11-14 16:15:19 +0100268 sys.exit(1)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100269
270 for task in TASKS:
271 if task in tasks:
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200272 if not TASKS[task]['test_function'](options.outcomes, TASKS[task]['args']):
273 result = False
Przemek Stekiel4e955902022-10-21 13:42:08 +0200274
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200275 if result is False:
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200276 sys.exit(1)
Valerio Setti3951d1b2023-03-13 18:37:34 +0100277 Results.log("SUCCESS :-)")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200278 except Exception: # pylint: disable=broad-except
279 # Print the backtrace and exit explicitly with our chosen status.
280 traceback.print_exc()
281 sys.exit(120)
282
283if __name__ == '__main__':
284 main()