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""" 16VTS Tradefed test runner class. 17""" 18 19import copy 20import logging 21 22from typing import List 23 24from atest import atest_utils 25from atest import constants 26 27from atest.atest_enum import ExitCode 28from atest.test_finders import test_info 29from atest.test_runners import atest_tf_test_runner 30 31class VtsTradefedTestRunner(atest_tf_test_runner.AtestTradefedTestRunner): 32 """TradeFed Test Runner class.""" 33 NAME = 'VtsTradefedTestRunner' 34 EXECUTABLE = 'vts10-tradefed' 35 _RUN_CMD = ('{exe} run commandAndExit {plan} -m {test} {args}') 36 _BUILD_REQ = {'vts10-tradefed-standalone'} 37 _DEFAULT_ARGS = ['--skip-all-system-status-check', 38 '--skip-preconditions', 39 '--primary-abi-only'] 40 41 def __init__(self, results_dir, **kwargs): 42 """Init stuff for vts10 tradefed runner class.""" 43 super().__init__(results_dir, **kwargs) 44 self.run_cmd_dict = {'exe': self.EXECUTABLE, 45 'test': '', 46 'args': ''} 47 48 def get_test_runner_build_reqs(self, test_infos: List[test_info.TestInfo]): 49 """Return the build requirements. 50 51 Args: 52 test_infos: List of TestInfo. 53 54 Returns: 55 Set of build targets. 56 """ 57 build_req = self._BUILD_REQ 58 build_req |= super().get_test_runner_build_reqs(test_infos) 59 return build_req 60 61 def run_tests(self, test_infos, extra_args, reporter): 62 """Run the list of test_infos. 63 64 Args: 65 test_infos: List of TestInfo. 66 extra_args: Dict of extra args to add to test run. 67 reporter: An instance of result_report.ResultReporter. 68 69 Returns: 70 Return code of the process for running tests. 71 """ 72 ret_code = ExitCode.SUCCESS 73 reporter.register_unsupported_runner(self.NAME) 74 run_cmds = self.generate_run_commands(test_infos, extra_args) 75 for run_cmd in run_cmds: 76 proc = super().run(run_cmd, output_to_stdout=True) 77 ret_code |= self.wait_for_subprocess(proc) 78 return ret_code 79 80 # pylint: disable=arguments-differ 81 def _parse_extra_args(self, extra_args): 82 """Convert the extra args into something vts10-tf can understand. 83 84 We want to transform the top-level args from atest into specific args 85 that vts10-tradefed supports. The only arg we take as is is EXTRA_ARG 86 since that is what the user intentionally wants to pass to the test 87 runner. 88 89 Args: 90 extra_args: Dict of args 91 92 Returns: 93 List of args to append. 94 """ 95 args_to_append = [] 96 args_not_supported = [] 97 for arg in extra_args: 98 if constants.SERIAL == arg: 99 args_to_append.append('--serial') 100 args_to_append.append(extra_args[arg]) 101 continue 102 if constants.CUSTOM_ARGS == arg: 103 args_to_append.extend(extra_args[arg]) 104 continue 105 if constants.DRY_RUN == arg: 106 continue 107 args_not_supported.append(arg) 108 if args_not_supported: 109 logging.info('%s does not support the following args: %s', 110 self.EXECUTABLE, args_not_supported) 111 return args_to_append 112 113 # pylint: disable=arguments-differ 114 def generate_run_commands(self, test_infos, extra_args): 115 """Generate a list of run commands from TestInfos. 116 117 Args: 118 test_infos: List of TestInfo tests to run. 119 extra_args: Dict of extra args to add to test run. 120 121 Returns: 122 A List of strings that contains the vts10-tradefed run command. 123 """ 124 cmds = [] 125 args = self._DEFAULT_ARGS 126 args.extend(self._parse_extra_args(extra_args)) 127 args.extend(atest_utils.get_result_server_args()) 128 for test_info in test_infos: 129 cmd_dict = copy.deepcopy(self.run_cmd_dict) 130 cmd_dict['plan'] = constants.VTS_STAGING_PLAN 131 cmd_dict['test'] = test_info.test_name 132 cmd_dict['args'] = ' '.join(args) 133 cmds.append(self._RUN_CMD.format(**cmd_dict)) 134 return cmds 135