1#!/usr/bin/env python3
2#
3#   Copyright 2019 - 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.
16import os
17import subprocess
18import sys
19
20from mobly import config_parser as mobly_config_parser
21
22import acts
23from acts import base_test
24from acts import signals
25
26# The number of seconds to wait before considering the test to have timed out.
27TIMEOUT = 60
28
29
30class ActsUnitTest(base_test.BaseTestClass):
31    """A class to run the ACTS unit tests in parallel.
32
33    This is a hack to run the ACTS unit tests through CI. Please use the main
34    function below if you need to run these tests.
35    """
36
37    def test_units(self):
38        """Runs all the ACTS unit tests in test_suite.py."""
39        test_script = os.path.join(os.path.dirname(acts.__path__[0]),
40                                   'tests/test_suite.py')
41        test_process = subprocess.Popen([sys.executable, test_script],
42                                        stdout=subprocess.PIPE,
43                                        stderr=subprocess.STDOUT)
44
45        killed = False
46        try:
47            stdout, _ = test_process.communicate(timeout=TIMEOUT)
48        except subprocess.TimeoutExpired:
49            killed = True
50            self.log.error('Test %s timed out after %s seconds.' %
51                           (test_process.args, TIMEOUT))
52            test_process.kill()
53            stdout, _ = test_process.communicate()
54
55        if test_process.returncode != 0 or killed:
56            self.log.error('=' * 79)
57            self.log.error('Test %s failed with error %s.' %
58                           (test_process.args, test_process.returncode))
59            self.log.error('=' * 79)
60            self.log.error('Failure for `%s`:\n%s' %
61                           (test_process.args,
62                            stdout.decode('utf-8', errors='replace')))
63            raise signals.TestFailure(
64                'One or more unit tests failed. See the logs.')
65        else:
66            self.log.debug('Output for `%s`:\n%s' %
67                           (test_process.args,
68                            stdout.decode('utf-8', errors='replace')))
69
70
71def main():
72    test_run_config = mobly_config_parser.TestRunConfig()
73    test_run_config.testbed_name = 'UnitTests'
74    test_run_config.log_path = ''
75    ActsUnitTest(test_run_config).test_units()
76
77
78if __name__ == '__main__':
79    main()
80