Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | |
| 8 | import datetime, logging, shelve, sys |
| 9 | |
| 10 | import common |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import mail |
| 12 | from autotest_lib.frontend import setup_django_readonly_environment |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 13 | |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 14 | # Django and the models are only setup after |
| 15 | # the setup_django_readonly_environment module is imported. |
| 16 | from autotest_lib.frontend.tko import models as tko_models |
| 17 | from django.db import models as django_models |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 18 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 19 | |
| 20 | _STORAGE_FILE = 'failure_storage' |
| 21 | _DAYS_TO_BE_FAILING_TOO_LONG = 60 |
| 22 | _TEST_PASS_STATUS_INDEX = 6 |
| 23 | _MAIL_RESULTS_FROM = 'chromeos-test-health@google.com' |
| 24 | _MAIL_RESULTS_TO = 'chromeos-lab-infrastructure@google.com' |
| 25 | |
| 26 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 27 | def load_storage(): |
| 28 | """ |
| 29 | Loads the storage object from disk. |
| 30 | |
| 31 | This object keeps track of which tests we have already sent mail about so |
| 32 | we only send emails when the status of a test changes. |
| 33 | |
| 34 | @return the storage object. |
| 35 | |
| 36 | """ |
| 37 | return shelve.open(_STORAGE_FILE) |
| 38 | |
| 39 | |
| 40 | def save_storage(storage): |
| 41 | """ |
| 42 | Saves the storage object to disk. |
| 43 | |
| 44 | @param storage: The storage object to save to disk. |
| 45 | |
| 46 | """ |
| 47 | storage.close() |
| 48 | |
| 49 | |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 50 | def is_valid_test_name(name): |
| 51 | """ |
| 52 | Returns if a test name is valid or not. |
| 53 | |
| 54 | There is a bunch of entries in the tko_test table that are not actually |
| 55 | test names. They are there as a side effect of how Autotest uses this |
| 56 | table. |
| 57 | |
| 58 | Two examples of bad tests names are as follows: |
| 59 | link-release/R29-4228.0.0/faft_ec/firmware_ECPowerG3_SERVER_JOB |
| 60 | try_new_image-chormeos1-rack2-host2 |
| 61 | |
| 62 | @param name: The candidate test names to check. |
| 63 | @return True if name is a valid test name and false otherwise. |
| 64 | |
| 65 | """ |
| 66 | return not '/' in name and not name.startswith('try_new_image') |
| 67 | |
| 68 | |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 69 | def get_last_pass_times(): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 70 | """ |
| 71 | Get all the tests that have passed and the time they last passed. |
| 72 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 73 | @return the dict of test_name:last_finish_time pairs for tests that have |
| 74 | passed. |
| 75 | |
| 76 | """ |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 77 | results = tko_models.Test.objects.values('test').filter( |
| 78 | status=_TEST_PASS_STATUS_INDEX).annotate( |
| 79 | last_pass=django_models.Max('started_time')) |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 80 | results_dict = {result['test']: result['last_pass'] |
| 81 | for result in results} |
| 82 | valid_test_names = filter(is_valid_test_name, results_dict) |
| 83 | # The shelve module does not accept Unicode objects as keys but does |
| 84 | # accept utf-8 strings. |
| 85 | return {name.encode('utf8'): results_dict[name] |
| 86 | for name in valid_test_names} |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 87 | |
| 88 | |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 89 | def get_all_test_names(): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 90 | """ |
| 91 | Get all the test names from the database. |
| 92 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 93 | @return a list of all the test names. |
| 94 | |
| 95 | """ |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 96 | results = tko_models.Test.objects.values('test').distinct() |
| 97 | test_names = [test['test'] for test in results] |
| 98 | valid_test_names = filter(is_valid_test_name, test_names) |
| 99 | return [test.encode('utf8') for test in valid_test_names] |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 100 | |
| 101 | |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 102 | def get_tests_to_analyze(): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 103 | """ |
| 104 | Get all the tests as well as the last time they have passed. |
| 105 | |
| 106 | The minimum datetime is given as last pass time for tests that have never |
| 107 | passed. |
| 108 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 109 | @return the dict of test_name:last_finish_time pairs. |
| 110 | |
| 111 | """ |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 112 | last_passes = get_last_pass_times() |
| 113 | all_test_names = get_all_test_names() |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 114 | failures_names = (set(all_test_names) - set(last_passes.keys())) |
| 115 | always_failed = {test: datetime.datetime.min for test in failures_names} |
| 116 | return dict(always_failed.items() + last_passes.items()) |
| 117 | |
| 118 | |
| 119 | def email_about_test_failure(tests, storage): |
| 120 | """ |
| 121 | Send emails based on the last time tests has passed. |
| 122 | |
| 123 | This involves updating the storage and sending an email if a test has |
| 124 | failed for a long time and we have not already sent an email about that |
| 125 | test. |
| 126 | |
| 127 | @param tests: The test_name:time_of_last_pass pairs. |
| 128 | @param storage: The storage object. |
| 129 | |
| 130 | """ |
| 131 | failing_time_cutoff = datetime.timedelta(_DAYS_TO_BE_FAILING_TOO_LONG) |
| 132 | update_status = [] |
| 133 | |
| 134 | today = datetime.datetime.today() |
| 135 | for test, last_fail in tests.iteritems(): |
| 136 | if today - last_fail >= failing_time_cutoff: |
| 137 | if test not in storage: |
| 138 | update_status.append(test) |
| 139 | storage[test] = today |
| 140 | else: |
| 141 | try: |
| 142 | del storage[test] |
| 143 | except KeyError: |
| 144 | pass |
| 145 | |
| 146 | if update_status: |
| 147 | logging.info('Found %i new failing tests out %i, sending email.', |
| 148 | len(update_status), |
| 149 | len(tests)) |
| 150 | mail.send(_MAIL_RESULTS_FROM, |
| 151 | [_MAIL_RESULTS_TO], |
| 152 | [], |
| 153 | 'Long Failing Tests', |
| 154 | 'The following tests have been failing for ' |
| 155 | 'at least %s days:\n\n' % (_DAYS_TO_BE_FAILING_TOO_LONG) + |
| 156 | '\n'.join(update_status)) |
| 157 | |
| 158 | |
| 159 | def main(): |
| 160 | """ |
| 161 | The script code. |
| 162 | |
| 163 | Allows other python code to import and run this code. This will be more |
| 164 | important if a nice way to test this code can be determined. |
| 165 | |
| 166 | """ |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 167 | storage = load_storage() |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 168 | tests = get_tests_to_analyze() |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 169 | email_about_test_failure(tests, storage) |
| 170 | save_storage(storage) |
| 171 | |
| 172 | return 0 |
| 173 | |
| 174 | |
| 175 | if __name__ == '__main__': |
| 176 | sys.exit(main()) |