blob: 32f353f4013754c55d10b2a78fa0667d4aa228aa [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
Dan Shi7e04fa82013-07-25 15:08:48 -07002# 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
Shuqian Zhao9866ab12016-04-05 15:55:13 -070016
Shuqian Zhao9866ab12016-04-05 15:55:13 -070017
Allen Li3d43e602016-12-08 15:09:51 -080018# Mock retry.retry used in test_push before importing test_push.
19retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func)
Dan Shi7e04fa82013-07-25 15:08:48 -070020from autotest_lib.site_utils import test_push
21
Dan Shi7e04fa82013-07-25 15:08:48 -070022class TestPushUnittests(mox.MoxTestBase):
23 """Unittest for test_push script."""
Dan Shief1a5c02015-04-07 17:37:09 -070024
Richard Barnette2af82212018-04-20 15:11:54 -070025 _ARGV = [
26 'command',
27 '--build', 'stumpy-release/R36-5881-0.0',
28 '--shard_build', 'quawks-release/R36-5881-0.0'
29 ]
30
Dan Shi7e04fa82013-07-25 15:08:48 -070031 def setUp(self):
32 """Initialize the unittest."""
33 super(TestPushUnittests, self).setUp()
34 # Overwrite expected test results.
35 test_push.EXPECTED_TEST_RESULTS = {
Alex Zamorzaevf0573b52019-04-05 12:07:59 -070036 '^SERVER_JOB$': ['GOOD'],
37 '.*control.dependency$': ['TEST_NA'],
38 '.*dummy_Fail.RetryFail$': ['FAIL', 'FAIL'],
Dan Shi7e04fa82013-07-25 15:08:48 -070039 }
Shuqian Zhao327b6952016-09-12 10:42:03 -070040 test_push.TKO = None
Dan Shi7e04fa82013-07-25 15:08:48 -070041
Dan Shief1a5c02015-04-07 17:37:09 -070042
43 def stub_out_methods(self, test_views):
Dan Shi7e04fa82013-07-25 15:08:48 -070044 """Stub out methods in test_push module with given test results.
45
46 @param test_views: Desired test result views.
Dan Shi7e04fa82013-07-25 15:08:48 -070047
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 Shi7e04fa82013-07-25 15:08:48 -070053
Dan Shia8da7602014-05-09 15:18:15 -070054 self.mox.StubOutWithMock(test_push, 'check_dut_image')
55 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
56 None)
57
Dan Shi7e04fa82013-07-25 15:08:48 -070058 self.mox.StubOutWithMock(test_push, 'do_run_suite')
Dan Shi81ddc422016-09-09 13:58:31 -070059 test_push.do_run_suite(
60 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(),
Richard Barnetteb12413a2018-04-25 01:00:27 +000061 mox.IgnoreArg()).AndReturn((1))
Dan Shi7e04fa82013-07-25 15:08:48 -070062
63 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
Dan Shi7e04fa82013-07-25 15:08:48 -070064 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -070065
66
67 def test_suite_success(self):
68 """Test test_suite method with matching results."""
Alex Zamorzaevf0573b52019-04-05 12:07:59 -070069 test_views = {'SERVER_JOB': ['GOOD'],
70 'dummy_fail/control.dependency': ['TEST_NA'],
71 'dummy_Fail.RetryFail': ['FAIL', 'FAIL'],
Dan Shi7e04fa82013-07-25 15:08:48 -070072 }
73
74 self.stub_out_methods(test_views)
75 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -070076 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
Richard Barnette2af82212018-04-20 15:11:54 -070077 arguments=test_push.parse_arguments(self._ARGV))
Dan Shi7e04fa82013-07-25 15:08:48 -070078 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 Zamorzaevf0573b52019-04-05 12:07:59 -070083 test_views = {'SERVER_JOB': ['GOOD'],
84 'dummy_fail/control.dependency': ['TEST_NA'],
Dan Shi7e04fa82013-07-25 15:08:48 -070085 }
86
Dan Shief1a5c02015-04-07 17:37:09 -070087 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -070088 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -070089 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
Richard Barnette2af82212018-04-20 15:11:54 -070090 arguments=test_push.parse_arguments(self._ARGV))
Dan Shi7e04fa82013-07-25 15:08:48 -070091 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 Zamorzaevf0573b52019-04-05 12:07:59 -070097 test_views = {'SERVER_JOB': ['FAIL'],
98 'dummy_fail/control.dependency': ['TEST_NA'],
99 'dummy_Fail.RetryFail': ['FAIL', 'FAIL'],
Dan Shi7e04fa82013-07-25 15:08:48 -0700100 }
101
Dan Shief1a5c02015-04-07 17:37:09 -0700102 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700103 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700104 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
Richard Barnette2af82212018-04-20 15:11:54 -0700105 arguments=test_push.parse_arguments(self._ARGV))
Dan Shi7e04fa82013-07-25 15:08:48 -0700106 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 Zamorzaevf0573b52019-04-05 12:07:59 -0700111 test_views = {'SERVER_JOB': ['GOOD'],
112 'dummy_fail/control.dependency': ['TEST_NA'],
113 'dummy_Fail.RetryFail': ['FAIL', 'FAIL'],
114 'dummy_Fail.ExtraTest': ['GOOD'],
Dan Shi7e04fa82013-07-25 15:08:48 -0700115 }
116
Dan Shief1a5c02015-04-07 17:37:09 -0700117 self.stub_out_methods(test_views)
Dan Shi7e04fa82013-07-25 15:08:48 -0700118 self.mox.ReplayAll()
Dan Shief1a5c02015-04-07 17:37:09 -0700119 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
Richard Barnette2af82212018-04-20 15:11:54 -0700120 arguments=test_push.parse_arguments(self._ARGV))
Dan Shi7e04fa82013-07-25 15:08:48 -0700121 self.mox.VerifyAll()
122
123
Dan Shi7e04fa82013-07-25 15:08:48 -0700124if __name__ == '__main__':
125 unittest.main()