mirror of
https://gitee.com/openharmony/testfwk_xdevice
synced 2024-11-23 07:20:44 +00:00
!31 解决xdevice支持标准设备运行socket稳定性问题
Merge pull request !31 from alex_hold/master
This commit is contained in:
commit
9a949f9380
@ -615,6 +615,7 @@ class JSUnitParser(IParser):
|
||||
self.state_machine = StateRecorder()
|
||||
self.suites_name = ""
|
||||
self.listeners = []
|
||||
self.suite_name = ""
|
||||
|
||||
def get_listeners(self):
|
||||
return self.listeners
|
||||
@ -630,11 +631,12 @@ class JSUnitParser(IParser):
|
||||
suite_result.is_completed = True
|
||||
for listener in self.get_listeners():
|
||||
suite = copy.copy(suite_result)
|
||||
listener.__ended__(LifeCycle.TestSuite, suite,
|
||||
listener.__ended__(LifeCycle.TestSuites, suite,
|
||||
suite_report=True)
|
||||
self.state_machine.current_suite = None
|
||||
|
||||
def parse(self, line):
|
||||
line = line.strip()
|
||||
if (self.state_machine.suites_is_started() or line.find(
|
||||
_START_JSUNIT_RUN_MARKER) != -1) and line.find(
|
||||
_ACE_LOG_MARKER) != -1:
|
||||
@ -669,7 +671,11 @@ class JSUnitParser(IParser):
|
||||
run_time = end_timestamp - start_timestamp
|
||||
_, status_end_index = re.match(pattern, filter_message).span()
|
||||
status = filter_message[:status_end_index]
|
||||
test_name = filter_message[status_end_index:]
|
||||
if " ;" in filter_message:
|
||||
test_name = filter_message[status_end_index:
|
||||
str(filter_message).find(" ;")]
|
||||
else:
|
||||
test_name = filter_message[status_end_index:]
|
||||
status_dict = {"pass": ResultCode.PASSED, "fail": ResultCode.FAILED,
|
||||
"ignore": ResultCode.SKIPPED}
|
||||
status = status_dict.get(status[1:-1])
|
||||
|
@ -27,6 +27,7 @@ from xdevice import Plugin
|
||||
from xdevice import exec_cmd
|
||||
from xdevice import ConfigConst
|
||||
|
||||
from xdevice_extension._core import utils
|
||||
from xdevice_extension._core.environment.dmlib import HdcHelper
|
||||
from xdevice_extension._core.exception import HdcError
|
||||
from xdevice_extension._core.environment.dmlib import CollectingOutputReceiver
|
||||
@ -72,7 +73,7 @@ def perform_device_action(func):
|
||||
if self.usb_type == DeviceConnectorType.hdc:
|
||||
cmd = "hdc reset"
|
||||
self.log.info("re-execute hdc reset")
|
||||
exec_cmd(cmd)
|
||||
exec_cmd(cmd)
|
||||
if not self.recover_device():
|
||||
LOG.debug("set device %s %s false" % (
|
||||
self.device_sn, ConfigConst.recover_state))
|
||||
@ -166,9 +167,7 @@ class Device(IDevice):
|
||||
return self.device_state_monitor.wait_for_device_available()
|
||||
|
||||
def get_device_type(self):
|
||||
model = self.get_property("ro.build.characteristics",
|
||||
abort_on_exception=True)
|
||||
self.label = self.model_dict.get(model.lower(), None)
|
||||
self.label = self.model_dict.get("default", None)
|
||||
|
||||
def get_property(self, prop_name, retry=RETRY_ATTEMPTS,
|
||||
abort_on_exception=False):
|
||||
@ -193,7 +192,7 @@ class Device(IDevice):
|
||||
if self.usb_type == DeviceConnectorType.hdc:
|
||||
LOG.debug("%s execute command hdc %s%s" % (
|
||||
convert_serial(self.device_sn), command, timeout_msg))
|
||||
cmd = ["hdc_std", "-t", self.device_sn]
|
||||
cmd = ["hdc_std", "-t", self.device_sn]
|
||||
if isinstance(command, list):
|
||||
cmd.extend(command)
|
||||
else:
|
||||
@ -292,9 +291,6 @@ class Device(IDevice):
|
||||
timeout = kwargs.get("timeout", TIMEOUT)
|
||||
HdcHelper.push_file(self, local, remote, is_create=is_create,
|
||||
timeout=timeout)
|
||||
if not self.is_file_exist(remote):
|
||||
LOG.error("push %s to %s failed" % (local, remote))
|
||||
raise HdcError("push %s to %s failed" % (local, remote))
|
||||
|
||||
@perform_device_action
|
||||
def pull_file(self, remote, local, **kwargs):
|
||||
@ -337,9 +333,7 @@ class Device(IDevice):
|
||||
|
||||
def is_file_exist(self, file_path):
|
||||
file_path = check_path_legal(file_path)
|
||||
command = ["hdc_std", "shell", "ls", file_path]
|
||||
output = exec_cmd(command)
|
||||
|
||||
output = self.execute_shell_command("ls {}".format(file_path))
|
||||
if output and "No such file or directory" not in output:
|
||||
return True
|
||||
return False
|
||||
@ -360,10 +354,20 @@ class Device(IDevice):
|
||||
self._stop_catch_device_log()
|
||||
|
||||
def _start_catch_device_log(self):
|
||||
pass
|
||||
if self.hilog_file_pipe:
|
||||
command = "hilog"
|
||||
if self.usb_type == DeviceConnectorType.hdc:
|
||||
cmd = ['hdc_std', "-t", self.device_sn, "shell", command]
|
||||
LOG.info("execute command: %s" % " ".join(cmd).replace(
|
||||
self.device_sn, convert_serial(self.device_sn)))
|
||||
self.device_hilog_proc = utils.start_standing_subprocess(
|
||||
cmd, self.hilog_file_pipe)
|
||||
|
||||
def _stop_catch_device_log(self):
|
||||
pass
|
||||
if self.device_hilog_proc:
|
||||
utils.stop_standing_subprocess(self.device_hilog_proc)
|
||||
self.device_hilog_proc = None
|
||||
self.hilog_file_pipe = None
|
||||
|
||||
def get_recover_result(self, retry=RETRY_ATTEMPTS):
|
||||
command = "getparam ro.product.model"
|
||||
|
@ -217,14 +217,17 @@ class HdcMonitor:
|
||||
self.main_hdc_connection)
|
||||
|
||||
if self.main_hdc_connection and not self.monitoring:
|
||||
LOG.debug("start monitoring list targets")
|
||||
self.monitoring_list_targets()
|
||||
len_buf = HdcHelper.read(self.main_hdc_connection,
|
||||
DATA_UNIT_LENGTH)
|
||||
length = struct.unpack("!I", len_buf)[0]
|
||||
LOG.debug("had received length is: %s" % length)
|
||||
if length >= 0:
|
||||
self.monitoring = True
|
||||
self.process_incoming_target_data(length)
|
||||
except (HdcError, Exception) as _:
|
||||
LOG.debug("loop_monitor happened error: %s" % _)
|
||||
self.handle_exception_monitor_loop()
|
||||
break
|
||||
|
||||
@ -273,22 +276,21 @@ class HdcMonitor:
|
||||
|
||||
def process_incoming_target_data(self, length):
|
||||
local_array_list = []
|
||||
if length > 0:
|
||||
data_buf = HdcHelper.read(self.main_hdc_connection, length)
|
||||
data_str = HdcHelper.reply_to_string(data_buf)
|
||||
if 'Empty' not in data_str:
|
||||
lines = data_str.split('\n')
|
||||
for line in lines:
|
||||
items = line.strip().split('\t')
|
||||
if not items[0] :
|
||||
continue
|
||||
items.append(DeviceState.ONLINE.value)
|
||||
device_instance = self._get_device_instance(
|
||||
items, DeviceOsType.default)
|
||||
local_array_list.append(device_instance)
|
||||
else:
|
||||
LOG.debug("please check device actually.[%s]" % data_str)
|
||||
|
||||
data_buf = HdcHelper.read(self.main_hdc_connection, length)
|
||||
data_str = HdcHelper.reply_to_string(data_buf)
|
||||
if 'Empty' not in data_str:
|
||||
lines = data_str.split('\n')
|
||||
for line in lines:
|
||||
items = line.strip().split('\t')
|
||||
if not items[0] :
|
||||
continue
|
||||
items.append(DeviceState.ONLINE.value)
|
||||
device_instance = self._get_device_instance(
|
||||
items, DeviceOsType.default)
|
||||
local_array_list.append(device_instance)
|
||||
else:
|
||||
LOG.debug("please check device actually.[%s]" % data_str)
|
||||
LOG.debug("process target data, local_array_list" % local_array_list)
|
||||
self.update_devices(local_array_list)
|
||||
|
||||
def _get_device_instance(self, items, os_type):
|
||||
@ -751,7 +753,7 @@ class HdcHelper:
|
||||
if not os.path.exists(local):
|
||||
raise HdcError("Local path doesn't exist.")
|
||||
sock = HdcHelper.socket(host=device.host, port=device.port,
|
||||
timeout=DEFAULT_TIMEOUT)
|
||||
timeout=timeout)
|
||||
HdcHelper.handle_shake(sock, device.device_sn)
|
||||
request = HdcHelper.form_hdc_request("file send %s %s" %
|
||||
(local, remote))
|
||||
@ -760,6 +762,7 @@ class HdcHelper:
|
||||
length = struct.unpack("!I", reply)[0]
|
||||
data_buf = HdcHelper.read(sock, length)
|
||||
data_str = HdcHelper.reply_to_string(data_buf)
|
||||
device.log.info(data_str)
|
||||
|
||||
@staticmethod
|
||||
def pull_file(device, remote, local, is_create=False,
|
||||
@ -769,7 +772,7 @@ class HdcHelper:
|
||||
(convert_serial(device.device_sn), remote, local))
|
||||
|
||||
sock = HdcHelper.socket(host=device.host, port=device.port,
|
||||
timeout=DEFAULT_TIMEOUT)
|
||||
timeout=timeout)
|
||||
HdcHelper.handle_shake(sock, device.device_sn)
|
||||
request = HdcHelper.form_hdc_request("file recv %s %s" %
|
||||
(local, remote))
|
||||
@ -778,6 +781,7 @@ class HdcHelper:
|
||||
length = struct.unpack("!I", reply)[0]
|
||||
data_buf = HdcHelper.read(sock, length)
|
||||
data_str = HdcHelper.reply_to_string(data_buf)
|
||||
device.log.info(data_str)
|
||||
|
||||
@staticmethod
|
||||
def _install_remote_package(device, remote_file_path, command):
|
||||
@ -844,6 +848,8 @@ class HdcHelper:
|
||||
output_flag = kwargs.get("output_flag", True)
|
||||
timeout_msg = '' if (timeout/1000) == 300.0 else \
|
||||
" with timeout %ss" % str(timeout/1000)
|
||||
end_mark = kwargs.get("end_mark", "")
|
||||
read_timeout = kwargs.get("read_timeout", None)
|
||||
if device.usb_type == DeviceConnectorType.hdc:
|
||||
message = "%s execute command: hdc shell %s%s" % \
|
||||
(convert_serial(device.device_sn), command,
|
||||
@ -856,26 +862,31 @@ class HdcHelper:
|
||||
HdcHelper.handle_shake(sock, device.device_sn)
|
||||
request = HdcHelper.form_hdc_request("shell %s" % command)
|
||||
HdcHelper.write(sock, request)
|
||||
len_buf = HdcHelper.read(sock, DATA_UNIT_LENGTH)
|
||||
if len_buf:
|
||||
length = struct.unpack("!I", len_buf)[0]
|
||||
resp = HdcResponse()
|
||||
resp.okay = True
|
||||
|
||||
from xdevice import Scheduler
|
||||
data = sock.recv(SOCK_DATA_MAX)
|
||||
while data != b'':
|
||||
start_time = int(time.time())
|
||||
while True:
|
||||
len_buf = HdcHelper.read(sock, DATA_UNIT_LENGTH)
|
||||
if len_buf:
|
||||
length = struct.unpack("!I", len_buf)[0]
|
||||
else:
|
||||
break
|
||||
data = sock.recv(length)
|
||||
ret = HdcHelper.reply_to_string(data)
|
||||
if ret:
|
||||
if receiver:
|
||||
receiver.__read__(ret)
|
||||
else:
|
||||
LOG.debug(ret)
|
||||
|
||||
if end_mark and end_mark in ret:
|
||||
break
|
||||
if read_timeout and \
|
||||
int(time.time()) - start_time > read_timeout:
|
||||
break
|
||||
if not Scheduler.is_execute:
|
||||
raise ExecuteTerminate()
|
||||
data = HdcHelper.read(sock, SOCK_DATA_MAX)
|
||||
|
||||
return resp
|
||||
except socket.timeout as _:
|
||||
device.log.error("%s shell %s timeout[%sS]" % (
|
||||
@ -905,6 +916,8 @@ class HdcHelper:
|
||||
Create an ASCII string preceded by four hex digits.
|
||||
"""
|
||||
try:
|
||||
if not req.endswith('\0'):
|
||||
req = "%s\0" % req
|
||||
req = req.encode("utf-8")
|
||||
fmt = "!I%ss" % len(req)
|
||||
result = struct.pack(fmt, len(req), req)
|
||||
|
@ -152,15 +152,7 @@ class PushKit(ITestKit):
|
||||
LOG.warning(error, error_no=error.error_no)
|
||||
continue
|
||||
remount(device)
|
||||
device.push_file(real_src_path, dst)
|
||||
if os.path.isdir(real_src_path):
|
||||
self.add_pushed_dir(real_src_path, dst)
|
||||
else:
|
||||
if device.is_directory(dst):
|
||||
self.pushed_file.append(
|
||||
os.path.join(dst, os.path.basename(real_src_path)))
|
||||
else:
|
||||
self.pushed_file.append(dst)
|
||||
device.hdc_command("file send {} {}".format(real_src_path, dst))
|
||||
LOG.debug("Push file finished from {} to {}".format(src, dst))
|
||||
for command in self.post_push:
|
||||
run_command(device, command)
|
||||
@ -573,12 +565,13 @@ class AppInstallKit(ITestKit):
|
||||
LOG.error("The app file {} does not exist".format(app))
|
||||
continue
|
||||
if app_file.endswith(".hap"):
|
||||
self.install_hap(device, app_file)
|
||||
# use app install command directly
|
||||
device.hdc_command("app install {}".format(app_file))
|
||||
else:
|
||||
result = device.install_package(
|
||||
app_file, get_install_args(
|
||||
device, app_file, self.ex_args))
|
||||
if not result.startswith("Success"):
|
||||
if not result or not result.startswith("Success"):
|
||||
raise AppInstallError(
|
||||
"Failed to install %s on %s. Reason:%s" %
|
||||
(app_file, device.__get_serial__(), result))
|
||||
@ -591,10 +584,9 @@ class AppInstallKit(ITestKit):
|
||||
app_name = get_app_name(app)
|
||||
|
||||
if app_name:
|
||||
result = device.uninstall_package(app_name)
|
||||
if not result or not result.startswith("Success"):
|
||||
LOG.warning("error uninstalling package %s %s" %
|
||||
(device.__get_serial__(), result))
|
||||
device.hdc_command("shell bm uninstall -n {}".
|
||||
format(app_name))
|
||||
time.sleep(20)
|
||||
else:
|
||||
LOG.warning("Can't find app_name for %s" % app)
|
||||
if self.is_pri_app:
|
||||
@ -682,16 +674,9 @@ class AppInstallKit(ITestKit):
|
||||
|
||||
|
||||
def remount(device):
|
||||
device.enable_hdc_root()
|
||||
cmd = "target mount" \
|
||||
if device.usb_type == DeviceConnectorType.hdc else "remount"
|
||||
device.hdc_command(cmd)
|
||||
device.execute_shell_command("remount")
|
||||
device.execute_shell_command("mount -o rw,remount /cust")
|
||||
device.execute_shell_command("mount -o rw,remount /product")
|
||||
device.execute_shell_command("mount -o rw,remount /hw_product")
|
||||
device.execute_shell_command("mount -o rw,remount /version")
|
||||
device.execute_shell_command("mount -o rw,remount /%s" % "system")
|
||||
|
||||
|
||||
def keep_screen_on(device):
|
||||
@ -735,7 +720,7 @@ def run_command(device, command):
|
||||
elif command.strip() == "reboot-delay":
|
||||
pass
|
||||
else:
|
||||
stdout = device.execute_shell_command(command)
|
||||
stdout = device.hdc_command("shell {}".format(command))
|
||||
LOG.debug("Run command result: %s" % (stdout if stdout else ""))
|
||||
return stdout
|
||||
|
||||
|
@ -149,7 +149,7 @@ class Scheduler(object):
|
||||
error_message = "%s[%s]" % (str(exception), error_no) \
|
||||
if error_no else str(exception)
|
||||
error_no = error_no if error_no else "00000"
|
||||
LOG.exception(exception, exc_info=False, error_no=error_no)
|
||||
LOG.exception(exception, exc_info=True, error_no=error_no)
|
||||
|
||||
finally:
|
||||
Scheduler._clear_test_dict_source()
|
||||
|
@ -137,7 +137,7 @@ def _get_find_proc(find_command, list_command):
|
||||
return proc
|
||||
|
||||
|
||||
def exec_cmd(cmd, timeout=5 * 60, error_print=True, join_result=False):
|
||||
def exec_cmd(cmd, timeout=1 * 60, error_print=True, join_result=False):
|
||||
"""
|
||||
Executes commands in a new shell. Directing stderr to PIPE.
|
||||
|
||||
@ -154,6 +154,10 @@ def exec_cmd(cmd, timeout=5 * 60, error_print=True, join_result=False):
|
||||
"""
|
||||
|
||||
sys_type = platform.system()
|
||||
if isinstance(cmd, list):
|
||||
LOG.info("The running command is: {}".format(" ".join(cmd)))
|
||||
if isinstance(cmd, str):
|
||||
LOG.info("The running command is: {}".format(cmd))
|
||||
if sys_type == "Linux" or sys_type == "Darwin":
|
||||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, shell=False,
|
||||
@ -173,7 +177,7 @@ def exec_cmd(cmd, timeout=5 * 60, error_print=True, join_result=False):
|
||||
return err if err else out
|
||||
|
||||
except (TimeoutError, KeyboardInterrupt, AttributeError, ValueError,
|
||||
EOFError, IOError):
|
||||
EOFError, IOError, subprocess.TimeoutExpired):
|
||||
sys_type = platform.system()
|
||||
if sys_type == "Linux" or sys_type == "Darwin":
|
||||
os.killpg(proc.pid, signal.SIGTERM)
|
||||
|
Loading…
Reference in New Issue
Block a user