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 mock 12 13 import common 14 from autotest_lib.client.common_lib.cros import retry 15 from autotest_lib.server import site_utils 16 17 18 # Mock retry.retry used in test_push before importing test_push. 19 retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func) 20 from autotest_lib.site_utils import test_push 21 22 23 class TestPushUnittests(mox.MoxTestBase): 24 """Unittest for test_push script.""" 25 26 def setUp(self): 27 """Initialize the unittest.""" 28 super(TestPushUnittests, self).setUp() 29 # Overwrite expected test results. 30 test_push.EXPECTED_TEST_RESULTS = { 31 '^SERVER_JOB$': 'GOOD', 32 '.*control.dependency$': 'TEST_NA', 33 '.*dummy_Fail.RetryFail$': 'FAIL', 34 } 35 test_push.TKO = None 36 37 38 def stub_out_methods(self, test_views): 39 """Stub out methods in test_push module with given test results. 40 41 @param test_views: Desired test result views. 42 43 """ 44 self.mox.UnsetStubs() 45 response = StringIO.StringIO('some_value') 46 self.mox.StubOutWithMock(urllib2, 'urlopen') 47 urllib2.urlopen(mox.IgnoreArg()).AndReturn(response) 48 49 self.mox.StubOutWithMock(test_push, 'get_default_build') 50 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( 51 'stumpy-release/R36-5881-0.0') 52 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( 53 'quawks-release/R36-5881-0.0') 54 55 self.mox.StubOutWithMock(test_push, 'check_dut_image') 56 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( 57 None) 58 59 self.mox.StubOutWithMock(test_push, 'do_run_suite') 60 test_push.do_run_suite( 61 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(), 62 mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1)) 63 64 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko') 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 if __name__ == '__main__': 126 unittest.main() 127