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 18from importlib import import_module 19import logging 20import os 21import sys 22import sysconfig 23import unittest 24 25 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. 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 40if sys.version_info.major == 3: 41 sys.path.insert(0, os.path.dirname(sysconfig.get_paths()['purelib'])) 42 43 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:]) 93