blob: bf72c7d0de187fa3f2dbee55637892e84ca21055 [file] [log] [blame]
#!/usr/bin/python
#
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import datetime, subprocess, unittest
import mox
import common
# This must come before the import of complete_failures in order to use the
# in-memory database.
from autotest_lib.frontend import setup_django_readonly_environment
from autotest_lib.frontend import setup_test_environment
from autotest_lib.frontend.health import passing_experimental
from autotest_lib.client.common_lib import mail
from autotest_lib.frontend.afe import models as afe_models
from autotest_lib.frontend.tko import models as tko_models
from django import test
GOOD_STATUS_IDX = 6
FAIL_STATUS_IDX = 4
# During the tests there is a point where Django does a type check on
# datetime.datetime. Unfortunately this means when datetime is mocked out,
# horrible failures happen when Django tries to do this check. The solution
# chosen is to create a pure Python class that inheirits from datetime.datetime
# so that the today class method can be directly mocked out. It is necesarry
# to mock out datetime.datetime completely as it a C class and so cannot have
# parts of itself mocked out.
class MockDatetime(datetime.datetime):
"""Used to mock out parts of datetime.datetime."""
pass
class PassingExperimentalFunctionalTests(mox.MoxTestBase, test.TestCase):
"""
Does a functional test of the passing_experimental.py script.
It uses an in-memory database, mocks out the saving and loading of the
storage object and mocks out the sending of the email. Everything else
is a full run.
"""
def setUp(self):
super(PassingExperimentalFunctionalTests, self).setUp()
setup_test_environment.set_up()
# All of our tests will involve mocking out the datetime.today() class
# method.
self.mox.StubOutWithMock(MockDatetime, 'today')
self.datetime = datetime.datetime
datetime.datetime = MockDatetime
# We need to mock out the send function in all tests or else the
# emails will be sent out during tests.
self.mox.StubOutWithMock(mail, 'send')
# We really do not want a script that modifies the DB to run during
# testing. So we will mock this out even though we will mock out the
# function that calls it in case of refactoring.
self.mox.StubOutWithMock(subprocess, 'call')
self.mox.StubOutWithMock(passing_experimental,
'update_afe_autotests_table')
self._orig_since_failure = passing_experimental._MIN_DAYS_SINCE_FAILURE
self._orig_since_pass = passing_experimental._MAX_DAYS_SINCE_LAST_PASS
def tearDown(self):
passing_experimental._MAX_DAYS_SINCE_LAST_PASS = self._orig_since_pass
passing_experimental._MIN_DAYS_SINCE_FAILURE = self._orig_since_failure
datetime.datetime = self.datetime
setup_test_environment.tear_down()
super(PassingExperimentalFunctionalTests, self).tearDown()
def test(self):
"""Does a basic test of as much of the system as possible."""
afe_models.Test(name='test1', test_type=0, path='test1',
experimental=True).save()
afe_models.Test(name='test2', test_type=0, path='test2',
experimental=True).save()
tko_models.Status(status_idx=6, word='GOOD').save()
job = tko_models.Job(job_idx=1)
kernel = tko_models.Kernel(kernel_idx=1)
machine = tko_models.Machine(machine_idx=1)
success_status = tko_models.Status(status_idx=GOOD_STATUS_IDX)
fail_status = tko_models.Status(status_idx=FAIL_STATUS_IDX)
tko_test1 = tko_models.Test(job=job, status=success_status,
kernel=kernel, machine=machine,
test='test1',
started_time=self.datetime(2012, 1, 20))
tko_test1.save()
tko_test2 = tko_models.Test(job=job, status=success_status,
kernel=kernel, machine=machine,
test='test2',
started_time=self.datetime(2012, 1, 20))
tko_test2.save()
passing_experimental._MAX_DAYS_SINCE_LAST_PASS = 10
passing_experimental._MIN_DAYS_SINCE_FAILURE = 10
passing_experimental.update_afe_autotests_table()
MockDatetime.today().AndReturn(self.datetime(2012, 1, 21))
MockDatetime.today().AndReturn(self.datetime(2012, 1, 21))
mail.send('chromeos-test-health@google.com',
['chromeos-lab-infrastructure@google.com'],
[],
'Long Passing Experimental Tests',
'The following experimental tests have been passing for at '
'least %i days:\n\ntest1\ntest2'
% passing_experimental._MIN_DAYS_SINCE_FAILURE)
self.mox.ReplayAll()
passing_experimental.main()
if __name__ == '__main__':
unittest.main()