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 | |
Keyar Hood | 4d5f9d1 | 2013-06-28 15:36:45 -0700 | [diff] [blame] | 8 | import datetime, shelve, sys |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 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 |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 17 | from autotest_lib.frontend.health import utils |
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' |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 21 | # Mark a test as failing too long if it has not passed in this many days |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 22 | _DAYS_TO_BE_FAILING_TOO_LONG = 60 |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 23 | # Ignore any tests that have not ran in this many days |
| 24 | _DAYS_NOT_RUNNING_CUTOFF = 60 |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 25 | _MAIL_RESULTS_FROM = 'chromeos-test-health@google.com' |
| 26 | _MAIL_RESULTS_TO = 'chromeos-lab-infrastructure@google.com' |
| 27 | |
| 28 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 29 | def load_storage(): |
| 30 | """ |
| 31 | Loads the storage object from disk. |
| 32 | |
| 33 | This object keeps track of which tests we have already sent mail about so |
| 34 | we only send emails when the status of a test changes. |
| 35 | |
| 36 | @return the storage object. |
| 37 | |
| 38 | """ |
| 39 | return shelve.open(_STORAGE_FILE) |
| 40 | |
| 41 | |
| 42 | def save_storage(storage): |
| 43 | """ |
| 44 | Saves the storage object to disk. |
| 45 | |
| 46 | @param storage: The storage object to save to disk. |
| 47 | |
| 48 | """ |
| 49 | storage.close() |
| 50 | |
| 51 | |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 52 | def is_valid_test_name(name): |
| 53 | """ |
| 54 | Returns if a test name is valid or not. |
| 55 | |
| 56 | There is a bunch of entries in the tko_test table that are not actually |
| 57 | test names. They are there as a side effect of how Autotest uses this |
| 58 | table. |
| 59 | |
| 60 | Two examples of bad tests names are as follows: |
| 61 | link-release/R29-4228.0.0/faft_ec/firmware_ECPowerG3_SERVER_JOB |
| 62 | try_new_image-chormeos1-rack2-host2 |
| 63 | |
| 64 | @param name: The candidate test names to check. |
| 65 | @return True if name is a valid test name and false otherwise. |
| 66 | |
| 67 | """ |
| 68 | return not '/' in name and not name.startswith('try_new_image') |
| 69 | |
| 70 | |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 71 | def prepare_last_passes(last_passes): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 72 | """ |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 73 | Fix up the last passes so they can be used by the system. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 74 | |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 75 | This filters out invalid test names and converts the test names to utf8 |
| 76 | encoding. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 77 | |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 78 | @param last_passes: The dictionary of test_name:last_pass pairs. |
| 79 | |
| 80 | @return: Valid entries in encoded as utf8 strings. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 81 | """ |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 82 | valid_test_names = filter(is_valid_test_name, last_passes) |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 83 | # The shelve module does not accept Unicode objects as keys but does |
| 84 | # accept utf-8 strings. |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 85 | return {name.encode('utf8'): last_passes[name] |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 86 | for name in valid_test_names} |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 87 | |
| 88 | |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 89 | def get_recently_ran_test_names(): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 90 | """ |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 91 | Get all the test names from the database that have been recently ran. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 92 | |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 93 | @return a set of the recently ran tests. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 94 | |
| 95 | """ |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 96 | cutoff_delta = datetime.timedelta(_DAYS_NOT_RUNNING_CUTOFF) |
| 97 | cutoff_date = datetime.datetime.today() - cutoff_delta |
| 98 | results = tko_models.Test.objects.filter( |
| 99 | started_time__gte=cutoff_date).values('test').distinct() |
Keyar Hood | 005539d | 2013-06-24 14:27:57 -0700 | [diff] [blame] | 100 | test_names = [test['test'] for test in results] |
| 101 | valid_test_names = filter(is_valid_test_name, test_names) |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 102 | return {test.encode('utf8') for test in valid_test_names} |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 103 | |
| 104 | |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 105 | def get_tests_to_analyze(): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 106 | """ |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 107 | Get all the recently ran tests as well as the last time they have passed. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 108 | |
| 109 | The minimum datetime is given as last pass time for tests that have never |
| 110 | passed. |
| 111 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 112 | @return the dict of test_name:last_finish_time pairs. |
| 113 | |
| 114 | """ |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 115 | recent_test_names = get_recently_ran_test_names() |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 116 | last_passes = utils.get_last_pass_times() |
| 117 | prepared_passes = prepare_last_passes(last_passes) |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 118 | |
| 119 | running_passes = {} |
Keyar Hood | 0f26dba | 2013-07-18 17:49:32 -0700 | [diff] [blame^] | 120 | for test, pass_time in prepared_passes.items(): |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 121 | if test in recent_test_names: |
| 122 | running_passes[test] = pass_time |
| 123 | |
| 124 | failures_names = recent_test_names.difference(running_passes) |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 125 | always_failed = {test: datetime.datetime.min for test in failures_names} |
Keyar Hood | e7b1184 | 2013-06-25 15:41:59 -0700 | [diff] [blame] | 126 | return dict(always_failed.items() + running_passes.items()) |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 127 | |
| 128 | |
Keyar Hood | 3d80768 | 2013-07-01 15:13:05 -0700 | [diff] [blame] | 129 | def store_results(tests, storage): |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 130 | """ |
Keyar Hood | 3d80768 | 2013-07-01 15:13:05 -0700 | [diff] [blame] | 131 | Store information about tests that have been failing for a long time. |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 132 | |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 133 | |
| 134 | @param tests: The test_name:time_of_last_pass pairs. |
| 135 | @param storage: The storage object. |
| 136 | |
| 137 | """ |
| 138 | failing_time_cutoff = datetime.timedelta(_DAYS_TO_BE_FAILING_TOO_LONG) |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 139 | |
| 140 | today = datetime.datetime.today() |
| 141 | for test, last_fail in tests.iteritems(): |
| 142 | if today - last_fail >= failing_time_cutoff: |
| 143 | if test not in storage: |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 144 | storage[test] = today |
| 145 | else: |
| 146 | try: |
| 147 | del storage[test] |
| 148 | except KeyError: |
| 149 | pass |
| 150 | |
Keyar Hood | 3d80768 | 2013-07-01 15:13:05 -0700 | [diff] [blame] | 151 | |
| 152 | def email_about_test_failure(storage): |
| 153 | """ |
| 154 | Send an email about all the tests in the storage object if there are any. |
| 155 | |
| 156 | @param storage: The storage object. |
| 157 | |
| 158 | """ |
Keyar Hood | 4d5f9d1 | 2013-06-28 15:36:45 -0700 | [diff] [blame] | 159 | if storage: |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 160 | mail.send(_MAIL_RESULTS_FROM, |
| 161 | [_MAIL_RESULTS_TO], |
| 162 | [], |
| 163 | 'Long Failing Tests', |
| 164 | 'The following tests have been failing for ' |
| 165 | 'at least %s days:\n\n' % (_DAYS_TO_BE_FAILING_TOO_LONG) + |
Keyar Hood | 4d5f9d1 | 2013-06-28 15:36:45 -0700 | [diff] [blame] | 166 | '\n'.join(storage.keys())) |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def main(): |
| 170 | """ |
| 171 | The script code. |
| 172 | |
| 173 | Allows other python code to import and run this code. This will be more |
| 174 | important if a nice way to test this code can be determined. |
| 175 | |
| 176 | """ |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 177 | storage = load_storage() |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 178 | tests = get_tests_to_analyze() |
Keyar Hood | 3d80768 | 2013-07-01 15:13:05 -0700 | [diff] [blame] | 179 | store_results(tests, storage) |
| 180 | email_about_test_failure(storage) |
Keyar Hood | 1a3c8dd | 2013-05-29 17:41:50 -0700 | [diff] [blame] | 181 | save_storage(storage) |
| 182 | |
| 183 | return 0 |
| 184 | |
| 185 | |
| 186 | if __name__ == '__main__': |
| 187 | sys.exit(main()) |