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