blob: c4e18c076b3ad42d138c03305446948135d31f66 [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.
xixuana96aff02016-03-29 14:22:08 -070016def mock_retry(ExceptionToCheck, timeout_min, exception_to_raise=None):
Shuqian Zhao9866ab12016-04-05 15:55:13 -070017 """A mock retry decorator to use in place of the actual one for testing.
18
19 @param ExceptionToCheck: the exception to check.
20 @param timeout_mins: Amount of time in mins to wait before timing out.
Aviv Keshet7935d562016-05-18 11:19:24 -070021 @param exception_to_raise: Ignored
Shuqian Zhao9866ab12016-04-05 15:55:13 -070022
23 """
24 def inner_retry(func):
25 """The actual decorator.
26
27 @param func: Function to be called in decorator.
28
29 """
30 return func
31
32 return inner_retry
33retry.retry = mock_retry
Dan Shi7e04fa82013-07-25 15:08:48 -070034from autotest_lib.site_utils import test_push
35
36AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT
37
38class TestPushUnittests(mox.MoxTestBase):
39 """Unittest for test_push script."""
Dan Shief1a5c02015-04-07 17:37:09 -070040
Dan Shi7e04fa82013-07-25 15:08:48 -070041 def setUp(self):
42 """Initialize the unittest."""
43 super(TestPushUnittests, self).setUp()
44 # Overwrite expected test results.
45 test_push.EXPECTED_TEST_RESULTS = {
46 '^SERVER_JOB$': 'GOOD',
47 '.*control.dependency$': 'TEST_NA',
48 '.*dummy_Fail.RetryFail$': 'FAIL',
49 }
Shuqian Zhao327b6952016-09-12 10:42:03 -070050 test_push.TKO = None
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')
Shuqian Zhao12861662016-08-31 19:23:17 -070065 test_push.get_default_build(mox.IgnoreArg()).AndReturn(
Dan Shi5ba5d2e2014-05-09 13:47:00 -070066 'stumpy-release/R36-5881-0.0')
Shuqian Zhao12861662016-08-31 19:23:17 -070067 test_push.get_default_build(mox.IgnoreArg()).AndReturn(
Jakob Juelich8f143912014-10-10 14:08:05 -070068 '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')
Dan Shi81ddc422016-09-09 13:58:31 -070075 test_push.do_run_suite(
76 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(),
77 mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1))
Dan Shi7e04fa82013-07-25 15:08:48 -070078
79 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
Dan Shi7e04fa82013-07-25 15:08:48 -070080 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -070081
82
83 def test_suite_success(self):
84 """Test test_suite method with matching results."""
85 test_views = {'SERVER_JOB': 'GOOD',
86 'dummy_fail/control.dependency': 'TEST_NA',
87 'dummy_Fail.RetryFail': 'FAIL'
88 }
89
90 self.stub_out_methods(test_views)
91 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -070092 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
93 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -070094 self.mox.VerifyAll()
95
96
97 def test_suite_fail_with_missing_test(self):
98 """Test test_suite method that should fail with missing test."""
99 test_views = {'SERVER_JOB': 'GOOD',
100 'dummy_fail/control.dependency': 'TEST_NA',
101 }
102
Dan Shief1a5c02015-04-07 17:37:09 -0700103 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700104 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700105 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
106 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700107 self.mox.VerifyAll()
108
109
110 def test_suite_fail_with_unexpected_test_results(self):
111 """Test test_suite method that should fail with unexpected test results.
112 """
113 test_views = {'SERVER_JOB': 'FAIL',
114 'dummy_fail/control.dependency': 'TEST_NA',
115 'dummy_Fail.RetryFail': 'FAIL',
116 }
117
Dan Shief1a5c02015-04-07 17:37:09 -0700118 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700119 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700120 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
121 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700122 self.mox.VerifyAll()
123
124
125 def test_suite_fail_with_extra_test(self):
126 """Test test_suite method that should fail with extra test."""
127 test_views = {'SERVER_JOB': 'GOOD',
128 'dummy_fail/control.dependency': 'TEST_NA',
129 'dummy_Fail.RetryFail': 'FAIL',
130 'dummy_Fail.ExtraTest': 'GOOD',
131 }
132
Dan Shief1a5c02015-04-07 17:37:09 -0700133 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700134 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700135 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
136 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -0700137 self.mox.VerifyAll()
138
139
Dan Shi7e04fa82013-07-25 15:08:48 -0700140if __name__ == '__main__':
141 unittest.main()