2014-04-14 18:43:26 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
# create new project by cocos-console
|
|
|
|
# compile, deploy project and run
|
2014-04-15 17:41:40 +08:00
|
|
|
# perpose: for emptytest.
|
2014-04-14 18:43:26 +08:00
|
|
|
# now support: mac- mac/ios/android
|
|
|
|
# will add: window-android,linux-android
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import socket
|
2014-04-21 18:18:48 +08:00
|
|
|
import platform
|
2014-04-14 18:43:26 +08:00
|
|
|
|
|
|
|
payload = {}
|
|
|
|
#get payload from os env
|
|
|
|
if os.environ.has_key('payload'):
|
|
|
|
payload_str = os.environ['payload']
|
|
|
|
#parse to json obj
|
|
|
|
payload = json.loads(payload_str)
|
|
|
|
print 'payload:',payload
|
2014-04-21 18:18:48 +08:00
|
|
|
pr_num = 6326
|
2014-04-14 18:43:26 +08:00
|
|
|
#get pull number
|
2014-04-21 18:18:48 +08:00
|
|
|
if payload.has_key('number'):
|
|
|
|
pr_num = payload['number']
|
2014-04-14 18:43:26 +08:00
|
|
|
print 'pr_num:' + str(pr_num)
|
2014-04-21 18:18:48 +08:00
|
|
|
run_app_time = 5
|
|
|
|
if os.environ.has_key('RUN_APP_TIME'):
|
|
|
|
global run_app_time
|
|
|
|
run_app_time = os.environ['RUN_APP_TIME']
|
|
|
|
print 'run_app_time:', run_app_time
|
2014-04-14 18:43:26 +08:00
|
|
|
|
2014-04-15 17:41:40 +08:00
|
|
|
test_name = ['cpp_empty_test']
|
|
|
|
if os.environ.has_key('TESTS_NAME'):
|
2014-04-21 18:18:48 +08:00
|
|
|
temp_var = os.environ['TESTS_NAME']
|
2014-04-15 17:41:40 +08:00
|
|
|
test_name = temp_var.split(', ')
|
2014-04-21 18:18:48 +08:00
|
|
|
package_name = ['org.cocos2dx.cpp_empty_test']
|
2014-04-15 17:41:40 +08:00
|
|
|
if os.environ.has_key('PACKAGE_NAME'):
|
2014-04-21 18:18:48 +08:00
|
|
|
temp_var = os.environ['PACKAGE_NAME']
|
2014-04-15 17:41:40 +08:00
|
|
|
package_name = temp_var.split(', ')
|
2014-04-21 18:18:48 +08:00
|
|
|
activity_name = ['org.cocos2dx.cpp_empty_test.AppActivity']
|
2014-04-15 17:41:40 +08:00
|
|
|
if os.environ.has_key('ACTIVITY_NAME'):
|
2014-04-21 18:18:48 +08:00
|
|
|
temp_var = os.environ['ACTIVITY_NAME']
|
2014-04-15 17:41:40 +08:00
|
|
|
activity_name = temp_var.split(', ')
|
|
|
|
gIdx = 0
|
|
|
|
if os.environ.has_key('TEST_INDEX'):
|
|
|
|
gIdx = os.environ('TEST_INDEX')
|
|
|
|
|
2014-04-21 18:18:48 +08:00
|
|
|
current_platform = platform.system()
|
|
|
|
print 'current platform is:', current_platform
|
|
|
|
|
|
|
|
empty_test_result = True
|
|
|
|
empty_test_result_info = ''
|
2014-04-14 18:43:26 +08:00
|
|
|
arrDevices = []
|
|
|
|
def getDevices():
|
|
|
|
cmd = 'adb devices'
|
|
|
|
info_devices = os.popen(cmd).read()
|
|
|
|
arr_info = info_devices.split('\n')
|
|
|
|
del arr_info[0]
|
|
|
|
count = 0
|
|
|
|
for device in arr_info:
|
|
|
|
if len(device) > 0:
|
|
|
|
count += 1
|
|
|
|
print 'device ', count,device
|
|
|
|
deviceInfo = device.split(' ')
|
|
|
|
global arrDevices
|
2014-04-15 13:43:38 +08:00
|
|
|
obj = {}
|
|
|
|
obj['name'] = deviceInfo[0]
|
|
|
|
arrDevices.append(obj)
|
2014-04-14 18:43:26 +08:00
|
|
|
return count
|
|
|
|
|
2014-04-15 13:43:38 +08:00
|
|
|
def getADBDeviceIP(device_name):
|
|
|
|
output = os.popen("adb -s "+device_name+" shell netcfg")
|
|
|
|
configs = output.read().split('\r\n')
|
2014-04-21 18:18:48 +08:00
|
|
|
output.close()
|
2014-04-15 13:43:38 +08:00
|
|
|
for l in configs:
|
|
|
|
items = l.split()
|
|
|
|
if len(items)>1 and items[1] == 'UP':
|
|
|
|
if items[2].find('127.0.0.1') < 0 and items[2].find('0.0.0.0') < 0:
|
|
|
|
return items[2]
|
|
|
|
return False
|
|
|
|
def mapIP():
|
|
|
|
for device in arrDevices:
|
|
|
|
ip_d = getADBDeviceIP(device['name'])
|
|
|
|
device['ip'] = ip_d
|
|
|
|
|
2014-04-21 18:18:48 +08:00
|
|
|
device_info = {}
|
|
|
|
info_list = '{"product":["model","brand","name","cpu.abi","cpu.abi2","manufacturer","locale.language","locale.region"],"build":["id","version.sdk","version.release"]}'
|
|
|
|
if os.environ.has_key('DEVICE_INFO_LIST'):
|
|
|
|
info_list = os.environ['DEVICE_INFO_LIST']
|
|
|
|
info_list = eval(info_list)
|
|
|
|
def getDeviceInfoByName(name):
|
|
|
|
cmd = ''
|
|
|
|
if len(name) > 0:
|
|
|
|
cmd = 'adb -s '+name+' shell cat /system/build.prop'
|
|
|
|
else:
|
|
|
|
cmd = 'adb shell cat /system/build.prop'
|
|
|
|
pip_cat = os.popen(cmd)
|
|
|
|
read_info = pip_cat.read()
|
|
|
|
read_info_list = read_info.split('\r\n')
|
|
|
|
def checkProperty(item_str):
|
|
|
|
for argv in info_list:
|
|
|
|
for item in info_list[argv]:
|
|
|
|
prop = argv+'.'+item
|
|
|
|
if item_str.find(prop) > -1:
|
|
|
|
arr_item = item_str.split('=')
|
|
|
|
device_info[prop] = arr_item[1]
|
|
|
|
break
|
|
|
|
for item in read_info_list:
|
|
|
|
checkProperty(item)
|
|
|
|
|
|
|
|
#getDeviceInfoByName('')
|
|
|
|
#print 'device_info:',device_info
|
|
|
|
def logDeviceInfomation():
|
|
|
|
getDeviceInfoByName('')
|
|
|
|
for key in device_info:
|
|
|
|
print '\t'+key+':'+device_info[key]
|
|
|
|
|
2014-04-14 18:43:26 +08:00
|
|
|
info_empty_test = {}
|
2014-04-21 18:18:48 +08:00
|
|
|
apk_name = 'apks/'+test_name[gIdx]+'/'+test_name[gIdx]+'_'+str(pr_num)+'.apk'
|
2014-04-14 18:43:26 +08:00
|
|
|
def install_apk():
|
2014-04-15 17:41:40 +08:00
|
|
|
print 'will install apk:', apk_name
|
2014-04-21 18:18:48 +08:00
|
|
|
global empty_test_result
|
2014-04-14 18:43:26 +08:00
|
|
|
if len(arrDevices) == 0:
|
2014-04-21 18:18:48 +08:00
|
|
|
empty_test_result = False
|
|
|
|
empty_test_result_info = 'no android device.'
|
|
|
|
print empty_test_result_info
|
2014-04-14 18:43:26 +08:00
|
|
|
return False
|
|
|
|
info_of_install = []
|
2014-04-21 18:18:48 +08:00
|
|
|
if not os.path.isfile(apk_name):
|
|
|
|
print apk_name, 'is not exist!'
|
|
|
|
empty_test_result = False
|
|
|
|
return False
|
2014-04-14 18:43:26 +08:00
|
|
|
for device in arrDevices:
|
2014-04-15 13:43:38 +08:00
|
|
|
name = device['name']
|
2014-04-15 17:41:40 +08:00
|
|
|
cmd = 'adb -s '+name+' install '+apk_name
|
2014-04-15 13:43:38 +08:00
|
|
|
print 'install on '+name
|
2014-04-14 18:43:26 +08:00
|
|
|
info_install = os.popen(cmd).read()
|
2014-04-15 17:41:40 +08:00
|
|
|
print 'infomation of install apk:', info_install
|
2014-04-14 18:43:26 +08:00
|
|
|
info_of_install.append(info_install.split('\r\n'))
|
|
|
|
info_empty_test['install'] = info_of_install
|
|
|
|
return True
|
|
|
|
|
|
|
|
def open_apk(type_of_apk):
|
2014-04-15 17:41:40 +08:00
|
|
|
print 'will open activity:'
|
2014-04-14 18:43:26 +08:00
|
|
|
for device in arrDevices:
|
2014-04-15 17:41:40 +08:00
|
|
|
cmd = 'adb -s '+device['name']+' shell am start -n '+package_name[gIdx]+'/'+activity_name[gIdx]
|
|
|
|
# print 'start activity:', cmd
|
2014-04-14 18:43:26 +08:00
|
|
|
info_start = os.popen(cmd).read()
|
|
|
|
info_start = info_start.split('\n')
|
2014-04-15 17:41:40 +08:00
|
|
|
# print 'info_start:', info_start
|
2014-04-14 18:43:26 +08:00
|
|
|
for info in info_start:
|
|
|
|
if info.find('Error:') > -1:
|
2014-04-15 17:41:40 +08:00
|
|
|
print 'infomation of open activity:',info
|
2014-04-21 18:18:48 +08:00
|
|
|
global empty_test_result
|
|
|
|
empty_test_result = False
|
|
|
|
empty_test_result_info = 'open :'+info
|
2014-04-14 18:43:26 +08:00
|
|
|
return info
|
2014-04-21 18:18:48 +08:00
|
|
|
if len(arrDevices):
|
|
|
|
print 'activity is opened.'
|
|
|
|
else:
|
|
|
|
print 'no device.'
|
2014-04-14 18:43:26 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
PORT = 5678
|
2014-04-15 17:41:40 +08:00
|
|
|
def socket_status(device_name):
|
2014-04-14 18:43:26 +08:00
|
|
|
soc = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
2014-04-21 18:18:48 +08:00
|
|
|
status_socket = False
|
|
|
|
global empty_test_result
|
2014-04-14 18:43:26 +08:00
|
|
|
try:
|
2014-04-21 18:18:48 +08:00
|
|
|
print 'telnet ', device_name['ip'], PORT
|
2014-04-15 17:41:40 +08:00
|
|
|
soc.connect((device_name['ip'], PORT))
|
2014-04-15 13:43:38 +08:00
|
|
|
cmd = 'resolution\r\n'
|
2014-04-15 17:41:40 +08:00
|
|
|
# print 'socket cmd :', cmd
|
2014-04-21 18:18:48 +08:00
|
|
|
print 'connected successfully.'
|
|
|
|
print 'send console command: resolution'
|
2014-04-14 18:43:26 +08:00
|
|
|
soc.send(cmd)
|
|
|
|
while True:
|
|
|
|
data = soc.recv(1024)
|
|
|
|
if len(data):
|
2014-04-15 13:43:38 +08:00
|
|
|
print data
|
|
|
|
if data.find('size:') > -1:
|
2014-04-21 18:18:48 +08:00
|
|
|
print 'OK'
|
|
|
|
print 'close', test_name[gIdx]
|
|
|
|
soc.send('director end')
|
|
|
|
print 'OK'
|
|
|
|
status_socket = True
|
|
|
|
break
|
2014-04-15 17:41:40 +08:00
|
|
|
if not data:
|
2014-04-21 18:18:48 +08:00
|
|
|
empty_test_result = False
|
|
|
|
empty_test_result_info = test_name[gIdx]+' is crashed!'
|
|
|
|
print empty_test_result_info
|
2014-04-15 17:41:40 +08:00
|
|
|
break
|
2014-04-14 18:43:26 +08:00
|
|
|
except Exception, e:
|
2014-04-21 18:18:48 +08:00
|
|
|
empty_test_result = False
|
|
|
|
empty_test_result_info = test_name[gIdx]+' is crashed!'
|
|
|
|
print empty_test_result_info
|
|
|
|
time.sleep(2)
|
|
|
|
soc.close()
|
|
|
|
return status_socket
|
2014-04-14 18:43:26 +08:00
|
|
|
|
2014-04-15 17:41:40 +08:00
|
|
|
def uninstall_apk(idx):
|
2014-04-14 18:43:26 +08:00
|
|
|
# adb shell pm uninstall -n org.cocos2dx.hellolua
|
2014-04-21 18:18:48 +08:00
|
|
|
print 'uninstall ', test_name[idx]
|
2014-04-15 13:43:38 +08:00
|
|
|
for device in arrDevices:
|
2014-04-15 17:41:40 +08:00
|
|
|
cmd = 'adb -s '+device['name']+' shell pm uninstall -n '+package_name[idx]
|
|
|
|
info_uninstall = os.popen(cmd).read()
|
2014-04-21 18:18:48 +08:00
|
|
|
if info_uninstall.find('Success') > -1:
|
|
|
|
print 'OK'
|
|
|
|
else:
|
|
|
|
empty_test_result_info = 'uninstall Failed!'
|
|
|
|
print empty_test_result_info
|
2014-04-14 18:43:26 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
def main():
|
|
|
|
print 'in main:'
|
|
|
|
getDevices()
|
2014-04-15 13:43:38 +08:00
|
|
|
if len(arrDevices):
|
|
|
|
mapIP()
|
|
|
|
print 'arrDevices:',arrDevices
|
2014-04-21 18:18:48 +08:00
|
|
|
uninstall_apk(gIdx)
|
|
|
|
time.sleep(1)
|
2014-04-22 11:04:17 +08:00
|
|
|
else:
|
|
|
|
print 'there is no device for emptytest, please check devices!'
|
|
|
|
return 1
|
2014-04-15 13:43:38 +08:00
|
|
|
print 'empty test start:'
|
2014-04-21 18:18:48 +08:00
|
|
|
print 'device infomation:'
|
|
|
|
if len(arrDevices):
|
|
|
|
logDeviceInfomation()
|
|
|
|
else:
|
|
|
|
print '\tno android device.'
|
2014-04-14 18:43:26 +08:00
|
|
|
install_info = install_apk()
|
2014-04-15 17:41:40 +08:00
|
|
|
open_info = open_apk(test_name[gIdx])
|
2014-04-14 18:43:26 +08:00
|
|
|
info_empty_test['open_info'] = open_info
|
|
|
|
if open_info:
|
|
|
|
time.sleep(5)
|
2014-04-15 17:41:40 +08:00
|
|
|
socket_info = socket_status(arrDevices[0])
|
2014-04-14 18:43:26 +08:00
|
|
|
info_empty_test['socket_info'] = socket_info
|
|
|
|
if install_info:
|
2014-04-21 18:18:48 +08:00
|
|
|
time.sleep(run_app_time)
|
2014-04-15 17:41:40 +08:00
|
|
|
info_uninstall = uninstall_apk(gIdx)
|
2014-04-14 18:43:26 +08:00
|
|
|
print 'info_empty_test:', info_empty_test
|
2014-04-21 18:18:48 +08:00
|
|
|
print 'empty test end', empty_test_result
|
|
|
|
if empty_test_result:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
print 'empty_test_result_info:', empty_test_result_info
|
|
|
|
return 1
|
2014-04-14 18:43:26 +08:00
|
|
|
|
|
|
|
# -------------- main --------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys_ret = 0
|
|
|
|
try:
|
|
|
|
sys_ret = main()
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
sys_ret = 1
|
|
|
|
finally:
|
|
|
|
sys.exit(sys_ret)
|