| 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 |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 14 | from autotest_lib.server.cros.dynamic_suite import reporting |
| Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 15 | # Mock the retry.retry used in the test_push before import it. |
| Shuqian Zhao | 6fc7bf4 | 2016-12-11 19:10:36 -0800 | [diff] [blame^] | 16 | def mock_retry(ExceptionToCheck, timeout_min, delay_sec, |
| 17 | exception_to_raise=None): |
| Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 18 | """A mock retry decorator to use in place of the actual one for testing. |
| 19 | |
| 20 | @param ExceptionToCheck: the exception to check. |
| 21 | @param timeout_mins: Amount of time in mins to wait before timing out. |
| Shuqian Zhao | 6fc7bf4 | 2016-12-11 19:10:36 -0800 | [diff] [blame^] | 22 | @param delay_sec: Amount of time in secs to wait before retry. |
| Aviv Keshet | 7935d56 | 2016-05-18 11:19:24 -0700 | [diff] [blame] | 23 | @param exception_to_raise: Ignored |
| Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 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 | } |
| Shuqian Zhao | 327b695 | 2016-09-12 10:42:03 -0700 | [diff] [blame] | 52 | test_push.TKO = None |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 53 | |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 54 | |
| 55 | def stub_out_methods(self, test_views): |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 56 | """Stub out methods in test_push module with given test results. |
| 57 | |
| 58 | @param test_views: Desired test result views. |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 59 | |
| 60 | """ |
| 61 | self.mox.UnsetStubs() |
| 62 | response = StringIO.StringIO('some_value') |
| 63 | self.mox.StubOutWithMock(urllib2, 'urlopen') |
| 64 | urllib2.urlopen(mox.IgnoreArg()).AndReturn(response) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 65 | |
| Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 66 | self.mox.StubOutWithMock(test_push, 'get_default_build') |
| Kevin Cheng | e691ce9 | 2016-12-15 12:17:13 -0800 | [diff] [blame] | 67 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 68 | 'stumpy-release/R36-5881-0.0') |
| Kevin Cheng | e691ce9 | 2016-12-15 12:17:13 -0800 | [diff] [blame] | 69 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| Jakob Juelich | 8f14391 | 2014-10-10 14:08:05 -0700 | [diff] [blame] | 70 | 'quawks-release/R36-5881-0.0') |
| Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 71 | |
| Dan Shi | a8da760 | 2014-05-09 15:18:15 -0700 | [diff] [blame] | 72 | self.mox.StubOutWithMock(test_push, 'check_dut_image') |
| 73 | test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 74 | None) |
| 75 | |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 76 | self.mox.StubOutWithMock(test_push, 'do_run_suite') |
| Dan Shi | 81ddc42 | 2016-09-09 13:58:31 -0700 | [diff] [blame] | 77 | test_push.do_run_suite( |
| 78 | test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(), |
| 79 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1)) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 80 | |
| 81 | self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko') |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 82 | site_utils.get_test_views_from_tko(1, None).AndReturn(test_views) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def test_suite_success(self): |
| 86 | """Test test_suite method with matching results.""" |
| 87 | test_views = {'SERVER_JOB': 'GOOD', |
| 88 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 89 | 'dummy_Fail.RetryFail': 'FAIL' |
| 90 | } |
| 91 | |
| 92 | self.stub_out_methods(test_views) |
| 93 | self.mox.ReplayAll() |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 94 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 95 | arguments=test_push.parse_arguments()) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 96 | self.mox.VerifyAll() |
| 97 | |
| 98 | |
| 99 | def test_suite_fail_with_missing_test(self): |
| 100 | """Test test_suite method that should fail with missing test.""" |
| 101 | test_views = {'SERVER_JOB': 'GOOD', |
| 102 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 103 | } |
| 104 | |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 105 | self.stub_out_methods(test_views) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 106 | self.mox.ReplayAll() |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 107 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 108 | arguments=test_push.parse_arguments()) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 109 | self.mox.VerifyAll() |
| 110 | |
| 111 | |
| 112 | def test_suite_fail_with_unexpected_test_results(self): |
| 113 | """Test test_suite method that should fail with unexpected test results. |
| 114 | """ |
| 115 | test_views = {'SERVER_JOB': 'FAIL', |
| 116 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 117 | 'dummy_Fail.RetryFail': 'FAIL', |
| 118 | } |
| 119 | |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 120 | self.stub_out_methods(test_views) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 121 | self.mox.ReplayAll() |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 122 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 123 | arguments=test_push.parse_arguments()) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 124 | self.mox.VerifyAll() |
| 125 | |
| 126 | |
| 127 | def test_suite_fail_with_extra_test(self): |
| 128 | """Test test_suite method that should fail with extra test.""" |
| 129 | test_views = {'SERVER_JOB': 'GOOD', |
| 130 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 131 | 'dummy_Fail.RetryFail': 'FAIL', |
| 132 | 'dummy_Fail.ExtraTest': 'GOOD', |
| 133 | } |
| 134 | |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 135 | self.stub_out_methods(test_views) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 136 | self.mox.ReplayAll() |
| Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 137 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 138 | arguments=test_push.parse_arguments()) |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 139 | self.mox.VerifyAll() |
| 140 | |
| 141 | |
| Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 142 | if __name__ == '__main__': |
| 143 | unittest.main() |