Xixuan Wu | 665cfad | 2018-08-10 10:08:14 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Common file shared by test_push of autotest and skylab. |
| 6 | |
| 7 | autotest: site_utils/test_push.py |
| 8 | skylab: venv/skylab_staging/test_push.py |
| 9 | """ |
| 10 | |
Xixuan Wu | 84a834f | 2018-08-10 15:22:26 -0700 | [diff] [blame] | 11 | import collections |
| 12 | import re |
| 13 | |
Xixuan Wu | 665cfad | 2018-08-10 10:08:14 -0700 | [diff] [blame] | 14 | # Dictionary of test results keyed by test name regular expression. |
| 15 | EXPECTED_TEST_RESULTS = {'^SERVER_JOB$': 'GOOD', |
| 16 | # This is related to dummy_Fail/control.dependency. |
| 17 | 'dummy_Fail.dependency$': 'TEST_NA', |
| 18 | 'login_LoginSuccess.*': 'GOOD', |
| 19 | 'provision_AutoUpdate.double': 'GOOD', |
| 20 | 'dummy_Pass.*': 'GOOD', |
| 21 | 'dummy_Fail.Fail$': 'FAIL', |
| 22 | 'dummy_Fail.RetryFail$': 'FAIL', |
| 23 | 'dummy_Fail.RetrySuccess': 'GOOD', |
| 24 | 'dummy_Fail.Error$': 'ERROR', |
| 25 | 'dummy_Fail.Warn$': 'WARN', |
| 26 | 'dummy_Fail.NAError$': 'TEST_NA', |
| 27 | 'dummy_Fail.Crash$': 'GOOD', |
| 28 | 'autotest_SyncCount$': 'GOOD', |
| 29 | } |
| 30 | |
| 31 | EXPECTED_TEST_RESULTS_DUMMY = {'^SERVER_JOB$': 'GOOD', |
| 32 | 'dummy_Pass.*': 'GOOD', |
| 33 | 'dummy_Fail.Fail': 'FAIL', |
| 34 | 'dummy_Fail.Warn': 'WARN', |
| 35 | 'dummy_Fail.Crash': 'GOOD', |
| 36 | 'dummy_Fail.Error': 'ERROR', |
| 37 | 'dummy_Fail.NAError': 'TEST_NA',} |
| 38 | |
| 39 | EXPECTED_TEST_RESULTS_POWERWASH = {'platform_Powerwash': 'GOOD', |
| 40 | 'SERVER_JOB': 'GOOD'} |
Xixuan Wu | 84a834f | 2018-08-10 15:22:26 -0700 | [diff] [blame] | 41 | |
| 42 | _TestPushErrors = collections.namedtuple( |
| 43 | '_TestPushErrors', |
| 44 | [ |
| 45 | 'mismatch_errors', |
| 46 | 'unknown_tests', |
| 47 | 'missing_tests', |
| 48 | ] |
| 49 | ) |
| 50 | |
| 51 | |
| 52 | def summarize_push(test_views, expected_results, ignored_tests=[]): |
| 53 | """Summarize the test push errors.""" |
| 54 | test_push_errors = _match_test_results(test_views, expected_results, |
| 55 | ignored_tests) |
| 56 | return _generate_push_summary(test_push_errors) |
| 57 | |
| 58 | |
| 59 | def _match_test_results(test_views, expected_results, ignored_tests): |
| 60 | """Match test results with expected results. |
| 61 | |
| 62 | @param test_views: A dictionary of test status keyed by test name, e.g., |
| 63 | {'dummy_Fail.Error': 'ERROR', 'dummy_Fail.NAError': 'TEST_NA'} |
| 64 | @param expected_results: A dictionary of test name to expected test result. |
| 65 | |
| 66 | @return: A _TestPushErrors tuple. |
| 67 | """ |
| 68 | mismatch_errors = [] |
| 69 | unknown_tests = [] |
| 70 | found_keys = set() |
| 71 | for test_name, test_status in test_views.iteritems(): |
| 72 | test_found = False |
| 73 | for test_name_pattern, expected_result in expected_results.items(): |
| 74 | if re.search(test_name_pattern, test_name): |
| 75 | test_found = True |
| 76 | found_keys.add(test_name_pattern) |
| 77 | if (expected_result != test_status and |
| 78 | _is_significant(test_name, ignored_tests)): |
| 79 | error = ('%s Expected: [%s], Actual: [%s]' % |
| 80 | (test_name, expected_result, test_status)) |
| 81 | mismatch_errors.append(error) |
| 82 | |
| 83 | if not test_found and _is_significant(test_name, ignored_tests): |
| 84 | unknown_tests.append(test_name) |
| 85 | |
| 86 | missing_tests = set(expected_results.keys()) - found_keys |
| 87 | missing_tests = [t for t in missing_tests |
| 88 | if _is_significant(t, ignored_tests)] |
| 89 | return _TestPushErrors(mismatch_errors=mismatch_errors, |
| 90 | unknown_tests=unknown_tests, |
| 91 | missing_tests=missing_tests) |
| 92 | |
| 93 | |
| 94 | def _is_significant(test, ignored_tests_patterns): |
Xixuan Wu | 51f41d8 | 2018-08-10 15:19:07 -0700 | [diff] [blame] | 95 | return all([test not in m for m in ignored_tests_patterns]) |
Xixuan Wu | 84a834f | 2018-08-10 15:22:26 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def _generate_push_summary(test_push_errors): |
| 99 | """Generate a list of summary based on the test_push results.""" |
| 100 | summary = [] |
| 101 | if test_push_errors.mismatch_errors: |
| 102 | summary.append(('Results of %d test(s) do not match expected ' |
| 103 | 'values:') % len(test_push_errors.mismatch_errors)) |
| 104 | summary.extend(test_push_errors.mismatch_errors) |
| 105 | summary.append('\n') |
| 106 | |
| 107 | if test_push_errors.unknown_tests: |
| 108 | summary.append('%d test(s) are not expected to be run:' % |
| 109 | len(test_push_errors.unknown_tests)) |
| 110 | summary.extend(test_push_errors.unknown_tests) |
| 111 | summary.append('\n') |
| 112 | |
| 113 | if test_push_errors.missing_tests: |
| 114 | summary.append('%d test(s) are missing from the results:' % |
| 115 | len(test_push_errors.missing_tests)) |
| 116 | summary.extend(test_push_errors.missing_tests) |
| 117 | summary.append('\n') |
| 118 | |
| 119 | return summary |