blob: 17eb20992d8120828585481c2c7a9568b41b28a8 [file] [log] [blame]
Keyar Hood1a3c8dd2013-05-29 17:41:50 -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
7import datetime, unittest
8
9import mox
10
Keyar Hood788e4982013-06-18 14:34:28 -070011import common
12# This must come before the import of complete_failures in order to use the
13# in memory database.
14from autotest_lib.frontend import setup_django_readonly_environment
15from autotest_lib.frontend import setup_test_environment
16import complete_failures
Keyar Hood0f26dba2013-07-18 17:49:32 -070017from autotest_lib.frontend.health import utils
Keyar Hood1a3c8dd2013-05-29 17:41:50 -070018from autotest_lib.client.common_lib import mail
Keyar Hood788e4982013-06-18 14:34:28 -070019from autotest_lib.frontend.tko import models
20from django import test
Keyar Hood1a3c8dd2013-05-29 17:41:50 -070021
22
Keyar Hood788e4982013-06-18 14:34:28 -070023GOOD_STATUS_IDX = 6
Keyar Hood788e4982013-06-18 14:34:28 -070024
Keyar Hoode7b11842013-06-25 15:41:59 -070025# See complte_failurs_functional_tests.py for why we need this.
26class MockDatetime(datetime.datetime):
27 """Used to mock out parts of datetime.datetime."""
28 pass
29
30
Keyar Hood3d807682013-07-01 15:13:05 -070031class StoreResultsTests(mox.MoxTestBase):
32 """Test that entries are properly stored in the storage object."""
33
34 def setUp(self):
35 super(StoreResultsTests, self).setUp()
36 self._orig_too_long = complete_failures._DAYS_TO_BE_FAILING_TOO_LONG
37
38
39 def tearDown(self):
40 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = self._orig_too_long
41 super(StoreResultsTests, self).tearDown()
42
43
44 def test_add_failing_test(self):
45 """Test adding a failing test to storage."""
46 # We will want to keep all the datetime logic intact and so we need to
47 # keep a reference to the unmocked datetime.
48 self.datetime = datetime.datetime
49 self.mox.StubOutWithMock(datetime, 'datetime')
50 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 1))
51 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
52
53 storage = {}
54
55 self.mox.ReplayAll()
56 complete_failures.store_results(
57 {'test': datetime.datetime.min}, storage)
58
59 self.assertEqual(storage['test'], self.datetime(2012, 1, 1))
60
61
62 def test_remove_test_if_it_has_succeeded_recently_enough(self):
63 """Test that we remove a passing test from the storage object."""
64 storage = {'test': datetime.datetime(2012, 1, 1)}
65 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
66 today = datetime.datetime(2012, 4, 10)
67 safe_date = datetime.datetime(2012, 4, 9)
68
69 self.mox.StubOutWithMock(datetime, 'datetime')
70 datetime.datetime.today().AndReturn(today)
71
72 self.mox.ReplayAll()
73 complete_failures.store_results({'test': safe_date}, storage)
74
75 self.assertTrue('test' not in storage)
76
77
78 def test_no_crashing_on_test_that_has_never_failed_for_too_long(self):
79 """Test that we do not crash for tests that have always passed."""
80 storage = {}
81 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
82 today = datetime.datetime(2012, 4, 10)
83 safe_date = datetime.datetime(2012, 4, 9)
84
85 self.mox.StubOutWithMock(datetime, 'datetime')
86 datetime.datetime.today().AndReturn(today)
87
88 self.mox.ReplayAll()
89 complete_failures.store_results({'test': safe_date}, storage)
90
91 self.assertTrue('test' not in storage)
92
93
94 def test_do_not_delete_if_still_failing(self):
95 """Test that an old failing test is not removed from storage."""
96 # We will want to keep all the datetime logic intact and so we need to
97 # keep a reference to the unmocked datetime.
98 self.datetime = datetime.datetime
99 today = datetime.datetime(2012, 1, 1)
100 self.mox.StubOutWithMock(datetime, 'datetime')
101 datetime.datetime.today().AndReturn(today)
102
103 storage = {'test': datetime.datetime.min}
104
105 # The ReplayAll is required or else a mox object sneaks its way into
106 # the storage object somehow.
107 self.mox.ReplayAll()
108 complete_failures.store_results(
109 {'test': datetime.datetime.min}, storage)
110
111 self.assertTrue('test' in storage)
112
113
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700114class EmailAboutTestFailureTests(mox.MoxTestBase):
115 """
Keyar Hood3d807682013-07-01 15:13:05 -0700116 Tests that emails are sent about failed tests.
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700117
Keyar Hood3d807682013-07-01 15:13:05 -0700118 This currently means an email is sent about all the entries of the
119 storage object.
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700120
121 """
122 def setUp(self):
123 super(EmailAboutTestFailureTests, self).setUp()
124
125 # We need to mock out the send function in all tests or else the
126 # emails will be sent out during tests.
127 self.mox.StubOutWithMock(mail, 'send')
128
Keyar Hoode7b11842013-06-25 15:41:59 -0700129 self._orig_too_long = complete_failures._DAYS_TO_BE_FAILING_TOO_LONG
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700130
131
132 def tearDown(self):
Keyar Hoode7b11842013-06-25 15:41:59 -0700133 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = self._orig_too_long
Keyar Hood788e4982013-06-18 14:34:28 -0700134 super(EmailAboutTestFailureTests, self).tearDown()
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700135
136
Keyar Hood3d807682013-07-01 15:13:05 -0700137 def test_email_sent_about_all_entries_in_storage(self):
138 """Test that the email report mentions all the entries in storage."""
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700139 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
140
141 mail.send(
142 'chromeos-test-health@google.com',
143 ['chromeos-lab-infrastructure@google.com'],
144 [],
145 'Long Failing Tests',
146 'The following tests have been failing for at '
147 'least %i days:\n\ntest'
148 % complete_failures._DAYS_TO_BE_FAILING_TOO_LONG)
149
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700150 storage = {'test': datetime.datetime.min}
151
152 # The ReplayAll is required or else a mox object sneaks its way into
153 # the storage object somehow.
154 self.mox.ReplayAll()
Keyar Hood3d807682013-07-01 15:13:05 -0700155 complete_failures.email_about_test_failure(storage)
Keyar Hood788e4982013-06-18 14:34:28 -0700156
157
Keyar Hood005539d2013-06-24 14:27:57 -0700158class IsValidTestNameTests(test.TestCase):
159 """Tests the is_valid_test_name function."""
160
161 def test_returns_true_for_valid_test_name(self):
162 """Test that a valid test name returns True."""
163 name = 'TestName.TestName'
164 self.assertTrue(complete_failures.is_valid_test_name(name))
165
166
167 def test_returns_false_if_name_has_slash_in_it(self):
168 """Test that a name with a slash in it returns False."""
169 name = 'path/to/test'
170 self.assertFalse(complete_failures.is_valid_test_name(name))
171
172
173 def test_returns_false_for_try_new_image_entries(self):
174 """Test that a name that starts with try_new_image returns False."""
175 name = 'try_new_image-blah'
176 self.assertFalse(complete_failures.is_valid_test_name(name))
177
178
Keyar Hood0f26dba2013-07-18 17:49:32 -0700179class PrepareLastPassesTests(test.TestCase):
180 """Tests the prepare_last_passes function."""
Keyar Hood788e4982013-06-18 14:34:28 -0700181
182 def setUp(self):
Keyar Hood0f26dba2013-07-18 17:49:32 -0700183 super(PrepareLastPassesTests, self).setUp()
Keyar Hood788e4982013-06-18 14:34:28 -0700184
185 def tearDown(self):
Keyar Hood0f26dba2013-07-18 17:49:32 -0700186 super(PrepareLastPassesTests, self).tearDown()
Keyar Hood788e4982013-06-18 14:34:28 -0700187
Keyar Hood005539d2013-06-24 14:27:57 -0700188 def test_does_not_return_invalid_test_names(self):
189 """Tests that tests with invalid test names are not returned."""
Keyar Hood0f26dba2013-07-18 17:49:32 -0700190 results = complete_failures.prepare_last_passes(['invalid_test/name'])
Keyar Hood005539d2013-06-24 14:27:57 -0700191
Keyar Hood0f26dba2013-07-18 17:49:32 -0700192 self.assertEqual(results, {})
Keyar Hood005539d2013-06-24 14:27:57 -0700193
Keyar Hood005539d2013-06-24 14:27:57 -0700194
195
Keyar Hoode7b11842013-06-25 15:41:59 -0700196class GetRecentlyRanTestNamesTests(mox.MoxTestBase, test.TestCase):
197 """Tests the get_recently_ran_test_names function."""
Keyar Hood788e4982013-06-18 14:34:28 -0700198
199 def setUp(self):
Keyar Hoode7b11842013-06-25 15:41:59 -0700200 super(GetRecentlyRanTestNamesTests, self).setUp()
201 self.mox.StubOutWithMock(MockDatetime, 'today')
202 self.datetime = datetime.datetime
203 datetime.datetime = MockDatetime
Keyar Hood788e4982013-06-18 14:34:28 -0700204 setup_test_environment.set_up()
Keyar Hoode7b11842013-06-25 15:41:59 -0700205 self._orig_cutoff = complete_failures._DAYS_NOT_RUNNING_CUTOFF
Keyar Hood788e4982013-06-18 14:34:28 -0700206
207
208 def tearDown(self):
Keyar Hoode7b11842013-06-25 15:41:59 -0700209 datetime.datetime = self.datetime
210 complete_failures._DAYS_NOT_RUNNING_CUTOFF = self._orig_cutoff
Keyar Hood788e4982013-06-18 14:34:28 -0700211 setup_test_environment.tear_down()
Keyar Hoode7b11842013-06-25 15:41:59 -0700212 super(GetRecentlyRanTestNamesTests, self).tearDown()
Keyar Hood788e4982013-06-18 14:34:28 -0700213
214
Keyar Hoode7b11842013-06-25 15:41:59 -0700215 def test_return_all_recently_ran_tests(self):
Keyar Hood788e4982013-06-18 14:34:28 -0700216 """Test that the function does as it says it does."""
Keyar Hood005539d2013-06-24 14:27:57 -0700217 job = models.Job(job_idx=1)
218 kernel = models.Kernel(kernel_idx=1)
219 machine = models.Machine(machine_idx=1)
220 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700221
Keyar Hoode7b11842013-06-25 15:41:59 -0700222 recent = models.Test(job=job, status=success_status,
223 kernel=kernel, machine=machine,
224 test='recent',
225 started_time=self.datetime(2012, 1, 1))
226 recent.save()
227 old = models.Test(job=job, status=success_status,
228 kernel=kernel, machine=machine,
229 test='old',
230 started_time=self.datetime(2011, 1, 2))
231 old.save()
Keyar Hood788e4982013-06-18 14:34:28 -0700232
Keyar Hoode7b11842013-06-25 15:41:59 -0700233 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 4))
234 complete_failures._DAYS_NOT_RUNNING_CUTOFF = 60
Keyar Hood788e4982013-06-18 14:34:28 -0700235
Keyar Hoode7b11842013-06-25 15:41:59 -0700236 self.mox.ReplayAll()
237 results = complete_failures.get_recently_ran_test_names()
238
239 self.assertEqual(set(results), set(['recent']))
Keyar Hood788e4982013-06-18 14:34:28 -0700240
241
242 def test_returns_no_duplicate_names(self):
243 """Test that each test name appears only once."""
Keyar Hood005539d2013-06-24 14:27:57 -0700244 job = models.Job(job_idx=1)
245 kernel = models.Kernel(kernel_idx=1)
246 machine = models.Machine(machine_idx=1)
247 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700248
Keyar Hood005539d2013-06-24 14:27:57 -0700249 test = models.Test(job=job, status=success_status,
250 kernel=kernel, machine=machine,
251 test='test',
Keyar Hoode7b11842013-06-25 15:41:59 -0700252 started_time=self.datetime(2012, 1, 1))
Keyar Hood788e4982013-06-18 14:34:28 -0700253 test.save()
Keyar Hood005539d2013-06-24 14:27:57 -0700254 duplicate = models.Test(job=job, status=success_status,
255 kernel=kernel, machine=machine,
256 test='test',
Keyar Hoode7b11842013-06-25 15:41:59 -0700257 started_time=self.datetime(2012, 1, 2))
Keyar Hood788e4982013-06-18 14:34:28 -0700258 duplicate.save()
259
Keyar Hoode7b11842013-06-25 15:41:59 -0700260 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 3))
261 complete_failures._DAYS_NOT_RUNNING_CUTOFF = 60
262
263 self.mox.ReplayAll()
264 results = complete_failures.get_recently_ran_test_names()
Keyar Hood788e4982013-06-18 14:34:28 -0700265
266 self.assertEqual(len(results), 1)
267
268
269class GetTestsToAnalyzeTests(mox.MoxTestBase):
270 """Tests the get_tests_to_analyze function."""
271
Keyar Hoode7b11842013-06-25 15:41:59 -0700272 def test_returns_recent_test_names(self):
Keyar Hood788e4982013-06-18 14:34:28 -0700273 """Test should return all the test names in the database."""
Keyar Hood0f26dba2013-07-18 17:49:32 -0700274 self.mox.StubOutWithMock(utils, 'get_last_pass_times')
Keyar Hoode7b11842013-06-25 15:41:59 -0700275 self.mox.StubOutWithMock(complete_failures,
276 'get_recently_ran_test_names')
Keyar Hood788e4982013-06-18 14:34:28 -0700277
Keyar Hood0f26dba2013-07-18 17:49:32 -0700278 utils.get_last_pass_times().AndReturn({'passing_test':
Keyar Hoode7b11842013-06-25 15:41:59 -0700279 datetime.datetime(2012, 1 ,1),
280 'old_passing_test': datetime.datetime(2011, 1, 1)})
281 complete_failures.get_recently_ran_test_names().AndReturn(
282 {'passing_test',
283 'failing_test'})
Keyar Hood788e4982013-06-18 14:34:28 -0700284 self.mox.ReplayAll()
285 results = complete_failures.get_tests_to_analyze()
286
Keyar Hoode7b11842013-06-25 15:41:59 -0700287 self.assertEqual(results,
288 {'passing_test': datetime.datetime(2012, 1, 1),
289 'failing_test': datetime.datetime.min})
Keyar Hood788e4982013-06-18 14:34:28 -0700290
291
292 def test_returns_failing_tests_with_min_datetime(self):
293 """Test that never-passed tests are paired with datetime.min."""
Keyar Hood0f26dba2013-07-18 17:49:32 -0700294 self.mox.StubOutWithMock(utils, 'get_last_pass_times')
Keyar Hoode7b11842013-06-25 15:41:59 -0700295 self.mox.StubOutWithMock(complete_failures,
296 'get_recently_ran_test_names')
Keyar Hood788e4982013-06-18 14:34:28 -0700297
Keyar Hood0f26dba2013-07-18 17:49:32 -0700298 utils.get_last_pass_times().AndReturn({})
Keyar Hoode7b11842013-06-25 15:41:59 -0700299 complete_failures.get_recently_ran_test_names().AndReturn({'test'})
Keyar Hood788e4982013-06-18 14:34:28 -0700300
301 self.mox.ReplayAll()
302 results = complete_failures.get_tests_to_analyze()
303
304 self.assertEqual(results, {'test': datetime.datetime.min})
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700305
306
307if __name__ == '__main__':
308 unittest.main()