def set_connect_type(self, device): pattern = r'^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[' \ r'01]?\d\d?)$' for item in device: if "label" in item.keys(): self.label = item.get("label") if "com" in item.keys(): self.device_connect_type = "local" if "ip" in item.keys(): if re.match(pattern, item.get("ip")): self.device_connect_type = "remote" else: self.error_message = "Remote device ip not in right" \ "format, please check user_config.xml" LOG.error(self.error_message) raise ParamError(self.error_message) if not self.label: self.error_message = "device label cannot be empty, " \ "please check" LOG.error(self.error_message) raise ParamError(self.error_message) else: if self.label != DeviceLabelType.wifiiot and self.label != \ DeviceLabelType.ipcamera and self.label != \ DeviceLabelType.watch: self.error_message = "device label should be ipcamera or" \ " wifiiot, please check" LOG.error(self.error_message) raise ParamError(self.error_message) if not self.device_connect_type: self.error_message = "device com or ip cannot be empty, " \ "please check" LOG.error(self.error_message) raise ParamError(self.error_message)
def _get_test_file(config, testcases_dirs): if os.path.isabs(config.testfile): if os.path.exists(config.testfile): return config.testfile else: raise ParamError("test file '%s' not exists" % config.testfile) for testcases_dir in testcases_dirs: test_file = os.path.join(testcases_dir, config.testfile) if os.path.exists(test_file): return test_file raise ParamError("test file '%s' not exists" % config.testfile)
def _get_retry_options(self, options): input_report_path = options.reportpath # get history_command, history_report_path if options.retry == "retry_previous_command": if len(Scheduler.command_queue) < 2: raise ParamError("no previous command executed") _, history_command, history_report_path = \ Scheduler.command_queue[-2] else: history_command, history_report_path = "", "" for command_tuple in Scheduler.command_queue[:-1]: if command_tuple[0] != options.retry: continue history_command, history_report_path = \ command_tuple[1], command_tuple[2] break if not history_command: raise ParamError("wrong task id input: %s" % options.retry) LOG.info("History command: %s", history_command) # parse history_command, set history_report_path if not os.path.exists(history_report_path) and \ Scheduler.mode != "decc": raise ParamError("history report path %s not exists" % history_report_path) (options, _, _, _) = self.argument_parser(history_command.split()) setattr(options, "history_report_path", history_report_path) # modify history_command -rp param if options.reportpath: if input_report_path: history_command = history_command.replace( options.reportpath, input_report_path) setattr(options, "reportpath", input_report_path) else: history_command = history_command.replace( options.reportpath, "").replace("-rp", "").replace("--reportpath", "") setattr(options, "reportpath", "") else: if input_report_path: history_command = "{}{}".format(history_command, " -rp %s" % input_report_path) setattr(options, "reportpath", input_report_path) history_command = history_command.strip() # add history command to Scheduler.command_queue LOG.info("Retry command: %s", history_command) Scheduler.command_queue[-1] = history_command return options
def __check_ipcamera_remote__(self, device=None): for item in device: if "label" not in item.keys(): if "port" in item.keys() and item.get( "port") and not item.get("port").isnumeric(): self.error_message = "ipcamera remote port should be " \ "a number, please check" LOG.error(self.error_message) raise ParamError(self.error_message) elif "port" not in item.keys(): self.error_message = "ipcamera remote port cannot be" \ " empty, please check" LOG.error(self.error_message) raise ParamError(self.error_message)
def swipe(device, start_point, end_point, timeout=800): if not isinstance(start_point, tuple) and not isinstance( end_point, tuple): raise ParamError( "The coordinates of the sliding point should be tuple") start_x, start_y = start_point end_x, end_y = end_point press_first_command = "%s%s%s%s%s" % ("uievent ", str(start_x), " ", str(start_y), " PRESSDOWN") press_end_command = "%s%s%s%s" % ("uievent ", str(end_x), str(end_y), " PRESSDOWN") release_end_command = "%s%s%s%s%s" % ("uievent ", str(end_x), " ", str(end_y), " RELEASE") press_first_point_result = LiteUiKit.execute_hdc_cmd_with_timeout( device, get_hdc_command(press_first_command, True), timeout) if press_first_point_result.return_code == 0: time.sleep(timeout / 1000) press_medium_point_result = \ LiteUiKit.execute_hdc_cmd_with_timeout( device, get_hdc_command(press_end_command, True), timeout, True) if press_medium_point_result.return_code == 0: time.sleep(timeout / 1000) press_end_point_result = \ LiteUiKit.execute_hdc_cmd_with_timeout( device, get_hdc_command(release_end_command, True), timeout, True) if press_end_point_result.return_code == 0: LOG.info("swipe success.") return True return False
def ui_dump_tree(device, **kwargs): args = kwargs node_id = args.get("node_id", "") timeout = args.get("timeout", "") if node_id: dump_tree_command = "%s%s" % ("uidump tree ", node_id) else: dump_tree_command = "uidump tree" dump_tree_value = LiteUiKit.execute_hdc_cmd_with_timeout( device, get_hdc_command(dump_tree_command), timeout, True) if dump_tree_value.return_code == 0: dump_str = "" from _core.environment.device_lite import get_hdc_path local_path = get_hdc_path() retry_times = 2 while retry_times > 0: command = "rfile user/log/dump_dom_tree.json" if LiteUiKit.download_file(device, command, timeout=1400): with open(local_path, "r") as file_stream: dump_str = file_stream.read() break retry_times -= 1 return dump_str else: LOG.error("dump failed") raise ParamError("dump failed")
def set_root_descriptor(self, root): if not isinstance(root, Descriptor): raise TypeError("need 'Descriptor' type param") self.root = root self._init_driver(root) if not self.test_drivers: raise ParamError("no test driver to execute")
def __check_ipcamera_local__(self, device): for item in device: if "label" not in item.keys(): if "com" not in item.keys() or ("com" in item.keys() and not item.get("com")): self.error_message = "ipcamera local com cannot be " \ "empty, please check" LOG.error(self.error_message) raise ParamError(self.error_message)
def _load_task(self, task_dir, file_name): task_file = os.path.join(task_dir, file_name) if not os.path.exists(task_file): raise ParamError("task file %s not exists" % task_file) # add kits to self.config json_config = JsonParser(task_file) kits = get_kit_instances(json_config, self.config.resource_path, self.config.testcases_path) self.config.kits.extend(kits)
def __check_watch__(self, device): for item in device: if "label" not in item.keys(): if "com" not in item.keys() or ("com" in item.keys() and not item.get("com")): self.error_message = "watch local com cannot be " \ "empty, please check" LOG.error(self.error_message) raise ParamError(self.error_message) else: hdc = get_hdc_path() result = exec_cmd([hdc]) com_list = parse_available_com(result) if item.get("com").upper() in com_list: return True else: self.error_message = "watch local com does not exist" LOG.error(self.error_message) raise ParamError(self.error_message)
def click_id(device, node_id, timeout=800): if not node_id: raise ParamError("miss node id") status, point = LiteUiKit.ui_dump_id(device, node_id=node_id, timeout=timeout) if status: LiteUiKit.click(device, point[0], point[1], timeout) else: LOG.info("click failed")
def click_text(device, text="", timeout=800): status = False if not text: raise ParamError("miss node text") dump_str = LiteUiKit.ui_dump_tree(device, timeout=timeout) if dump_str and text: status, point = get_center(dump_str, "text", text) if status: LiteUiKit.click(point[0], point[1], timeout) else: LOG.info("click failed")
def _do_parse(self, path_or_content): try: if path_or_content.find("{") != -1: json_content = json.loads(path_or_content, encoding="utf-8") else: if not os.path.exists(path_or_content): raise ParamError("The json file {} does not exist".format( path_or_content)) with open(path_or_content, encoding="utf-8") as file_content: json_content = json.load(file_content) except (TypeError, ValueError, AttributeError) as error: raise ParamError("%s %s" % (path_or_content, error)) self._check_config(json_content) # set self.config self.config = Config() self.config.description = json_content.get("description", "") self.config.kits = json_content.get("kits", []) self.config.environment = json_content.get("environment", []) self.config.driver = json_content.get("driver", {})
def _get_task_dir(self, task_file): from xdevice import Variables exec_task_dir = os.path.abspath( os.path.join(Variables.exec_dir, self.TASK_CONFIG_DIR)) if not os.path.exists(os.path.join(exec_task_dir, task_file)): if os.path.normcase(Variables.exec_dir) == \ os.path.normcase(Variables.top_dir): raise ParamError("task file %s not exists, please add task " "file to '%s'" % (task_file, exec_task_dir)) top_task_dir = os.path.abspath( os.path.join(Variables.top_dir, self.TASK_CONFIG_DIR)) if not os.path.exists(os.path.join(top_task_dir, task_file)): raise ParamError("task file %s not exists, please add task " "file to '%s' or '%s'" % (task_file, exec_task_dir, top_task_dir)) else: return top_task_dir else: return exec_task_dir
def _parse_combination_param(combination_value): # sample: size:xxx1;exclude-annotation:xxx parse_result = {} key_value_pairs = str(combination_value).split(";") for key_value_pair in key_value_pairs: key, value = key_value_pair.split(":", 1) if not value: raise ParamError("'%s' no value" % key) value_list = str(value).split(",") exist_list = parse_result.get(key, []) exist_list.extend(value_list) parse_result[key] = exist_list return parse_result
def _normalize_test_sources(testcases_dirs, test_sources): norm_test_sources = [] for test_source in test_sources: append_result = False for testcases_dir in testcases_dirs: append_result = _append_norm_test_source( norm_test_sources, test_source, testcases_dir) if append_result: break if not append_result: norm_test_sources.append(test_source) if not norm_test_sources: raise ParamError("test source not found") return norm_test_sources
def __check_wifiiot_config__(self, device): com_type_set = set() for item in device: if "label" not in item.keys(): if "com" not in item.keys() or ("com" in item.keys() and not item.get("com")): self.error_message = "wifiiot local com cannot be " \ "empty, please check" LOG.error(self.error_message) raise ParamError(self.error_message) if "type" not in item.keys() or ("type" not in item.keys() and not item.get("type")): self.error_message = "wifiiot com type cannot be " \ "empty, please check" LOG.error(self.error_message) raise ParamError(self.error_message) else: com_type_set.add(item.get("type")) if len(com_type_set) < 2: self.error_message = "wifiiot need cmd com and deploy com" \ " at the same time, please check" LOG.error(self.error_message) raise ParamError(self.error_message)
def __setup__(self, device, **kwargs): if device.label != DeviceLabelType.ipcamera: return request = kwargs.get("request", None) if not request: raise ParamError("the request of queryKit is None") self.mount_kit.__setup__(device, request=request) device.execute_command_with_timeout(command="cd /", timeout=0.2) output, _, _ = device.execute_command_with_timeout( command=".{}".format(self.query), timeout=5) product_param = {} for line in output.split("\n"): process_product_params(line, product_param) product_param["version"] = get_test_component_version(request.config) request.product_params = product_param
def _host_test_execute(self, task): """execute host test""" test_drivers = task.test_drivers if not test_drivers: raise ParamError("test driver not found") params_list = [] for test_driver in task.test_drivers: listeners = self._create_listeners(task) params_list.append((listeners, task, [test_driver])) try: Concurrent.concurrent_execute(self._execute_test_drivers, params_list, 2) finally: # generate reports self._generate_task_report(task)
def _create_descriptor(config_file, filename, test_source, test_type): from _core.executor.request import Descriptor uid = unique_id("TestSource", filename) # create Descriptor if os.path.isfile(test_source): desc = Descriptor(uuid=uid, name=filename, source=TestSource(test_source, "", config_file, filename, test_type)) elif is_config_str(test_source): desc = Descriptor(uuid=uid, name=filename, source=TestSource("", test_source, config_file, filename, test_type)) else: raise ParamError("test source '%s' or '%s' not exists" % ( test_source, "%s%s" % (test_source, ".json"))) return desc
def get_kit_instances(json_config, resource_path, testcases_path): from _core.testkit.json_parser import JsonParser kit_instances = [] if not isinstance(json_config, JsonParser): return kit_instances for kit in json_config.config.kits: kit["paths"] = [resource_path, testcases_path] kit_type = kit.get("type", "") if get_plugin(plugin_type=Plugin.TEST_KIT, plugin_id=kit_type): test_kit = \ get_plugin(plugin_type=Plugin.TEST_KIT, plugin_id=kit_type)[0] test_kit_instance = test_kit.__class__() test_kit_instance.__check_config__(kit) kit_instances.append(test_kit_instance) else: raise ParamError("kit %s not exists" % kit_type) return kit_instances
def get_hdc_path(): from xdevice import Variables user_path = os.path.join(Variables.exec_dir, "resource/tools") top_user_path = os.path.join(Variables.top_dir, "config") config_path = os.path.join(Variables.res_dir, "config") paths = [user_path, top_user_path, config_path] file_path = "" for path in paths: if os.path.exists(os.path.abspath(os.path.join(path, HDC))): file_path = os.path.abspath(os.path.join(path, HDC)) break if os.path.exists(file_path): return file_path else: raise ParamError("config file not found")
def find_testdict_descriptors(config): from xdevice import Variables if getattr(config, "testdict", "") == "": return None testdict = config.testdict test_descriptors = [] for test_type_key, files in testdict.items(): for file_name in files: if not os.path.isabs(file_name): file_name = os.path.join(Variables.exec_dir, file_name) if os.path.isfile( file_name) and test_type_key in TEST_TYPE_DICT.keys(): desc = _make_test_descriptor(os.path.abspath(file_name), test_type_key) if desc is not None: test_descriptors.append(desc) if not test_descriptors: raise ParamError("test source is none") return test_descriptors
def __setup__(self, device, **kwargs): """ Mount the file to the board by the nfs server. """ request = kwargs.get("request", None) if not request: raise ParamError("MountKit setup request is None") device.connect() config_manager = UserConfigManager(env=request.config.test_environment) remote_info = config_manager.get_user_config("testcases/server", filter_name=self.server) copy_list = self.copy_to_server(remote_info.get("dir"), remote_info.get("ip"), request, request.config.testcases_path) self.mount_on_board(device=device, remote_info=remote_info, case_type=DeviceTestType.cpp_test_lite) return copy_list
def get_file_absolute_path(input_name, paths=None, alt_dir=None): abs_paths = set(paths) if paths else set() _update_paths(abs_paths) for path in abs_paths: if alt_dir: file_path = os.path.join(path, alt_dir, input_name) if os.path.exists(file_path): return os.path.abspath(file_path) file_path = os.path.join(path, input_name) if os.path.exists(file_path): return os.path.abspath(file_path) err_msg = "The file {} does not exist".format(input_name) LOG.error(err_msg) if alt_dir: LOG.debug("alt_dir is %s" % alt_dir) LOG.debug("paths is:") for path in abs_paths: LOG.debug(path) raise ParamError(err_msg)
def _find_test_root_descriptor(config): # read test list from testfile, testlist or task if getattr(config, "testfile", "") or getattr(config, "testlist", "") \ or getattr(config, "task", ""): test_set = config.testfile or config.testlist or config.task fname, _ = get_filename_extension(test_set) uid = unique_id("Scheduler", fname) root = Descriptor(uuid=uid, name=fname, source=TestSetSource(test_set), container=True) root.children = find_test_descriptors(config) return root # read test list from testdict elif getattr(config, "testdict", "") != "": uid = unique_id("Scheduler", "testdict") root = Descriptor(uuid=uid, name="testdict", source=TestSetSource(config.testdict), container=True) root.children = find_testdict_descriptors(config) return root else: raise ParamError("no test file, list, dict or task found")
def _check_report_path(cls, report_path): for _, _, files in os.walk(report_path): for _file in files: if _file.endswith(".xml"): raise ParamError("xml file exists in '%s'" % report_path)
def _check_type_key_exist(cls, key, value): if not isinstance(value, dict): raise ParamError("%s under %s should be dict" % (value, key)) if "type" not in value.keys(): raise ParamError("'type' key not exists in %s under %s" % (value, key))
def mount_on_board(self, device=None, remote_info=None, case_type=""): """ Init the environment on the device server, eg. mount the testcases to server Parameters: device: DeviceLite, device lite on local or remote remote_info: dict, includes linux_host: str, nfs_server ip linux_directory: str, the directory on the linux is_remote: str, server is remote or not case_type: str, CppTestLite or CTestLite, default value is DeviceTestType.cpp_test_lite Returns: True or False, represent init Failed or success """ if not remote_info: raise ParamError("failed to get server environment") linux_host = remote_info.get("ip", "") linux_directory = remote_info.get("dir", "") is_remote = remote_info.get("remote", "false") liteos_commands = [ "cd /", "umount device_directory", "mount nfs_ip:nfs_directory device" "_directory nfs" ] linux_commands = [ "cd /", "umount device_directory", "mount -t nfs -o nolock -o tcp nfs_ip:nfs_directory" "device_directory", "chmod 755 -R device_directory" ] if not linux_host or not linux_directory: raise LiteDeviceMountError("nfs server miss ip or directory") if device.device_connect_type == "local": device.local_device.flush_input() commands = [] if device.label == "ipcamera": env_result, status, _ = device.execute_command_with_timeout( command="uname", timeout=1) if status: if env_result.find(DeviceLiteKernel.linux_kernel) != -1: commands = linux_commands device.__set_device_kernel__(DeviceLiteKernel.linux_kernel) else: commands = liteos_commands device.__set_device_kernel__(DeviceLiteKernel.lite_kernel) else: raise LiteDeviceMountError("failed to get device env") for mount_file in self.mount_list: target = mount_file.get("target", "/test_root") if target in self.mounted_dir: continue mkdir_on_board(device, target) # local nfs server need use alias of dir to mount if is_remote.lower() == "false": linux_directory = get_mount_dir(linux_directory) for command in commands: command = command.replace("nfs_ip", linux_host). \ replace("nfs_directory", linux_directory).replace( "device_directory", target) timeout = 15 if command.startswith("mount") else 1 result, status, _ = device.execute_command_with_timeout( command=command, case_type=case_type, timeout=timeout) if command.startswith("mount"): self.mounted_dir.add(target) LOG.info('prepare environment success')