1#!/usr/bin/env python3 2# 3# Copyright 2018, The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Unittests for test_suite_test_runner.""" 18 19 20import unittest 21from unittest import mock 22 23from atest import unittest_constants as uc 24from atest import unittest_utils 25from atest.logstorage import logstorage_utils 26from atest.test_finders import test_info 27from atest.test_runners import suite_plan_test_runner 28 29 30# pylint: disable=protected-access 31class SuitePlanTestRunnerUnittests(unittest.TestCase): 32 """Unit tests for test_suite_test_runner.py""" 33 34 def setUp(self): 35 self.suite_tr = suite_plan_test_runner.SuitePlanTestRunner( 36 results_dir=uc.TEST_INFO_DIR, extra_args={} 37 ) 38 39 def tearDown(self): 40 mock.patch.stopall() 41 42 @mock.patch('atest.atest_utils.get_result_server_args') 43 def test_generate_run_commands(self, mock_resultargs): 44 """Test _generate_run_command method. 45 46 Strategy: 47 48 suite_name: cts --> run_cmd: cts-tradefed run commandAndExit cts 49 suite_name: cts-common --> run_cmd: 50 cts-tradefed run commandAndExit cts-common 51 """ 52 test_infos = set() 53 suite_name = 'cts' 54 t_info = test_info.TestInfo( 55 suite_name, 56 suite_plan_test_runner.SuitePlanTestRunner.NAME, 57 {suite_name}, 58 suite=suite_name, 59 ) 60 test_infos.add(t_info) 61 62 # Basic Run Cmd 63 run_cmd = [] 64 exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite_name 65 run_cmd.append( 66 suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 67 exe=exe_cmd, test=suite_name, args='' 68 ) 69 ) 70 mock_resultargs.return_value = [] 71 unittest_utils.assert_strict_equal( 72 self, self.suite_tr.generate_run_commands(test_infos, ''), run_cmd 73 ) 74 75 # Run cmd with --serial LG123456789. 76 run_cmd = [] 77 run_cmd.append( 78 suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 79 exe=exe_cmd, test=suite_name, args='--serial LG123456789' 80 ) 81 ) 82 unittest_utils.assert_strict_equal( 83 self, 84 self.suite_tr.generate_run_commands( 85 test_infos, {'SERIAL': 'LG123456789'} 86 ), 87 run_cmd, 88 ) 89 90 test_infos = set() 91 suite_name = 'cts-common' 92 suite = 'cts' 93 t_info = test_info.TestInfo( 94 suite_name, 95 suite_plan_test_runner.SuitePlanTestRunner.NAME, 96 {suite_name}, 97 suite=suite, 98 ) 99 test_infos.add(t_info) 100 101 # Basic Run Cmd 102 run_cmd = [] 103 exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite 104 run_cmd.append( 105 suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 106 exe=exe_cmd, test=suite_name, args='' 107 ) 108 ) 109 mock_resultargs.return_value = [] 110 unittest_utils.assert_strict_equal( 111 self, self.suite_tr.generate_run_commands(test_infos, ''), run_cmd 112 ) 113 114 # Run cmd with --serial LG123456789. 115 run_cmd = [] 116 run_cmd.append( 117 suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 118 exe=exe_cmd, test=suite_name, args='--serial LG123456789' 119 ) 120 ) 121 unittest_utils.assert_strict_equal( 122 self, 123 self.suite_tr.generate_run_commands( 124 test_infos, {'SERIAL': 'LG123456789'} 125 ), 126 run_cmd, 127 ) 128 129 @mock.patch.object(logstorage_utils, 'BuildClient') 130 @mock.patch.object(logstorage_utils, 'do_upload_flow') 131 @mock.patch('atest.atest_utils.get_manifest_branch') 132 @mock.patch.object(logstorage_utils.BuildClient, 'update_invocation') 133 @mock.patch('subprocess.Popen') 134 @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner, 'run') 135 @mock.patch.object( 136 suite_plan_test_runner.SuitePlanTestRunner, 'generate_run_commands' 137 ) 138 def test_run_tests( 139 self, 140 _mock_gen_cmd, 141 _mock_run, 142 _mock_popen, 143 _mock_upd_inv, 144 _mock_get_branch, 145 _mock_do_upload_flow, 146 _mock_client, 147 ): 148 """Test run_tests method.""" 149 test_infos = [] 150 extra_args = {} 151 mock_reporter = mock.Mock() 152 _mock_gen_cmd.return_value = ['cmd1', 'cmd2'] 153 process = _mock_popen.return_value.__enter__.return_value 154 process.returncode = 0 155 process.communicate.return_value = (b'output', b'error') 156 _mock_do_upload_flow.return_value = None, None 157 158 # Test Build Pass 159 _mock_popen.return_value.returncode = 0 160 self.assertEqual( 161 0, self.suite_tr.run_tests(test_infos, extra_args, mock_reporter) 162 ) 163 164 # Test Build Pass 165 _mock_popen.return_value.returncode = 1 166 self.assertNotEqual( 167 0, self.suite_tr.run_tests(test_infos, extra_args, mock_reporter) 168 ) 169 170 171if __name__ == '__main__': 172 unittest.main() 173