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