blob: 66ad81dc344d678e5c7a85e7101bc55345758443 [file] [log] [blame]
markdrdd1893d2018-02-05 17:13:47 -08001#!/usr/bin/env python3
Benny Peake2be7f952016-12-20 15:48:13 -08002#
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
Benny Peake2be7f952016-12-20 15:48:13 -080020import uuid
21
markdrb2e826a2017-08-04 21:42:08 -070022if sys.version_info < (3, ):
Benny Peake3d4c4d92016-12-21 17:18:05 -080023 import warnings
Benny Peake2be7f952016-12-20 15:48:13 -080024
Benny Peake3d4c4d92016-12-21 17:18:05 -080025 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 Peake2be7f952016-12-20 15:48:13 -080033 return imp.load_source(name, path)
Benny Peake3d4c4d92016-12-21 17:18:05 -080034
35 def import_acts():
36 return importlib.import_module('acts')
Benny Peake2be7f952016-12-20 15:48:13 -080037else:
38 import importlib.machinery
Benny Peake3d4c4d92016-12-21 17:18:05 -080039 import unittest
Benny Peake2be7f952016-12-20 15:48:13 -080040
Benny Peake3d4c4d92016-12-21 17:18:05 -080041 def import_module(name, path):
Benny Peake2be7f952016-12-20 15:48:13 -080042 return importlib.machinery.SourceFileLoader(name, path).load_module()
Benny Peake3d4c4d92016-12-21 17:18:05 -080043
44 def import_acts():
45 return importlib.import_module('acts')
Benny Peake2be7f952016-12-20 15:48:13 -080046
47
48PY_FILE_REGEX = re.compile('.+\.py$')
49
Benny Peake3d4c4d92016-12-21 17:18:05 -080050BLACKLIST = [
Mark De Ruyteracc3a532019-09-04 12:02:15 -070051 'acts/controllers/rohdeschwarz_lib/contest.py',
markdrb2e826a2017-08-04 21:42:08 -070052 'acts/controllers/native.py',
53 'acts/controllers/native_android_device.py',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -080054 'acts/controllers/packet_sender.py',
Omar El Ayach7f7f10e2019-07-10 16:53:39 -070055 'acts/test_utils/wifi/ota_chamber.py',
Mark De Ruyterdfeb7e82019-06-12 17:54:42 -070056 'acts/controllers/buds_lib/dev_utils/proto/gen/nanopb_pb2.py',
Omar El Ayach9badc4b2019-02-04 10:48:39 -080057 'acts/test_utils/wifi/wifi_performance_test_utils.py',
markdrb2e826a2017-08-04 21:42:08 -070058 'acts/test_utils/wifi/wifi_power_test_utils.py',
Omar El Ayachdfdbd3e2017-10-02 15:23:53 -070059 'acts/test_utils/wifi/wifi_retail_ap.py',
markdrb629b322017-05-22 13:44:06 -070060 'acts/test_utils/bt/bt_power_test_utils.py',
Daniel Davee33326472019-02-11 17:08:01 -080061 'acts/test_utils/coex/coex_test_utils.py',
62 'acts/test_utils/tel/twilio_client.py',
Qi36ec56f2020-01-02 11:02:41 -080063 'acts/test_utils/bt/A2dpBaseTest.py',
Abhinav Jadonce00b5f2019-11-22 10:59:26 -080064 'acts/test_utils/bt/BtSarBaseTest.py',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -080065 '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 Jia3ff307f2019-03-14 12:08:45 -070074 'tests/google/wifi/WifiRoamingPerformanceTest.py',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -080075 '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 Jia89a18de2019-02-19 16:52:20 -080084 'tests/google/tel/live/TelLiveConnectivityMonitorTest.py',
85 'tests/google/tel/live/TelLiveConnectivityMonitorMobilityTest.py',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -080086 'tests/google/fuchsia/bt/FuchsiaCmdLineTest.py',
Xianyuan Jia16d53212019-07-01 15:50:07 -070087 'tests/google/fuchsia/bt/gatt/GattServerSetupTest.py',
88 'tests/google/fuchsia/wlan/RebootStressTest.py',
Jing Liang1e1d3722019-10-21 19:01:00 -070089 'acts/test_utils/gnss/gnss_testlog_utils.py',
git statusb29b1bf2019-10-04 14:23:16 -070090 'tests/google/fuchsia/bt/BluetoothCmdLineTest.py',
Benny Peake3d4c4d92016-12-21 17:18:05 -080091]
Benny Peake2be7f952016-12-20 15:48:13 -080092
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -080093BLACKLIST_DIRECTORIES = [
Xianyuan Jia617bd922019-09-06 11:39:07 -070094 'acts/controllers/buds_lib',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -080095 'acts/test_utils/audio_analysis_lib/',
96 'acts/test_utils/coex/',
97 'acts/test_utils/power/',
98 'tests/google/coex/',
Xianyuan Jia617bd922019-09-06 11:39:07 -070099 'tests/google/gnss/',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800100 'tests/google/power/',
Abhinav Jadonce00b5f2019-11-22 10:59:26 -0800101 'tests/google/bt/performance/',
102 'tests/google/bt/sar/',
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800103]
104
105BANNED_IMPORTS = ['mobly.controllers']
Mark De Ruytera4b9cd32018-10-17 16:26:02 -0700106
Benny Peake2be7f952016-12-20 15:48:13 -0800107
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800108class ActsImportUnitTest(unittest.TestCase):
Mark De Ruytera4b9cd32018-10-17 16:26:02 -0700109 """Test that all acts framework imports work."""
Benny Peake2be7f952016-12-20 15:48:13 -0800110
111 def test_import_acts_successful(self):
Mark De Ruytera4b9cd32018-10-17 16:26:02 -0700112 """Test that importing ACTS works."""
Benny Peake3d4c4d92016-12-21 17:18:05 -0800113 acts = import_acts()
Benny Peake2be7f952016-12-20 15:48:13 -0800114 self.assertIsNotNone(acts)
115
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800116 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 Peake3d4c4d92016-12-21 17:18:05 -0800119 acts = import_acts()
Benny Peake2be7f952016-12-20 15:48:13 -0800120 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 Jia0a61c9e2019-02-19 16:52:20 -0800124 tests_path = os.path.normpath(os.path.join(acts_path, '../../tests'))
Benny Peake2be7f952016-12-20 15:48:13 -0800125
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800126 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 Peake2be7f952016-12-20 15:48:13 -0800134
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800135 path = os.path.relpath(os.path.join(root, f), os.getcwd())
Benny Peake2be7f952016-12-20 15:48:13 -0800136
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800137 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 Peake2be7f952016-12-20 15:48:13 -0800142
Xianyuan Jia0a61c9e2019-02-19 16:52:20 -0800143 # Suppress verbose output on assertion failure.
144 self.longMessage = False
145
146 for banned_import in BANNED_IMPORTS:
Mark De Ruyter1457dc02019-08-01 14:38:06 -0700147 self.assertNotIn(
148 banned_import, sys.modules,
149 'Attempted to import the banned package/module '
150 '%s.' % banned_import)
Benny Peake2be7f952016-12-20 15:48:13 -0800151
152
Mark De Ruytera4b9cd32018-10-17 16:26:02 -0700153if __name__ == '__main__':
Benny Peake2be7f952016-12-20 15:48:13 -0800154 unittest.main()