Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 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 |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 16 | |
Shuqian Zhao | 9866ab1 | 2016-04-05 15:55:13 -0700 | [diff] [blame] | 17 | |
Allen Li | 3d43e60 | 2016-12-08 15:09:51 -0800 | [diff] [blame] | 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) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 20 | from autotest_lib.site_utils import test_push |
| 21 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 22 | class TestPushUnittests(mox.MoxTestBase): |
| 23 | """Unittest for test_push script.""" |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 24 | |
Richard Barnette | 2af8221 | 2018-04-20 15:11:54 -0700 | [diff] [blame] | 25 | _ARGV = [ |
| 26 | 'command', |
| 27 | '--build', 'stumpy-release/R36-5881-0.0', |
| 28 | '--shard_build', 'quawks-release/R36-5881-0.0' |
| 29 | ] |
| 30 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 31 | def setUp(self): |
| 32 | """Initialize the unittest.""" |
| 33 | super(TestPushUnittests, self).setUp() |
| 34 | # Overwrite expected test results. |
| 35 | test_push.EXPECTED_TEST_RESULTS = { |
Alex Zamorzaev | f0573b5 | 2019-04-05 12:07:59 -0700 | [diff] [blame] | 36 | '^SERVER_JOB$': ['GOOD'], |
| 37 | '.*control.dependency$': ['TEST_NA'], |
| 38 | '.*dummy_Fail.RetryFail$': ['FAIL', 'FAIL'], |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 39 | } |
Shuqian Zhao | 327b695 | 2016-09-12 10:42:03 -0700 | [diff] [blame] | 40 | test_push.TKO = None |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 41 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 42 | |
| 43 | def stub_out_methods(self, test_views): |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 44 | """Stub out methods in test_push module with given test results. |
| 45 | |
| 46 | @param test_views: Desired test result views. |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 47 | |
| 48 | """ |
| 49 | self.mox.UnsetStubs() |
| 50 | response = StringIO.StringIO('some_value') |
| 51 | self.mox.StubOutWithMock(urllib2, 'urlopen') |
| 52 | urllib2.urlopen(mox.IgnoreArg()).AndReturn(response) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 53 | |
Dan Shi | a8da760 | 2014-05-09 15:18:15 -0700 | [diff] [blame] | 54 | self.mox.StubOutWithMock(test_push, 'check_dut_image') |
| 55 | test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 56 | None) |
| 57 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 58 | self.mox.StubOutWithMock(test_push, 'do_run_suite') |
Dan Shi | 81ddc42 | 2016-09-09 13:58:31 -0700 | [diff] [blame] | 59 | test_push.do_run_suite( |
| 60 | test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(), |
Richard Barnette | b12413a | 2018-04-25 01:00:27 +0000 | [diff] [blame] | 61 | mox.IgnoreArg()).AndReturn((1)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 62 | |
| 63 | self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko') |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 64 | site_utils.get_test_views_from_tko(1, None).AndReturn(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def test_suite_success(self): |
| 68 | """Test test_suite method with matching results.""" |
Alex Zamorzaev | f0573b5 | 2019-04-05 12:07:59 -0700 | [diff] [blame] | 69 | test_views = {'SERVER_JOB': ['GOOD'], |
| 70 | 'dummy_fail/control.dependency': ['TEST_NA'], |
| 71 | 'dummy_Fail.RetryFail': ['FAIL', 'FAIL'], |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | self.stub_out_methods(test_views) |
| 75 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 76 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
Richard Barnette | 2af8221 | 2018-04-20 15:11:54 -0700 | [diff] [blame] | 77 | arguments=test_push.parse_arguments(self._ARGV)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 78 | self.mox.VerifyAll() |
| 79 | |
| 80 | |
| 81 | def test_suite_fail_with_missing_test(self): |
| 82 | """Test test_suite method that should fail with missing test.""" |
Alex Zamorzaev | f0573b5 | 2019-04-05 12:07:59 -0700 | [diff] [blame] | 83 | test_views = {'SERVER_JOB': ['GOOD'], |
| 84 | 'dummy_fail/control.dependency': ['TEST_NA'], |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 87 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 88 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 89 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
Richard Barnette | 2af8221 | 2018-04-20 15:11:54 -0700 | [diff] [blame] | 90 | arguments=test_push.parse_arguments(self._ARGV)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 91 | self.mox.VerifyAll() |
| 92 | |
| 93 | |
| 94 | def test_suite_fail_with_unexpected_test_results(self): |
| 95 | """Test test_suite method that should fail with unexpected test results. |
| 96 | """ |
Alex Zamorzaev | f0573b5 | 2019-04-05 12:07:59 -0700 | [diff] [blame] | 97 | test_views = {'SERVER_JOB': ['FAIL'], |
| 98 | 'dummy_fail/control.dependency': ['TEST_NA'], |
| 99 | 'dummy_Fail.RetryFail': ['FAIL', 'FAIL'], |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 102 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 103 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 104 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
Richard Barnette | 2af8221 | 2018-04-20 15:11:54 -0700 | [diff] [blame] | 105 | arguments=test_push.parse_arguments(self._ARGV)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 106 | self.mox.VerifyAll() |
| 107 | |
| 108 | |
| 109 | def test_suite_fail_with_extra_test(self): |
| 110 | """Test test_suite method that should fail with extra test.""" |
Alex Zamorzaev | f0573b5 | 2019-04-05 12:07:59 -0700 | [diff] [blame] | 111 | test_views = {'SERVER_JOB': ['GOOD'], |
| 112 | 'dummy_fail/control.dependency': ['TEST_NA'], |
| 113 | 'dummy_Fail.RetryFail': ['FAIL', 'FAIL'], |
| 114 | 'dummy_Fail.ExtraTest': ['GOOD'], |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 117 | self.stub_out_methods(test_views) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 118 | self.mox.ReplayAll() |
Dan Shi | ef1a5c0 | 2015-04-07 17:37:09 -0700 | [diff] [blame] | 119 | test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, |
Richard Barnette | 2af8221 | 2018-04-20 15:11:54 -0700 | [diff] [blame] | 120 | arguments=test_push.parse_arguments(self._ARGV)) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 121 | self.mox.VerifyAll() |
| 122 | |
| 123 | |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 124 | if __name__ == '__main__': |
| 125 | unittest.main() |