blob: d291a0e5c0aa7331aa1bdca9713ee82bc13d045f [file] [log] [blame]
Dan Shi7e04fa82013-07-25 15:08:48 -07001#!/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
6import StringIO
7import mox
8import unittest
9import urllib2
10
11import common
Shuqian Zhao9866ab12016-04-05 15:55:13 -070012from autotest_lib.client.common_lib.cros import retry
Dan Shi7e04fa82013-07-25 15:08:48 -070013from autotest_lib.server import site_utils
14from autotest_lib.server.cros.dynamic_suite import constants
15from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
16from autotest_lib.server.cros.dynamic_suite import reporting
17from autotest_lib.site_utils import phapi_lib
Shuqian Zhao9866ab12016-04-05 15:55:13 -070018# Mock the retry.retry used in the test_push before import it.
xixuana96aff02016-03-29 14:22:08 -070019def mock_retry(ExceptionToCheck, timeout_min, exception_to_raise=None):
Shuqian Zhao9866ab12016-04-05 15:55:13 -070020 """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
35retry.retry = mock_retry
Dan Shi7e04fa82013-07-25 15:08:48 -070036from autotest_lib.site_utils import test_push
37
38AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT
39
40class TestPushUnittests(mox.MoxTestBase):
41 """Unittest for test_push script."""
Dan Shief1a5c02015-04-07 17:37:09 -070042
Dan Shi7e04fa82013-07-25 15:08:48 -070043 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 Shi7e04fa82013-07-25 15:08:48 -070052
Dan Shief1a5c02015-04-07 17:37:09 -070053
54 def stub_out_methods(self, test_views):
Dan Shi7e04fa82013-07-25 15:08:48 -070055 """Stub out methods in test_push module with given test results.
56
57 @param test_views: Desired test result views.
Dan Shi7e04fa82013-07-25 15:08:48 -070058
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 Shi7e04fa82013-07-25 15:08:48 -070064
Dan Shi5ba5d2e2014-05-09 13:47:00 -070065 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 Juelich8f143912014-10-10 14:08:05 -070068 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
69 'quawks-release/R36-5881-0.0')
Dan Shi5ba5d2e2014-05-09 13:47:00 -070070
Dan Shia8da7602014-05-09 15:18:15 -070071 self.mox.StubOutWithMock(test_push, 'check_dut_image')
72 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
73 None)
74
Dan Shi7e04fa82013-07-25 15:08:48 -070075 self.mox.StubOutWithMock(test_push, 'do_run_suite')
Jakob Juelich8f143912014-10-10 14:08:05 -070076 test_push.do_run_suite(test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(),
Shuqian Zhaod4864772015-08-06 09:46:22 -070077 mox.IgnoreArg(), mox.IgnoreArg()
Dan Shi7e04fa82013-07-25 15:08:48 -070078 ).AndReturn((1))
Dan Shi7e04fa82013-07-25 15:08:48 -070079
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 Shi7e04fa82013-07-25 15:08:48 -070085
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 Shief1a5c02015-04-07 17:37:09 -070096 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
97 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -070098 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 Shief1a5c02015-04-07 17:37:09 -0700107 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700108 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700109 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
110 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700111 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 Shief1a5c02015-04-07 17:37:09 -0700122 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700123 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700124 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
125 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700126 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 Shief1a5c02015-04-07 17:37:09 -0700137 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700138 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700139 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
140 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700141 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 Zhao9866ab12016-04-05 15:55:13 -0700167 def test_check_bug_filed_fail_to_find_bug(self):
168 """Test check_bug_filed method that failed to find a bug.
Dan Shi7e04fa82013-07-25 15:08:48 -0700169 """
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 Zhao9866ab12016-04-05 15:55:13 -0700175 test_push.check_bug_filed, None)
Dan Shi7e04fa82013-07-25 15:08:48 -0700176 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 Zhao9866ab12016-04-05 15:55:13 -0700193 def test_check_bug_filed_fail_to_find_bug2(self):
194 """Test check_bug_filed method that failed to find a bug.
Dan Shi7e04fa82013-07-25 15:08:48 -0700195 """
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 Zhao9866ab12016-04-05 15:55:13 -0700203 test_push.check_bug_filed, [100])
Dan Shi7e04fa82013-07-25 15:08:48 -0700204 self.mox.VerifyAll()
205
206
Shuqian Zhao9866ab12016-04-05 15:55:13 -0700207 def test_check_bug_filed_fail_to_dedupe(self):
208 """Test check_bug_deduped method that failed with dedupe.
Dan Shi7e04fa82013-07-25 15:08:48 -0700209 """
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 Zhao9866ab12016-04-05 15:55:13 -0700217 test_push.check_bug_filed, [99])
Dan Shi7e04fa82013-07-25 15:08:48 -0700218 self.mox.VerifyAll()
219
220
Shuqian Zhao9866ab12016-04-05 15:55:13 -0700221 def test_check_bug_deduped_fail_more_than_1_bug(self):
222 """Test check_bug_filed method that failed with finding
Dan Shi7e04fa82013-07-25 15:08:48 -0700223 more than one bug.
224 """
225 issue = self.create_mock_issue(100, [AUTOFILED_COUNT_2])
Shuqian Zhao9866ab12016-04-05 15:55:13 -0700226 second_issue = self.create_mock_issue(100)
Dan Shi7e04fa82013-07-25 15:08:48 -0700227
Dan Shi7e04fa82013-07-25 15:08:48 -0700228 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 Zhao9866ab12016-04-05 15:55:13 -0700234 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
235 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
236 second_issue)
Dan Shi7e04fa82013-07-25 15:08:48 -0700237 self.mox.ReplayAll()
238 self.assertRaises(test_push.TestPushException,
Shuqian Zhao9866ab12016-04-05 15:55:13 -0700239 test_push.check_bug_deduped, issue)
Dan Shi7e04fa82013-07-25 15:08:48 -0700240 self.mox.VerifyAll()
241
242
Shuqian Zhao9866ab12016-04-05 15:55:13 -0700243 def test_check_bug_deduped_succeed_to_dedupe(self):
Dan Shi7e04fa82013-07-25 15:08:48 -0700244 """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 Shi7e04fa82013-07-25 15:08:48 -0700250 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 Zhao9866ab12016-04-05 15:55:13 -0700258 test_push.check_bug_deduped(issue)
Dan Shi7e04fa82013-07-25 15:08:48 -0700259 self.mox.VerifyAll()
260
261
262if __name__ == '__main__':
263 unittest.main()