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 7import datetime, subprocess, unittest 8 9import mox 10 11import 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 16from autotest_lib.frontend.health import passing_experimental 17from autotest_lib.frontend.afe import models as afe_models 18from autotest_lib.frontend.tko import models as tko_models 19from autotest_lib.server.cros.dynamic_suite import reporting 20from django import test 21 22 23GOOD_STATUS_IDX = 6 24FAIL_STATUS_IDX = 4 25 26# During the tests there is a point where Django does a type check on 27# datetime.datetime. Unfortunately this means when datetime is mocked out, 28# horrible failures happen when Django tries to do this check. The solution 29# chosen is to create a pure Python class that inheirits from datetime.datetime 30# so that the today class method can be directly mocked out. It is necesarry 31# to mock out datetime.datetime completely as it a C class and so cannot have 32# parts of itself mocked out. 33class MockDatetime(datetime.datetime): 34 """Used to mock out parts of datetime.datetime.""" 35 pass 36 37 38class PassingExperimentalFunctionalTests(mox.MoxTestBase, test.TestCase): 39 """ 40 Does a functional test of the passing_experimental.py script. 41 42 It uses an in-memory database, mocks out the saving and loading of the 43 storage object and mocks out the sending of the bugs. Everything else 44 is a full run. 45 46 """ 47 48 def setUp(self): 49 super(PassingExperimentalFunctionalTests, self).setUp() 50 setup_test_environment.set_up() 51 # All of our tests will involve mocking out the datetime.today() class 52 # method. 53 self.mox.StubOutWithMock(MockDatetime, 'today') 54 self.datetime = datetime.datetime 55 datetime.datetime = MockDatetime 56 # We really do not want a script that modifies the DB to run during 57 # testing. So we will mock this out even though we will mock out the 58 # function that calls it in case of refactoring. 59 self.mox.StubOutWithMock(subprocess, 'call') 60 # We need to mock out this function so bugs are not filed. 61 self.mox.StubOutClassWithMocks(reporting, 'Bug') 62 self.mox.StubOutClassWithMocks(reporting, 'Reporter') 63 self._orig_since_failure = passing_experimental._MIN_DAYS_SINCE_FAILURE 64 self._orig_since_pass = passing_experimental._MAX_DAYS_SINCE_LAST_PASS 65 66 67 def tearDown(self): 68 passing_experimental._MAX_DAYS_SINCE_LAST_PASS = self._orig_since_pass 69 passing_experimental._MIN_DAYS_SINCE_FAILURE = self._orig_since_failure 70 datetime.datetime = self.datetime 71 setup_test_environment.tear_down() 72 super(PassingExperimentalFunctionalTests, self).tearDown() 73 74 75 def test(self): 76 """Does a basic test of as much of the system as possible.""" 77 afe_models.Test(name='test1', test_type=0, path='test1', 78 experimental=True).save() 79 afe_models.Test(name='test2', test_type=0, path='test2', 80 experimental=True).save() 81 82 tko_models.Status(status_idx=6, word='GOOD').save() 83 84 job = tko_models.Job(job_idx=1) 85 kernel = tko_models.Kernel(kernel_idx=1) 86 machine = tko_models.Machine(machine_idx=1) 87 success_status = tko_models.Status(status_idx=GOOD_STATUS_IDX) 88 fail_status = tko_models.Status(status_idx=FAIL_STATUS_IDX) 89 90 tko_test1 = tko_models.Test(job=job, status=success_status, 91 kernel=kernel, machine=machine, 92 test='test1', 93 started_time=self.datetime(2012, 1, 20)) 94 tko_test1.save() 95 tko_test2 = tko_models.Test(job=job, status=success_status, 96 kernel=kernel, machine=machine, 97 test='test2', 98 started_time=self.datetime(2012, 1, 20)) 99 tko_test2.save() 100 101 passing_experimental._MAX_DAYS_SINCE_LAST_PASS = 10 102 passing_experimental._MIN_DAYS_SINCE_FAILURE = 10 103 104 MockDatetime.today().AndReturn(self.datetime(2012, 1, 21)) 105 MockDatetime.today().AndReturn(self.datetime(2012, 1, 21)) 106 reporter1 = reporting.Reporter() 107 bug1 = reporting.Bug( 108 title=u'test1 should be promoted to non-experimental.', 109 summary=mox.IgnoreArg(), 110 search_marker=u'PassingExperimental(test1)') 111 reporter1.report(bug1).AndReturn((11, 1)) 112 reporter2 = reporting.Reporter() 113 bug2 = reporting.Bug( 114 title=u'test2 should be promoted to non-experimental.', 115 summary=mox.IgnoreArg(), 116 search_marker=u'PassingExperimental(test2)') 117 reporter2.report(bug2).AndReturn((11, 1)) 118 119 self.mox.ReplayAll() 120 passing_experimental.main() 121 122 123if __name__ == '__main__': 124 unittest.main() 125