1# Copyright 2018, The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15""" 16SUITE Tradefed test runner class. 17""" 18 19import copy 20import logging 21 22# pylint: disable=import-error 23from test_runners import atest_tf_test_runner 24import atest_utils 25import constants 26 27 28class SuitePlanTestRunner(atest_tf_test_runner.AtestTradefedTestRunner): 29 """Suite Plan Test Runner class.""" 30 NAME = 'SuitePlanTestRunner' 31 EXECUTABLE = '%s-tradefed' 32 _RUN_CMD = ('{exe} run commandAndExit {test} {args}') 33 34 def __init__(self, results_dir, **kwargs): 35 """Init stuff for suite tradefed runner class.""" 36 super(SuitePlanTestRunner, self).__init__(results_dir, **kwargs) 37 self.run_cmd_dict = {'exe': '', 38 'test': '', 39 'args': ''} 40 41 def get_test_runner_build_reqs(self): 42 """Return the build requirements. 43 44 Returns: 45 Set of build targets. 46 """ 47 build_req = set() 48 build_req |= super(SuitePlanTestRunner, 49 self).get_test_runner_build_reqs() 50 return build_req 51 52 def run_tests(self, test_infos, extra_args, reporter): 53 """Run the list of test_infos. 54 Args: 55 test_infos: List of TestInfo. 56 extra_args: Dict of extra args to add to test run. 57 reporter: An instance of result_report.ResultReporter. 58 59 Returns: 60 Return code of the process for running tests. 61 """ 62 reporter.register_unsupported_runner(self.NAME) 63 run_cmds = self.generate_run_commands(test_infos, extra_args) 64 ret_code = constants.EXIT_CODE_SUCCESS 65 for run_cmd in run_cmds: 66 proc = super(SuitePlanTestRunner, self).run(run_cmd, 67 output_to_stdout=True) 68 ret_code |= self.wait_for_subprocess(proc) 69 return ret_code 70 71 def _parse_extra_args(self, extra_args): 72 """Convert the extra args into something *ts-tf can understand. 73 74 We want to transform the top-level args from atest into specific args 75 that *ts-tradefed supports. The only arg we take as is 76 EXTRA_ARG since that is what the user intentionally wants to pass to 77 the test runner. 78 79 Args: 80 extra_args: Dict of args 81 82 Returns: 83 List of args to append. 84 """ 85 args_to_append = [] 86 args_not_supported = [] 87 for arg in extra_args: 88 if constants.SERIAL == arg: 89 args_to_append.append('--serial') 90 args_to_append.append(extra_args[arg]) 91 continue 92 if constants.CUSTOM_ARGS == arg: 93 args_to_append.extend(extra_args[arg]) 94 continue 95 if constants.DRY_RUN == arg: 96 continue 97 args_not_supported.append(arg) 98 if args_not_supported: 99 logging.info('%s does not support the following args: %s', 100 self.EXECUTABLE, args_not_supported) 101 return args_to_append 102 103 # pylint: disable=arguments-differ 104 def generate_run_commands(self, test_infos, extra_args): 105 """Generate a list of run commands from TestInfos. 106 107 Args: 108 test_infos: List of TestInfo tests to run. 109 extra_args: Dict of extra args to add to test run. 110 111 Returns: 112 A List of strings that contains the run command 113 which *ts-tradefed supports. 114 """ 115 cmds = [] 116 args = [] 117 args.extend(self._parse_extra_args(extra_args)) 118 args.extend(atest_utils.get_result_server_args()) 119 for test_info in test_infos: 120 cmd_dict = copy.deepcopy(self.run_cmd_dict) 121 cmd_dict['test'] = test_info.test_name 122 cmd_dict['args'] = ' '.join(args) 123 cmd_dict['exe'] = self.EXECUTABLE % test_info.suite 124 cmds.append(self._RUN_CMD.format(**cmd_dict)) 125 return cmds 126