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# pylint: disable=line-too-long 20 21import unittest 22 23from unittest import mock 24 25import unittest_utils 26 27from test_finders import test_info 28from test_runners import suite_plan_test_runner 29 30 31# pylint: disable=protected-access 32class SuitePlanTestRunnerUnittests(unittest.TestCase): 33 """Unit tests for test_suite_test_runner.py""" 34 35 def setUp(self): 36 self.suite_tr = suite_plan_test_runner.SuitePlanTestRunner(results_dir='') 37 38 def tearDown(self): 39 mock.patch.stopall() 40 41 @mock.patch('atest_utils.get_result_server_args') 42 def test_generate_run_commands(self, mock_resultargs): 43 """Test _generate_run_command method. 44 Strategy: 45 suite_name: cts --> run_cmd: cts-tradefed run commandAndExit cts 46 suite_name: cts-common --> run_cmd: 47 cts-tradefed run commandAndExit cts-common 48 """ 49 test_infos = set() 50 suite_name = 'cts' 51 t_info = test_info.TestInfo(suite_name, 52 suite_plan_test_runner.SuitePlanTestRunner.NAME, 53 {suite_name}, 54 suite=suite_name) 55 test_infos.add(t_info) 56 57 # Basic Run Cmd 58 run_cmd = [] 59 exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite_name 60 run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 61 exe=exe_cmd, 62 test=suite_name, 63 args='')) 64 mock_resultargs.return_value = [] 65 unittest_utils.assert_strict_equal( 66 self, 67 self.suite_tr.generate_run_commands(test_infos, ''), 68 run_cmd) 69 70 # Run cmd with --serial LG123456789. 71 run_cmd = [] 72 run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 73 exe=exe_cmd, 74 test=suite_name, 75 args='--serial LG123456789')) 76 unittest_utils.assert_strict_equal( 77 self, 78 self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}), 79 run_cmd) 80 81 test_infos = set() 82 suite_name = 'cts-common' 83 suite = 'cts' 84 t_info = test_info.TestInfo(suite_name, 85 suite_plan_test_runner.SuitePlanTestRunner.NAME, 86 {suite_name}, 87 suite=suite) 88 test_infos.add(t_info) 89 90 # Basic Run Cmd 91 run_cmd = [] 92 exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite 93 run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 94 exe=exe_cmd, 95 test=suite_name, 96 args='')) 97 mock_resultargs.return_value = [] 98 unittest_utils.assert_strict_equal( 99 self, 100 self.suite_tr.generate_run_commands(test_infos, ''), 101 run_cmd) 102 103 # Run cmd with --serial LG123456789. 104 run_cmd = [] 105 run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format( 106 exe=exe_cmd, 107 test=suite_name, 108 args='--serial LG123456789')) 109 unittest_utils.assert_strict_equal( 110 self, 111 self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}), 112 run_cmd) 113 114 @mock.patch('subprocess.Popen') 115 @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner, 'run') 116 @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner, 117 'generate_run_commands') 118 def test_run_tests(self, _mock_gen_cmd, _mock_run, _mock_popen): 119 """Test run_tests method.""" 120 test_infos = [] 121 extra_args = [] 122 mock_reporter = mock.Mock() 123 _mock_gen_cmd.return_value = ["cmd1", "cmd2"] 124 # Test Build Pass 125 _mock_popen.return_value.returncode = 0 126 self.assertEqual( 127 0, 128 self.suite_tr.run_tests(test_infos, extra_args, mock_reporter)) 129 130 # Test Build Pass 131 _mock_popen.return_value.returncode = 1 132 self.assertNotEqual( 133 0, 134 self.suite_tr.run_tests(test_infos, extra_args, mock_reporter)) 135 136if __name__ == '__main__': 137 unittest.main() 138