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