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