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 |
Gilles Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame^] | 10 | import re |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 11 | import sys |
| 12 | import traceback |
| 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 | |
| 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 | |
| 35 | class 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 Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame^] | 50 | class 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 | |
| 64 | def collect_available_test_cases(): |
| 65 | """Collect the available test cases.""" |
| 66 | explorer = TestDescriptions() |
| 67 | explorer.walk_all() |
| 68 | return sorted(explorer.descriptions) |
| 69 | |
| 70 | def 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 Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 80 | def analyze_outcomes(outcomes): |
| 81 | """Run all analyses on the given outcome collection.""" |
| 82 | results = Results() |
Gilles Peskine | 8d3c70a | 2020-06-25 18:37:43 +0200 | [diff] [blame^] | 83 | analyze_coverage(results, outcomes) |
Gilles Peskine | 15c2cbf | 2020-06-25 18:36:28 +0200 | [diff] [blame] | 84 | return results |
| 85 | |
| 86 | def read_outcome_file(outcome_file): |
| 87 | """Parse an outcome file and return an outcome collection. |
| 88 | |
| 89 | An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects. |
| 90 | The keys are the test suite name and the test case description, separated |
| 91 | by 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 | |
| 107 | def analyze_outcome_file(outcome_file): |
| 108 | """Analyze the given outcome file.""" |
| 109 | outcomes = read_outcome_file(outcome_file) |
| 110 | return analyze_outcomes(outcomes) |
| 111 | |
| 112 | def 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 | |
| 126 | if __name__ == '__main__': |
| 127 | main() |