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