blob: 62d04713d87cd30b6cfd59bdd5d6a31aa45c0b5b [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
Allen Li3d43e602016-12-08 15:09:51 -080011import mock
12
Dan Shi7e04fa82013-07-25 15:08:48 -070013import common
Shuqian Zhao9866ab12016-04-05 15:55:13 -070014from autotest_lib.client.common_lib.cros import retry
Dan Shi7e04fa82013-07-25 15:08:48 -070015from autotest_lib.server import site_utils
Dan Shi7e04fa82013-07-25 15:08:48 -070016from autotest_lib.server.cros.dynamic_suite import reporting
Shuqian Zhao9866ab12016-04-05 15:55:13 -070017
Shuqian Zhao9866ab12016-04-05 15:55:13 -070018
Allen Li3d43e602016-12-08 15:09:51 -080019# Mock retry.retry used in test_push before importing test_push.
20retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func)
Dan Shi7e04fa82013-07-25 15:08:48 -070021from autotest_lib.site_utils import test_push
22
23AUTOFILED_COUNT_2 = '%s2' % reporting.Reporter.AUTOFILED_COUNT
24
25class TestPushUnittests(mox.MoxTestBase):
26 """Unittest for test_push script."""
Dan Shief1a5c02015-04-07 17:37:09 -070027
Dan Shi7e04fa82013-07-25 15:08:48 -070028 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 Zhao327b6952016-09-12 10:42:03 -070037 test_push.TKO = None
Dan Shi7e04fa82013-07-25 15:08:48 -070038
Dan Shief1a5c02015-04-07 17:37:09 -070039
40 def stub_out_methods(self, test_views):
Dan Shi7e04fa82013-07-25 15:08:48 -070041 """Stub out methods in test_push module with given test results.
42
43 @param test_views: Desired test result views.
Dan Shi7e04fa82013-07-25 15:08:48 -070044
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 Shi7e04fa82013-07-25 15:08:48 -070050
Dan Shi5ba5d2e2014-05-09 13:47:00 -070051 self.mox.StubOutWithMock(test_push, 'get_default_build')
Kevin Chenge691ce92016-12-15 12:17:13 -080052 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
Dan Shi5ba5d2e2014-05-09 13:47:00 -070053 'stumpy-release/R36-5881-0.0')
Kevin Chenge691ce92016-12-15 12:17:13 -080054 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
Jakob Juelich8f143912014-10-10 14:08:05 -070055 'quawks-release/R36-5881-0.0')
Dan Shi5ba5d2e2014-05-09 13:47:00 -070056
Dan Shia8da7602014-05-09 15:18:15 -070057 self.mox.StubOutWithMock(test_push, 'check_dut_image')
58 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
59 None)
60
Dan Shi7e04fa82013-07-25 15:08:48 -070061 self.mox.StubOutWithMock(test_push, 'do_run_suite')
Dan Shi81ddc422016-09-09 13:58:31 -070062 test_push.do_run_suite(
63 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(),
64 mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1))
Dan Shi7e04fa82013-07-25 15:08:48 -070065
66 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
Dan Shi7e04fa82013-07-25 15:08:48 -070067 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -070068
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 Shief1a5c02015-04-07 17:37:09 -070079 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
80 arguments=test_push.parse_arguments())
Dan Shi7e04fa82013-07-25 15:08:48 -070081 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 Shief1a5c02015-04-07 17:37:09 -070090 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -070091 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_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 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_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 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
Dan Shi7e04fa82013-07-25 15:08:48 -0700127if __name__ == '__main__':
128 unittest.main()