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 | |
Keyar Hood | 2ff7b41 | 2013-08-20 11:48:06 -0700 | [diff] [blame] | 8 | import argparse, datetime, sys |
Keyar Hood | 30d94bd | 2013-07-23 10:41:12 -0700 | [diff] [blame] | 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 | |
Keyar Hood | 30d94bd | 2013-07-23 10:41:12 -0700 | [diff] [blame] | 29 | def get_experimental_tests(): |
| 30 | """ |
| 31 | Get all the tests marked experimental from the afe_autotests table. |
| 32 | |
| 33 | @return the set of experimental test names. |
| 34 | |
| 35 | """ |
| 36 | entries = afe_models.Test.objects.values('name').filter(experimental=True) |
| 37 | return {entry['name'] for entry in entries} |
| 38 | |
| 39 | |
| 40 | def find_long_passing_tests(pass_times, fail_times, valid_names): |
| 41 | """ |
| 42 | Determine the experimental tests that have been passsing for a long time. |
| 43 | |
| 44 | @param pass_times: The dictionary of test_name:pass_time pairs. |
| 45 | @param fail_times: The dictionary of test_name:fail_time pairs. |
| 46 | @param valid_names: An iterable of experimental test names. |
| 47 | |
| 48 | @return the list of experimental test names that have been passing for a |
| 49 | long time. |
| 50 | |
| 51 | """ |
| 52 | failure_cutoff_date = (datetime.datetime.today() - |
| 53 | datetime.timedelta(_MIN_DAYS_SINCE_FAILURE)) |
| 54 | pass_cutoff_date = (datetime.datetime.today() - |
| 55 | datetime.timedelta(_MAX_DAYS_SINCE_LAST_PASS)) |
| 56 | |
| 57 | valid_passes = {test for test in valid_names if test in pass_times} |
| 58 | valid_failures = {test for test in valid_names if test in fail_times} |
| 59 | |
| 60 | recent_passes = {test for test in valid_passes |
| 61 | if (pass_times[test] > pass_cutoff_date)} |
| 62 | recent_fails = {test for test in valid_failures |
| 63 | if (fail_times[test] > failure_cutoff_date)} |
| 64 | |
| 65 | return recent_passes - recent_fails |
| 66 | |
| 67 | |
Keyar Hood | 2ff7b41 | 2013-08-20 11:48:06 -0700 | [diff] [blame] | 68 | def parse_options(args): |
| 69 | """Parse the command line options.""" |
| 70 | |
| 71 | description = ('Collects information about which experimental tests ' |
| 72 | 'have been passing for a long time and creates an email ' |
| 73 | 'summarizing the results.') |
| 74 | parser = argparse.ArgumentParser(description=description) |
| 75 | parser.parse_args(args) |
| 76 | |
| 77 | |
| 78 | def main(args=None): |
Keyar Hood | 30d94bd | 2013-07-23 10:41:12 -0700 | [diff] [blame] | 79 | """ |
| 80 | The script code. |
| 81 | |
| 82 | Allows other python code to import and run this code. This will be more |
| 83 | important if a nice way to test this code can be determined. |
| 84 | |
Keyar Hood | 2ff7b41 | 2013-08-20 11:48:06 -0700 | [diff] [blame] | 85 | @param args: The command line arguments being passed in. |
| 86 | |
Keyar Hood | 30d94bd | 2013-07-23 10:41:12 -0700 | [diff] [blame] | 87 | """ |
Keyar Hood | 2ff7b41 | 2013-08-20 11:48:06 -0700 | [diff] [blame] | 88 | args = [] if args is None else args |
| 89 | parse_options(args) |
| 90 | |
Keyar Hood | 30d94bd | 2013-07-23 10:41:12 -0700 | [diff] [blame] | 91 | experimental_tests = get_experimental_tests() |
| 92 | pass_times = utils.get_last_pass_times() |
| 93 | fail_times = utils.get_last_fail_times() |
| 94 | |
| 95 | long_passers = find_long_passing_tests(pass_times, fail_times, |
| 96 | experimental_tests) |
| 97 | |
| 98 | if long_passers: |
| 99 | mail.send(_MAIL_RESULTS_FROM, |
| 100 | [_MAIL_RESULTS_TO], |
| 101 | [], |
| 102 | 'Long Passing Experimental Tests', |
| 103 | 'The following experimental tests have been passing for at ' |
| 104 | 'least %i days:\n\n%s' |
| 105 | % (_MIN_DAYS_SINCE_FAILURE, '\n'.join(sorted(long_passers)))) |
| 106 | |
| 107 | return 0 |
| 108 | |
| 109 | |
| 110 | if __name__ == '__main__': |
Keyar Hood | 33ca53b | 2013-08-21 16:04:01 -0700 | [diff] [blame] | 111 | sys.exit(main(sys.argv[1:])) |