Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import StringIO |
| 7 | import mox |
| 8 | import unittest |
| 9 | import urllib2 |
| 10 | |
| 11 | import common |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib.cros import retry |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 13 | from autotest_lib.server import site_utils |
| 14 | from autotest_lib.server.cros.dynamic_suite import constants |
| 15 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
| 16 | from autotest_lib.server.cros.dynamic_suite import reporting |
| 17 | from autotest_lib.site_utils import phapi_lib |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 18 | # Mock the retry.retry used in the test_push before import it. |
xixuan | a96aff0 | 2016-03-29 14:22:08 -0700 | [diff] [blame^] | 19 | def mock_retry(ExceptionToCheck, timeout_min, exception_to_raise=None): |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 20 | """A mock retry decorator to use in place of the actual one for testing. |
| 21 | |
| 22 | @param ExceptionToCheck: the exception to check. |
| 23 | @param timeout_mins: Amount of time in mins to wait before timing out. |
| 24 | |
| 25 | """ |
| 26 | def inner_retry(func): |
| 27 | """The actual decorator. |
| 28 | |
| 29 | @param func: Function to be called in decorator. |
| 30 | |
| 31 | """ |
| 32 | return func |
| 33 | |
| 34 | return inner_retry |
| 35 | retry.retry = mock_retry |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 36 | from autotest_lib.site_utils import test_push |
| 37 | |
| 38 | AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT |
| 39 | |
| 40 | class TestPushUnittests(mox.MoxTestBase): |
| 41 | """Unittest for test_push script.""" |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 42 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 43 | def setUp(self): |
| 44 | """Initialize the unittest.""" |
| 45 | super(TestPushUnittests, self).setUp() |
| 46 | # Overwrite expected test results. |
| 47 | test_push.EXPECTED_TEST_RESULTS = { |
| 48 | '^SERVER_JOB$': 'GOOD', |
| 49 | '.*control.dependency$': 'TEST_NA', |
| 50 | '.*dummy_Fail.RetryFail$': 'FAIL', |
| 51 | } |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 52 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 53 | |
| 54 | def stub_out_methods(self, test_views): |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 55 | """Stub out methods in test_push module with given test results. |
| 56 | |
| 57 | @param test_views: Desired test result views. |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 58 | |
| 59 | """ |
| 60 | self.mox.UnsetStubs() |
| 61 | response = StringIO.StringIO('some_value') |
| 62 | self.mox.StubOutWithMock(urllib2, 'urlopen') |
| 63 | urllib2.urlopen(mox.IgnoreArg()).AndReturn(response) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 64 | |
Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 65 | self.mox.StubOutWithMock(test_push, 'get_default_build') |
| 66 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 67 | 'stumpy-release/R36-5881-0.0') |
Jakob Juelich | 8f14391 | 2014-10-10 14:08:05 -0700 | [diff] [blame] | 68 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 69 | 'quawks-release/R36-5881-0.0') |
Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 70 | |
Dan Shi | a8da760 | 2014-05-09 15:18:15 -0700 | [diff] [blame] | 71 | self.mox.StubOutWithMock(test_push, 'check_dut_image') |
| 72 | test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 73 | None) |
| 74 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 75 | self.mox.StubOutWithMock(test_push, 'do_run_suite') |
Jakob Juelich | 8f14391 | 2014-10-10 14:08:05 -0700 | [diff] [blame] | 76 | test_push.do_run_suite(test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), |
Shuqian Zhao | d486477 | 2015-08-06 09:46:22 -0700 | [diff] [blame] | 77 | mox.IgnoreArg(), mox.IgnoreArg() |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 78 | ).AndReturn((1)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 79 | |
| 80 | self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko') |
| 81 | self.mox.StubOutWithMock(frontend_wrappers, 'RetryingTKO') |
| 82 | frontend_wrappers.RetryingTKO(timeout_min=0.1, |
| 83 | delay_sec=10).AndReturn(None) |
| 84 | site_utils.get_test_views_from_tko(1, None).AndReturn(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def test_suite_success(self): |
| 88 | """Test test_suite method with matching results.""" |
| 89 | test_views = {'SERVER_JOB': 'GOOD', |
| 90 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 91 | 'dummy_Fail.RetryFail': 'FAIL' |
| 92 | } |
| 93 | |
| 94 | self.stub_out_methods(test_views) |
| 95 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 96 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 97 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 98 | self.mox.VerifyAll() |
| 99 | |
| 100 | |
| 101 | def test_suite_fail_with_missing_test(self): |
| 102 | """Test test_suite method that should fail with missing test.""" |
| 103 | test_views = {'SERVER_JOB': 'GOOD', |
| 104 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 105 | } |
| 106 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 107 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 108 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 109 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 110 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 111 | self.mox.VerifyAll() |
| 112 | |
| 113 | |
| 114 | def test_suite_fail_with_unexpected_test_results(self): |
| 115 | """Test test_suite method that should fail with unexpected test results. |
| 116 | """ |
| 117 | test_views = {'SERVER_JOB': 'FAIL', |
| 118 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 119 | 'dummy_Fail.RetryFail': 'FAIL', |
| 120 | } |
| 121 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 122 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 123 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 124 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 125 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 126 | self.mox.VerifyAll() |
| 127 | |
| 128 | |
| 129 | def test_suite_fail_with_extra_test(self): |
| 130 | """Test test_suite method that should fail with extra test.""" |
| 131 | test_views = {'SERVER_JOB': 'GOOD', |
| 132 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 133 | 'dummy_Fail.RetryFail': 'FAIL', |
| 134 | 'dummy_Fail.ExtraTest': 'GOOD', |
| 135 | } |
| 136 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 137 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 138 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 139 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 140 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 141 | self.mox.VerifyAll() |
| 142 | |
| 143 | |
| 144 | def test_close_bug_fail(self): |
| 145 | """Test close_bug method that failed to close a bug.""" |
| 146 | issue = self.mox.CreateMock(phapi_lib.Issue) |
| 147 | issue.id = 100 |
| 148 | issue.labels = [] |
| 149 | issue.state = constants.ISSUE_OPEN |
| 150 | |
| 151 | self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker') |
| 152 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
| 153 | issue) |
| 154 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
| 155 | issue) |
| 156 | self.mox.StubOutWithMock(reporting.Reporter, 'modify_bug_report') |
| 157 | reporting.Reporter.modify_bug_report(mox.IgnoreArg(), |
| 158 | comment=mox.IgnoreArg(), |
| 159 | label_update=mox.IgnoreArg(), |
| 160 | status=mox.IgnoreArg()).AndReturn( |
| 161 | None) |
| 162 | self.mox.ReplayAll() |
| 163 | self.assertRaises(test_push.TestPushException, test_push.close_bug) |
| 164 | self.mox.VerifyAll() |
| 165 | |
| 166 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 167 | def test_check_bug_filed_fail_to_find_bug(self): |
| 168 | """Test check_bug_filed method that failed to find a bug. |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 169 | """ |
| 170 | self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker') |
| 171 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
| 172 | None) |
| 173 | self.mox.ReplayAll() |
| 174 | self.assertRaises(test_push.TestPushException, |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 175 | test_push.check_bug_filed, None) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 176 | self.mox.VerifyAll() |
| 177 | |
| 178 | |
| 179 | def create_mock_issue(self, id, labels=[]): |
| 180 | """Create a mock issue with given id and lables. |
| 181 | |
| 182 | @param id: id of the issue. |
| 183 | @param labels: labels of the issue. |
| 184 | |
| 185 | """ |
| 186 | issue = self.mox.CreateMock(phapi_lib.Issue) |
| 187 | issue.id = id |
| 188 | issue.labels = labels |
| 189 | issue.state = constants.ISSUE_OPEN |
| 190 | return issue |
| 191 | |
| 192 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 193 | def test_check_bug_filed_fail_to_find_bug2(self): |
| 194 | """Test check_bug_filed method that failed to find a bug. |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 195 | """ |
| 196 | issue = self.create_mock_issue(100) |
| 197 | |
| 198 | self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker') |
| 199 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
| 200 | issue) |
| 201 | self.mox.ReplayAll() |
| 202 | self.assertRaises(test_push.TestPushException, |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 203 | test_push.check_bug_filed, [100]) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 204 | self.mox.VerifyAll() |
| 205 | |
| 206 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 207 | def test_check_bug_filed_fail_to_dedupe(self): |
| 208 | """Test check_bug_deduped method that failed with dedupe. |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 209 | """ |
| 210 | issue = self.create_mock_issue(100) |
| 211 | |
| 212 | self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker') |
| 213 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
| 214 | issue) |
| 215 | self.mox.ReplayAll() |
| 216 | self.assertRaises(test_push.TestPushException, |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 217 | test_push.check_bug_filed, [99]) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 218 | self.mox.VerifyAll() |
| 219 | |
| 220 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 221 | def test_check_bug_deduped_fail_more_than_1_bug(self): |
| 222 | """Test check_bug_filed method that failed with finding |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 223 | more than one bug. |
| 224 | """ |
| 225 | issue = self.create_mock_issue(100, [AUTOFILED_COUNT_2]) |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 226 | second_issue = self.create_mock_issue(100) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 227 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 228 | self.mox.StubOutWithMock(reporting.Reporter, 'modify_bug_report') |
| 229 | reporting.Reporter.modify_bug_report(mox.IgnoreArg(), |
| 230 | comment=mox.IgnoreArg(), |
| 231 | label_update=mox.IgnoreArg(), |
| 232 | status=mox.IgnoreArg() |
| 233 | ).AndReturn(None) |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 234 | self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker') |
| 235 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
| 236 | second_issue) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 237 | self.mox.ReplayAll() |
| 238 | self.assertRaises(test_push.TestPushException, |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 239 | test_push.check_bug_deduped, issue) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 240 | self.mox.VerifyAll() |
| 241 | |
| 242 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 243 | def test_check_bug_deduped_succeed_to_dedupe(self): |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 244 | """Test check_bug_filed_and_deduped method that succeeded with dedupe. |
| 245 | """ |
| 246 | issue = self.create_mock_issue(100, [AUTOFILED_COUNT_2]) |
| 247 | |
| 248 | self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker') |
| 249 | reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn( |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 250 | None) |
| 251 | self.mox.StubOutWithMock(reporting.Reporter, 'modify_bug_report') |
| 252 | reporting.Reporter.modify_bug_report(mox.IgnoreArg(), |
| 253 | comment=mox.IgnoreArg(), |
| 254 | label_update=mox.IgnoreArg(), |
| 255 | status=mox.IgnoreArg() |
| 256 | ).AndReturn(None) |
| 257 | self.mox.ReplayAll() |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 258 | test_push.check_bug_deduped(issue) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 259 | self.mox.VerifyAll() |
| 260 | |
| 261 | |
| 262 | if __name__ == '__main__': |
| 263 | unittest.main() |