def read_local_output_test(com=None,
                               command=None,
                               timeout=TIMEOUT,
                               receiver=None):
        input_command = command
        error_message = ""
        start_time = time.time()
        result = ""
        status = True
        from xdevice import Scheduler

        while time.time() - start_time < timeout:
            if not Scheduler.is_execute:
                raise ExecuteTerminate()
            data = com.readline().decode('gbk', errors='ignore')
            data = PATTERN.sub('', data)
            result = "{}{}".format(result, data.replace("\r", ""))
            if check_read_test_end(result, input_command):
                break
        else:
            error_message = "execute command with timeout=%s " % command
            LOG.debug("error_message:%s" % error_message)
            status = False

        if receiver:
            if command in result:
                index = result.find(command) + len(command)
                result_output = result[index:]
            else:
                result_output = result
            generate_report(receiver, result_output)

        LOG.info('Info: execute command success')
        return result, status, error_message
    def __execute__(self, request):
        kits = []
        try:
            self.config = request.config
            self.init_cpp_config()
            self.config.device = request.config.environment.devices[0]
            self.__init_nfs_server__(request=request)
            config_file = request.root.source.config_file
            json_config = JsonParser(config_file)
            self._get_driver_config(json_config)

            bin_file = get_config_value('execute', json_config.get_driver(),
                                        False)
            kits = get_kit_instances(json_config,
                                     request.config.resource_path,
                                     request.config.testcases_path)
            from xdevice import Scheduler
            for kit in kits:
                if not Scheduler.is_execute:
                    raise ExecuteTerminate()
                kit.__setup__(request.config.device, request=request)

            execute_dir = "/".join(bin_file.split("/")[0:-1])
            execute_bin = bin_file.split("/")[-1]

            self.config.device.execute_command_with_timeout(
                command="cd {}".format(execute_dir), timeout=1)

            command, report_path, self.has_param = get_execute_command(
                execute_bin, self.config.xml_output, execute_dir)
            self.config.device_xml_path = (self.linux_directory +
                                           report_path).replace("//", "/")
            self.set_file_name(request, command)
            self.need_download = self.is_download(
                request.config.testcases_path)
            if self.need_download and self.has_param:
                self.delete_device_xml(request, self.config.device_xml_path)
            if os.path.exists(self.result):
                os.remove(self.result)
            if request.config.testargs.get("dry_run"):
                self.config.dry_run = request.config.testargs.get(
                    "dry_run")[0].lower()
                self.dry_run(command, request.listeners)
            else:
                self.run_cpp_test(command, request)
                self.generate_device_xml(request, execute_bin)
                device_log_file = get_device_log_file(
                    request.config.report_path,
                    request.config.device.__get_serial__())
                with open(device_log_file, "a", encoding="UTF-8") as file_name:
                    file_name.write(self.config.command_result)

        except (LiteDeviceExecuteCommandError, Exception) as exception:
            LOG.exception(exception)
            self.error_message = exception
        finally:
            LOG.info("-------------finally-----------------")
            self._after_command(kits, request)
    def __execute__(self, request):
        kits = []
        try:
            self.config = request.config
            setattr(self.config, "command_result", "")
            self.config.device = request.config.environment.devices[0]
            self.__init_nfs_server__(request=request)
            config_file = request.root.source.config_file
            json_config = JsonParser(config_file)
            pre_cmd = get_config_value('pre_cmd', json_config.get_driver(),
                                       False)
            execute_dir = get_config_value('execute', json_config.get_driver(),
                                           False)
            kits = get_kit_instances(json_config,
                                     request.config.resource_path,
                                     request.config.testcases_path)
            from xdevice import Scheduler
            for kit in kits:
                if not Scheduler.is_execute:
                    raise ExecuteTerminate()
                copy_list = kit.__setup__(request.config.device,
                                          request=request)

            self.file_name = request.root.source.test_name
            self.set_file_name(request, request.root.source.test_name)
            self.config.device.execute_command_with_timeout(
                command=pre_cmd, timeout=0.5)
            self.config.device.execute_command_with_timeout(
                command="cd {}".format(execute_dir), timeout=1)
            for test_bin in copy_list:
                if not test_bin.endswith(".run-test"):
                    continue
                command, _, _ = get_execute_command(test_bin, False)
                self._do_test_run(command, request)
            device_log_file = get_device_log_file(
                request.config.report_path,
                request.config.device.__get_serial__())
            with open(device_log_file, "a", encoding="UTF-8") as file_name:
                file_name.write(self.config.command_result)

        except (LiteDeviceExecuteCommandError, Exception) as exception:
            LOG.error(exception)
            self.error_message = exception
        finally:
            LOG.info("-------------finally-----------------")
            # umount the dirs already mount
            for kit in kits:
                kit.__teardown__(request.config.device)
            close_error = self.config.device.close()
            self.error_message = close_error if close_error else\
                "case results are abnormal"
            report_name = "report" if request.root.source. \
                test_name.startswith("{") else get_filename_extension(
                    request.root.source.test_name)[0]
            self.result = check_result_report(
                request.config.report_path, self.result, self.error_message,
                report_name)
    def execute_remote_cmd_with_timeout(telnet,
                                        command="",
                                        timeout=TIMEOUT,
                                        receiver=None):
        """
        Executes command on the device.

        Parameters:
            telnet:
            command: the command to execute
            timeout: timeout for read result
            receiver: parser handler
        """
        from xdevice import Scheduler
        time.sleep(2)
        start_time = time.time()
        status = True
        error_message = ""
        result = ""
        LOG.info("execute command shell %s with timeout %s" %
                 (command, str(timeout)))
        if not telnet:
            raise LiteDeviceConnectError("remote device is not connected.")

        telnet.write(command.encode('ascii') + b"\n")

        expect_result = [
            bytes(CPP_TEST_STANDARD_SIGN, encoding="utf8"),
            bytes(CPP_SYS_STANDARD_SIGN, encoding="utf8"),
            bytes(CPP_TEST_END_SIGN, encoding="utf8")
        ]
        while time.time() - start_time < timeout:
            if not Scheduler.is_execute:
                raise ExecuteTerminate()
            _, _, data = telnet.expect(expect_result, timeout=1)
            data = PATTERN.sub('', data.decode('gbk', 'ignore'))
            result = result + data.replace("\r", "")

            if check_read_test_end(result, command):
                break
        else:
            error_message = "execute command with timeout=%s " % command
            LOG.debug("error_message=%s" % error_message)
            status = False

        if receiver:
            if command in result:
                index = result.find(command) + len(command)
                result_output = result[index:]
            else:
                result_output = result
            generate_report(receiver, result_output)

        return result, status, error_message
Esempio n. 5
0
def set_up_env(request, source):
    if not source.endswith(".json"):
        source = "%s.json" % source
    json_config = JsonParser(source)
    test_args = get_config_value('xml-output', json_config.get_driver())

    kits = get_kit_instances(json_config, request.config.resource_path,
                             request.config.testcases_path)
    kits_copy = copy.deepcopy(kits)
    from xdevice import Scheduler
    for kit in kits:
        if not Scheduler.is_execute:
            raise ExecuteTerminate()
        kit.__setup__(request.config.device, request=request)
    return test_args, kits, kits_copy
 def _result_hdc_out_process(proc, timeout, result_print):
     result = ""
     from xdevice import Scheduler
     while proc.poll() is None:
         if not Scheduler.is_execute:
             raise ExecuteTerminate()
         line = proc.stdout.readline()
         line = line.strip()
         if isinstance(line, bytes):
             line = line.decode('utf-8', 'ignore').strip()
         if line != "":
             result = "%s%s%s" % (result, line, "\n")
             if result_print:
                 LOG.info(line)
         if timeout.is_timeout:
             LOG.info("timeout flag is True")
             timeout.is_timeout = False
             break
     return result
    def read_local_output_ctest(com=None,
                                command=None,
                                timeout=TIMEOUT,
                                receiver=None):
        result = ""
        input_command = command

        start = time.time()
        from xdevice import Scheduler
        while True:
            if not Scheduler.is_execute:
                raise ExecuteTerminate()
            data = com.readline().decode('gbk', errors='ignore')
            data = PATTERN.sub('', data)
            if isinstance(input_command, list):
                data = "{} {}".format(get_current_time(), data)
                result = "{}{}".format(result, data.replace("\r", ""))
                if re.search(
                        r"\d+\s+Tests\s+\d+\s+Failures\s+\d+\s+"
                        r"Ignored", data):
                    start = time.time()
                if (int(time.time()) - int(start)) > timeout:
                    break
            else:
                result = "{}{}".format(
                    result,
                    data.replace("\r", "").replace("\n", "").strip())
                if AT_CMD_ENDS in data:
                    return result, True, ""
                if (int(time.time()) - int(start)) > timeout:
                    return result, False, ""

        result = PATTERN.sub('', result)
        if receiver:
            result = "{}{}".format(
                result, "{} {}".format(get_current_time(), CTEST_END_SIGN))
            generate_report(receiver, result)
        LOG.info('Info: execute command success')
        return result, True, ""
 def _reset_device(self, request, source):
     json_config = JsonParser(source)
     reset_cmd = []
     kit_instances = get_kit_instances(json_config,
                                       request.config.resource_path,
                                       request.config.testcases_path)
     from xdevice import Scheduler
     for (kit_instance, kit_info) in zip(kit_instances,
                                         json_config.get_kits()):
         if not isinstance(kit_instance, DeployKit):
             continue
         if not self.file_name:
             self.file_name = get_config_value(
                 'burn_file', kit_info)[0].split("\\")[-1].split(".")[0]
         reset_cmd = kit_instance.burn_command
         if not Scheduler.is_execute:
             raise ExecuteTerminate()
         setup_result = kit_instance.__setup__(
             self.config.device.local_device)
         if not setup_result:
             raise DeviceError("set_up wifiiot failed")
     reset_cmd = [int(item, 16) for item in reset_cmd]
     return reset_cmd