blob: ce59496783305a6f6d86299b4562878c9cdf2a8d [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
Kevin Chengda4f07a2018-06-26 10:25:05 -070043
44def 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
71def 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
91if __name__ == '__main__':
92 main(sys.argv[1:])