def append(self, payload): """! Append stream buffer with payload and process. Returns non-KV strings""" logger = HtrunLogger('CONN') try: self.buff += payload.decode('utf-8') except UnicodeDecodeError: logger.prn_wrn("UnicodeDecodeError encountered!") self.buff += payload.decode('utf-8','ignore') lines = self.buff.split('\n') self.buff = lines[-1] # remaining lines.pop(-1) # List of line or strings that did not match K,V pair. discarded = [] for line in lines: m = self.re_kv.search(line) if m: (key, value) = m.groups() self.kvl.append((key, value, time())) line = line.strip() match = m.group(0) pos = line.find(match) before = line[:pos] after = line[pos + len(match):] if len(before) > 0: discarded.append(before) if len(after) > 0: # not a K,V pair part discarded.append(after) else: # not a K,V pair discarded.append(line) return discarded
def __init__(self, options): """ ctor """ # For compatibility with old mbed. We can use command line options for Mbed object # or we can pass options directly from . self.options = options self.logger = HtrunLogger('MBED') # Options related to copy / reset mbed device self.port = self.options.port self.disk = self.options.disk self.target_id = self.options.target_id self.image_path = self.options.image_path.strip( '"') if self.options.image_path is not None else '' self.copy_method = self.options.copy_method self.retry_copy = self.options.retry_copy self.program_cycle_s = float( self.options.program_cycle_s if self.options. program_cycle_s is not None else 2.0) self.polling_timeout = self.options.polling_timeout # Serial port settings self.serial_baud = DEFAULT_BAUD_RATE self.serial_timeout = 1 # Users can use command to pass port speeds together with port name. E.g. COM4:115200:1 # Format if PORT:SPEED:TIMEOUT port_config = self.port.split(':') if self.port else '' if len(port_config) == 2: # -p COM4:115200 self.port = port_config[0] self.serial_baud = int(port_config[1]) elif len(port_config) == 3: # -p COM4:115200:0.5 self.port = port_config[0] self.serial_baud = int(port_config[1]) self.serial_timeout = float(port_config[2]) # Overriding baud rate value with command line specified value self.serial_baud = self.options.baud_rate if self.options.baud_rate else self.serial_baud # Test configuration in JSON format self.test_cfg = None if self.options.json_test_configuration is not None: # We need to normalize path before we open file json_test_configuration_path = self.options.json_test_configuration.strip( "\"'") try: self.logger.prn_inf("Loading test configuration from '%s'..." % json_test_configuration_path) with open(json_test_configuration_path) as data_file: self.test_cfg = json.load(data_file) except IOError as e: self.logger.prn_err( "Test configuration JSON file '{0}' I/O error({1}): {2}". format(json_test_configuration_path, e.errno, e.strerror)) except: self.logger.prn_err( "Test configuration JSON Unexpected error:", str(e)) raise
class ConnectorPrimitive(object): def __init__(self, name): self.LAST_ERROR = None self.logger = HtrunLogger(name) self.polling_timeout = 60 def write_kv(self, key, value): """! Forms and sends Key-Value protocol message. @details On how to parse K-V sent from DUT see KiViBufferWalker::KIVI_REGEX On how DUT sends K-V please see greentea_write_postamble() function in greentea-client @return Returns buffer with K-V message sent to DUT on success, None on failure """ # All Key-Value messages ends with newline character kv_buff = "{{%s;%s}}" % (key, value) + '\n' if self.write(kv_buff): self.logger.prn_txd(kv_buff.rstrip()) return kv_buff else: return None def read(self, count): """! Read data from DUT @param count Number of bytes to read @return Bytes read """ raise NotImplementedError def write(self, payload, log=False): """! Read data from DUT @param payload Buffer with data to send @param log Set to True if you want to enable logging for this function @return Payload (what was actually sent - if possible to establish that) """ raise NotImplementedError def flush(self): """! Flush read/write channels of DUT """ raise NotImplementedError def connected(self): """! Check if there is a connection to DUT @return True if there is conenction to DUT (read/write/flush API works) """ raise NotImplementedError def error(self): """! Returns LAST_ERROR value @return Value of self.LAST_ERROR """ return self.LAST_ERROR def finish(self): """! Handle DUT dtor like (close resource) operations here """ raise NotImplementedError
def __init__(self): """ ctor """ # Setting Host Test Logger instance ht_loggers = { 'BasePlugin' : HtrunLogger('PLGN'), 'CopyMethod' : HtrunLogger('COPY'), 'ResetMethod' : HtrunLogger('REST'), } self.plugin_logger = ht_loggers.get(self.type, ht_loggers['BasePlugin'])
class ConnectorPrimitive(object): def __init__(self, name): self.LAST_ERROR = None self.logger = HtrunLogger(name) def write_kv(self, key, value): """! Forms and sends Key-Value protocol message. @details On how to parse K-V sent from DUT see KiViBufferWalker::KIVI_REGEX On how DUT sends K-V please see greentea_write_postamble() function in greentea-client @return Returns buffer with K-V message sent to DUT """ # All Key-Value messages ends with newline character kv_buff = "{{%s;%s}}"% (key, value) + '\n' self.write(kv_buff) self.logger.prn_txd(kv_buff.rstrip()) return kv_buff def read(self, count): """! Read data from DUT @param count Number of bytes to read @return Bytes read """ raise NotImplementedError def write(self, payload, log=False): """! Read data from DUT @param payload Buffer with data to send @param log Set to True if you want to enable logging for this function @return Payload (what was actually sent - if possible to establish that) """ raise NotImplementedError def flush(self): """! Flush read/write channels of DUT """ raise NotImplementedError def connected(self): """! Check if there is a connection to DUT @return True if there is conenction to DUT (read/write/flush API works) """ raise NotImplementedError def error(self): """! Returns LAST_ERROR value @return Value of self.LAST_ERROR """ raise NotImplementedError return self.LAST_ERROR def finish(self): """! Handle DUT dtor like (close resource) operations here """ raise NotImplementedError
def __init__(self, options): """! ctor """ self.options = options self.logger = HtrunLogger('HTST') # Handle extra command from if options: if options.enum_host_tests: path = self.options.enum_host_tests enum_host_tests(path, verbose=options.verbose) if options.list_reg_hts: # --list option print_ht_list(verbose=options.verbose) sys.exit(0) if options.list_plugins: # --plugins option host_tests_plugins.print_plugin_info() sys.exit(0) if options.version: # --version import pkg_resources # part of setuptools version = pkg_resources.require("mbed-host-tests")[0].version print version sys.exit(0) if options.send_break_cmd: # -b with -p PORT (and optional -r RESET_TYPE) handle_send_break_cmd(port=options.port, disk=options.disk, reset_type=options.forced_reset_type, baudrate=options.baud_rate, verbose=options.verbose) sys.exit(0) if options.global_resource_mgr: # If Global Resource Mgr is working it will handle reset/flashing workflow # So local plugins are offline self.options.skip_reset = True self.options.skip_flashing = True if options.compare_log: with open(options.compare_log, "r") as f: self.compare_log = f.read().splitlines() else: self.compare_log = None self.serial_output_file = options.serial_output_file self.compare_log_idx = 0 DefaultTestSelectorBase.__init__(self, options)
def __init__(self): super(SoftAPBasicHostTests, self).__init__() self.logger = HtrunLogger('TEST') self.log = self.logger.prn_inf self.__result = False self.wifi_dev = 'wlan0' # The SoftAP config must be consisten with client side self.softap_ssid = 'MBEDSoftAPTest' self.softap_password = '******' self.softap_ip_address = '192.168.10.1' self.softap_event_action_table = {'up': self.host_wifi_softap_up_test}
def __init__(self, options): """! ctor """ self.options = options self.logger = HtrunLogger("HTST") # Handle extra command from if options: if options.enum_host_tests: path = self.options.enum_host_tests enum_host_tests(path, verbose=options.verbose) if options.list_reg_hts: # --list option print_ht_list(verbose=options.verbose) sys.exit(0) if options.list_plugins: # --plugins option host_tests_plugins.print_plugin_info() sys.exit(0) if options.version: # --version import pkg_resources # part of setuptools version = pkg_resources.require("mbed-host-tests")[0].version print version sys.exit(0) if options.send_break_cmd: # -b with -p PORT (and optional -r RESET_TYPE) handle_send_break_cmd( port=options.port, disk=options.disk, reset_type=options.forced_reset_type, baudrate=options.baud_rate, verbose=options.verbose, ) sys.exit(0) if options.global_resource_mgr: # If Global Resource Mgr is working it will handle reset/flashing workflow # So local plugins are offline self.options.skip_reset = True self.options.skip_flashing = True DefaultTestSelectorBase.__init__(self, options)
def __init__(self): super(HTTPHostTests, self).__init__() self.logger = HtrunLogger('TEST')
class Mbed: """! Base class for a host driven test @details This class stores information about things like disk, port, serial speed etc. Class is also responsible for manipulation of serial port between host and mbed device """ def __init__(self, options): """ ctor """ # For compatibility with old mbed. We can use command line options for Mbed object # or we can pass options directly from . self.options = options self.logger = HtrunLogger('MBED') # Options related to copy / reset mbed device self.port = self.options.port self.disk = self.options.disk self.target_id = self.options.target_id self.image_path = self.options.image_path.strip( '"') if self.options.image_path is not None else '' self.copy_method = self.options.copy_method self.retry_copy = self.options.retry_copy self.program_cycle_s = float( self.options.program_cycle_s if self.options. program_cycle_s is not None else 2.0) self.polling_timeout = self.options.polling_timeout # Serial port settings self.serial_baud = DEFAULT_BAUD_RATE self.serial_timeout = 1 # Users can use command to pass port speeds together with port name. E.g. COM4:115200:1 # Format if PORT:SPEED:TIMEOUT port_config = self.port.split(':') if self.port else '' if len(port_config) == 2: # -p COM4:115200 self.port = port_config[0] self.serial_baud = int(port_config[1]) elif len(port_config) == 3: # -p COM4:115200:0.5 self.port = port_config[0] self.serial_baud = int(port_config[1]) self.serial_timeout = float(port_config[2]) # Overriding baud rate value with command line specified value self.serial_baud = self.options.baud_rate if self.options.baud_rate else self.serial_baud # Test configuration in JSON format self.test_cfg = None if self.options.json_test_configuration is not None: # We need to normalize path before we open file json_test_configuration_path = self.options.json_test_configuration.strip( "\"'") try: self.logger.prn_inf("Loading test configuration from '%s'..." % json_test_configuration_path) with open(json_test_configuration_path) as data_file: self.test_cfg = json.load(data_file) except IOError as e: self.logger.prn_err( "Test configuration JSON file '{0}' I/O error({1}): {2}". format(json_test_configuration_path, e.errno, e.strerror)) except: self.logger.prn_err( "Test configuration JSON Unexpected error:", str(e)) raise def copy_image(self, image_path=None, disk=None, copy_method=None, port=None, retry_copy=5): """! Closure for copy_image_raw() method. @return Returns result from copy plugin """ def get_remount_count(disk_path, tries=2): """! Get the remount count from 'DETAILS.TXT' file @return Returns count, None if not-available """ for cur_try in range(1, tries + 1): try: files_on_disk = [x.upper() for x in os.listdir(disk_path)] if 'DETAILS.TXT' in files_on_disk: with open(os.path.join(disk_path, 'DETAILS.TXT'), 'r') as details_txt: for line in details_txt.readlines(): if 'Remount count:' in line: return int( line.replace('Remount count: ', '')) # Remount count not found in file return None # 'DETAILS.TXT file not found else: return None except OSError as e: self.logger.prn_err( "Failed to get remount count due to OSError.", str(e)) self.logger.prn_inf("Retrying in 1 second (try %s of %s)" % (cur_try, tries)) sleep(1) # Failed to get remount count return None def check_flash_error(target_id, disk, initial_remount_count): """! Check for flash errors @return Returns false if FAIL.TXT present, else true """ if not target_id: self.logger.prn_wrn( "Target ID not found: Skipping flash check and retry") return True bad_files = set(['FAIL.TXT']) # Re-try at max 5 times with 0.5 sec in delay for i in range(5): # mbed_lstools.create() should be done inside the loop. Otherwise it will loop on same data. mbeds = mbed_lstools.create() mbed_list = mbeds.list_mbeds() #list of mbeds present # get first item in list with a matching target_id, if present mbed_target = next( (x for x in mbed_list if x['target_id'] == target_id), None) if mbed_target is not None: if 'mount_point' in mbed_target and mbed_target[ 'mount_point'] is not None: if not initial_remount_count is None: new_remount_count = get_remount_count(disk) if not new_remount_count is None and new_remount_count == initial_remount_count: sleep(0.5) continue common_items = [] try: items = set([ x.upper() for x in os.listdir(mbed_target['mount_point']) ]) common_items = bad_files.intersection(items) except OSError as e: print("Failed to enumerate disk files, retrying") continue for common_item in common_items: full_path = os.path.join( mbed_target['mount_point'], common_item) self.logger.prn_err("Found %s" % (full_path)) bad_file_contents = "[failed to read bad file]" try: with open(full_path, "r") as bad_file: bad_file_contents = bad_file.read() except IOError as error: self.logger.prn_err("Error opening '%s': %s" % (full_path, error)) self.logger.prn_err("Error file contents:\n%s" % bad_file_contents) if common_items: return False sleep(0.5) return True # Set-up closure environment if not image_path: image_path = self.image_path if not disk: disk = self.disk if not copy_method: copy_method = self.copy_method if not port: port = self.port if not retry_copy: retry_copy = self.retry_copy target_id = self.target_id for count in range(0, retry_copy): initial_remount_count = get_remount_count(disk) # Call proper copy method result = self.copy_image_raw(image_path, disk, copy_method, port) sleep(self.program_cycle_s) if not result: continue result = check_flash_error(target_id, disk, initial_remount_count) if result: break return result def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None): """! Copy file depending on method you want to use. Handles exception and return code from shell copy commands. @return Returns result from copy plugin @details Method which is actually copying image to mbed """ # image_path - Where is binary with target's firmware # Select copy_method # We override 'default' method with 'shell' method copy_method = { None: 'shell', 'default': 'shell', }.get(copy_method, copy_method) result = ht_plugins.call_plugin('CopyMethod', copy_method, image_path=image_path, serial=port, destination_disk=disk, target_id=self.target_id, pooling_timeout=self.polling_timeout) return result def hw_reset(self): """ Performs hardware reset of target ned device. :return: """ device_info = {} result = ht_plugins.call_plugin('ResetMethod', 'power_cycle', target_id=self.target_id, device_info=device_info) if result: self.port = device_info['serial_port'] self.disk = device_info['mount_point'] return result
def __init__(self, name): self.LAST_ERROR = None self.logger = HtrunLogger(name) self.polling_timeout = 60
def __init__(self, name): self.LAST_ERROR = None self.logger = HtrunLogger(name)
class DefaultTestSelector(DefaultTestSelectorBase): """! Select default host_test supervision (replaced after auto detection) """ RESET_TYPE_SW_RST = "software_reset" RESET_TYPE_HW_RST = "hardware_reset" def __init__(self, options): """! ctor """ self.options = options self.logger = HtrunLogger('HTST') # Handle extra command from if options: if options.enum_host_tests: path = self.options.enum_host_tests enum_host_tests(path, verbose=options.verbose) if options.list_reg_hts: # --list option print_ht_list(verbose=options.verbose) sys.exit(0) if options.list_plugins: # --plugins option host_tests_plugins.print_plugin_info() sys.exit(0) if options.version: # --version import pkg_resources # part of setuptools version = pkg_resources.require("mbed-host-tests")[0].version print version sys.exit(0) if options.send_break_cmd: # -b with -p PORT (and optional -r RESET_TYPE) handle_send_break_cmd(port=options.port, disk=options.disk, reset_type=options.forced_reset_type, baudrate=options.baud_rate, verbose=options.verbose) sys.exit(0) if options.global_resource_mgr: # If Global Resource Mgr is working it will handle reset/flashing workflow # So local plugins are offline self.options.skip_reset = True self.options.skip_flashing = True DefaultTestSelectorBase.__init__(self, options) def is_host_test_obj_compatible(self, obj_instance): """! Check if host test object loaded is actually host test class derived from 'mbed_host_tests.BaseHostTest()' Additionaly if host test class implements custom ctor it should call BaseHostTest().__Init__() @param obj_instance Instance of host test derived class @return True if obj_instance is derived from mbed_host_tests.BaseHostTest() and BaseHostTest.__init__() was called, else return False """ result = False if obj_instance: result = True self.logger.prn_inf("host test class: '%s'"% obj_instance.__class__) # Check if host test (obj_instance) is derived from mbed_host_tests.BaseHostTest() if not isinstance(obj_instance, BaseHostTest): # In theory we should always get host test objects inheriting from BaseHostTest() # because loader will only load those. self.logger.prn_err("host test must inherit from mbed_host_tests.BaseHostTest() class") result = False # Check if BaseHostTest.__init__() was called when custom host test is created if not obj_instance.base_host_test_inited(): self.logger.prn_err("custom host test __init__() must call BaseHostTest.__init__(self)") result = False return result def run_test(self): """! This function implements key-value protocol state-machine. Handling of all events and connector are handled here. @return Return self.TestResults.RESULT_* enum """ result = None timeout_duration = 10 # Default test case timeout event_queue = Queue() # Events from DUT to host dut_event_queue = Queue() # Events from host to DUT {k;v} def callback__notify_prn(key, value, timestamp): """! Handles __norify_prn. Prints all lines in separate log line """ for line in value.splitlines(): self.logger.prn_inf(line) callbacks = { "__notify_prn" : callback__notify_prn } # if True we will allow host test to consume all events after test is finished callbacks_consume = True # Flag check if __exit event occurred callbacks__exit = False # Flag check if __exit_event_queue event occurred callbacks__exit_event_queue = False # Handle to dynamically loaded host test object self.test_supervisor = None # Version: greentea-client version from DUT self.client_version = None self.logger.prn_inf("starting host test process...") def start_conn_process(): # Create device info here as it may change after restart. config = { "digest" : "serial", "port" : self.mbed.port, "baudrate" : self.mbed.serial_baud, "program_cycle_s" : self.options.program_cycle_s, "reset_type" : self.options.forced_reset_type, "target_id" : self.options.target_id, "serial_pooling" : self.options.pooling_timeout, "forced_reset_timeout" : self.options.forced_reset_timeout, "sync_behavior" : self.options.sync_behavior, "platform_name" : self.options.micro, "image_path" : self.mbed.image_path, } if self.options.global_resource_mgr: grm_module, grm_host, grm_port = self.options.global_resource_mgr.split(':') config.update({ "conn_resource" : 'grm', "grm_module" : grm_module, "grm_host" : grm_host, "grm_port" : grm_port, }) # DUT-host communication process args = (event_queue, dut_event_queue, config) p = Process(target=conn_process, args=args) p.deamon = True p.start() return p p = start_conn_process() start_time = time() try: consume_preamble_events = True while (time() - start_time) < timeout_duration: # Handle default events like timeout, host_test_name, ... try: (key, value, timestamp) = event_queue.get(timeout=1) except QueueEmpty: continue if consume_preamble_events: if key == '__timeout': # Override default timeout for this event queue start_time = time() timeout_duration = int(value) # New timeout self.logger.prn_inf("setting timeout to: %d sec"% int(value)) elif key == '__version': self.client_version = value self.logger.prn_inf("DUT greentea-client version: " + self.client_version) elif key == '__host_test_name': # Load dynamically requested host test self.test_supervisor = get_host_test(value) # Check if host test object loaded is actually host test class # derived from 'mbed_host_tests.BaseHostTest()' # Additionaly if host test class implements custom ctor it should # call BaseHostTest().__Init__() if self.test_supervisor and self.is_host_test_obj_compatible(self.test_supervisor): # Pass communication queues and setup() host test self.test_supervisor.setup_communication(event_queue, dut_event_queue) try: # After setup() user should already register all callbacks self.test_supervisor.setup() except (TypeError, ValueError): # setup() can throw in normal circumstances TypeError and ValueError self.logger.prn_err("host test setup() failed, reason:") self.logger.prn_inf("==== Traceback start ====") for line in traceback.format_exc().splitlines(): print line self.logger.prn_inf("==== Traceback end ====") result = self.RESULT_ERROR event_queue.put(('__exit_event_queue', 0, time())) self.logger.prn_inf("host test setup() call...") if self.test_supervisor.get_callbacks(): callbacks.update(self.test_supervisor.get_callbacks()) self.logger.prn_inf("CALLBACKs updated") else: self.logger.prn_wrn("no CALLBACKs specified by host test") self.logger.prn_inf("host test detected: %s"% value) else: self.logger.prn_err("host test not detected: %s"% value) result = self.RESULT_ERROR event_queue.put(('__exit_event_queue', 0, time())) consume_preamble_events = False elif key == '__sync': # This is DUT-Host Test handshake event self.logger.prn_inf("sync KV found, uuid=%s, timestamp=%f"% (str(value), timestamp)) elif key == '__notify_conn_lost': # This event is sent by conn_process, DUT connection was lost self.logger.prn_err(value) self.logger.prn_wrn("stopped to consume events due to %s event"% key) callbacks_consume = False result = self.RESULT_IO_SERIAL event_queue.put(('__exit_event_queue', 0, time())) elif key == '__exit_event_queue': # This event is sent by the host test indicating no more events expected self.logger.prn_inf("%s received"% (key)) callbacks__exit_event_queue = True break elif key.startswith('__'): # Consume other system level events pass else: self.logger.prn_err("orphan event in preamble phase: {{%s;%s}}, timestamp=%f"% (key, str(value), timestamp)) else: if key == '__notify_complete': # This event is sent by Host Test, test result is in value # or if value is None, value will be retrieved from HostTest.result() method self.logger.prn_inf("%s(%s)"% (key, str(value))) result = value elif key == '__reset_dut': # Disconnect to avoid connection lost event dut_event_queue.put(('__host_test_finished', True, time())) p.join() if value == DefaultTestSelector.RESET_TYPE_SW_RST: self.logger.prn_inf("Performing software reset.") # Just disconnecting and re-connecting comm process will soft reset DUT elif value == DefaultTestSelector.RESET_TYPE_HW_RST: self.logger.prn_inf("Performing hard reset.") # request hardware reset self.mbed.hw_reset() else: self.logger.prn_err("Invalid reset type (%s). Supported types [%s]." % (value, ", ".join([DefaultTestSelector.RESET_TYPE_HW_RST, DefaultTestSelector.RESET_TYPE_SW_RST]))) self.logger.prn_inf("Software reset will be performed.") # connect to the device p = start_conn_process() elif key == '__notify_conn_lost': # This event is sent by conn_process, DUT connection was lost self.logger.prn_err(value) self.logger.prn_wrn("stopped to consume events due to %s event"% key) callbacks_consume = False result = self.RESULT_IO_SERIAL event_queue.put(('__exit_event_queue', 0, time())) elif key == '__exit': # This event is sent by DUT, test suite exited self.logger.prn_inf("%s(%s)"% (key, str(value))) callbacks__exit = True event_queue.put(('__exit_event_queue', 0, time())) elif key == '__exit_event_queue': # This event is sent by the host test indicating no more events expected self.logger.prn_inf("%s received"% (key)) callbacks__exit_event_queue = True break elif key in callbacks: # Handle callback callbacks[key](key, value, timestamp) else: self.logger.prn_err("orphan event in main phase: {{%s;%s}}, timestamp=%f"% (key, str(value), timestamp)) except Exception: self.logger.prn_err("something went wrong in event main loop!") self.logger.prn_inf("==== Traceback start ====") for line in traceback.format_exc().splitlines(): print line self.logger.prn_inf("==== Traceback end ====") result = self.RESULT_ERROR time_duration = time() - start_time self.logger.prn_inf("test suite run finished after %.2f sec..."% time_duration) # Force conn_proxy process to return dut_event_queue.put(('__host_test_finished', True, time())) p.join() self.logger.prn_inf("CONN exited with code: %s"% str(p.exitcode)) # Callbacks... self.logger.prn_inf("No events in queue" if event_queue.empty() else "Some events in queue") # If host test was used we will: # 1. Consume all existing events in queue if consume=True # 2. Check result from host test and call teardown() # NOTE: with the introduction of the '__exit_event_queue' event, there # should never be left events assuming the DUT has stopped sending data # over the serial data. Leaving this for now to catch anything that slips through. if callbacks_consume: # We are consuming all remaining events if requested while not event_queue.empty(): try: (key, value, timestamp) = event_queue.get(timeout=1) except QueueEmpty: break if key == '__notify_complete': # This event is sent by Host Test, test result is in value # or if value is None, value will be retrieved from HostTest.result() method self.logger.prn_inf("%s(%s)"% (key, str(value))) result = value elif key.startswith('__'): # Consume other system level events pass elif key in callbacks: callbacks[key](key, value, timestamp) else: self.logger.prn_wrn(">>> orphan event: {{%s;%s}}, timestamp=%f"% (key, str(value), timestamp)) self.logger.prn_inf("stopped consuming events") if result is not None: # We must compare here against None! # Here for example we've received some error code like IOERR_COPY self.logger.prn_inf("host test result() call skipped, received: %s"% str(result)) else: if self.test_supervisor: result = self.test_supervisor.result() self.logger.prn_inf("host test result(): %s"% str(result)) if not callbacks__exit: self.logger.prn_wrn("missing __exit event from DUT") if not callbacks__exit_event_queue: self.logger.prn_wrn("missing __exit_event_queue event from host test") #if not callbacks__exit_event_queue and not result: if not callbacks__exit_event_queue and result is None: self.logger.prn_err("missing __exit_event_queue event from " + \ "host test and no result from host test, timeout...") result = self.RESULT_TIMEOUT self.logger.prn_inf("calling blocking teardown()") if self.test_supervisor: self.test_supervisor.teardown() self.logger.prn_inf("teardown() finished") return result def execute(self): """! Test runner for host test. @details This function will start executing test and forward test result via serial port to test suite. This function is sensitive to work-flow flags such as --skip-flashing, --skip-reset etc. First function will flash device with binary, initialize serial port for communication, reset target. On serial port handshake with test case will be performed. It is when host test reads property data from serial port (sent over serial port). At the end of the procedure proper host test (defined in set properties) will be executed and test execution timeout will be measured. """ result = self.RESULT_UNDEF # hello sting with htrun version, for debug purposes self.logger.prn_inf(self.get_hello_string()) try: # Copy image to device if self.options.skip_flashing: self.logger.prn_inf("copy image onto target... SKIPPED!") else: self.logger.prn_inf("copy image onto target...") result = self.mbed.copy_image() if not result: result = self.RESULT_IOERR_COPY return self.get_test_result_int(result) # Execute test if flashing was successful or skipped test_result = self.run_test() if test_result == True: result = self.RESULT_SUCCESS elif test_result == False: result = self.RESULT_FAILURE elif test_result is None: result = self.RESULT_ERROR else: result = test_result # This will be captured by Greentea self.logger.prn_inf("{{result;%s}}"% result) return self.get_test_result_int(result) except KeyboardInterrupt: return(-3) # Keyboard interrupt
def conn_process(event_queue, dut_event_queue, config): logger = HtrunLogger('CONN') logger.prn_inf("starting connection process...") # Send connection process start event to host process # NOTE: Do not send any other Key-Value pairs before this! event_queue.put(('__conn_process_start', 1, time())) # Configuration of conn_opriocess behaviour sync_behavior = int(config.get('sync_behavior', 1)) sync_timeout = config.get('sync_timeout', 1.0) conn_resource = config.get('conn_resource', 'serial') # Create connector instance with proper configuration connector = conn_primitive_factory(conn_resource, config, event_queue, logger) # Create simple buffer we will use for Key-Value protocol data kv_buffer = KiViBufferWalker() # List of all sent to target UUIDs (if multiple found) sync_uuid_list = [] # We will ignore all kv pairs before we get sync back sync_uuid_discovered = False def __send_sync(timeout=None): sync_uuid = str(uuid.uuid4()) # Handshake, we will send {{sync;UUID}} preamble and wait for mirrored reply if timeout: logger.prn_inf("resending new preamble '%s' after %0.2f sec"% (sync_uuid, timeout)) else: logger.prn_inf("sending preamble '%s'"% sync_uuid) connector.write_kv('__sync', sync_uuid) return sync_uuid # Send simple string to device to 'wake up' greentea-client k-v parser connector.write("mbed" * 10, log=True) # Sync packet management allows us to manipulate the way htrun sends __sync packet(s) # With current settings we can force on htrun to send __sync packets in this manner: # # * --sync=0 - No sync packets will be sent to target platform # * --sync=-10 - __sync packets will be sent unless we will reach # timeout or proper response is sent from target platform # * --sync=N - Send up to N __sync packets to target platform. Response # is sent unless we get response from target platform or # timeout occur if sync_behavior > 0: # Sending up to 'n' __sync packets logger.prn_inf("sending up to %s __sync packets (specified with --sync=%s)"% (sync_behavior, sync_behavior)) sync_uuid_list.append(__send_sync()) sync_behavior -= 1 elif sync_behavior == 0: # No __sync packets logger.prn_wrn("skipping __sync packet (specified with --sync=%s)"% sync_behavior) else: # Send __sync until we go reply logger.prn_inf("sending multiple __sync packets (specified with --sync=%s)"% sync_behavior) sync_uuid_list.append(__send_sync()) sync_behavior -= 1 loop_timer = time() while True: # Check if connection is lost to serial if not connector.connected(): error_msg = connector.error() connector.finish() event_queue.put(('__notify_conn_lost', error_msg, time())) break # Send data to DUT try: (key, value, _) = dut_event_queue.get(block=False) except QueueEmpty: pass # Check if target sent something else: # Return if state machine in host_test_default has finished to end process if key == '__host_test_finished' and value == True: logger.prn_inf("received special even '%s' value='%s', finishing"% (key, value)) connector.finish() return 0 connector.write_kv(key, value) # Since read is done every 0.2 sec, with maximum baud rate we can receive 2304 bytes in one read in worst case. data = connector.read(2304) if data: # Stream data stream KV parsing print_lines = kv_buffer.append(data) for line in print_lines: logger.prn_rxd(line) event_queue.put(('__rxd_line', line, time())) while kv_buffer.search(): key, value, timestamp = kv_buffer.pop_kv() if sync_uuid_discovered: event_queue.put((key, value, timestamp)) logger.prn_inf("found KV pair in stream: {{%s;%s}}, queued..."% (key, value)) else: if key == '__sync': if value in sync_uuid_list: sync_uuid_discovered = True event_queue.put((key, value, time())) idx = sync_uuid_list.index(value) logger.prn_inf("found SYNC in stream: {{%s;%s}} it is #%d sent, queued..."% (key, value, idx)) else: logger.prn_err("found faulty SYNC in stream: {{%s;%s}}, ignored..."% (key, value)) else: logger.prn_wrn("found KV pair in stream: {{%s;%s}}, ignoring..."% (key, value)) if not sync_uuid_discovered: # Resending __sync after 'sync_timeout' secs (default 1 sec) # to target platform. If 'sync_behavior' counter is != 0 we # will continue to send __sync packets to target platform. # If we specify 'sync_behavior' < 0 we will send 'forever' # (or until we get reply) if sync_behavior != 0: time_to_sync_again = time() - loop_timer if time_to_sync_again > sync_timeout: sync_uuid_list.append(__send_sync(timeout=time_to_sync_again)) sync_behavior -= 1 loop_timer = time() return 0
def __init__(self, options): """! ctor """ self.options = options self.logger = HtrunLogger('HTST') self.registry = HostRegistry() self.registry.register_host_test("echo", EchoTest()) self.registry.register_host_test("default", DefaultAuto()) self.registry.register_host_test("rtc_auto", RTCTest()) self.registry.register_host_test("hello_auto", HelloTest()) self.registry.register_host_test("detect_auto", DetectPlatformTest()) self.registry.register_host_test("default_auto", DefaultAuto()) self.registry.register_host_test("wait_us_auto", WaitusTest()) self.registry.register_host_test("dev_null_auto", DevNullTest()) # Handle extra command from if options: if options.enum_host_tests: for path in options.enum_host_tests: self.registry.register_from_path(path, verbose=options.verbose) if options.list_reg_hts: # --list option print(self.registry.table(options.verbose)) sys.exit(0) if options.list_plugins: # --plugins option host_tests_plugins.print_plugin_info() sys.exit(0) if options.version: # --version import pkg_resources # part of setuptools version = pkg_resources.require("mbed-host-tests")[0].version print(version) sys.exit(0) if options.send_break_cmd: # -b with -p PORT (and optional -r RESET_TYPE) handle_send_break_cmd(port=options.port, disk=options.disk, reset_type=options.forced_reset_type, baudrate=options.baud_rate, verbose=options.verbose) sys.exit(0) if options.global_resource_mgr or options.fast_model_connection: # If Global/Simulator Resource Mgr is working it will handle reset/flashing workflow # So local plugins are offline self.options.skip_reset = True self.options.skip_flashing = True if options.compare_log: with open(options.compare_log, "r") as f: self.compare_log = f.read().splitlines() else: self.compare_log = None self.serial_output_file = options.serial_output_file self.compare_log_idx = 0 DefaultTestSelectorBase.__init__(self, options)
class DefaultTestSelector(DefaultTestSelectorBase): """! Select default host_test supervision (replaced after auto detection) """ RESET_TYPE_SW_RST = "software_reset" RESET_TYPE_HW_RST = "hardware_reset" def __init__(self, options): """! ctor """ self.options = options self.logger = HtrunLogger('HTST') # Handle extra command from if options: if options.enum_host_tests: path = self.options.enum_host_tests enum_host_tests(path, verbose=options.verbose) if options.list_reg_hts: # --list option print_ht_list(verbose=options.verbose) sys.exit(0) if options.list_plugins: # --plugins option host_tests_plugins.print_plugin_info() sys.exit(0) if options.version: # --version import pkg_resources # part of setuptools version = pkg_resources.require("mbed-host-tests")[0].version print version sys.exit(0) if options.send_break_cmd: # -b with -p PORT (and optional -r RESET_TYPE) handle_send_break_cmd(port=options.port, disk=options.disk, reset_type=options.forced_reset_type, baudrate=options.baud_rate, verbose=options.verbose) sys.exit(0) if options.global_resource_mgr: # If Global Resource Mgr is working it will handle reset/flashing workflow # So local plugins are offline self.options.skip_reset = True self.options.skip_flashing = True DefaultTestSelectorBase.__init__(self, options) def is_host_test_obj_compatible(self, obj_instance): """! Check if host test object loaded is actually host test class derived from 'mbed_host_tests.BaseHostTest()' Additionaly if host test class implements custom ctor it should call BaseHostTest().__Init__() @param obj_instance Instance of host test derived class @return True if obj_instance is derived from mbed_host_tests.BaseHostTest() and BaseHostTest.__init__() was called, else return False """ result = False if obj_instance: result = True self.logger.prn_inf("host test class: '%s'" % obj_instance.__class__) # Check if host test (obj_instance) is derived from mbed_host_tests.BaseHostTest() if not isinstance(obj_instance, BaseHostTest): # In theory we should always get host test objects inheriting from BaseHostTest() # because loader will only load those. self.logger.prn_err( "host test must inherit from mbed_host_tests.BaseHostTest() class" ) result = False # Check if BaseHostTest.__init__() was called when custom host test is created if not obj_instance.base_host_test_inited(): self.logger.prn_err( "custom host test __init__() must call BaseHostTest.__init__(self)" ) result = False return result def run_test(self): """! This function implements key-value protocol state-machine. Handling of all events and connector are handled here. @return Return self.TestResults.RESULT_* enum """ result = None timeout_duration = 10 # Default test case timeout coverage_idle_timeout = 10 # Default coverage idle timeout event_queue = Queue() # Events from DUT to host dut_event_queue = Queue() # Events from host to DUT {k;v} def callback__notify_prn(key, value, timestamp): """! Handles __norify_prn. Prints all lines in separate log line """ for line in value.splitlines(): self.logger.prn_inf(line) callbacks = {"__notify_prn": callback__notify_prn} # if True we will allow host test to consume all events after test is finished callbacks_consume = True # Flag check if __exit event occurred callbacks__exit = False # Flag check if __exit_event_queue event occurred callbacks__exit_event_queue = False # Handle to dynamically loaded host test object self.test_supervisor = None # Version: greentea-client version from DUT self.client_version = None self.logger.prn_inf("starting host test process...") def start_conn_process(): # Create device info here as it may change after restart. config = { "digest": "serial", "port": self.mbed.port, "baudrate": self.mbed.serial_baud, "program_cycle_s": self.options.program_cycle_s, "reset_type": self.options.forced_reset_type, "target_id": self.options.target_id, "serial_pooling": self.options.pooling_timeout, "forced_reset_timeout": self.options.forced_reset_timeout, "sync_behavior": self.options.sync_behavior, "platform_name": self.options.micro, "image_path": self.mbed.image_path, } if self.options.global_resource_mgr: grm_module, grm_host, grm_port = self.options.global_resource_mgr.split( ':') config.update({ "conn_resource": 'grm', "grm_module": grm_module, "grm_host": grm_host, "grm_port": grm_port, }) # DUT-host communication process args = (event_queue, dut_event_queue, config) p = Process(target=conn_process, args=args) p.deamon = True p.start() return p def process_code_coverage(key, value, timestamp): """! Process the found coverage key value and perform an idle loop checking for more timeing out if there is no response from the target within the idle timeout. @param key The key from the first coverage event @param value The value from the first coverage event @param timestamp The timestamp from the first coverage event @return The elapsed time taken by the processing of code coverage, and the (key, value, and timestamp) of the next event """ original_start_time = time() start_time = time() # Perform callback on first event callbacks[key](key, value, timestamp) # Start idle timeout loop looking for other events while (time() - start_time) < coverage_idle_timeout: try: (key, value, timestamp) = event_queue.get(timeout=1) except QueueEmpty: continue # If coverage detected use idle loop # Prevent breaking idle loop for __rxd_line (occurs between keys) if key == '__coverage_start' or key == '__rxd_line': start_time = time() # Perform callback callbacks[key](key, value, timestamp) continue elapsed_time = time() - original_start_time return elapsed_time, (key, value, timestamp) p = start_conn_process() conn_process_started = False try: (key, value, timestamp) = event_queue.get( timeout=self.options.process_start_timeout) if key == '__conn_process_start': conn_process_started = True else: self.logger.prn_err( "First expected event was '__conn_process_start', received '%s' instead" % key) except QueueEmpty: self.logger.prn_err("Conn process failed to start in %f sec" % self.options.process_start_timeout) if not conn_process_started: p.terminate() return self.RESULT_TIMEOUT start_time = time() try: consume_preamble_events = True while (time() - start_time) < timeout_duration: # Handle default events like timeout, host_test_name, ... try: (key, value, timestamp) = event_queue.get(timeout=1) except QueueEmpty: continue if consume_preamble_events: if key == '__timeout': # Override default timeout for this event queue start_time = time() timeout_duration = int(value) # New timeout self.logger.prn_inf("setting timeout to: %d sec" % int(value)) elif key == '__version': self.client_version = value self.logger.prn_inf("DUT greentea-client version: " + self.client_version) elif key == '__host_test_name': # Load dynamically requested host test self.test_supervisor = get_host_test(value) # Check if host test object loaded is actually host test class # derived from 'mbed_host_tests.BaseHostTest()' # Additionaly if host test class implements custom ctor it should # call BaseHostTest().__Init__() if self.test_supervisor and self.is_host_test_obj_compatible( self.test_supervisor): # Pass communication queues and setup() host test self.test_supervisor.setup_communication( event_queue, dut_event_queue) try: # After setup() user should already register all callbacks self.test_supervisor.setup() except (TypeError, ValueError): # setup() can throw in normal circumstances TypeError and ValueError self.logger.prn_err( "host test setup() failed, reason:") self.logger.prn_inf( "==== Traceback start ====") for line in traceback.format_exc().splitlines( ): print line self.logger.prn_inf("==== Traceback end ====") result = self.RESULT_ERROR event_queue.put( ('__exit_event_queue', 0, time())) self.logger.prn_inf("host test setup() call...") if self.test_supervisor.get_callbacks(): callbacks.update( self.test_supervisor.get_callbacks()) self.logger.prn_inf("CALLBACKs updated") else: self.logger.prn_wrn( "no CALLBACKs specified by host test") self.logger.prn_inf("host test detected: %s" % value) else: self.logger.prn_err("host test not detected: %s" % value) result = self.RESULT_ERROR event_queue.put(('__exit_event_queue', 0, time())) consume_preamble_events = False elif key == '__sync': # This is DUT-Host Test handshake event self.logger.prn_inf( "sync KV found, uuid=%s, timestamp=%f" % (str(value), timestamp)) elif key == '__notify_conn_lost': # This event is sent by conn_process, DUT connection was lost self.logger.prn_err(value) self.logger.prn_wrn( "stopped to consume events due to %s event" % key) callbacks_consume = False result = self.RESULT_IO_SERIAL event_queue.put(('__exit_event_queue', 0, time())) elif key == '__exit_event_queue': # This event is sent by the host test indicating no more events expected self.logger.prn_inf("%s received" % (key)) callbacks__exit_event_queue = True break elif key.startswith('__'): # Consume other system level events pass else: self.logger.prn_err( "orphan event in preamble phase: {{%s;%s}}, timestamp=%f" % (key, str(value), timestamp)) else: # If coverage detected switch to idle loop if key == '__coverage_start': self.logger.prn_inf( "starting coverage idle timeout loop...") elapsed_time, (key, value, timestamp) = process_code_coverage( key, value, timestamp) # Ignore the time taken by the code coverage timeout_duration += elapsed_time self.logger.prn_inf( "exiting coverage idle timeout loop (elapsed_time: %.2f" % elapsed_time) if key == '__notify_complete': # This event is sent by Host Test, test result is in value # or if value is None, value will be retrieved from HostTest.result() method self.logger.prn_inf("%s(%s)" % (key, str(value))) result = value elif key == '__reset_dut': # Disconnect to avoid connection lost event dut_event_queue.put( ('__host_test_finished', True, time())) p.join() if value == DefaultTestSelector.RESET_TYPE_SW_RST: self.logger.prn_inf("Performing software reset.") # Just disconnecting and re-connecting comm process will soft reset DUT elif value == DefaultTestSelector.RESET_TYPE_HW_RST: self.logger.prn_inf("Performing hard reset.") # request hardware reset self.mbed.hw_reset() else: self.logger.prn_err( "Invalid reset type (%s). Supported types [%s]." % (value, ", ".join([ DefaultTestSelector.RESET_TYPE_HW_RST, DefaultTestSelector.RESET_TYPE_SW_RST ]))) self.logger.prn_inf( "Software reset will be performed.") # connect to the device p = start_conn_process() elif key == '__notify_conn_lost': # This event is sent by conn_process, DUT connection was lost self.logger.prn_err(value) self.logger.prn_wrn( "stopped to consume events due to %s event" % key) callbacks_consume = False result = self.RESULT_IO_SERIAL event_queue.put(('__exit_event_queue', 0, time())) elif key == '__exit': # This event is sent by DUT, test suite exited self.logger.prn_inf("%s(%s)" % (key, str(value))) callbacks__exit = True event_queue.put(('__exit_event_queue', 0, time())) elif key == '__exit_event_queue': # This event is sent by the host test indicating no more events expected self.logger.prn_inf("%s received" % (key)) callbacks__exit_event_queue = True break elif key in callbacks: # Handle callback callbacks[key](key, value, timestamp) else: self.logger.prn_err( "orphan event in main phase: {{%s;%s}}, timestamp=%f" % (key, str(value), timestamp)) except Exception: self.logger.prn_err("something went wrong in event main loop!") self.logger.prn_inf("==== Traceback start ====") for line in traceback.format_exc().splitlines(): print line self.logger.prn_inf("==== Traceback end ====") result = self.RESULT_ERROR time_duration = time() - start_time self.logger.prn_inf("test suite run finished after %.2f sec..." % time_duration) # Force conn_proxy process to return dut_event_queue.put(('__host_test_finished', True, time())) p.join() self.logger.prn_inf("CONN exited with code: %s" % str(p.exitcode)) # Callbacks... self.logger.prn_inf("No events in queue" if event_queue.empty( ) else "Some events in queue") # If host test was used we will: # 1. Consume all existing events in queue if consume=True # 2. Check result from host test and call teardown() # NOTE: with the introduction of the '__exit_event_queue' event, there # should never be left events assuming the DUT has stopped sending data # over the serial data. Leaving this for now to catch anything that slips through. if callbacks_consume: # We are consuming all remaining events if requested while not event_queue.empty(): try: (key, value, timestamp) = event_queue.get(timeout=1) except QueueEmpty: break if key == '__notify_complete': # This event is sent by Host Test, test result is in value # or if value is None, value will be retrieved from HostTest.result() method self.logger.prn_inf("%s(%s)" % (key, str(value))) result = value elif key.startswith('__'): # Consume other system level events pass elif key in callbacks: callbacks[key](key, value, timestamp) else: self.logger.prn_wrn( ">>> orphan event: {{%s;%s}}, timestamp=%f" % (key, str(value), timestamp)) self.logger.prn_inf("stopped consuming events") if result is not None: # We must compare here against None! # Here for example we've received some error code like IOERR_COPY self.logger.prn_inf( "host test result() call skipped, received: %s" % str(result)) else: if self.test_supervisor: result = self.test_supervisor.result() self.logger.prn_inf("host test result(): %s" % str(result)) if not callbacks__exit: self.logger.prn_wrn("missing __exit event from DUT") if not callbacks__exit_event_queue: self.logger.prn_wrn( "missing __exit_event_queue event from host test") #if not callbacks__exit_event_queue and not result: if not callbacks__exit_event_queue and result is None: self.logger.prn_err("missing __exit_event_queue event from " + \ "host test and no result from host test, timeout...") result = self.RESULT_TIMEOUT self.logger.prn_inf("calling blocking teardown()") if self.test_supervisor: self.test_supervisor.teardown() self.logger.prn_inf("teardown() finished") return result def execute(self): """! Test runner for host test. @details This function will start executing test and forward test result via serial port to test suite. This function is sensitive to work-flow flags such as --skip-flashing, --skip-reset etc. First function will flash device with binary, initialize serial port for communication, reset target. On serial port handshake with test case will be performed. It is when host test reads property data from serial port (sent over serial port). At the end of the procedure proper host test (defined in set properties) will be executed and test execution timeout will be measured. """ result = self.RESULT_UNDEF # hello sting with htrun version, for debug purposes self.logger.prn_inf(self.get_hello_string()) try: # Copy image to device if self.options.skip_flashing: self.logger.prn_inf("copy image onto target... SKIPPED!") else: self.logger.prn_inf("copy image onto target...") result = self.mbed.copy_image() if not result: result = self.RESULT_IOERR_COPY return self.get_test_result_int(result) # Execute test if flashing was successful or skipped test_result = self.run_test() if test_result == True: result = self.RESULT_SUCCESS elif test_result == False: result = self.RESULT_FAILURE elif test_result is None: result = self.RESULT_ERROR else: result = test_result # This will be captured by Greentea self.logger.prn_inf("{{result;%s}}" % result) return self.get_test_result_int(result) except KeyboardInterrupt: return (-3) # Keyboard interrupt
def __init__(self): super(SDKTests, self).__init__() self.logger = HtrunLogger('TEST')