1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*c2e18aaaSAndroid Build Coastguard Worker# 3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2018 The Android Open Source Project 4*c2e18aaaSAndroid Build Coastguard Worker# 5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*c2e18aaaSAndroid Build Coastguard Worker# 11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard Worker"""Main entrypoint for all of atest's unittest.""" 18*c2e18aaaSAndroid Build Coastguard Worker 19*c2e18aaaSAndroid Build Coastguard Workerfrom importlib import import_module 20*c2e18aaaSAndroid Build Coastguard Workerimport logging 21*c2e18aaaSAndroid Build Coastguard Workerimport os 22*c2e18aaaSAndroid Build Coastguard Workerimport sys 23*c2e18aaaSAndroid Build Coastguard Workerimport unittest 24*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock 25*c2e18aaaSAndroid Build Coastguard Worker 26*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_constants 27*c2e18aaaSAndroid Build Coastguard Worker 28*c2e18aaaSAndroid Build Coastguard Worker# Setup logging to be silent so unittests can pass through TF. 29*c2e18aaaSAndroid Build Coastguard Workerlogging.disable(logging.ERROR) 30*c2e18aaaSAndroid Build Coastguard Worker 31*c2e18aaaSAndroid Build Coastguard Worker# TODO: (b/275449248) remove these mock variables and create these mock on 32*c2e18aaaSAndroid Build Coastguard Worker# demand in class-wide. 33*c2e18aaaSAndroid Build Coastguard WorkerENV = { 34*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_BUILD_TOP': '/', 35*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_PRODUCT_OUT': '/out/prod', 36*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_TARGET_OUT_TESTCASES': '/out/prod/tcases', 37*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_HOST_OUT': '/out/host', 38*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_HOST_OUT_TESTCASES': '/out/host/tcases', 39*c2e18aaaSAndroid Build Coastguard Worker 'TARGET_PRODUCT': 'aosp_cf_x86_64', 40*c2e18aaaSAndroid Build Coastguard Worker 'TARGET_BUILD_VARIANT': 'userdebug', 41*c2e18aaaSAndroid Build Coastguard Worker} 42*c2e18aaaSAndroid Build Coastguard Worker 43*c2e18aaaSAndroid Build Coastguard Worker 44*c2e18aaaSAndroid Build Coastguard Workerdef get_test_modules(): 45*c2e18aaaSAndroid Build Coastguard Worker """Returns a list of testable modules. 46*c2e18aaaSAndroid Build Coastguard Worker 47*c2e18aaaSAndroid Build Coastguard Worker Finds all the test files (*_unittest.py) and get their no-absolute 48*c2e18aaaSAndroid Build Coastguard Worker path (internal/lib/utils_test.py) and translate it to an import path and 49*c2e18aaaSAndroid Build Coastguard Worker strip the py ext (internal.lib.utils_test). 50*c2e18aaaSAndroid Build Coastguard Worker 51*c2e18aaaSAndroid Build Coastguard Worker Returns: 52*c2e18aaaSAndroid Build Coastguard Worker List of strings (the testable module import path). 53*c2e18aaaSAndroid Build Coastguard Worker """ 54*c2e18aaaSAndroid Build Coastguard Worker testable_modules = [] 55*c2e18aaaSAndroid Build Coastguard Worker package = unittest_constants.ATEST_PKG_DIR 56*c2e18aaaSAndroid Build Coastguard Worker base_path = os.path.dirname(package) 57*c2e18aaaSAndroid Build Coastguard Worker 58*c2e18aaaSAndroid Build Coastguard Worker for dirpath, _, files in os.walk(package): 59*c2e18aaaSAndroid Build Coastguard Worker for f in files: 60*c2e18aaaSAndroid Build Coastguard Worker if f.endswith('_unittest.py') or f.endswith('_unittest.pyc'): 61*c2e18aaaSAndroid Build Coastguard Worker # Now transform it into a no-absolute import path. 62*c2e18aaaSAndroid Build Coastguard Worker full_file_path = os.path.join(dirpath, f) 63*c2e18aaaSAndroid Build Coastguard Worker rel_file_path = os.path.relpath(full_file_path, base_path) 64*c2e18aaaSAndroid Build Coastguard Worker rel_file_path, _ = os.path.splitext(rel_file_path) 65*c2e18aaaSAndroid Build Coastguard Worker rel_file_path = rel_file_path.replace(os.sep, '.') 66*c2e18aaaSAndroid Build Coastguard Worker testable_modules.append(rel_file_path) 67*c2e18aaaSAndroid Build Coastguard Worker 68*c2e18aaaSAndroid Build Coastguard Worker return testable_modules 69*c2e18aaaSAndroid Build Coastguard Worker 70*c2e18aaaSAndroid Build Coastguard Worker 71*c2e18aaaSAndroid Build Coastguard Workerdef run_test_modules(test_modules): 72*c2e18aaaSAndroid Build Coastguard Worker """Main method of running unit tests. 73*c2e18aaaSAndroid Build Coastguard Worker 74*c2e18aaaSAndroid Build Coastguard Worker Args: test_modules; a list of module names. 75*c2e18aaaSAndroid Build Coastguard Worker 76*c2e18aaaSAndroid Build Coastguard Worker Returns: 77*c2e18aaaSAndroid Build Coastguard Worker result: a namespace of unittest result. 78*c2e18aaaSAndroid Build Coastguard Worker """ 79*c2e18aaaSAndroid Build Coastguard Worker for mod in test_modules: 80*c2e18aaaSAndroid Build Coastguard Worker import_module(mod) 81*c2e18aaaSAndroid Build Coastguard Worker 82*c2e18aaaSAndroid Build Coastguard Worker loader = unittest.defaultTestLoader 83*c2e18aaaSAndroid Build Coastguard Worker test_suite = loader.loadTestsFromNames(test_modules) 84*c2e18aaaSAndroid Build Coastguard Worker runner = unittest.TextTestRunner(verbosity=2) 85*c2e18aaaSAndroid Build Coastguard Worker return runner.run(test_suite) 86*c2e18aaaSAndroid Build Coastguard Worker 87*c2e18aaaSAndroid Build Coastguard Worker 88*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__': 89*c2e18aaaSAndroid Build Coastguard Worker print(sys.version_info) 90*c2e18aaaSAndroid Build Coastguard Worker with mock.patch.dict('os.environ', ENV): 91*c2e18aaaSAndroid Build Coastguard Worker result = run_test_modules(get_test_modules()) 92*c2e18aaaSAndroid Build Coastguard Worker if not result.wasSuccessful(): 93*c2e18aaaSAndroid Build Coastguard Worker sys.exit(not result.wasSuccessful()) 94*c2e18aaaSAndroid Build Coastguard Worker sys.exit(0) 95