1#!/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 mock 12 13import common 14from autotest_lib.client.common_lib.cros import retry 15from autotest_lib.server import site_utils 16from autotest_lib.server.cros.dynamic_suite import reporting 17 18 19# Mock retry.retry used in test_push before importing test_push. 20retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func) 21from 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.""" 27 28 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 } 37 test_push.TKO = None 38 39 40 def stub_out_methods(self, test_views): 41 """Stub out methods in test_push module with given test results. 42 43 @param test_views: Desired test result views. 44 45 """ 46 self.mox.UnsetStubs() 47 response = StringIO.StringIO('some_value') 48 self.mox.StubOutWithMock(urllib2, 'urlopen') 49 urllib2.urlopen(mox.IgnoreArg()).AndReturn(response) 50 51 self.mox.StubOutWithMock(test_push, 'get_default_build') 52 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( 53 'stumpy-release/R36-5881-0.0') 54 test_push.get_default_build(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( 55 'quawks-release/R36-5881-0.0') 56 57 self.mox.StubOutWithMock(test_push, 'check_dut_image') 58 test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( 59 None) 60 61 self.mox.StubOutWithMock(test_push, 'do_run_suite') 62 test_push.do_run_suite( 63 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(), 64 mox.IgnoreArg(), mox.IgnoreArg()).AndReturn((1)) 65 66 self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko') 67 site_utils.get_test_views_from_tko(1, None).AndReturn(test_views) 68 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() 79 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, 80 arguments=test_push.parse_arguments()) 81 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 90 self.stub_out_methods(test_views) 91 self.mox.ReplayAll() 92 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, 93 arguments=test_push.parse_arguments()) 94 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 105 self.stub_out_methods(test_views) 106 self.mox.ReplayAll() 107 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, 108 arguments=test_push.parse_arguments()) 109 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 120 self.stub_out_methods(test_views) 121 self.mox.ReplayAll() 122 test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views, 123 arguments=test_push.parse_arguments()) 124 self.mox.VerifyAll() 125 126 127if __name__ == '__main__': 128 unittest.main() 129