blob: a500cb4e6ab9a0b72b74b268a5434aac19f68ff1 [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
12from autotest_lib.server import site_utils
13from autotest_lib.server.cros.dynamic_suite import constants
14from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
15from autotest_lib.server.cros.dynamic_suite import reporting
16from autotest_lib.site_utils import phapi_lib
17from autotest_lib.site_utils import test_push
18
19AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT
20
21class TestPushUnittests(mox.MoxTestBase):
22 """Unittest for test_push script."""
23 def setUp(self):
24 """Initialize the unittest."""
25 super(TestPushUnittests, self).setUp()
26 # Overwrite expected test results.
27 test_push.EXPECTED_TEST_RESULTS = {
28 '^SERVER_JOB$': 'GOOD',
29 '.*control.dependency$': 'TEST_NA',
30 '.*dummy_Fail.RetryFail$': 'FAIL',
31 }
32 test_push.EXPECTED_TEST_RESULTS_AU = test_push.EXPECTED_TEST_RESULTS
33
34
35 def stub_out_methods(self, test_views, fail_first_run_suite=False):
36 """Stub out methods in test_push module with given test results.
37
38 @param test_views: Desired test result views.
39 @param fail_first_run_suite: If it's True, only two urlopen and one
40 get_test_views_from_tko calls need to stub out.
41
42 """
43 self.mox.UnsetStubs()
44 response = StringIO.StringIO('some_value')
45 self.mox.StubOutWithMock(urllib2, 'urlopen')
46 urllib2.urlopen(mox.IgnoreArg()).AndReturn(response)
Dan Shi7e04fa82013-07-25 15:08:48 -070047 if not fail_first_run_suite:
48 urllib2.urlopen(mox.IgnoreArg()).AndReturn(response)
49
Dan Shi5ba5d2e2014-05-09 13:47:00 -070050 self.mox.StubOutWithMock(test_push, 'get_default_build')
51 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
52 'stumpy-release/R36-5881-0.0')
53
Dan Shia8da7602014-05-09 15:18:15 -070054 self.mox.StubOutWithMock(test_push, 'check_dut_image')
55 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
56 None)
57
Dan Shi7e04fa82013-07-25 15:08:48 -070058 self.mox.StubOutWithMock(test_push, 'do_run_suite')
59 test_push.do_run_suite(test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg()
60 ).AndReturn((1))
61 if not fail_first_run_suite:
62 test_push.do_run_suite(test_push.AU_SUITE, mox.IgnoreArg()
63 ).AndReturn((1))
64
65 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
66 self.mox.StubOutWithMock(frontend_wrappers, 'RetryingTKO')
67 frontend_wrappers.RetryingTKO(timeout_min=0.1,
68 delay_sec=10).AndReturn(None)
69 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
70 if not fail_first_run_suite:
71 frontend_wrappers.RetryingTKO(timeout_min=0.1,
72 delay_sec=10).AndReturn(None)
73 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
74
75 self.mox.StubOutWithMock(test_push, 'close_bug')
76 test_push.close_bug().AndReturn(None)
77 if not fail_first_run_suite:
78 self.mox.StubOutWithMock(test_push, 'check_bug_filed_and_deduped')
79 test_push.check_bug_filed_and_deduped(None).AndReturn(None)
80
81
82 def test_suite_success(self):
83 """Test test_suite method with matching results."""
84 test_views = {'SERVER_JOB': 'GOOD',
85 'dummy_fail/control.dependency': 'TEST_NA',
86 'dummy_Fail.RetryFail': 'FAIL'
87 }
88
89 self.stub_out_methods(test_views)
90 self.mox.ReplayAll()
91 test_push.main()
92 self.mox.VerifyAll()
93
94
95 def test_suite_fail_with_missing_test(self):
96 """Test test_suite method that should fail with missing test."""
97 test_views = {'SERVER_JOB': 'GOOD',
98 'dummy_fail/control.dependency': 'TEST_NA',
99 }
100
101 self.stub_out_methods(test_views, fail_first_run_suite=True)
102 self.mox.ReplayAll()
103 self.assertRaises(test_push.TestPushException, test_push.main)
104
105 self.mox.VerifyAll()
106
107
108 def test_suite_fail_with_unexpected_test_results(self):
109 """Test test_suite method that should fail with unexpected test results.
110 """
111 test_views = {'SERVER_JOB': 'FAIL',
112 'dummy_fail/control.dependency': 'TEST_NA',
113 'dummy_Fail.RetryFail': 'FAIL',
114 }
115
116 self.stub_out_methods(test_views, fail_first_run_suite=True)
117 self.mox.ReplayAll()
118 self.assertRaises(test_push.TestPushException, test_push.main)
119
120 self.mox.VerifyAll()
121
122
123 def test_suite_fail_with_extra_test(self):
124 """Test test_suite method that should fail with extra test."""
125 test_views = {'SERVER_JOB': 'GOOD',
126 'dummy_fail/control.dependency': 'TEST_NA',
127 'dummy_Fail.RetryFail': 'FAIL',
128 'dummy_Fail.ExtraTest': 'GOOD',
129 }
130
131 self.stub_out_methods(test_views, fail_first_run_suite=True)
132 self.mox.ReplayAll()
133 self.assertRaises(test_push.TestPushException, test_push.main)
134
135 self.mox.VerifyAll()
136
137
138 def test_close_bug_fail(self):
139 """Test close_bug method that failed to close a bug."""
140 issue = self.mox.CreateMock(phapi_lib.Issue)
141 issue.id = 100
142 issue.labels = []
143 issue.state = constants.ISSUE_OPEN
144
145 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
146 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
147 issue)
148 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
149 issue)
150 self.mox.StubOutWithMock(reporting.Reporter, 'modify_bug_report')
151 reporting.Reporter.modify_bug_report(mox.IgnoreArg(),
152 comment=mox.IgnoreArg(),
153 label_update=mox.IgnoreArg(),
154 status=mox.IgnoreArg()).AndReturn(
155 None)
156 self.mox.ReplayAll()
157 self.assertRaises(test_push.TestPushException, test_push.close_bug)
158 self.mox.VerifyAll()
159
160
161 def test_check_bug_filed_and_deduped_fail_to_find_bug(self):
162 """Test check_bug_filed_and_deduped method that failed to find a bug.
163 """
164 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
165 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
166 None)
167 self.mox.ReplayAll()
168 self.assertRaises(test_push.TestPushException,
169 test_push.check_bug_filed_and_deduped, None)
170 self.mox.VerifyAll()
171
172
173 def create_mock_issue(self, id, labels=[]):
174 """Create a mock issue with given id and lables.
175
176 @param id: id of the issue.
177 @param labels: labels of the issue.
178
179 """
180 issue = self.mox.CreateMock(phapi_lib.Issue)
181 issue.id = id
182 issue.labels = labels
183 issue.state = constants.ISSUE_OPEN
184 return issue
185
186
187 def test_check_bug_filed_and_deduped_fail_to_find_bug2(self):
188 """Test check_bug_filed_and_deduped method that failed to find a bug.
189 """
190 issue = self.create_mock_issue(100)
191
192 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
193 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
194 issue)
195 self.mox.ReplayAll()
196 self.assertRaises(test_push.TestPushException,
197 test_push.check_bug_filed_and_deduped, [100])
198 self.mox.VerifyAll()
199
200
201 def test_check_bug_filed_and_deduped_fail_to_dedupe(self):
202 """Test check_bug_filed_and_deduped method that failed with dedupe.
203 """
204 issue = self.create_mock_issue(100)
205
206 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
207 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
208 issue)
209 self.mox.ReplayAll()
210 self.assertRaises(test_push.TestPushException,
211 test_push.check_bug_filed_and_deduped, [99])
212 self.mox.VerifyAll()
213
214
215 def test_check_bug_filed_and_deduped_fail_more_than_1_bug(self):
216 """Test check_bug_filed_and_deduped method that failed with finding
217 more than one bug.
218 """
219 issue = self.create_mock_issue(100, [AUTOFILED_COUNT_2])
220 second_issue = self.create_mock_issue(101)
221
222 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
223 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
224 issue)
225 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
226 second_issue)
227 self.mox.StubOutWithMock(reporting.Reporter, 'modify_bug_report')
228 reporting.Reporter.modify_bug_report(mox.IgnoreArg(),
229 comment=mox.IgnoreArg(),
230 label_update=mox.IgnoreArg(),
231 status=mox.IgnoreArg()
232 ).AndReturn(None)
233 self.mox.ReplayAll()
234 self.assertRaises(test_push.TestPushException,
235 test_push.check_bug_filed_and_deduped, [99])
236 self.mox.VerifyAll()
237
238
239 def test_check_bug_filed_and_deduped_succeed_to_dedupe(self):
240 """Test check_bug_filed_and_deduped method that succeeded with dedupe.
241 """
242 issue = self.create_mock_issue(100, [AUTOFILED_COUNT_2])
243
244 self.mox.StubOutWithMock(reporting.Reporter, 'find_issue_by_marker')
245 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
246 issue)
247 reporting.Reporter.find_issue_by_marker(mox.IgnoreArg()).AndReturn(
248 None)
249 self.mox.StubOutWithMock(reporting.Reporter, 'modify_bug_report')
250 reporting.Reporter.modify_bug_report(mox.IgnoreArg(),
251 comment=mox.IgnoreArg(),
252 label_update=mox.IgnoreArg(),
253 status=mox.IgnoreArg()
254 ).AndReturn(None)
255 self.mox.ReplayAll()
256 test_push.check_bug_filed_and_deduped([99])
257 self.mox.VerifyAll()
258
259
260if __name__ == '__main__':
261 unittest.main()