blob: a517e9694e680dfaa43dbcd5d33c2c4d273dbda8 [file] [log] [blame]
Dan Shi7e04fa82013-07-25 15:08:48 -07001#!/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
6import StringIO
7import mox
8import unittest
9import urllib2
10
11import common
Shuqian Zhao9866ab12016-04-05 15:55:13 -070012from autotest_lib.client.common_lib.cros import retry
Dan Shi7e04fa82013-07-25 15:08:48 -070013from autotest_lib.server import site_utils
Dan Shi7e04fa82013-07-25 15:08:48 -070014from autotest_lib.server.cros.dynamic_suite import reporting
Shuqian Zhao9866ab12016-04-05 15:55:13 -070015# Mock the retry.retry used in the test_push before import it.
Shuqian Zhao6fc7bf42016-12-11 19:10:36 -080016def mock_retry(ExceptionToCheck, timeout_min, delay_sec,
17 exception_to_raise=None):
Shuqian Zhao9866ab12016-04-05 15:55:13 -070018 """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 Zhao6fc7bf42016-12-11 19:10:36 -080022 @param delay_sec: Amount of time in secs to wait before retry.
Aviv Keshet7935d562016-05-18 11:19:24 -070023 @param exception_to_raise: Ignored
Shuqian Zhao9866ab12016-04-05 15:55:13 -070024
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
35retry.retry = mock_retry
Dan Shi7e04fa82013-07-25 15:08:48 -070036from autotest_lib.site_utils import test_push
37
38AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT
39
40class TestPushUnittests(mox.MoxTestBase):
41 """Unittest for test_push script."""
Dan Shief1a5c02015-04-07 17:37:09 -070042
Dan Shi7e04fa82013-07-25 15:08:48 -070043 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 Zhao327b6952016-09-12 10:42:03 -070052 test_push.TKO = None
Dan Shi7e04fa82013-07-25 15:08:48 -070053
Dan Shief1a5c02015-04-07 17:37:09 -070054
55 def stub_out_methods(self, test_views):
Dan Shi7e04fa82013-07-25 15:08:48 -070056 """Stub out methods in test_push module with given test results.
57
58 @param test_views: Desired test result views.
Dan Shi7e04fa82013-07-25 15:08:48 -070059
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 Shi7e04fa82013-07-25 15:08:48 -070065
Dan Shi5ba5d2e2014-05-09 13:47:00 -070066 self.mox.StubOutWithMock(test_push, 'get_default_build')
Kevin Chenge691ce92016-12-15 12:17:13 -080067 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
Dan Shi5ba5d2e2014-05-09 13:47:00 -070068 'stumpy-release/R36-5881-0.0')
Kevin Chenge691ce92016-12-15 12:17:13 -080069 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
Jakob Juelich8f143912014-10-10 14:08:05 -070070 'quawks-release/R36-5881-0.0')
Dan Shi5ba5d2e2014-05-09 13:47:00 -070071
Dan Shia8da7602014-05-09 15:18:15 -070072 self.mox.StubOutWithMock(test_push, 'check_dut_image')
73 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
74 None)
75
Dan Shi7e04fa82013-07-25 15:08:48 -070076 self.mox.StubOutWithMock(test_push, 'do_run_suite')
Dan Shi81ddc422016-09-09 13:58:31 -070077 test_push.do_run_suite(
78 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(),
79 mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1))
Dan Shi7e04fa82013-07-25 15:08:48 -070080
81 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
Dan Shi7e04fa82013-07-25 15:08:48 -070082 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -070083
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 Shief1a5c02015-04-07 17:37:09 -070094 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
95 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -070096 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 Shief1a5c02015-04-07 17:37:09 -0700105 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700106 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700107 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
108 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700109 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 Shief1a5c02015-04-07 17:37:09 -0700120 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700121 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700122 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
123 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700124 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 Shief1a5c02015-04-07 17:37:09 -0700135 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700136 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700137 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
138 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700139 self.mox.VerifyAll()
140
141
Dan Shi7e04fa82013-07-25 15:08:48 -0700142if __name__ == '__main__':
143 unittest.main()