blob: 35223106bf40f529e8522f13179e9180536b22a8 [file] [log] [blame]
Keyar Hood30d94bd2013-07-23 10:41:12 -07001#!/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 Hood2ff7b412013-08-20 11:48:06 -07008import argparse, datetime, sys
Keyar Hood30d94bd2013-07-23 10:41:12 -07009
10import common
Keyar Hood30d94bd2013-07-23 10:41:12 -070011from autotest_lib.frontend import setup_django_readonly_environment
Keyar Hood551b37e2013-08-21 15:17:31 -070012from autotest_lib.server.cros.dynamic_suite import reporting
Keyar Hood30d94bd2013-07-23 10:41:12 -070013
14# Django and the models are only setup after
15# the setup_django_readonly_environment module is imported.
16from autotest_lib.frontend.afe import models as afe_models
17from 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
Keyar Hood30d94bd2013-07-23 10:41:12 -070025
Keyar Hood30d94bd2013-07-23 10:41:12 -070026def get_experimental_tests():
27 """
28 Get all the tests marked experimental from the afe_autotests table.
29
30 @return the set of experimental test names.
31
32 """
33 entries = afe_models.Test.objects.values('name').filter(experimental=True)
34 return {entry['name'] for entry in entries}
35
36
37def find_long_passing_tests(pass_times, fail_times, valid_names):
38 """
39 Determine the experimental tests that have been passsing for a long time.
40
41 @param pass_times: The dictionary of test_name:pass_time pairs.
42 @param fail_times: The dictionary of test_name:fail_time pairs.
43 @param valid_names: An iterable of experimental test names.
44
45 @return the list of experimental test names that have been passing for a
46 long time.
47
48 """
49 failure_cutoff_date = (datetime.datetime.today() -
50 datetime.timedelta(_MIN_DAYS_SINCE_FAILURE))
51 pass_cutoff_date = (datetime.datetime.today() -
52 datetime.timedelta(_MAX_DAYS_SINCE_LAST_PASS))
53
54 valid_passes = {test for test in valid_names if test in pass_times}
55 valid_failures = {test for test in valid_names if test in fail_times}
56
57 recent_passes = {test for test in valid_passes
58 if (pass_times[test] > pass_cutoff_date)}
59 recent_fails = {test for test in valid_failures
60 if (fail_times[test] > failure_cutoff_date)}
61
62 return recent_passes - recent_fails
63
64
Keyar Hood2ff7b412013-08-20 11:48:06 -070065def parse_options(args):
66 """Parse the command line options."""
67
68 description = ('Collects information about which experimental tests '
Keyar Hood551b37e2013-08-21 15:17:31 -070069 'have been passing for a long time and creates a bug '
70 'report for each one.')
Keyar Hood2ff7b412013-08-20 11:48:06 -070071 parser = argparse.ArgumentParser(description=description)
72 parser.parse_args(args)
73
74
Keyar Hood551b37e2013-08-21 15:17:31 -070075def submit_bug_reports(tests):
76 """
77 Submits bug reports to make the long passing tests as not experimental.
78
79 @param tests: The tests that need to be marked as not experimental.
80 """
81 reporter = reporting.Reporter()
82
83 for test in tests:
84 title = '%s should be promoted to non-experimental.' % test
85 summary = ('This bug has been automatically filed to track the '
86 'following issue:\n\n'
87 'Test: %s\n'
88 'Issue: Promote to non-experimental as it has been passing '
89 'for at least %d days.\n'
90 'Suggested Actions: Navigate to the test\'s control file '
91 'and remove the EXPERIMENTAL flag.\n'
92 '\tSee http://www.chromium.org/chromium-os/testing/'
93 'autotest-best-practices#TOC-Control-files' %
94 (test, _MIN_DAYS_SINCE_FAILURE))
95 search_marker = 'PassingExperimental(%s)' % test
96 bug = reporting.Bug(title=title, summary=summary,
97 search_marker=search_marker)
98 reporter.report(bug)
99
100
Keyar Hood2ff7b412013-08-20 11:48:06 -0700101def main(args=None):
Keyar Hood30d94bd2013-07-23 10:41:12 -0700102 """
103 The script code.
104
105 Allows other python code to import and run this code. This will be more
106 important if a nice way to test this code can be determined.
107
Keyar Hood2ff7b412013-08-20 11:48:06 -0700108 @param args: The command line arguments being passed in.
109
Keyar Hood30d94bd2013-07-23 10:41:12 -0700110 """
Keyar Hood2ff7b412013-08-20 11:48:06 -0700111 args = [] if args is None else args
112 parse_options(args)
113
Keyar Hood30d94bd2013-07-23 10:41:12 -0700114 experimental_tests = get_experimental_tests()
115 pass_times = utils.get_last_pass_times()
116 fail_times = utils.get_last_fail_times()
117
118 long_passers = find_long_passing_tests(pass_times, fail_times,
119 experimental_tests)
120
Keyar Hood551b37e2013-08-21 15:17:31 -0700121 submit_bug_reports(long_passers)
Keyar Hood30d94bd2013-07-23 10:41:12 -0700122
123 return 0
124
125
126if __name__ == '__main__':
Keyar Hood33ca53b2013-08-21 16:04:01 -0700127 sys.exit(main(sys.argv[1:]))