blob: bce06e7bc93c8f4f83305b5f868c2c174ccbe29b [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 Hood1a3c8dd2013-05-29 17:41:50 -070017from autotest_lib.client.common_lib import mail
Keyar Hood788e4982013-06-18 14:34:28 -070018from autotest_lib.frontend.tko import models
19from django import test
Keyar Hood1a3c8dd2013-05-29 17:41:50 -070020
21
Keyar Hood788e4982013-06-18 14:34:28 -070022GOOD_STATUS_IDX = 6
23FAIL_STATUS_IDX = 4
24
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 Hood1a3c8dd2013-05-29 17:41:50 -070031class EmailAboutTestFailureTests(mox.MoxTestBase):
32 """
33 Test the core logic of the comlete_failures.py script.
34
35 The core logic is to send emails only if we have not yet done so for a
36 given test before and to take actions if the test has been failing for
37 long enough.
38
39 """
40 def setUp(self):
41 super(EmailAboutTestFailureTests, self).setUp()
42
43 # We need to mock out the send function in all tests or else the
44 # emails will be sent out during tests.
45 self.mox.StubOutWithMock(mail, 'send')
46
Keyar Hoode7b11842013-06-25 15:41:59 -070047 self._orig_too_long = complete_failures._DAYS_TO_BE_FAILING_TOO_LONG
Keyar Hood1a3c8dd2013-05-29 17:41:50 -070048
49
50 def tearDown(self):
Keyar Hoode7b11842013-06-25 15:41:59 -070051 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = self._orig_too_long
Keyar Hood788e4982013-06-18 14:34:28 -070052 super(EmailAboutTestFailureTests, self).tearDown()
Keyar Hood1a3c8dd2013-05-29 17:41:50 -070053
54
55 def test_deal_with_new_failing_test(self):
56 """
57 Test adding a failing test to the storage.
58
59 We expect the email sending code to be called if it is added.
60
61 """
62 # We will want to keep all the datetime logic intact and so we need to
63 # keep a reference to the unmocked datetime.
64 self.datetime = datetime.datetime
65 self.mox.StubOutWithMock(datetime, 'datetime')
66 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 1))
67 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
68
69 mail.send(
70 'chromeos-test-health@google.com',
71 ['chromeos-lab-infrastructure@google.com'],
72 [],
73 'Long Failing Tests',
74 'The following tests have been failing for at '
75 'least %i days:\n\ntest'
76 % complete_failures._DAYS_TO_BE_FAILING_TOO_LONG)
77
78 storage = {}
79
80 # The ReplayAll is required or else a mox object sneaks its way into
81 # the storage object somehow.
82 self.mox.ReplayAll()
83 complete_failures.email_about_test_failure(
84 {'test': datetime.datetime.min}, storage)
85
86 self.assertEqual(storage['test'], self.datetime(2012, 1, 1))
Keyar Hood1a3c8dd2013-05-29 17:41:50 -070087
88
89 def test_remove_test_if_it_has_succeeded_recently_enough(self):
90 """Test that we remove a passing test from the storage object."""
91 storage = {'test': datetime.datetime(2012, 1, 1)}
92 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
93 today = datetime.datetime(2012, 4, 10)
94 safe_date = datetime.datetime(2012, 4, 9)
95
96 self.mox.StubOutWithMock(datetime, 'datetime')
97 datetime.datetime.today().AndReturn(today)
98
99 self.mox.ReplayAll()
100 complete_failures.email_about_test_failure({'test': safe_date}, storage)
101
102 self.assertTrue('test' not in storage)
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700103
104
105 def test_no_crashing_on_test_that_has_never_failed_for_too_long(self):
106 """Test that we do not crash for tests that have always passed."""
107 storage = {}
108 complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
109 today = datetime.datetime(2012,4,10)
110 safe_date = datetime.datetime(2012,4,9)
111
112 self.mox.StubOutWithMock(datetime, 'datetime')
113 datetime.datetime.today().AndReturn(today)
114
115 self.mox.ReplayAll()
116 complete_failures.email_about_test_failure({'test': safe_date}, storage)
117
118 self.assertTrue('test' not in storage)
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700119
120
121 def test_do_not_send_email_if_test_already_in_storage(self):
122 """Test only send emails on newly problematic tests."""
123 storage = {'test': datetime.datetime(2012, 1, 1)}
124 self.datetime = datetime.datetime
125 self.mox.StubOutWithMock(datetime, 'datetime')
126 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 1))
127
128 self.mox.ReplayAll()
129 complete_failures.email_about_test_failure(
130 {'test': datetime.datetime.min}, storage)
131
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700132
133 def test_do_not_delete_if_still_failing(self):
134 """Test that an old failing test is not removed from storage."""
135 # We will want to keep all the datetime logic intact and so we need to
136 # keep a reference to the unmocked datetime.
137 self.datetime = datetime.datetime
138 today = datetime.datetime(2012, 1, 1)
139 self.mox.StubOutWithMock(datetime, 'datetime')
140 datetime.datetime.today().AndReturn(today)
141
142 storage = {'test': datetime.datetime.min}
143
144 # The ReplayAll is required or else a mox object sneaks its way into
145 # the storage object somehow.
146 self.mox.ReplayAll()
147 complete_failures.email_about_test_failure(
148 {'test': datetime.datetime.min}, storage)
149
150 self.assertTrue('test' in storage)
Keyar Hood788e4982013-06-18 14:34:28 -0700151
152
Keyar Hood005539d2013-06-24 14:27:57 -0700153class IsValidTestNameTests(test.TestCase):
154 """Tests the is_valid_test_name function."""
155
156 def test_returns_true_for_valid_test_name(self):
157 """Test that a valid test name returns True."""
158 name = 'TestName.TestName'
159 self.assertTrue(complete_failures.is_valid_test_name(name))
160
161
162 def test_returns_false_if_name_has_slash_in_it(self):
163 """Test that a name with a slash in it returns False."""
164 name = 'path/to/test'
165 self.assertFalse(complete_failures.is_valid_test_name(name))
166
167
168 def test_returns_false_for_try_new_image_entries(self):
169 """Test that a name that starts with try_new_image returns False."""
170 name = 'try_new_image-blah'
171 self.assertFalse(complete_failures.is_valid_test_name(name))
172
173
Keyar Hood788e4982013-06-18 14:34:28 -0700174class GetLastPassTimesTests(mox.MoxTestBase, test.TestCase):
175 """Tests the get_last_pass_times function."""
176
177 def setUp(self):
178 super(GetLastPassTimesTests, self).setUp()
179 setup_test_environment.set_up()
180
181
182 def tearDown(self):
183 setup_test_environment.tear_down()
184 super(GetLastPassTimesTests, self).tearDown()
185
186
187 def test_return_most_recent_pass(self):
188 """The last time a test passed should be returned."""
189 # To add a test entry to the database, Django the test object to
190 # be instantiated with various other model instances. We give these
191 # instances dummy id values.
Keyar Hood005539d2013-06-24 14:27:57 -0700192 job = models.Job(job_idx=1)
193 kernel = models.Kernel(kernel_idx=1)
194 machine = models.Machine(machine_idx=1)
195 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700196
Keyar Hood005539d2013-06-24 14:27:57 -0700197 early_pass = models.Test(job=job, status=success_status,
198 kernel=kernel, machine=machine,
199 test='test',
200 started_time=datetime.datetime(2012, 1, 1))
Keyar Hood788e4982013-06-18 14:34:28 -0700201 early_pass.save()
Keyar Hood005539d2013-06-24 14:27:57 -0700202 late_pass = models.Test(job=job, status=success_status,
203 kernel=kernel, machine=machine,
204 test='test',
205 started_time=datetime.datetime(2012, 1, 2))
Keyar Hood788e4982013-06-18 14:34:28 -0700206 late_pass.save()
207
208 results = complete_failures.get_last_pass_times()
209
210 self.assertEquals(results, {'test': datetime.datetime(2012, 1, 2)})
211
212
213 def test_only_return_passing_tests(self):
214 """Tests that only tests that have passed at some point are returned."""
Keyar Hood005539d2013-06-24 14:27:57 -0700215 job = models.Job(job_idx=1)
216 kernel = models.Kernel(kernel_idx=1)
217 machine = models.Machine(machine_idx=1)
218 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
219 fail_status = models.Status(status_idx=FAIL_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700220
Keyar Hood005539d2013-06-24 14:27:57 -0700221 passing_test = models.Test(job=job, status=success_status,
222 kernel=kernel, machine=machine,
223 test='passing_test',
224 started_time=datetime.datetime(2012, 1, 1))
Keyar Hood788e4982013-06-18 14:34:28 -0700225 passing_test.save()
Keyar Hood005539d2013-06-24 14:27:57 -0700226 failing_test = models.Test(job=job, status=fail_status,
227 kernel=kernel, machine=machine,
228 test='failing_test',
229 started_time=datetime.datetime(2012, 1, 1))
Keyar Hood788e4982013-06-18 14:34:28 -0700230 failing_test.save()
231
232 results = complete_failures.get_last_pass_times()
233
234 self.assertEquals(results,
235 {'passing_test': datetime.datetime(2012, 1, 1)})
236
237
238 def test_return_all_passing_tests(self):
239 """This function returns all tests that passed at least once."""
Keyar Hood005539d2013-06-24 14:27:57 -0700240 job = models.Job(job_idx=1)
241 kernel = models.Kernel(kernel_idx=1)
242 machine = models.Machine(machine_idx=1)
243 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700244
Keyar Hood005539d2013-06-24 14:27:57 -0700245 test1 = models.Test(job=job, status=success_status,
246 kernel=kernel, machine=machine,
247 test='test1',
248 started_time=datetime.datetime(2012, 1, 1))
Keyar Hood788e4982013-06-18 14:34:28 -0700249 test1.save()
Keyar Hood005539d2013-06-24 14:27:57 -0700250 test2 = models.Test(job=job, status=success_status,
251 kernel=kernel, machine=machine,
252 test='test2',
253 started_time=datetime.datetime(2012, 1, 2))
Keyar Hood788e4982013-06-18 14:34:28 -0700254 test2.save()
255
256 results = complete_failures.get_last_pass_times()
257
258 self.assertEquals(results, {'test1': datetime.datetime(2012, 1, 1),
259 'test2': datetime.datetime(2012, 1, 2)})
260
261
Keyar Hood005539d2013-06-24 14:27:57 -0700262 def test_does_not_return_invalid_test_names(self):
263 """Tests that tests with invalid test names are not returned."""
264 job = models.Job(job_idx=1)
265 kernel = models.Kernel(kernel_idx=1)
266 machine = models.Machine(machine_idx=1)
267 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
268 fail_status = models.Status(status_idx=FAIL_STATUS_IDX)
269
270 invalid_test = models.Test(job=job, status=success_status,
271 kernel=kernel, machine=machine,
272 test='invalid_test/name',
273 started_time=datetime.datetime(2012, 1, 1))
274 invalid_test.save()
275
276 results = complete_failures.get_last_pass_times()
277
278 self.assertTrue(not results)
279
280
Keyar Hoode7b11842013-06-25 15:41:59 -0700281class GetRecentlyRanTestNamesTests(mox.MoxTestBase, test.TestCase):
282 """Tests the get_recently_ran_test_names function."""
Keyar Hood788e4982013-06-18 14:34:28 -0700283
284 def setUp(self):
Keyar Hoode7b11842013-06-25 15:41:59 -0700285 super(GetRecentlyRanTestNamesTests, self).setUp()
286 self.mox.StubOutWithMock(MockDatetime, 'today')
287 self.datetime = datetime.datetime
288 datetime.datetime = MockDatetime
Keyar Hood788e4982013-06-18 14:34:28 -0700289 setup_test_environment.set_up()
Keyar Hoode7b11842013-06-25 15:41:59 -0700290 self._orig_cutoff = complete_failures._DAYS_NOT_RUNNING_CUTOFF
Keyar Hood788e4982013-06-18 14:34:28 -0700291
292
293 def tearDown(self):
Keyar Hoode7b11842013-06-25 15:41:59 -0700294 datetime.datetime = self.datetime
295 complete_failures._DAYS_NOT_RUNNING_CUTOFF = self._orig_cutoff
Keyar Hood788e4982013-06-18 14:34:28 -0700296 setup_test_environment.tear_down()
Keyar Hoode7b11842013-06-25 15:41:59 -0700297 super(GetRecentlyRanTestNamesTests, self).tearDown()
Keyar Hood788e4982013-06-18 14:34:28 -0700298
299
Keyar Hoode7b11842013-06-25 15:41:59 -0700300 def test_return_all_recently_ran_tests(self):
Keyar Hood788e4982013-06-18 14:34:28 -0700301 """Test that the function does as it says it does."""
Keyar Hood005539d2013-06-24 14:27:57 -0700302 job = models.Job(job_idx=1)
303 kernel = models.Kernel(kernel_idx=1)
304 machine = models.Machine(machine_idx=1)
305 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700306
Keyar Hoode7b11842013-06-25 15:41:59 -0700307 recent = models.Test(job=job, status=success_status,
308 kernel=kernel, machine=machine,
309 test='recent',
310 started_time=self.datetime(2012, 1, 1))
311 recent.save()
312 old = models.Test(job=job, status=success_status,
313 kernel=kernel, machine=machine,
314 test='old',
315 started_time=self.datetime(2011, 1, 2))
316 old.save()
Keyar Hood788e4982013-06-18 14:34:28 -0700317
Keyar Hoode7b11842013-06-25 15:41:59 -0700318 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 4))
319 complete_failures._DAYS_NOT_RUNNING_CUTOFF = 60
Keyar Hood788e4982013-06-18 14:34:28 -0700320
Keyar Hoode7b11842013-06-25 15:41:59 -0700321 self.mox.ReplayAll()
322 results = complete_failures.get_recently_ran_test_names()
323
324 self.assertEqual(set(results), set(['recent']))
Keyar Hood788e4982013-06-18 14:34:28 -0700325
326
327 def test_returns_no_duplicate_names(self):
328 """Test that each test name appears only once."""
Keyar Hood005539d2013-06-24 14:27:57 -0700329 job = models.Job(job_idx=1)
330 kernel = models.Kernel(kernel_idx=1)
331 machine = models.Machine(machine_idx=1)
332 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
Keyar Hood788e4982013-06-18 14:34:28 -0700333
Keyar Hood005539d2013-06-24 14:27:57 -0700334 test = models.Test(job=job, status=success_status,
335 kernel=kernel, machine=machine,
336 test='test',
Keyar Hoode7b11842013-06-25 15:41:59 -0700337 started_time=self.datetime(2012, 1, 1))
Keyar Hood788e4982013-06-18 14:34:28 -0700338 test.save()
Keyar Hood005539d2013-06-24 14:27:57 -0700339 duplicate = models.Test(job=job, status=success_status,
340 kernel=kernel, machine=machine,
341 test='test',
Keyar Hoode7b11842013-06-25 15:41:59 -0700342 started_time=self.datetime(2012, 1, 2))
Keyar Hood788e4982013-06-18 14:34:28 -0700343 duplicate.save()
344
Keyar Hoode7b11842013-06-25 15:41:59 -0700345 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 3))
346 complete_failures._DAYS_NOT_RUNNING_CUTOFF = 60
347
348 self.mox.ReplayAll()
349 results = complete_failures.get_recently_ran_test_names()
Keyar Hood788e4982013-06-18 14:34:28 -0700350
351 self.assertEqual(len(results), 1)
352
353
Keyar Hood005539d2013-06-24 14:27:57 -0700354 def test_does_not_return_invalid_test_names(self):
355 """Tests that only tests with invalid test names are not returned."""
356 job = models.Job(job_idx=1)
357 kernel = models.Kernel(kernel_idx=1)
358 machine = models.Machine(machine_idx=1)
359 success_status = models.Status(status_idx=GOOD_STATUS_IDX)
360
361 invalid_test = models.Test(job=job, status=success_status,
362 kernel=kernel, machine=machine,
363 test='invalid_test/name',
Keyar Hoode7b11842013-06-25 15:41:59 -0700364 started_time=self.datetime(2012, 1, 1))
Keyar Hood005539d2013-06-24 14:27:57 -0700365 invalid_test.save()
366
Keyar Hoode7b11842013-06-25 15:41:59 -0700367 datetime.datetime.today().AndReturn(self.datetime(2012, 1, 2))
368 complete_failures._DAYS_NOT_RUNNING_CUTOFF = 60
369
370 self.mox.ReplayAll()
371 results = complete_failures.get_recently_ran_test_names()
Keyar Hood005539d2013-06-24 14:27:57 -0700372
373 self.assertTrue(not results)
374
375
Keyar Hood788e4982013-06-18 14:34:28 -0700376class GetTestsToAnalyzeTests(mox.MoxTestBase):
377 """Tests the get_tests_to_analyze function."""
378
Keyar Hoode7b11842013-06-25 15:41:59 -0700379 def test_returns_recent_test_names(self):
Keyar Hood788e4982013-06-18 14:34:28 -0700380 """Test should return all the test names in the database."""
381 self.mox.StubOutWithMock(complete_failures, 'get_last_pass_times')
Keyar Hoode7b11842013-06-25 15:41:59 -0700382 self.mox.StubOutWithMock(complete_failures,
383 'get_recently_ran_test_names')
Keyar Hood788e4982013-06-18 14:34:28 -0700384
385 complete_failures.get_last_pass_times().AndReturn({'passing_test':
Keyar Hoode7b11842013-06-25 15:41:59 -0700386 datetime.datetime(2012, 1 ,1),
387 'old_passing_test': datetime.datetime(2011, 1, 1)})
388 complete_failures.get_recently_ran_test_names().AndReturn(
389 {'passing_test',
390 'failing_test'})
Keyar Hood788e4982013-06-18 14:34:28 -0700391 self.mox.ReplayAll()
392 results = complete_failures.get_tests_to_analyze()
393
Keyar Hoode7b11842013-06-25 15:41:59 -0700394 self.assertEqual(results,
395 {'passing_test': datetime.datetime(2012, 1, 1),
396 'failing_test': datetime.datetime.min})
Keyar Hood788e4982013-06-18 14:34:28 -0700397
398
399 def test_returns_failing_tests_with_min_datetime(self):
400 """Test that never-passed tests are paired with datetime.min."""
401 self.mox.StubOutWithMock(complete_failures, 'get_last_pass_times')
Keyar Hoode7b11842013-06-25 15:41:59 -0700402 self.mox.StubOutWithMock(complete_failures,
403 'get_recently_ran_test_names')
Keyar Hood788e4982013-06-18 14:34:28 -0700404
405 complete_failures.get_last_pass_times().AndReturn({})
Keyar Hoode7b11842013-06-25 15:41:59 -0700406 complete_failures.get_recently_ran_test_names().AndReturn({'test'})
Keyar Hood788e4982013-06-18 14:34:28 -0700407
408 self.mox.ReplayAll()
409 results = complete_failures.get_tests_to_analyze()
410
411 self.assertEqual(results, {'test': datetime.datetime.min})
Keyar Hood1a3c8dd2013-05-29 17:41:50 -0700412
413
414if __name__ == '__main__':
415 unittest.main()