3 #******************************************************************
5 # Copyright 2016 Intel Corporation All Rights Reserved.
7 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
21 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
29 import multiprocessing
33 def find_avalible_console_port():
35 Find an open port number that will be used for the avd console port. Start searching for the
36 at port number 5554 and continue incrementing port number till and open port is found.
37 Returns port number as a string
39 # 5554 is the default console port used when starting an avd without specifying a port number
40 # since this is the default port number by default we start checking to see if that port is
41 # currently in use. If it is being used increase the port to the next even port number.
42 # Each instance of the avd uses two ports the console port and the avd port.The avd port is
43 # always console port +1 so we will check that that port is also open.
45 nc = psutil.net_connections(kind='inet')
47 console_port_already_being_used = False
48 adb_port_already_being_used = False
50 if(i.laddr[1] == ret_port):
51 console_port_already_being_used = True
52 if(i.laddr[1] == (ret_port + 1)):
53 adb_port_already_being_used = True
54 if((not console_port_already_being_used) and (not adb_port_already_being_used)):
56 ret_port += 2 #for adv the port must be a multiple of 2
58 def start_avd(avd_name, console_port):
60 Start up the avd specified by the avd_name parameter use the specify the console port that the avd will use
61 with the console_port parameter. The find_avalible_console_port() function should be used to determine an
62 open console_port that can be passed into this function.
64 - all data on the avd will be wiped to start with a known starting condition.
65 - the avd will be started with the no-window option so there is no visual indication
66 that the avd is launched.
69 avd_name -- the name of the created android virtual device
70 console_port -- the port number that will attempt to be used
71 by the when starting the avd
73 command = 'emulator -avd ' + avd_name + ' -port ' + console_port + ' -wipe-data -no-boot-anim -no-window'
74 subprocess.Popen([command], shell=True)
76 def wait_for_avd_boot(console_port):
78 After calling start_avd this function is used to wait for the avd to complete booting the console_port
79 option must match the console_port option used to start the avd
82 console_port -- the port number that was specified when starting the avd
84 #dev_cmd = 'adb -s emulator-' + console_port + ' shell getprop dev.bootcomplete'
85 #sys_cmd = 'adb -s emulator-' + console_port + ' shell getprop sys.boot_completed'
86 dev_cmd = ['adb', '-s', 'emulator-' + console_port, 'shell', 'getprop', 'dev.bootcomplete']
89 adb_process = subprocess.Popen(['adb', '-s', 'emulator-' + console_port, 'shell', 'getprop', 'dev.bootcomplete'],
90 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
91 output, error = adb_process.communicate()
92 #print 'output = ' + str(output) + ' error = ' + str(error) + ' return code = ' + str(adb_process.returncode)
93 if(adb_process.returncode == 0):
94 if(output.startswith('1')):
95 print('property dev.bootcomplete indicates that the avd boot has completed')
98 #print('Waiting for emulator to start')
101 #print('Waiting for emulator to start')
105 adb_process = subprocess.Popen(['adb', '-s', 'emulator-' + console_port, 'shell', 'getprop', 'sys.boot_completed'],
106 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
107 output, error = adb_process.communicate()
108 #print 'output = ' + str(output) + ' error = ' + str(error) + ' return code = ' + str(adb_process.returncode)
109 if(adb_process.returncode == 0):
110 if(output.startswith('1')):
111 print('property sys.boot_completed indicates that the avd boot has completed')
112 wait_for_boot = False
114 #print('Waiting for emulator to start')
117 #print('Waiting for emulator to start')
120 def build_smoketests():
122 Use gradlew to build the android smoke tests
124 os.environ['ANDROID_NDK_HOME'] = os.path.abspath(os.getcwd() + '/../../extlibs/android/ndk/android-ndk-r10d')
125 command = './gradlew assembleAndroidTest'
126 subprocess.Popen([command], shell=True).wait()
128 def install_smoketests(console_port):
130 Install the android smoke tests. Must run build_smoketests() before running this function
133 console_port -- the port number that was specified when starting the avd
135 command = 'adb -s emulator-' + console_port + ' install -r ./build/outputs/apk/iotivity-android-debug-androidTest-unaligned.apk'
136 subprocess.Popen([command], shell=True).wait()
138 def run_smoketests(console_port):
140 run the android smoke test
143 console_port -- the port number that was specified when starting the avd
145 command = 'adb -s emulator-' + console_port + ' shell am instrument -w org.iotivity.base.test/android.test.InstrumentationTestRunner'
147 subprocess.Popen([command], shell=True).wait()
149 def kill_avd(console_port):
154 console_port -- the port number that was specified when starting the avd
156 command = 'adb -s emulator-' + console_port + ' emu kill'
157 subprocess.Popen([command], shell=True).wait()
159 def create_avd(avd_name, target, abi):
161 Create a new android virtual device
164 avd_name -- the name of the created avd
165 target -- the target Target ID of the system image to use with the new AVD. example android-21
166 abi -- the architecture type for the avd example armeabi, x86, or x86_64
168 run command $android list targets to get a list of targets and there Tag/ABIs
170 command = ['android', '-s', 'create', 'avd', '-f', '--name', avd_name, '--target', target, '--abi', abi]
171 p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
172 # Just use the default avd no need to specify extra options.
176 def delete_avd(avd_name):
177 command = ['android', '-s', 'delete', 'avd', '--name', avd_name]
178 subprocess.Popen(command).wait();
180 def start_android_and_run_tests(target, abi):
182 This function does the following
183 1. creates a new avd named smoke_test_avd_####
184 where the #### is the port number that is used to talk with the avd
185 the port number is assigned automatically.
186 2. starts the created avd
187 3. waits for the avd to boot
188 4. builds android smoke tests
189 5. installs the smoke tests on the avd
190 6. runs the smoke tests
191 7. shuts down the avd
195 avd_name -- the name of the created avd
196 target -- the target Target ID of the system image to use with the new AVD. example android-21
197 abi -- the architecture type for the avd example armeabi, x86, or x86_64
199 run command $android list targets to get a list of targets and there Tag/ABIs
201 avalible_port = find_avalible_console_port()
202 avd_name = 'smoke_test_avd_' + avalible_port
204 create_avd(avd_name, target, abi)
206 start_avd(avd_name, avalible_port)
207 wait_for_avd_boot(avalible_port)
210 install_smoketests(avalible_port)
211 run_smoketests(avalible_port)
213 kill_avd(avalible_port)
217 target = 'android-21'
220 parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, epilog=textwrap.dedent('''\
221 If ABI is not specified it will default to x86_64.
222 If TARGET is not specified it will default to android-21.'''))
223 parser.add_argument('-a', '--abi', help='specify the abi of the android avd example "x86_64"')
224 parser.add_argument('-t', '--target', help='the andrdoid target example "android-21"')
225 args = parser.parse_args()
227 if (args.abi != None):
229 if (args.target != None):
232 print '*****************************************************'
233 print 'Running andriod smoke test with the following options'
234 print ' The android target is -- ', target
235 print ' The android abi is -- ', abi
236 print '*****************************************************'
238 start_android_and_run_tests(target, abi)
240 if __name__ == "__main__":