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 frontend_wrappers |
| 15 | from autotest_lib.server.cros.dynamic_suite import reporting |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 16 | # Mock the retry.retry used in the test_push before import it. |
xixuan | a96aff0 | 2016-03-29 14:22:08 -0700 | [diff] [blame] | 17 | def mock_retry(ExceptionToCheck, timeout_min, 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. |
Aviv Keshet | 7935d56 | 2016-05-18 11:19:24 -0700 | [diff] [blame] | 22 | @param exception_to_raise: Ignored |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 23 | |
| 24 | """ |
| 25 | def inner_retry(func): |
| 26 | """The actual decorator. |
| 27 | |
| 28 | @param func: Function to be called in decorator. |
| 29 | |
| 30 | """ |
| 31 | return func |
| 32 | |
| 33 | return inner_retry |
| 34 | retry.retry = mock_retry |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 35 | from autotest_lib.site_utils import test_push |
| 36 | |
| 37 | AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT |
| 38 | |
| 39 | class TestPushUnittests(mox.MoxTestBase): |
| 40 | """Unittest for test_push script.""" |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 41 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 42 | def setUp(self): |
| 43 | """Initialize the unittest.""" |
| 44 | super(TestPushUnittests, self).setUp() |
| 45 | # Overwrite expected test results. |
| 46 | test_push.EXPECTED_TEST_RESULTS = { |
| 47 | '^SERVER_JOB$': 'GOOD', |
| 48 | '.*control.dependency$': 'TEST_NA', |
| 49 | '.*dummy_Fail.RetryFail$': 'FAIL', |
| 50 | } |
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') |
| 65 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 66 | 'stumpy-release/R36-5881-0.0') |
Jakob Juelich | 8f14391 | 2014-10-10 14:08:05 -0700 | [diff] [blame] | 67 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 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') |
Jakob Juelich | 8f14391 | 2014-10-10 14:08:05 -0700 | [diff] [blame] | 75 | test_push.do_run_suite(test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), |
Shuqian Zhao | d486477 | 2015-08-06 09:46:22 -0700 | [diff] [blame] | 76 | mox.IgnoreArg(), mox.IgnoreArg() |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 77 | ).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') |
| 80 | self.mox.StubOutWithMock(frontend_wrappers, 'RetryingTKO') |
| 81 | frontend_wrappers.RetryingTKO(timeout_min=0.1, |
| 82 | delay_sec=10).AndReturn(None) |
| 83 | site_utils.get_test_views_from_tko(1, None).AndReturn(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def test_suite_success(self): |
| 87 | """Test test_suite method with matching results.""" |
| 88 | test_views = {'SERVER_JOB': 'GOOD', |
| 89 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 90 | 'dummy_Fail.RetryFail': 'FAIL' |
| 91 | } |
| 92 | |
| 93 | self.stub_out_methods(test_views) |
| 94 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 95 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 96 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 97 | self.mox.VerifyAll() |
| 98 | |
| 99 | |
| 100 | def test_suite_fail_with_missing_test(self): |
| 101 | """Test test_suite method that should fail with missing test.""" |
| 102 | test_views = {'SERVER_JOB': 'GOOD', |
| 103 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 104 | } |
| 105 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 106 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 107 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 108 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 109 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 110 | self.mox.VerifyAll() |
| 111 | |
| 112 | |
| 113 | def test_suite_fail_with_unexpected_test_results(self): |
| 114 | """Test test_suite method that should fail with unexpected test results. |
| 115 | """ |
| 116 | test_views = {'SERVER_JOB': 'FAIL', |
| 117 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 118 | 'dummy_Fail.RetryFail': 'FAIL', |
| 119 | } |
| 120 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 121 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 122 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 123 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 124 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 125 | self.mox.VerifyAll() |
| 126 | |
| 127 | |
| 128 | def test_suite_fail_with_extra_test(self): |
| 129 | """Test test_suite method that should fail with extra test.""" |
| 130 | test_views = {'SERVER_JOB': 'GOOD', |
| 131 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 132 | 'dummy_Fail.RetryFail': 'FAIL', |
| 133 | 'dummy_Fail.ExtraTest': 'GOOD', |
| 134 | } |
| 135 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 136 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 137 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 138 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 139 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 140 | self.mox.VerifyAll() |
| 141 | |
| 142 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 143 | if __name__ == '__main__': |
| 144 | unittest.main() |