blob: a8137bd6168d2d437b00469913dd8443128eae6c [file] [log] [blame]
Kevin Chengda4f07a2018-06-26 10:25:05 -07001#!/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
18from importlib import import_module
Kevin Chengcecb7fe2018-10-02 10:40:18 -070019import logging
Kevin Chengda4f07a2018-06-26 10:25:05 -070020import os
21import sys
chojoyce2a82d302019-12-24 18:13:36 +080022import sysconfig
Kevin Chengda4f07a2018-06-26 10:25:05 -070023import unittest
24
herbertxue1512f8a2019-06-27 13:56:23 +080025
Kevin Chengcecb7fe2018-10-02 10:40:18 -070026# 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.
30OAUTH2_LOGGER = logging.getLogger('oauth2client.contrib.multistore_file')
31OAUTH2_LOGGER.setLevel(logging.CRITICAL)
32OAUTH2_LOGGER.addHandler(logging.FileHandler("/dev/null"))
33
34# Setup logging to be silent so unittests can pass through TF.
35ACLOUD_LOGGER = "acloud"
36logger = logging.getLogger(ACLOUD_LOGGER)
37logger.setLevel(logging.CRITICAL)
38logger.addHandler(logging.FileHandler("/dev/null"))
39
chojoyce2a82d302019-12-24 18:13:36 +080040if sys.version_info.major == 3:
41 sys.path.insert(0, os.path.dirname(sysconfig.get_paths()['purelib']))
42
Jim Tangd2b82222022-03-23 10:29:11 +080043# (b/219847353) Move googleapiclient to the last position of sys.path when
44# existed.
45for lib in sys.path:
46 if 'googleapiclient' in lib:
47 sys.path.remove(lib)
48 sys.path.append(lib)
49 break
50
Kevin Chengda4f07a2018-06-26 10:25:05 -070051
52def GetTestModules():
53 """Return list of testable modules.
54
55 We need to find all the test files (*_test.py) and get their relative
56 path (internal/lib/utils_test.py) and translate it to an import path and
57 strip the py ext (internal.lib.utils_test).
58
59 Returns:
60 List of strings (the testable module import path).
61 """
62 testable_modules = []
63 base_path = os.path.dirname(os.path.realpath(__file__))
64
65 # Get list of all python files that end in _test.py (except for __file__).
66 for dirpath, _, files in os.walk(base_path):
67 for f in files:
68 if f.endswith("_test.py") and f != os.path.basename(__file__):
69 # Now transform it into a relative import path.
70 full_file_path = os.path.join(dirpath, f)
71 rel_file_path = os.path.relpath(full_file_path, base_path)
72 rel_file_path, _ = os.path.splitext(rel_file_path)
73 rel_file_path = rel_file_path.replace(os.sep, ".")
74 testable_modules.append(rel_file_path)
75
76 return testable_modules
77
78
79def main(_):
80 """Main unittest entry.
81
82 Args:
83 argv: A list of system arguments. (unused)
84
85 Returns:
86 0 if success. None-zero if fails.
87 """
88 test_modules = GetTestModules()
89 for mod in test_modules:
90 import_module(mod)
91
92 loader = unittest.defaultTestLoader
93 test_suite = loader.loadTestsFromNames(test_modules)
94 runner = unittest.TextTestRunner(verbosity=2)
95 result = runner.run(test_suite)
96 sys.exit(not result.wasSuccessful())
97
98
99if __name__ == '__main__':
100 main(sys.argv[1:])