markdr | dd1893d | 2018-02-05 17:13:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 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 | |
| 17 | import os |
| 18 | import re |
| 19 | import sys |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 20 | import uuid |
| 21 | |
markdr | b2e826a | 2017-08-04 21:42:08 -0700 | [diff] [blame] | 22 | if sys.version_info < (3, ): |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 23 | import warnings |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 24 | |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 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): |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 33 | return imp.load_source(name, path) |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 34 | |
| 35 | def import_acts(): |
| 36 | return importlib.import_module('acts') |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 37 | else: |
| 38 | import importlib.machinery |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 39 | import unittest |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 40 | |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 41 | def import_module(name, path): |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 42 | return importlib.machinery.SourceFileLoader(name, path).load_module() |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 43 | |
| 44 | def import_acts(): |
| 45 | return importlib.import_module('acts') |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 46 | |
| 47 | |
| 48 | PY_FILE_REGEX = re.compile('.+\.py$') |
| 49 | |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 50 | BLACKLIST = [ |
Mark De Ruyter | acc3a53 | 2019-09-04 12:02:15 -0700 | [diff] [blame] | 51 | 'acts/controllers/rohdeschwarz_lib/contest.py', |
markdr | b2e826a | 2017-08-04 21:42:08 -0700 | [diff] [blame] | 52 | 'acts/controllers/native.py', |
| 53 | 'acts/controllers/native_android_device.py', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 54 | 'acts/controllers/packet_sender.py', |
Omar El Ayach | 7f7f10e | 2019-07-10 16:53:39 -0700 | [diff] [blame] | 55 | 'acts/test_utils/wifi/ota_chamber.py', |
Mark De Ruyter | dfeb7e8 | 2019-06-12 17:54:42 -0700 | [diff] [blame] | 56 | 'acts/controllers/buds_lib/dev_utils/proto/gen/nanopb_pb2.py', |
Omar El Ayach | 9badc4b | 2019-02-04 10:48:39 -0800 | [diff] [blame] | 57 | 'acts/test_utils/wifi/wifi_performance_test_utils.py', |
markdr | b2e826a | 2017-08-04 21:42:08 -0700 | [diff] [blame] | 58 | 'acts/test_utils/wifi/wifi_power_test_utils.py', |
Omar El Ayach | dfdbd3e | 2017-10-02 15:23:53 -0700 | [diff] [blame] | 59 | 'acts/test_utils/wifi/wifi_retail_ap.py', |
markdr | b629b32 | 2017-05-22 13:44:06 -0700 | [diff] [blame] | 60 | 'acts/test_utils/bt/bt_power_test_utils.py', |
Daniel Davee | 3332647 | 2019-02-11 17:08:01 -0800 | [diff] [blame] | 61 | 'acts/test_utils/coex/coex_test_utils.py', |
| 62 | 'acts/test_utils/tel/twilio_client.py', |
Qi | 36ec56f | 2020-01-02 11:02:41 -0800 | [diff] [blame] | 63 | 'acts/test_utils/bt/A2dpBaseTest.py', |
Abhinav Jadon | ce00b5f | 2019-11-22 10:59:26 -0800 | [diff] [blame] | 64 | 'acts/test_utils/bt/BtSarBaseTest.py', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 65 | 'tests/google/ble/beacon_tests/BeaconSwarmTest.py', |
| 66 | 'tests/google/bt/pts/BtCmdLineTest.py', |
| 67 | 'tests/google/bt/headphone_automation/SineWaveQualityTest.py', |
| 68 | 'tests/google/bt/audio_lab/BtChameleonTest.py', |
| 69 | 'tests/google/native/bt/BtNativeTest.py', |
| 70 | 'tests/google/wifi/WifiRvrTest.py', |
| 71 | 'tests/google/wifi/WifiStaApConcurrencyStressTest.py', |
| 72 | 'tests/google/wifi/WifiPasspointTest.py', |
| 73 | 'tests/google/wifi/WifiOtaTest.py', |
Xianyuan Jia | 3ff307f | 2019-03-14 12:08:45 -0700 | [diff] [blame] | 74 | 'tests/google/wifi/WifiRoamingPerformanceTest.py', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 75 | 'tests/google/wifi/WifiRssiTest.py', |
| 76 | 'tests/google/wifi/WifiPingTest.py', |
| 77 | 'tests/google/wifi/WifiThroughputStabilityTest.py', |
| 78 | 'tests/google/wifi/WifiSensitivityTest.py', |
| 79 | 'tests/google/wifi/WifiSoftApPerformanceTest.py', |
| 80 | 'tests/google/tel/live/TelLiveMobilityStressTest.py', |
| 81 | 'tests/google/tel/live/TelLiveNoSimTest.py', |
| 82 | 'tests/google/tel/live/TelLiveLockedSimTest.py', |
| 83 | 'tests/google/tel/live/TelLiveEmergencyTest.py', |
Xianyuan Jia | 89a18de | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 84 | 'tests/google/tel/live/TelLiveConnectivityMonitorTest.py', |
| 85 | 'tests/google/tel/live/TelLiveConnectivityMonitorMobilityTest.py', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 86 | 'tests/google/fuchsia/bt/FuchsiaCmdLineTest.py', |
Xianyuan Jia | 16d5321 | 2019-07-01 15:50:07 -0700 | [diff] [blame] | 87 | 'tests/google/fuchsia/bt/gatt/GattServerSetupTest.py', |
| 88 | 'tests/google/fuchsia/wlan/RebootStressTest.py', |
Jing Liang | 1e1d372 | 2019-10-21 19:01:00 -0700 | [diff] [blame] | 89 | 'acts/test_utils/gnss/gnss_testlog_utils.py', |
git status | b29b1bf | 2019-10-04 14:23:16 -0700 | [diff] [blame] | 90 | 'tests/google/fuchsia/bt/BluetoothCmdLineTest.py', |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 91 | ] |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 92 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 93 | BLACKLIST_DIRECTORIES = [ |
Xianyuan Jia | 617bd92 | 2019-09-06 11:39:07 -0700 | [diff] [blame] | 94 | 'acts/controllers/buds_lib', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 95 | 'acts/test_utils/audio_analysis_lib/', |
| 96 | 'acts/test_utils/coex/', |
| 97 | 'acts/test_utils/power/', |
| 98 | 'tests/google/coex/', |
Xianyuan Jia | 617bd92 | 2019-09-06 11:39:07 -0700 | [diff] [blame] | 99 | 'tests/google/gnss/', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 100 | 'tests/google/power/', |
Abhinav Jadon | ce00b5f | 2019-11-22 10:59:26 -0800 | [diff] [blame] | 101 | 'tests/google/bt/performance/', |
| 102 | 'tests/google/bt/sar/', |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 103 | ] |
| 104 | |
| 105 | BANNED_IMPORTS = ['mobly.controllers'] |
Mark De Ruyter | a4b9cd3 | 2018-10-17 16:26:02 -0700 | [diff] [blame] | 106 | |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 107 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 108 | class ActsImportUnitTest(unittest.TestCase): |
Mark De Ruyter | a4b9cd3 | 2018-10-17 16:26:02 -0700 | [diff] [blame] | 109 | """Test that all acts framework imports work.""" |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 110 | |
| 111 | def test_import_acts_successful(self): |
Mark De Ruyter | a4b9cd3 | 2018-10-17 16:26:02 -0700 | [diff] [blame] | 112 | """Test that importing ACTS works.""" |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 113 | acts = import_acts() |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 114 | self.assertIsNotNone(acts) |
| 115 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 116 | def test_import_framework_and_tests_successful(self): |
| 117 | """Dynamically test all imports from the framework and ACTS tests. |
| 118 | Ensure that no imports of banned packages/modules took place.""" |
Benny Peake | 3d4c4d9 | 2016-12-21 17:18:05 -0800 | [diff] [blame] | 119 | acts = import_acts() |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 120 | if hasattr(acts, '__path__') and len(acts.__path__) > 0: |
| 121 | acts_path = acts.__path__[0] |
| 122 | else: |
| 123 | acts_path = os.path.dirname(acts.__file__) |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 124 | tests_path = os.path.normpath(os.path.join(acts_path, '../../tests')) |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 125 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 126 | for base_dir in [acts_path, tests_path]: |
| 127 | for root, _, files in os.walk(base_dir): |
| 128 | for f in files: |
| 129 | full_path = os.path.join(root, f) |
| 130 | if (any(full_path.endswith(e) for e in BLACKLIST) |
| 131 | or any(e in full_path |
| 132 | for e in BLACKLIST_DIRECTORIES)): |
| 133 | continue |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 134 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 135 | path = os.path.relpath(os.path.join(root, f), os.getcwd()) |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 136 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 137 | if PY_FILE_REGEX.match(full_path): |
| 138 | with self.subTest(msg='import %s' % path): |
| 139 | fake_module_name = str(uuid.uuid4()) |
| 140 | module = import_module(fake_module_name, path) |
| 141 | self.assertIsNotNone(module) |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 142 | |
Xianyuan Jia | 0a61c9e | 2019-02-19 16:52:20 -0800 | [diff] [blame] | 143 | # Suppress verbose output on assertion failure. |
| 144 | self.longMessage = False |
| 145 | |
| 146 | for banned_import in BANNED_IMPORTS: |
Mark De Ruyter | 1457dc0 | 2019-08-01 14:38:06 -0700 | [diff] [blame] | 147 | self.assertNotIn( |
| 148 | banned_import, sys.modules, |
| 149 | 'Attempted to import the banned package/module ' |
| 150 | '%s.' % banned_import) |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 151 | |
| 152 | |
Mark De Ruyter | a4b9cd3 | 2018-10-17 16:26:02 -0700 | [diff] [blame] | 153 | if __name__ == '__main__': |
Benny Peake | 2be7f95 | 2016-12-20 15:48:13 -0800 | [diff] [blame] | 154 | unittest.main() |