blob: 8636a7af3818975f92f15b6e29ee3e8f10390944 [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 frontend_wrappers
15from autotest_lib.server.cros.dynamic_suite import reporting
Shuqian Zhao9866ab12016-04-05 15:55:13 -070016# Mock the retry.retry used in the test_push before import it.
xixuana96aff02016-03-29 14:22:08 -070017def mock_retry(ExceptionToCheck, timeout_min, 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.
Aviv Keshet7935d562016-05-18 11:19:24 -070022 @param exception_to_raise: Ignored
Shuqian Zhao9866ab12016-04-05 15:55:13 -070023
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
34retry.retry = mock_retry
Dan Shi7e04fa82013-07-25 15:08:48 -070035from autotest_lib.site_utils import test_push
36
37AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT
38
39class TestPushUnittests(mox.MoxTestBase):
40 """Unittest for test_push script."""
Dan Shief1a5c02015-04-07 17:37:09 -070041
Dan Shi7e04fa82013-07-25 15:08:48 -070042 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 Shi7e04fa82013-07-25 15:08:48 -070051
Dan Shief1a5c02015-04-07 17:37:09 -070052
53 def stub_out_methods(self, test_views):
Dan Shi7e04fa82013-07-25 15:08:48 -070054 """Stub out methods in test_push module with given test results.
55
56 @param test_views: Desired test result views.
Dan Shi7e04fa82013-07-25 15:08:48 -070057
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 Shi7e04fa82013-07-25 15:08:48 -070063
Dan Shi5ba5d2e2014-05-09 13:47:00 -070064 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 Juelich8f143912014-10-10 14:08:05 -070067 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
68 'quawks-release/R36-5881-0.0')
Dan Shi5ba5d2e2014-05-09 13:47:00 -070069
Dan Shia8da7602014-05-09 15:18:15 -070070 self.mox.StubOutWithMock(test_push, 'check_dut_image')
71 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
72 None)
73
Dan Shi7e04fa82013-07-25 15:08:48 -070074 self.mox.StubOutWithMock(test_push, 'do_run_suite')
Jakob Juelich8f143912014-10-10 14:08:05 -070075 test_push.do_run_suite(test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(),
Shuqian Zhaod4864772015-08-06 09:46:22 -070076 mox.IgnoreArg(), mox.IgnoreArg()
Dan Shi7e04fa82013-07-25 15:08:48 -070077 ).AndReturn((1))
Dan Shi7e04fa82013-07-25 15:08:48 -070078
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 Shi7e04fa82013-07-25 15:08:48 -070084
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 Shief1a5c02015-04-07 17:37:09 -070095 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
96 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -070097 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 Shief1a5c02015-04-07 17:37:09 -0700106 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700107 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700108 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
109 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700110 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 Shief1a5c02015-04-07 17:37:09 -0700121 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700122 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700123 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
124 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700125 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 Shief1a5c02015-04-07 17:37:09 -0700136 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700137 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700138 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
139 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700140 self.mox.VerifyAll()
141
142
Dan Shi7e04fa82013-07-25 15:08:48 -0700143if __name__ == '__main__':
144 unittest.main()