blob: 96599bd5306e672b7edfa1bb47b2d28df27f1bc1 [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
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020010import re
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020011import sys
12import traceback
13
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):
40 self.successes = []
41 self.failures = []
42
43 def hits(self):
44 """Return the number of times a test case has been run.
45
46 This includes passes and failures, but not skips.
47 """
48 return len(self.successes) + len(self.failures)
49
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020050class TestDescriptions(check_test_cases.TestDescriptionExplorer):
51 """Collect the available test cases."""
52
53 def __init__(self):
54 super().__init__()
55 self.descriptions = set()
56
57 def process_test_case(self, _per_file_state,
58 file_name, _line_number, description):
59 """Record an available test case."""
60 base_name = re.sub(r'\.[^.]*$', '', re.sub(r'.*/', '', file_name))
61 key = ';'.join([base_name, description.decode('utf-8')])
62 self.descriptions.add(key)
63
64def collect_available_test_cases():
65 """Collect the available test cases."""
66 explorer = TestDescriptions()
67 explorer.walk_all()
68 return sorted(explorer.descriptions)
69
70def analyze_coverage(results, outcomes):
71 """Check that all available test cases are executed at least once."""
72 available = collect_available_test_cases()
73 for key in available:
74 hits = outcomes[key].hits() if key in outcomes else 0
75 if hits == 0:
76 # Make this a warning, not an error, as long as we haven't
77 # fixed this branch to have full coverage of test cases.
78 results.warning('Test case not executed: {}', key)
79
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020080def analyze_outcomes(outcomes):
81 """Run all analyses on the given outcome collection."""
82 results = Results()
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020083 analyze_coverage(results, outcomes)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020084 return results
85
86def read_outcome_file(outcome_file):
87 """Parse an outcome file and return an outcome collection.
88
89An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects.
90The keys are the test suite name and the test case description, separated
91by a semicolon.
92"""
93 outcomes = {}
94 with open(outcome_file, 'r', encoding='utf-8') as input_file:
95 for line in input_file:
96 (platform, config, suite, case, result, _cause) = line.split(';')
97 key = ';'.join([suite, case])
98 setup = ';'.join([platform, config])
99 if key not in outcomes:
100 outcomes[key] = TestCaseOutcomes()
101 if result == 'PASS':
102 outcomes[key].successes.append(setup)
103 elif result == 'FAIL':
104 outcomes[key].failures.append(setup)
105 return outcomes
106
107def analyze_outcome_file(outcome_file):
108 """Analyze the given outcome file."""
109 outcomes = read_outcome_file(outcome_file)
110 return analyze_outcomes(outcomes)
111
112def main():
113 try:
114 parser = argparse.ArgumentParser(description=__doc__)
115 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
116 help='Outcome file to analyze')
117 options = parser.parse_args()
118 results = analyze_outcome_file(options.outcomes)
119 if results.error_count > 0:
120 sys.exit(1)
121 except Exception: # pylint: disable=broad-except
122 # Print the backtrace and exit explicitly with our chosen status.
123 traceback.print_exc()
124 sys.exit(120)
125
126if __name__ == '__main__':
127 main()