Keyar Hood | 30d94bd | 2013-07-23 10:41:12 -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, os, subprocess, sys |
| 9 | |
| 10 | import common |
| 11 | from autotest_lib.client.common_lib import mail |
| 12 | from autotest_lib.frontend import setup_django_readonly_environment |
| 13 | |
| 14 | # Django and the models are only setup after |
| 15 | # the setup_django_readonly_environment module is imported. |
| 16 | from autotest_lib.frontend.afe import models as afe_models |
| 17 | from autotest_lib.frontend.health import utils |
| 18 | |
| 19 | |
| 20 | # Keep tests that have not failed for at least this many days. |
| 21 | _MIN_DAYS_SINCE_FAILURE = 30 |
| 22 | # Ignore any tests that have not passed in this many days. |
| 23 | _MAX_DAYS_SINCE_LAST_PASS = 30 |
| 24 | |
| 25 | _MAIL_RESULTS_FROM = 'chromeos-test-health@google.com' |
| 26 | _MAIL_RESULTS_TO = 'chromeos-lab-infrastructure@google.com' |
| 27 | |
| 28 | |
| 29 | def update_afe_autotests_table(): |
| 30 | """Runs the test_importer.py script to update the afe_autotests table.""" |
| 31 | dirname = os.path.dirname(__file__) |
| 32 | utils_dir = os.path.abspath(os.path.join(dirname, os.pardir, os.pardir, |
| 33 | 'utils')) |
| 34 | test_importer_script = os.path.join(utils_dir, 'test_importer.py') |
| 35 | return_code = subprocess.call([test_importer_script]) |
| 36 | |
| 37 | if return_code != 0: |
| 38 | logging.warn('Update DB failed: ' |
| 39 | 'test_importer.py had nonzero return code %d.', |
| 40 | return_code) |
| 41 | |
| 42 | |
| 43 | def get_experimental_tests(): |
| 44 | """ |
| 45 | Get all the tests marked experimental from the afe_autotests table. |
| 46 | |
| 47 | @return the set of experimental test names. |
| 48 | |
| 49 | """ |
| 50 | entries = afe_models.Test.objects.values('name').filter(experimental=True) |
| 51 | return {entry['name'] for entry in entries} |
| 52 | |
| 53 | |
| 54 | def find_long_passing_tests(pass_times, fail_times, valid_names): |
| 55 | """ |
| 56 | Determine the experimental tests that have been passsing for a long time. |
| 57 | |
| 58 | @param pass_times: The dictionary of test_name:pass_time pairs. |
| 59 | @param fail_times: The dictionary of test_name:fail_time pairs. |
| 60 | @param valid_names: An iterable of experimental test names. |
| 61 | |
| 62 | @return the list of experimental test names that have been passing for a |
| 63 | long time. |
| 64 | |
| 65 | """ |
| 66 | failure_cutoff_date = (datetime.datetime.today() - |
| 67 | datetime.timedelta(_MIN_DAYS_SINCE_FAILURE)) |
| 68 | pass_cutoff_date = (datetime.datetime.today() - |
| 69 | datetime.timedelta(_MAX_DAYS_SINCE_LAST_PASS)) |
| 70 | |
| 71 | valid_passes = {test for test in valid_names if test in pass_times} |
| 72 | valid_failures = {test for test in valid_names if test in fail_times} |
| 73 | |
| 74 | recent_passes = {test for test in valid_passes |
| 75 | if (pass_times[test] > pass_cutoff_date)} |
| 76 | recent_fails = {test for test in valid_failures |
| 77 | if (fail_times[test] > failure_cutoff_date)} |
| 78 | |
| 79 | return recent_passes - recent_fails |
| 80 | |
| 81 | |
| 82 | def main(): |
| 83 | """ |
| 84 | The script code. |
| 85 | |
| 86 | Allows other python code to import and run this code. This will be more |
| 87 | important if a nice way to test this code can be determined. |
| 88 | |
| 89 | """ |
| 90 | update_afe_autotests_table() |
| 91 | |
| 92 | experimental_tests = get_experimental_tests() |
| 93 | pass_times = utils.get_last_pass_times() |
| 94 | fail_times = utils.get_last_fail_times() |
| 95 | |
| 96 | long_passers = find_long_passing_tests(pass_times, fail_times, |
| 97 | experimental_tests) |
| 98 | |
| 99 | if long_passers: |
| 100 | mail.send(_MAIL_RESULTS_FROM, |
| 101 | [_MAIL_RESULTS_TO], |
| 102 | [], |
| 103 | 'Long Passing Experimental Tests', |
| 104 | 'The following experimental tests have been passing for at ' |
| 105 | 'least %i days:\n\n%s' |
| 106 | % (_MIN_DAYS_SINCE_FAILURE, '\n'.join(sorted(long_passers)))) |
| 107 | |
| 108 | return 0 |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | sys.exit(main()) |