1#!/usr/bin/env python3 2# 3# Copyright 2016 - 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 17import os 18import re 19import sys 20import uuid 21 22if sys.version_info < (3, ): 23 import warnings 24 25 with warnings.catch_warnings(): 26 warnings.filterwarnings('ignore', category=PendingDeprecationWarning) 27 import imp 28 29 import importlib 30 import unittest2 as unittest 31 32 def import_module(name, path): 33 return imp.load_source(name, path) 34 35 def import_acts(): 36 return importlib.import_module('acts') 37else: 38 import importlib.machinery 39 import unittest 40 41 def import_module(name, path): 42 return importlib.machinery.SourceFileLoader(name, path).load_module() 43 44 def import_acts(): 45 return importlib.import_module('acts') 46 47 48PY_FILE_REGEX = re.compile('.+\.py$') 49 50BLACKLIST = [ 51 'acts/controllers/native.py', 52 'acts/controllers/native_android_device.py', 53 'acts/controllers/packet_sender.py', 54 'acts/controllers/buds_lib/dev_utils/proto/gen/nanopb_pb2.py', 55 'acts/controllers/buds_lib/data_storage/bigquery/bigquery_logger_utils.py', 56 'acts/controllers/buds_lib/data_storage/bigquery/test_bigquery_utils.py', 57 'acts/controllers/buds_lib/data_storage/bigquery/test_bigquery_logger.py', 58 'acts/controllers/buds_lib/data_storage/bigquery/bigquery_buffer.py', 59 'acts/controllers/buds_lib/data_storage/bigquery/bigquery_logger.py', 60 'acts/controllers/buds_lib/data_storage/bigquery/bigquery_scheduled_automatic_client.py', 61 'acts/controllers/buds_lib/data_storage/_sponge/sponge_client_lite.py', 62 'acts/test_utils/wifi/wifi_performance_test_utils.py', 63 'acts/test_utils/wifi/wifi_power_test_utils.py', 64 'acts/test_utils/wifi/wifi_retail_ap.py', 65 'acts/test_utils/bt/bt_power_test_utils.py', 66 'acts/test_utils/coex/coex_test_utils.py', 67 'acts/test_utils/tel/twilio_client.py', 68 'acts/test_utils/bt/A2dpCodecBaseTest.py', 69 'acts/test_utils/bt/BtRangeBaseTest.py', 70 'tests/google/ble/beacon_tests/BeaconSwarmTest.py', 71 'tests/google/bt/pts/BtCmdLineTest.py', 72 'tests/google/bt/headphone_automation/SineWaveQualityTest.py', 73 'tests/google/bt/audio_lab/BtChameleonTest.py', 74 'tests/google/native/bt/BtNativeTest.py', 75 'tests/google/wifi/WifiRvrTest.py', 76 'tests/google/wifi/WifiStaApConcurrencyStressTest.py', 77 'tests/google/wifi/WifiPasspointTest.py', 78 'tests/google/wifi/WifiOtaTest.py', 79 'tests/google/wifi/WifiRoamingPerformanceTest.py', 80 'tests/google/wifi/WifiRssiTest.py', 81 'tests/google/wifi/WifiPingTest.py', 82 'tests/google/wifi/WifiThroughputStabilityTest.py', 83 'tests/google/wifi/WifiSensitivityTest.py', 84 'tests/google/wifi/WifiSoftApPerformanceTest.py', 85 'tests/google/tel/live/TelLiveMobilityStressTest.py', 86 'tests/google/tel/live/TelLiveNoSimTest.py', 87 'tests/google/tel/live/TelLiveLockedSimTest.py', 88 'tests/google/tel/live/TelLiveEmergencyTest.py', 89 'tests/google/tel/live/TelLiveConnectivityMonitorTest.py', 90 'tests/google/tel/live/TelLiveConnectivityMonitorMobilityTest.py', 91 'tests/google/fuchsia/bt/FuchsiaCmdLineTest.py', 92 'tests/google/fuchsia/bt/gatt/GattServerSetupTest.py' 93] 94 95BLACKLIST_DIRECTORIES = [ 96 'acts/test_utils/audio_analysis_lib/', 97 'acts/test_utils/coex/', 98 'acts/test_utils/power/', 99 'tests/google/coex/', 100 'tests/google/power/', 101 'tests/google/bt/performance/' 102] 103 104BANNED_IMPORTS = ['mobly.controllers'] 105 106 107class ActsImportUnitTest(unittest.TestCase): 108 """Test that all acts framework imports work.""" 109 110 def test_import_acts_successful(self): 111 """Test that importing ACTS works.""" 112 acts = import_acts() 113 self.assertIsNotNone(acts) 114 115 def test_import_framework_and_tests_successful(self): 116 """Dynamically test all imports from the framework and ACTS tests. 117 Ensure that no imports of banned packages/modules took place.""" 118 acts = import_acts() 119 if hasattr(acts, '__path__') and len(acts.__path__) > 0: 120 acts_path = acts.__path__[0] 121 else: 122 acts_path = os.path.dirname(acts.__file__) 123 tests_path = os.path.normpath(os.path.join(acts_path, '../../tests')) 124 125 for base_dir in [acts_path, tests_path]: 126 for root, _, files in os.walk(base_dir): 127 for f in files: 128 full_path = os.path.join(root, f) 129 if (any(full_path.endswith(e) for e in BLACKLIST) 130 or any(e in full_path 131 for e in BLACKLIST_DIRECTORIES)): 132 continue 133 134 path = os.path.relpath(os.path.join(root, f), os.getcwd()) 135 136 if PY_FILE_REGEX.match(full_path): 137 with self.subTest(msg='import %s' % path): 138 fake_module_name = str(uuid.uuid4()) 139 module = import_module(fake_module_name, path) 140 self.assertIsNotNone(module) 141 142 # Suppress verbose output on assertion failure. 143 self.longMessage = False 144 145 for banned_import in BANNED_IMPORTS: 146 self.assertNotIn(banned_import, sys.modules, 147 'Attempted to import the banned package/module ' 148 '%s.' % banned_import) 149 150 151if __name__ == '__main__': 152 unittest.main() 153