Kevin Cheng | da4f07a | 2018-06-26 10:25:05 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2018 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. |
| 16 | """Main entry point for all of acloud's unittest.""" |
| 17 | |
| 18 | from importlib import import_module |
Kevin Cheng | cecb7fe | 2018-10-02 10:40:18 -0700 | [diff] [blame] | 19 | import logging |
Kevin Cheng | da4f07a | 2018-06-26 10:25:05 -0700 | [diff] [blame] | 20 | import os |
| 21 | import sys |
chojoyce | 2a82d30 | 2019-12-24 18:13:36 +0800 | [diff] [blame] | 22 | import sysconfig |
Kevin Cheng | da4f07a | 2018-06-26 10:25:05 -0700 | [diff] [blame] | 23 | import unittest |
| 24 | |
herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 25 | |
Kevin Cheng | cecb7fe | 2018-10-02 10:40:18 -0700 | [diff] [blame] | 26 | # Needed to silence oauth2client. |
| 27 | # This is a workaround to get rid of below warning message: |
| 28 | # 'No handlers could be found for logger "oauth2client.contrib.multistore_file' |
| 29 | # TODO(b/112803893): Remove this code once bug is fixed. |
| 30 | OAUTH2_LOGGER = logging.getLogger('oauth2client.contrib.multistore_file') |
| 31 | OAUTH2_LOGGER.setLevel(logging.CRITICAL) |
| 32 | OAUTH2_LOGGER.addHandler(logging.FileHandler("/dev/null")) |
| 33 | |
| 34 | # Setup logging to be silent so unittests can pass through TF. |
| 35 | ACLOUD_LOGGER = "acloud" |
| 36 | logger = logging.getLogger(ACLOUD_LOGGER) |
| 37 | logger.setLevel(logging.CRITICAL) |
| 38 | logger.addHandler(logging.FileHandler("/dev/null")) |
| 39 | |
chojoyce | 2a82d30 | 2019-12-24 18:13:36 +0800 | [diff] [blame] | 40 | if sys.version_info.major == 3: |
| 41 | sys.path.insert(0, os.path.dirname(sysconfig.get_paths()['purelib'])) |
| 42 | |
Kevin Cheng | da4f07a | 2018-06-26 10:25:05 -0700 | [diff] [blame] | 43 | |
| 44 | def GetTestModules(): |
| 45 | """Return list of testable modules. |
| 46 | |
| 47 | We need to find all the test files (*_test.py) and get their relative |
| 48 | path (internal/lib/utils_test.py) and translate it to an import path and |
| 49 | strip the py ext (internal.lib.utils_test). |
| 50 | |
| 51 | Returns: |
| 52 | List of strings (the testable module import path). |
| 53 | """ |
| 54 | testable_modules = [] |
| 55 | base_path = os.path.dirname(os.path.realpath(__file__)) |
| 56 | |
| 57 | # Get list of all python files that end in _test.py (except for __file__). |
| 58 | for dirpath, _, files in os.walk(base_path): |
| 59 | for f in files: |
| 60 | if f.endswith("_test.py") and f != os.path.basename(__file__): |
| 61 | # Now transform it into a relative import path. |
| 62 | full_file_path = os.path.join(dirpath, f) |
| 63 | rel_file_path = os.path.relpath(full_file_path, base_path) |
| 64 | rel_file_path, _ = os.path.splitext(rel_file_path) |
| 65 | rel_file_path = rel_file_path.replace(os.sep, ".") |
| 66 | testable_modules.append(rel_file_path) |
| 67 | |
| 68 | return testable_modules |
| 69 | |
| 70 | |
| 71 | def main(_): |
| 72 | """Main unittest entry. |
| 73 | |
| 74 | Args: |
| 75 | argv: A list of system arguments. (unused) |
| 76 | |
| 77 | Returns: |
| 78 | 0 if success. None-zero if fails. |
| 79 | """ |
| 80 | test_modules = GetTestModules() |
| 81 | for mod in test_modules: |
| 82 | import_module(mod) |
| 83 | |
| 84 | loader = unittest.defaultTestLoader |
| 85 | test_suite = loader.loadTestsFromNames(test_modules) |
| 86 | runner = unittest.TextTestRunner(verbosity=2) |
| 87 | result = runner.run(test_suite) |
| 88 | sys.exit(not result.wasSuccessful()) |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main(sys.argv[1:]) |