Remove unused dependencies to RD LIB
[contrib/iotivity.git] / java / iotivity-android / run_android_smoketests.py
1 #!/usr/bin/python
2
3 #******************************************************************
4 #
5 # Copyright 2016 Intel Corporation All Rights Reserved.
6 #
7 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 #
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
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22
23 import os
24 import sys
25 import textwrap
26 import argparse
27 import platform
28 import subprocess
29 import multiprocessing
30 import time
31 import psutil
32
33 def find_avalible_console_port():
34     '''
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
38     '''
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.
44     ret_port = 5554
45     nc = psutil.net_connections(kind='inet')
46     while True:
47         console_port_already_being_used = False
48         adb_port_already_being_used = False
49         for i in nc:
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)):
55             return str(ret_port)
56         ret_port += 2 #for adv the port must be a multiple of 2
57
58 def start_avd(avd_name, console_port):
59     '''
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.
63     note:
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.
67
68     Keyword arguments:
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 
72     '''
73     command = 'emulator -avd ' + avd_name + ' -port ' + console_port + ' -wipe-data -no-boot-anim -no-window'
74     subprocess.Popen([command], shell=True)
75
76 def wait_for_avd_boot(console_port):
77     '''
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
80
81     Keyword arguments:
82     console_port -- the port number that was specified when starting the avd
83     '''
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']
87     wait_for_boot = True
88     while wait_for_boot:
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')
96                 wait_for_boot = False
97             else:
98                 #print('Waiting for emulator to start')
99                 time.sleep(1);
100         else:
101             #print('Waiting for emulator to start')
102             time.sleep(1)
103     wait_for_boot = True
104     while wait_for_boot:
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
113             else:
114                 #print('Waiting for emulator to start')
115                 time.sleep(1)
116         else:
117             #print('Waiting for emulator to start')
118             time.sleep(1)
119
120 def build_smoketests():
121     '''
122     Use gradlew to build the android smoke tests
123     '''
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()
127
128 def install_smoketests(console_port):
129     '''
130     Install the android smoke tests. Must run build_smoketests() before running this function
131
132     Keyword arguments:
133     console_port -- the port number that was specified when starting the avd
134     '''
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()
137
138 def run_smoketests(console_port):
139     '''
140     run the android smoke test
141
142     Keyword arguments:
143     console_port -- the port number that was specified when starting the avd
144     '''
145     command = 'adb -s emulator-' + console_port + ' shell am instrument -w org.iotivity.base.test/android.test.InstrumentationTestRunner'
146     print command
147     subprocess.Popen([command], shell=True).wait()
148
149 def kill_avd(console_port):
150     '''
151     shut down the avd
152
153     Keyword arguments:
154     console_port -- the port number that was specified when starting the avd
155     '''
156     command = 'adb -s emulator-' + console_port + ' emu kill'
157     subprocess.Popen([command], shell=True).wait()
158
159 def create_avd(avd_name, target, abi):
160     '''
161     Create a new android virtual device 
162
163     Keyword arguments:
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
167
168     run command $android list targets to get a list of targets and there Tag/ABIs
169     '''
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.
173     p.communicate('no')
174     p.wait()
175
176 def delete_avd(avd_name):
177     command = ['android', '-s', 'delete', 'avd', '--name', avd_name]
178     subprocess.Popen(command).wait();
179
180 def start_android_and_run_tests(target, abi):
181     '''
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
192     8. deletes the avd
193
194     Keyword arguments:
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
198
199     run command $android list targets to get a list of targets and there Tag/ABIs
200     '''
201     avalible_port = find_avalible_console_port()
202     avd_name = 'smoke_test_avd_' + avalible_port
203     
204     create_avd(avd_name, target, abi)
205     
206     start_avd(avd_name, avalible_port)
207     wait_for_avd_boot(avalible_port)
208     
209     build_smoketests();
210     install_smoketests(avalible_port)
211     run_smoketests(avalible_port)
212     
213     kill_avd(avalible_port)
214     delete_avd(avd_name)
215
216 def main(argv):
217     target = 'android-21'
218     abi = 'x86_64'
219
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()
226
227     if (args.abi != None):
228         abi = args.abi
229     if (args.target != None):
230         target = args.target
231
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 '*****************************************************'
237
238     start_android_and_run_tests(target, abi)
239
240 if __name__ == "__main__":
241    main(sys.argv[1:])
242
243