blob: bb44396534a895a75d9f36c5001684d305e45d16 [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
Przemek Stekiel733c76e2022-11-14 08:33:21 +010064def analyze_driver_vs_reference(outcomes, component_ref, component_driver, ignored_tests):
Przemek Stekiel4e955902022-10-21 13:42:08 +020065 """Check that all tests executed in the reference component are also
66 executed in the corresponding driver component.
Przemek Stekiel6856f4c2022-11-09 10:50:29 +010067 Skip test suites provided in ignored_tests list.
Przemek Stekiel4e955902022-10-21 13:42:08 +020068 """
Przemek Stekiel4e955902022-10-21 13:42:08 +020069 available = check_test_cases.collect_available_test_cases()
70 result = True
71
72 for key in available:
73 # Skip ignored test suites
Przemek Stekiel6856f4c2022-11-09 10:50:29 +010074 test_suite = key.split(';')[0] # retrieve test suit name
75 test_suite = test_suite.split('.')[0] # retrieve main part of test suit name
76 if test_suite in ignored_tests:
Przemek Stekiel4e955902022-10-21 13:42:08 +020077 continue
78 # Continue if test was not executed by any component
79 hits = outcomes[key].hits() if key in outcomes else 0
Przemek Stekielc86dedf2022-10-24 09:16:04 +020080 if hits == 0:
Przemek Stekiel4e955902022-10-21 13:42:08 +020081 continue
82 # Search for tests that run in reference component and not in driver component
83 driver_test_passed = False
84 reference_test_passed = False
85 for entry in outcomes[key].successes:
Przemek Stekiel51f30ff2022-11-09 12:07:29 +010086 if component_driver in entry:
Przemek Stekiel4e955902022-10-21 13:42:08 +020087 driver_test_passed = True
Przemek Stekiel51f30ff2022-11-09 12:07:29 +010088 if component_ref in entry:
Przemek Stekiel4e955902022-10-21 13:42:08 +020089 reference_test_passed = True
Przemek Stekielc86dedf2022-10-24 09:16:04 +020090 if(driver_test_passed is False and reference_test_passed is True):
Przemek Stekiel4e955902022-10-21 13:42:08 +020091 print('{}: driver: skipped/failed; reference: passed'.format(key))
92 result = False
93 return result
94
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020095def analyze_outcomes(outcomes):
96 """Run all analyses on the given outcome collection."""
97 results = Results()
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020098 analyze_coverage(results, outcomes)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020099 return results
100
101def read_outcome_file(outcome_file):
102 """Parse an outcome file and return an outcome collection.
103
104An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects.
105The keys are the test suite name and the test case description, separated
106by a semicolon.
107"""
108 outcomes = {}
109 with open(outcome_file, 'r', encoding='utf-8') as input_file:
110 for line in input_file:
111 (platform, config, suite, case, result, _cause) = line.split(';')
112 key = ';'.join([suite, case])
113 setup = ';'.join([platform, config])
114 if key not in outcomes:
115 outcomes[key] = TestCaseOutcomes()
116 if result == 'PASS':
117 outcomes[key].successes.append(setup)
118 elif result == 'FAIL':
119 outcomes[key].failures.append(setup)
120 return outcomes
121
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200122def do_analyze_coverage(outcome_file, args):
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100123 """Perform coverage analysis."""
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200124 del args # unused
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200125 outcomes = read_outcome_file(outcome_file)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200126 results = analyze_outcomes(outcomes)
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200127 return results.error_count == 0
Przemek Stekiel4e955902022-10-21 13:42:08 +0200128
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200129def do_analyze_driver_vs_reference(outcome_file, args):
Przemek Stekiel4e955902022-10-21 13:42:08 +0200130 """Perform driver vs reference analyze."""
Przemek Stekielbe279c72022-11-09 12:17:08 +0100131 ignored_tests = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100132
Przemek Stekiel4e955902022-10-21 13:42:08 +0200133 outcomes = read_outcome_file(outcome_file)
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100134 return analyze_driver_vs_reference(outcomes, args['component_ref'],
135 args['component_driver'], ignored_tests)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200136
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100137# List of tasks with a function that can handle this task and additional arguments if required
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200138TASKS = {
139 'analyze_coverage': {
140 'test_function': do_analyze_coverage,
141 'args': {}},
142 'analyze_driver_vs_reference_hash': {
143 'test_function': do_analyze_driver_vs_reference,
144 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100145 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
146 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Przemek Stekiel733c76e2022-11-14 08:33:21 +0100147 'ignored_suites': ['shax', 'mdx', # the software implementations that are being excluded
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100148 'md', # the legacy abstraction layer that's being excluded
Przemek Stekielbe279c72022-11-09 12:17:08 +0100149 ]}}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200150}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200151
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200152def main():
153 try:
154 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200155 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200156 help='Outcome file to analyze')
Przemek Stekiel542d9322022-11-17 09:43:34 +0100157 parser.add_argument('task', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100158 help='Analysis to be done. By default, run all tasks. '
159 'With one or more TASK, run only those. '
160 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100161 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100162 parser.add_argument('--list', action='store_true',
163 help='List all available tasks and exit.')
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200164 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200165
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100166 if options.list:
167 for task in TASKS:
168 print(task)
169 sys.exit(0)
170
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200171 result = True
Przemek Stekiel4e955902022-10-21 13:42:08 +0200172
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200173 if options.task == 'all':
Przemek Stekield3068af2022-11-14 16:15:19 +0100174 tasks = TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100175 else:
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100176 tasks = re.split(r'[, ]+', options.task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100177
Przemek Stekield3068af2022-11-14 16:15:19 +0100178 for task in tasks:
179 if task not in TASKS:
180 print('Error: invalid task: {}'.format(task))
181 sys.exit(1)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100182
183 for task in TASKS:
184 if task in tasks:
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200185 if not TASKS[task]['test_function'](options.outcomes, TASKS[task]['args']):
186 result = False
Przemek Stekiel4e955902022-10-21 13:42:08 +0200187
Przemek Stekielc86dedf2022-10-24 09:16:04 +0200188 if result is False:
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200189 sys.exit(1)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200190 print("SUCCESS :-)")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200191 except Exception: # pylint: disable=broad-except
192 # Print the backtrace and exit explicitly with our chosen status.
193 traceback.print_exc()
194 sys.exit(120)
195
196if __name__ == '__main__':
197 main()