def dry_run(self, command, listener=None): parsers = get_plugin(Plugin.PARSER, ParserType.cpp_test_list_lite) parser_instances = [] for parser in parsers: parser_instance = parser.__class__() parser_instance.suites_name = os.path.basename(self.result) if listener: parser_instance.listeners = listener parser_instances.append(parser_instance) handler = ShellHandler(parser_instances) collect_test_command = "%s --gtest_list_tests" % command.split(" ")[0] result, _, _ = self.config.device.execute_command_with_timeout( command=collect_test_command, case_type=DeviceTestType.cpp_test_lite, timeout=5, receiver=handler) self.config.command_result = "{}{}".format( self.config.command_result, result) from xdevice import SuiteReporter if parser_instances[0].tests and len(parser_instances[0].tests) > 0: SuiteReporter.set_suite_list([item.test_name for item in parser_instances[0].tests]) else: SuiteReporter.set_suite_list([]) return parser_instances[0].tests
def add_task_file_handler(log_file=None): if log_file is None: return plugins = get_plugin(Plugin.LOG, LogType.tool) for log_plugin in plugins: if log_plugin.get_plugin_config().enabled: log_plugin.add_task_file_handler(log_file)
def _init_driver(self, test_descriptor): plugin_id = None source = test_descriptor.source if isinstance(source, TestSource): if source.test_type is not None: plugin_id = source.test_type else: LOG.error("'%s' no test driver specified" % source.test_name) drivers = get_plugin(plugin_type=Plugin.DRIVER, plugin_id=plugin_id) if plugin_id is not None: if len(drivers) == 0: LOG.error("'%s' can not find test driver '%s'" % (source.test_name, plugin_id)) else: from xdevice import Scheduler check_result = False for driver in drivers: driver_instance = driver.__class__() device_options = Scheduler.get_device_options( self.config.__dict__, source) check_result = driver_instance.__check_environment__( device_options) if check_result or check_result is None: self.test_drivers.append( (driver_instance, test_descriptor)) break if check_result is False: LOG.error("'%s' can not find suitable test driver '%s'" % (source.test_name, plugin_id)) for desc in test_descriptor.children: self._init_driver(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 _process_command_run(cls, command, options): scheduler = get_plugin(plugin_type=Plugin.SCHEDULER, plugin_id=SchedulerType.scheduler)[0] if scheduler is None: LOG.error("Can not find the scheduler plugin.") else: scheduler.exec_command(command, options) return
def _create_listeners(cls, task): listeners = [] # append log listeners log_listeners = get_plugin(Plugin.LISTENER, ListenerType.log) for log_listener in log_listeners: log_listener_instance = log_listener.__class__() listeners.append(log_listener_instance) # append report listeners report_listeners = get_plugin(Plugin.LISTENER, ListenerType.report) for report_listener in report_listeners: report_listener_instance = report_listener.__class__() setattr(report_listener_instance, "report_path", task.config.report_path) listeners.append(report_listener_instance) # append upload listeners upload_listeners = get_plugin(Plugin.LISTENER, ListenerType.upload) for upload_listener in upload_listeners: upload_listener_instance = upload_listener.__class__() listeners.append(upload_listener_instance) return listeners
def get_shell_handler(request, parser_type): suite_name = request.root.source.test_name parsers = get_plugin(Plugin.PARSER, parser_type) parser_instances = [] for listener in request.listeners: listener.device_sn = request.config.environment.devices[0].device_sn for parser in parsers: parser_instance = parser.__class__() parser_instance.suite_name = suite_name parser_instance.listeners = request.listeners parser_instances.append(parser_instance) handler = ShellHandler(parser_instances) return handler
def _run_ctest(self, source=None, request=None): parser_instances = [] parsers = get_plugin(Plugin.PARSER, ParserType.ctest_lite) result = "Execute command error" try: if not source: LOG.error("Error: source don't exist %s." % source) return if not self.config.device.local_device: LOG.error( "CTest must have a local device, please check " "your config.") return reset_cmd = self._reset_device(request, source) self.result = "%s.xml" % os.path.join( request.config.report_path, "result", self.file_name) self.config.device.local_device.com_dict.get( ComType.deploy_com).connect() result, _, error = self.config.device.local_device. \ execute_command_with_timeout( command=reset_cmd, case_type=DeviceTestType.ctest_lite, key=ComType.deploy_com, timeout=90) 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("{}{}".format( "\n".join(result.split("\n")[0:-1]), "\n")) if error: raise LiteDeviceReadOutputError(error) except (LiteDeviceExecuteCommandError, Exception) as exception: LOG.error(exception) self.error_message = exception finally: close_error = self.config.device.local_device.com_dict.get( ComType.deploy_com).close() if close_error: self.error_message = close_error version = get_test_component_version(self.config) for parser in parsers: parser_instance = parser.__class__() parser_instance.suites_name = self.file_name parser_instance.product_params.setdefault("Version", version) parser_instance.listeners = request.listeners parser_instances.append(parser_instance) handler = ShellHandler(parser_instances) generate_report(handler, result)
def _init_logger(): import time from _core.constants import LogType from _core.logger import Log from _core.plugin import Plugin from _core.plugin import get_plugin tool_logger_plugin = get_plugin(Plugin.LOG, LogType.tool) if tool_logger_plugin: return @Plugin(type=Plugin.LOG, id=LogType.tool, enabled=True) class ToolLog(Log): @classmethod def get_plugin_type(cls): return Plugin.LOG @classmethod def get_plugin_id(cls): return LogType.tool tool_log_file = None if Variables.exec_dir and os.path.normcase( Variables.exec_dir) == os.path.normcase(Variables.top_dir): host_log_path = os.path.join(Variables.exec_dir, Variables.report_vars.report_dir, Variables.report_vars.log_dir) os.makedirs(host_log_path, exist_ok=True) time_str = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()) tool_file_name = "platform_log_{}.log".format(time_str) tool_log_file = os.path.join(host_log_path, tool_file_name) tool_logger_plugin = get_plugin(Plugin.LOG, LogType.tool)[0] or ToolLog() tool_logger_plugin.__initial__(Variables.report_vars.log_handler, tool_log_file, Variables.report_vars.log_level, Variables.report_vars.log_format)
def run(self, command=None, listener=None, timeout=20): parsers = get_plugin(Plugin.PARSER, ParserType.open_source_test) parser_instances = [] for parser in parsers: parser_instance = parser.__class__() parser_instance.suite_name = self.file_name parser_instance.test_name = command.replace("./", "") parser_instance.listeners = listener parser_instances.append(parser_instance) self.handler = ShellHandler(parser_instances) result, _, error = self.config.device.execute_command_with_timeout( command=command, case_type=DeviceTestType.open_source_test, timeout=timeout, receiver=self.handler) self.config.command_result = "{}{}".format( self.config.command_result, result) return error, result, self.handler
def run(self, command=None, listener=None, timeout=900): if listener: parsers = get_plugin(Plugin.PARSER, ParserType.cpp_test_lite) parser_instances = [] for parser in parsers: parser_instance = parser.__class__() parser_instance.suite_name = self.file_name parser_instance.listeners = listener parser_instances.append(parser_instance) handler = ShellHandler(parser_instances) else: handler = None result, _, error = self.config.device.execute_command_with_timeout( command=command, case_type=DeviceTestType.cpp_test_lite, timeout=timeout, receiver=handler) self.config.command_result += result return error, result, handler
def init_environment(self, environment=""): device_lite = get_plugin(plugin_type=Plugin.DEVICE, plugin_id=DeviceOsType.lite)[0] devices = UserConfigManager( env=environment).get_com_device("environment/device") for device in devices: try: device_lite_instance = device_lite.__class__() device_lite_instance.__init_device__(device) device_lite_instance.device_allocation_state = \ DeviceAllocationState.available except (LiteDeviceConnectError, ParamError): continue self.devices_list.append(device_lite_instance)
def remove_task_file_handler(): plugins = get_plugin(Plugin.LOG, LogType.tool) for log_plugin in plugins: if log_plugin.get_plugin_config().enabled: log_plugin.remove_task_file_handler()
def device_logger(name=None): plugins = get_plugin(Plugin.LOG, LogType.device) for log_plugin in plugins: if log_plugin.get_plugin_config().enabled: return log_plugin.__logger__(name) return _init_global_logger(name)