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