2014-04-14 18:43:26 +08:00
|
|
|
#!/usr/bin/python
|
2014-04-23 16:32:37 +08:00
|
|
|
# check crash of cppemptytest
|
|
|
|
# install/open/uninstall apk
|
2014-04-15 17:41:40 +08:00
|
|
|
# perpose: for emptytest.
|
2014-04-23 16:32:37 +08:00
|
|
|
# now support: android
|
|
|
|
# will add: autotest-win7
|
2014-04-14 18:43:26 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import socket
|
2014-04-21 18:18:48 +08:00
|
|
|
import platform
|
2014-04-23 16:32:37 +08:00
|
|
|
import threading
|
|
|
|
import codecs
|
|
|
|
import re
|
|
|
|
import paramiko
|
2014-04-14 18:43:26 +08:00
|
|
|
|
|
|
|
payload = {}
|
|
|
|
#get payload from os env
|
|
|
|
if os.environ.has_key('payload'):
|
2015-01-29 08:40:19 +08:00
|
|
|
payload_str = os.environ['payload']
|
|
|
|
#parse to json obj
|
|
|
|
payload = json.loads(payload_str)
|
2014-04-14 18:43:26 +08:00
|
|
|
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'):
|
2015-01-29 08:40:19 +08:00
|
|
|
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'):
|
2015-01-29 08:40:19 +08:00
|
|
|
run_app_time = os.environ['RUN_APP_TIME']
|
2014-04-21 18:18:48 +08:00
|
|
|
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'):
|
2015-01-29 08:40:19 +08:00
|
|
|
temp_var = os.environ['TESTS_NAME']
|
|
|
|
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'):
|
2015-01-29 08:40:19 +08:00
|
|
|
temp_var = os.environ['PACKAGE_NAME']
|
|
|
|
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'):
|
2015-01-29 08:40:19 +08:00
|
|
|
temp_var = os.environ['ACTIVITY_NAME']
|
|
|
|
activity_name = temp_var.split(', ')
|
2014-04-15 17:41:40 +08:00
|
|
|
gIdx = 0
|
|
|
|
if os.environ.has_key('TEST_INDEX'):
|
2015-01-29 08:40:19 +08:00
|
|
|
gIdx = os.environ('TEST_INDEX')
|
2014-04-15 17:41:40 +08:00
|
|
|
|
2014-04-21 18:18:48 +08:00
|
|
|
current_platform = platform.system()
|
|
|
|
print 'current platform is:', current_platform
|
|
|
|
|
2014-04-14 18:43:26 +08:00
|
|
|
arrDevices = []
|
|
|
|
def getDevices():
|
2015-01-29 08:40:19 +08:00
|
|
|
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
|
|
|
|
obj = {}
|
|
|
|
obj['name'] = deviceInfo[0]
|
|
|
|
arrDevices.append(obj)
|
|
|
|
return count
|
2014-04-14 18:43:26 +08:00
|
|
|
|
2014-04-15 13:43:38 +08:00
|
|
|
def getADBDeviceIP(device_name):
|
2015-01-29 08:40:19 +08:00
|
|
|
output = os.popen("adb -s "+device_name+" shell netcfg")
|
|
|
|
configs = output.read().split('\r\n')
|
|
|
|
output.close()
|
|
|
|
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
|
2014-04-15 13:43:38 +08:00
|
|
|
def mapIP():
|
2015-01-29 08:40:19 +08:00
|
|
|
for device in arrDevices:
|
|
|
|
ip_d = getADBDeviceIP(device['name'])
|
|
|
|
device['ip'] = ip_d
|
2014-04-15 13:43:38 +08:00
|
|
|
|
2014-04-23 16:32:37 +08:00
|
|
|
allThreadIsRunning = {}
|
|
|
|
def setThreadStatus():
|
2015-01-29 08:40:19 +08:00
|
|
|
for device in arrDevices:
|
|
|
|
allThreadIsRunning[device['name']] = 1
|
2014-04-23 16:32:37 +08:00
|
|
|
|
|
|
|
devices_info = {}
|
2014-04-21 18:18:48 +08:00
|
|
|
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'):
|
2015-01-29 08:40:19 +08:00
|
|
|
info_list = os.environ['DEVICE_INFO_LIST']
|
2014-04-21 18:18:48 +08:00
|
|
|
info_list = eval(info_list)
|
|
|
|
def getDeviceInfoByName(name):
|
2015-01-29 08:40:19 +08:00
|
|
|
cmd = ''
|
|
|
|
dev_name = name
|
|
|
|
if len(name) > 0:
|
|
|
|
cmd = 'adb -s '+name+' shell cat /system/build.prop'
|
|
|
|
else:
|
|
|
|
cmd = 'adb shell cat /system/build.prop'
|
|
|
|
dev_name = 'device one'
|
|
|
|
pip_cat = os.popen(cmd)
|
|
|
|
read_info = pip_cat.read()
|
|
|
|
read_info_list = read_info.split('\r\n')
|
|
|
|
device_info_one = {}
|
|
|
|
def checkProperty(item_str, device_name):
|
|
|
|
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_one[prop] = arr_item[1]
|
|
|
|
break
|
|
|
|
for item in read_info_list:
|
|
|
|
checkProperty(item, dev_name)
|
|
|
|
devices_info[dev_name] = device_info_one
|
2014-04-21 18:18:48 +08:00
|
|
|
|
|
|
|
#getDeviceInfoByName('')
|
|
|
|
#print 'device_info:',device_info
|
2014-04-23 16:32:37 +08:00
|
|
|
def getDeviceInfomation():
|
2015-01-29 08:40:19 +08:00
|
|
|
for device in arrDevices:
|
|
|
|
getDeviceInfoByName(device['name'])
|
2014-04-21 18:18:48 +08:00
|
|
|
|
2014-04-14 18:43:26 +08:00
|
|
|
info_empty_test = {}
|
2014-04-23 16:32:37 +08:00
|
|
|
info_empty_test_pro = ['install','open','socket','uninstall']
|
|
|
|
def init_info_empty_test():
|
2015-01-29 08:40:19 +08:00
|
|
|
for item in info_empty_test_pro:
|
|
|
|
info_empty_test[item] = {}
|
2014-04-23 16:32:37 +08:00
|
|
|
init_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-23 16:32:37 +08:00
|
|
|
def install_apk_on_device(device):
|
2015-01-29 08:40:19 +08:00
|
|
|
name = device['name']
|
|
|
|
cmd = 'adb -s '+name+' install '+apk_name
|
|
|
|
print 'install on '+name
|
|
|
|
info_install = os.popen(cmd).read()
|
|
|
|
print 'infomation of install apk:', info_install
|
|
|
|
info_install_arr = info_install.split('\r\n')
|
|
|
|
info_install_result = False
|
|
|
|
for item in info_install_arr:
|
|
|
|
if item.find('Success') > -1:
|
|
|
|
info_install_result = True
|
|
|
|
info_empty_test['install'][name] = info_install_result
|
|
|
|
return True
|
2014-04-14 18:43:26 +08:00
|
|
|
|
2014-04-23 16:32:37 +08:00
|
|
|
def open_apk_on_device(device):
|
2015-01-29 08:40:19 +08:00
|
|
|
print 'will open activity:'
|
|
|
|
name = device['name']
|
|
|
|
cmd = 'adb -s '+name+' shell am start -n '+package_name[gIdx]+'/'+activity_name[gIdx]
|
|
|
|
# print 'start activity:', cmd
|
|
|
|
info_start = os.popen(cmd).read()
|
|
|
|
info_start = info_start.split('\r\n')
|
|
|
|
# print 'info_start:', info_start
|
|
|
|
info_start_result = True
|
|
|
|
for info in info_start:
|
|
|
|
if info.find('Error:') > -1:
|
|
|
|
print 'infomation of open activity:',info
|
|
|
|
info_start_result = False
|
|
|
|
info_empty_test['open'][name] = info_start_result
|
|
|
|
return True
|
2014-04-14 18:43:26 +08:00
|
|
|
|
|
|
|
PORT = 5678
|
2014-04-23 16:32:37 +08:00
|
|
|
def socket_status_on_device(device):
|
2015-01-29 08:40:19 +08:00
|
|
|
name = device['name']
|
|
|
|
ip = device['ip']
|
|
|
|
soc = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
|
|
|
status_socket = False
|
|
|
|
info_of_socket_result = ''
|
|
|
|
try:
|
|
|
|
print 'telnet ', ip, PORT
|
|
|
|
soc.connect((ip, PORT))
|
|
|
|
cmd = 'resolution\r\n'
|
|
|
|
print 'connected successfully.'
|
|
|
|
print 'send console command: resolution'
|
|
|
|
soc.send(cmd)
|
|
|
|
while True:
|
|
|
|
data = soc.recv(1024)
|
|
|
|
if len(data):
|
|
|
|
print data
|
|
|
|
if data.find('size:') > -1:
|
|
|
|
info_of_socket_result = 'OK'
|
|
|
|
print 'close', test_name[gIdx]
|
|
|
|
soc.send('director end')
|
|
|
|
status_socket = True
|
|
|
|
break
|
|
|
|
if not data:
|
|
|
|
info_of_socket_result = test_name[gIdx]+' is crashed!'
|
|
|
|
break
|
|
|
|
except Exception, e:
|
|
|
|
info_of_socket_result = test_name[gIdx]+' is crashed!'
|
|
|
|
time.sleep(2)
|
|
|
|
soc.close()
|
|
|
|
time.sleep(2)
|
|
|
|
info_empty_test['socket'][name] = info_of_socket_result
|
|
|
|
return status_socket
|
2014-04-14 18:43:26 +08:00
|
|
|
|
2014-04-23 16:32:37 +08:00
|
|
|
def uninstall_apk_on_device(device):
|
2015-01-29 08:40:19 +08:00
|
|
|
# adb shell pm uninstall -n org.cocos2dx.hellolua
|
|
|
|
print 'uninstall ', test_name[gIdx]
|
|
|
|
name = device['name']
|
|
|
|
cmd = 'adb -s '+name+' shell pm uninstall -n '+package_name[gIdx]
|
|
|
|
info_uninstall = os.popen(cmd).read()
|
|
|
|
info_uninstall_result = ''
|
|
|
|
if info_uninstall.find('Success') > -1:
|
|
|
|
info_uninstall_result = 'OK'
|
|
|
|
else:
|
|
|
|
info_uninstall_result = 'uninstall Failed!'
|
|
|
|
info_empty_test['uninstall'][name] = info_uninstall_result
|
|
|
|
return True
|
2014-04-14 18:43:26 +08:00
|
|
|
|
2014-04-23 16:32:37 +08:00
|
|
|
def excute_test_on_device(device):
|
2015-01-29 08:40:19 +08:00
|
|
|
uninstall_apk_on_device(device)
|
|
|
|
print device
|
|
|
|
info_install = install_apk_on_device(device)
|
|
|
|
print 'install:', info_install
|
|
|
|
info_open = open_apk_on_device(device)
|
|
|
|
print 'open:', info_open
|
|
|
|
time.sleep(3)
|
|
|
|
info_socket = socket_status_on_device(device)
|
|
|
|
print 'socket:', info_socket
|
|
|
|
info_uninstall = uninstall_apk_on_device(device)
|
|
|
|
print 'uninstall:', info_uninstall
|
|
|
|
allThreadIsRunning[device['name']] = 0
|
2014-04-23 16:32:37 +08:00
|
|
|
|
2014-04-23 17:55:53 +08:00
|
|
|
address_of_result_html = ''
|
2014-04-23 16:32:37 +08:00
|
|
|
def send_result_to_master():
|
2015-01-29 08:40:19 +08:00
|
|
|
if not os.environ.has_key('REMOTE_IP'):
|
|
|
|
return false
|
|
|
|
remote_ip = os.environ['REMOTE_IP']
|
|
|
|
remote_port = os.environ['REMOTE_PORT']
|
|
|
|
remote_user = os.environ['REMOTE_USER']
|
|
|
|
remote_pwd = os.environ['REMOTE_PWD']
|
|
|
|
remote_dir = os.environ['REMOTE_DIR']
|
|
|
|
remote_dir = remote_dir + str(pr_num)+'/'
|
|
|
|
print remote_dir
|
|
|
|
ssh = paramiko.SSHClient()
|
|
|
|
print 'ssh:',ssh
|
|
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
ssh.connect(remote_ip, int(remote_port), remote_user, remote_pwd)
|
|
|
|
# create dir
|
|
|
|
stdin, stdout, stderr = ssh.exec_command("mkdir "+remote_dir)
|
|
|
|
ssh.close()
|
|
|
|
trans = paramiko.Transport((remote_ip,int(remote_port)))
|
|
|
|
trans.connect(username = remote_user, password = remote_pwd)
|
|
|
|
sftp = paramiko.SFTPClient.from_transport(trans)
|
|
|
|
remotepath = remote_dir+test_name[gIdx]+'_'+str(pr_num)+'.html'
|
|
|
|
localpath = 'html/cpp_empty_test/cpp_empty_test_'+str(pr_num)+'.html'
|
|
|
|
sftp.put(localpath, remotepath)
|
|
|
|
sftp.close()
|
|
|
|
result_dir = remote_dir.replace('/data',':9000')
|
|
|
|
global address_of_result_html
|
|
|
|
address_of_result_html = 'http://'+remote_ip+result_dir+test_name[gIdx]+'_'+str(pr_num)+'.html'
|
|
|
|
|
2014-04-23 16:32:37 +08:00
|
|
|
def check_thread_is_running():
|
2015-01-29 08:40:19 +08:00
|
|
|
while 1:
|
|
|
|
in_running = 0
|
|
|
|
for key in allThreadIsRunning:
|
|
|
|
in_running = in_running + allThreadIsRunning[key]
|
|
|
|
# print 'in_running:', in_running
|
|
|
|
time.sleep(3)
|
|
|
|
if not in_running:
|
|
|
|
break
|
|
|
|
print 'all cpp emptytest is finished.'
|
|
|
|
log_emptytest_result()
|
|
|
|
print 'will send result:'
|
|
|
|
send_result_to_master()
|
|
|
|
print 'end of check thread is running.'
|
|
|
|
print 'address of result is:',address_of_result_html
|
2014-04-23 16:32:37 +08:00
|
|
|
|
|
|
|
def generate_html_with_result(result):
|
2015-01-29 08:40:19 +08:00
|
|
|
target_path = 'html/cpp_empty_test/cpp_empty_test_'+str(pr_num)+'.html'
|
|
|
|
data = codecs.open('hello.html', encoding='UTF-8').read()
|
|
|
|
data = re.sub("content", result, data)
|
|
|
|
codecs.open(target_path, 'wb', encoding='UTF-8').write(data)
|
2014-04-23 16:32:37 +08:00
|
|
|
|
|
|
|
empty_test_result = True
|
|
|
|
str_result = ''
|
|
|
|
def log_emptytest_result():
|
2015-01-29 08:40:19 +08:00
|
|
|
def appendToResult(one_str):
|
|
|
|
global str_result
|
|
|
|
str_result = str_result + one_str + '\r\n'
|
|
|
|
appendToResult('<pre>')
|
|
|
|
appendToResult('empty test start:')
|
|
|
|
for device in arrDevices:
|
|
|
|
name = device['name']
|
|
|
|
appendToResult('device infomation of : '+name)
|
|
|
|
for key in devices_info[name]:
|
|
|
|
appendToResult('\t'+key+':'+devices_info[name][key])
|
|
|
|
appendToResult('install: ' + str(info_empty_test['install'][name]))
|
|
|
|
appendToResult('open: ' + str(info_empty_test['open'][name]))
|
|
|
|
appendToResult('telnet ' + str(device['ip'])+' : ' + str(info_empty_test['socket'][name]))
|
|
|
|
appendToResult('close: ' + str(info_empty_test['socket'][name]))
|
|
|
|
appendToResult('uninstall: ' + str(info_empty_test['uninstall'][name]))
|
|
|
|
if not info_empty_test['install'][name] or not info_empty_test['open'][name] or not info_empty_test['socket'][name] or not info_empty_test['uninstall'][name]:
|
|
|
|
empty_test_result = False
|
|
|
|
appendToResult('run failed!')
|
|
|
|
else: appendToResult('run successfully!')
|
|
|
|
appendToResult('')
|
|
|
|
appendToResult('empty test end.</pre>')
|
|
|
|
generate_html_with_result(str_result)
|
|
|
|
print 'log empty test end'
|
2014-04-23 16:32:37 +08:00
|
|
|
|
|
|
|
class myThread(threading.Thread):
|
|
|
|
def __init__(self,threadname):
|
|
|
|
threading.Thread.__init__(self,name=threadname)
|
|
|
|
def run(self):
|
|
|
|
time.sleep(2)
|
|
|
|
device = self.getName()
|
|
|
|
print type(device)
|
|
|
|
device = eval(device)
|
|
|
|
print type(device), device
|
|
|
|
excute_test_on_device(device)
|
|
|
|
|
|
|
|
def run_emptytest():
|
2015-01-29 08:40:19 +08:00
|
|
|
for device in arrDevices:
|
|
|
|
th = myThread(device)
|
|
|
|
th.start()
|
2014-04-23 16:32:37 +08:00
|
|
|
|
2014-04-14 18:43:26 +08:00
|
|
|
def main():
|
2015-01-29 08:40:19 +08:00
|
|
|
print 'in main:'
|
|
|
|
getDevices()
|
|
|
|
if len(arrDevices):
|
|
|
|
mapIP()
|
|
|
|
setThreadStatus()
|
|
|
|
print 'arrDevices:',arrDevices
|
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
print 'there is no device for emptytest, please check devices!'
|
|
|
|
return 1
|
|
|
|
if len(arrDevices):
|
|
|
|
getDeviceInfomation()
|
|
|
|
run_emptytest()
|
|
|
|
check_thread_is_running()
|
|
|
|
print 'info_empty_test:', info_empty_test
|
|
|
|
print 'empty test end', empty_test_result
|
|
|
|
if empty_test_result:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 1
|
2014-04-14 18:43:26 +08:00
|
|
|
|
|
|
|
# -------------- main --------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys_ret = 0
|
|
|
|
try:
|
2015-01-29 08:40:19 +08:00
|
|
|
sys_ret = main()
|
2014-04-14 18:43:26 +08:00
|
|
|
except:
|
2015-01-29 08:40:19 +08:00
|
|
|
traceback.print_exc()
|
|
|
|
sys_ret = 1
|
2014-04-14 18:43:26 +08:00
|
|
|
finally:
|
2015-01-29 08:40:19 +08:00
|
|
|
sys.exit(sys_ret)
|