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 | |
Allen Li | 3d43e60 | 2016-12-08 15:09:51 -0800 | [diff] [blame] | 11 | import mock |
| 12 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 13 | import common |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 14 | from autotest_lib.client.common_lib.cros import retry |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 15 | from autotest_lib.server import site_utils |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 16 | from autotest_lib.server.cros.dynamic_suite import reporting |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 17 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 18 | |
Allen Li | 3d43e60 | 2016-12-08 15:09:51 -0800 | [diff] [blame] | 19 | # Mock retry.retry used in test_push before importing test_push. |
| 20 | retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 21 | from autotest_lib.site_utils import test_push |
| 22 | |
| 23 | AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT |
| 24 | |
| 25 | class TestPushUnittests(mox.MoxTestBase): |
| 26 | """Unittest for test_push script.""" |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 27 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 28 | def setUp(self): |
| 29 | """Initialize the unittest.""" |
| 30 | super(TestPushUnittests, self).setUp() |
| 31 | # Overwrite expected test results. |
| 32 | test_push.EXPECTED_TEST_RESULTS = { |
| 33 | '^SERVER_JOB$': 'GOOD', |
| 34 | '.*control.dependency$': 'TEST_NA', |
| 35 | '.*dummy_Fail.RetryFail$': 'FAIL', |
| 36 | } |
Shuqian Zhao | 327b695 | 2016-09-12 10:42:03 -0700 | [diff] [blame] | 37 | test_push.TKO = None |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 38 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 39 | |
| 40 | def stub_out_methods(self, test_views): |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 41 | """Stub out methods in test_push module with given test results. |
| 42 | |
| 43 | @param test_views: Desired test result views. |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 44 | |
| 45 | """ |
| 46 | self.mox.UnsetStubs() |
| 47 | response = StringIO.StringIO('some_value') |
| 48 | self.mox.StubOutWithMock(urllib2, 'urlopen') |
| 49 | urllib2.urlopen(mox.IgnoreArg()).AndReturn(response) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 50 | |
Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 51 | self.mox.StubOutWithMock(test_push, 'get_default_build') |
Kevin Cheng | e691ce9 | 2016-12-15 12:17:13 -0800 | [diff] [blame] | 52 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 53 | 'stumpy-release/R36-5881-0.0') |
Kevin Cheng | e691ce9 | 2016-12-15 12:17:13 -0800 | [diff] [blame] | 54 | test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
Jakob Juelich | 8f14391 | 2014-10-10 14:08:05 -0700 | [diff] [blame] | 55 | 'quawks-release/R36-5881-0.0') |
Dan Shi | 5ba5d2e | 2014-05-09 13:47:00 -0700 | [diff] [blame] | 56 | |
Dan Shi | a8da760 | 2014-05-09 15:18:15 -0700 | [diff] [blame] | 57 | self.mox.StubOutWithMock(test_push, 'check_dut_image') |
| 58 | test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 59 | None) |
| 60 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 61 | self.mox.StubOutWithMock(test_push, 'do_run_suite') |
Dan Shi | 81ddc42 | 2016-09-09 13:58:31 -0700 | [diff] [blame] | 62 | test_push.do_run_suite( |
| 63 | test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(), |
| 64 | mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 65 | |
| 66 | self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko') |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 67 | site_utils.get_test_views_from_tko(1, None).AndReturn(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def test_suite_success(self): |
| 71 | """Test test_suite method with matching results.""" |
| 72 | test_views = {'SERVER_JOB': 'GOOD', |
| 73 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 74 | 'dummy_Fail.RetryFail': 'FAIL' |
| 75 | } |
| 76 | |
| 77 | self.stub_out_methods(test_views) |
| 78 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 79 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
| 80 | arguments=test_push.parse_arguments()) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 81 | self.mox.VerifyAll() |
| 82 | |
| 83 | |
| 84 | def test_suite_fail_with_missing_test(self): |
| 85 | """Test test_suite method that should fail with missing test.""" |
| 86 | test_views = {'SERVER_JOB': 'GOOD', |
| 87 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 88 | } |
| 89 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 90 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 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_unexpected_test_results(self): |
| 98 | """Test test_suite method that should fail with unexpected test results. |
| 99 | """ |
| 100 | test_views = {'SERVER_JOB': 'FAIL', |
| 101 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 102 | 'dummy_Fail.RetryFail': 'FAIL', |
| 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_extra_test(self): |
| 113 | """Test test_suite method that should fail with extra test.""" |
| 114 | test_views = {'SERVER_JOB': 'GOOD', |
| 115 | 'dummy_fail/control.dependency': 'TEST_NA', |
| 116 | 'dummy_Fail.RetryFail': 'FAIL', |
| 117 | 'dummy_Fail.ExtraTest': 'GOOD', |
| 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 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 127 | if __name__ == '__main__': |
| 128 | unittest.main() |