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"""
16Regression Detection test runner class.
17"""
18
19import constants
20
21from test_runners import test_runner_base
22
23
24class RegressionTestRunner(test_runner_base.TestRunnerBase):
25    """Regression Test Runner class."""
26    NAME = 'RegressionTestRunner'
27    EXECUTABLE = 'tradefed.sh'
28    _RUN_CMD = '{exe} run commandAndExit regression -n {args}'
29    _BUILD_REQ = {'tradefed-core', constants.ATEST_TF_MODULE}
30
31    def __init__(self, results_dir):
32        """Init stuff for base class."""
33        super(RegressionTestRunner, self).__init__(results_dir)
34        self.run_cmd_dict = {'exe': self.EXECUTABLE,
35                             'args': ''}
36
37    # pylint: disable=unused-argument
38    def run_tests(self, test_infos, extra_args, reporter):
39        """Run the list of test_infos.
40
41        Args:
42            test_infos: List of TestInfo.
43            extra_args: Dict of args to add to regression detection test run.
44            reporter: A ResultReporter instance.
45
46        Returns:
47            Return code of the process for running tests.
48        """
49        run_cmds = self.generate_run_commands(test_infos, extra_args)
50        proc = super(RegressionTestRunner, self).run(run_cmds[0],
51                                                     output_to_stdout=True)
52        proc.wait()
53        return proc.returncode
54
55    # pylint: disable=unnecessary-pass
56    # Please keep above disable flag to ensure host_env_check is overriden.
57    def host_env_check(self):
58        """Check that host env has everything we need.
59
60        We actually can assume the host env is fine because we have the same
61        requirements that atest has. Update this to check for android env vars
62        if that changes.
63        """
64        pass
65
66    def get_test_runner_build_reqs(self):
67        """Return the build requirements.
68
69        Returns:
70            Set of build targets.
71        """
72        return self._BUILD_REQ
73
74    # pylint: disable=unused-argument
75    def generate_run_commands(self, test_infos, extra_args, port=None):
76        """Generate a list of run commands from TestInfos.
77
78        Args:
79            test_infos: A set of TestInfo instances.
80            extra_args: A Dict of extra args to append.
81            port: Optional. An int of the port number to send events to.
82                  Subprocess reporter in TF won't try to connect if it's None.
83
84        Returns:
85            A list that contains the string of atest tradefed run command.
86            Only one command is returned.
87        """
88        pre = extra_args.pop(constants.PRE_PATCH_FOLDER)
89        post = extra_args.pop(constants.POST_PATCH_FOLDER)
90        args = ['--pre-patch-metrics', pre, '--post-patch-metrics', post]
91        self.run_cmd_dict['args'] = ' '.join(args)
92        run_cmd = self._RUN_CMD.format(**self.run_cmd_dict)
93        return [run_cmd]
94