def test_resolve_data_decodefail(self): event_object = mock.MagicMock() event_callback = mock.MagicMock() event_flag = EventFlag() if IS_PYTHON3: event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "\x00\x00\x00\x00\x00\x00\x01\xc8", event_object, flag=event_flag, callback=event_callback) else: event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, repr("\x00\x00\x00\x00\x00\x00\x01\xc8"), event_object, flag=event_flag, callback=event_callback) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "\x00\x00\x00\x00\x00\x00\x01\xc8") event_callback.assert_called_once() obj, = event_callback.call_args[0] self.assertEqual(obj.ref, event_object) self.assertEqual(obj.event_data, "\x00\x00\x00\x00\x00\x00\x01\xc8") if IS_PYTHON3: match_equal = "\x00\x00\x00\x00\x00\x00\x01\xc8" else: match_equal = repr("\x00\x00\x00\x00\x00\x00\x01\xc8") self.assertEqual(obj.match, match_equal) self.assertTrue(event_flag.isSet()) event_flag.clear()
def case(self): self.logger.info("cli_ready_trigger will help Icetea wait until application is ready " "for communication.") # following examples shows how to create triggers from received data EventMatcher(EventTypes.DUT_LINE_RECEIVED, # event id "ping", # match string or regex (see documentation) self.get_dut(1), # dut which data want to follow callback=self.ping_cb) # callback which is called when regex matches # this will trig above callback self.command(1, "echo ping")
def case(self): self.logger.info( "cli_ready_trigger will help icetea wait until application is ready for communication." ) # create triggers from received data EventMatcher(EventTypes.DUT_LINE_RECEIVED, "ping", self.get_dut(1), callback=self.ping_cb) # this will trig above callback self.command(1, "echo ping")
def test_resolve_data_no_caller(self): test_object = mock.MagicMock() callback = mock.MagicMock() event_flag = EventFlag() eventmatcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "test", caller=None, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "test") callback.assert_called_once_with(test_object, "test") self.assertTrue(event_flag.isSet())
def test_resolve_match_data(self): event_object = mock.MagicMock() callback = mock.MagicMock() event_flag = EventFlag() event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "test", event_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "test") callback.assert_called_once() obj, = callback.call_args[0] self.assertEqual(obj.ref, event_object) self.assertEqual(obj.event_data, "test") self.assertEqual(obj.match, "test") self.assertTrue(event_flag.isSet()) event_flag.clear() callback.reset_mock() # Recreate matcher because it forgets itself once it has matched once. event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "regex:test*", event_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "nothing") self.assertFalse(event_flag.isSet()) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "test1") callback.assert_called_once() obj, = callback.call_args[0] self.assertEqual(obj.ref, event_object) self.assertEqual(obj.event_data, "test1") self.assertIsInstance(obj.match, MATCH_TYPE) self.assertTrue(event_flag.isSet()) event_flag.clear() callback.reset_mock() event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "regex:test:[0-9]", event_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "test") self.assertFalse(event_flag.isSet()) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "test:1") callback.assert_called_once() obj, = callback.call_args[0] self.assertEqual(obj.ref, event_object) self.assertEqual(obj.event_data, "test:1") self.assertIsInstance(obj.match, MATCH_TYPE) self.assertTrue(event_flag.isSet())
def test_resolve_data_no_caller(self): event_object = mock.MagicMock() event_callback = mock.MagicMock() event_flag = EventFlag() event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "test", caller=None, flag=event_flag, callback=event_callback) event = Event(EventTypes.DUT_LINE_RECEIVED, event_object, "test") event_callback.assert_called_once() obj, = event_callback.call_args[0] self.assertEqual(obj.ref, event_object) self.assertEqual(obj.event_data, "test") self.assertEqual(obj.match, "test") self.assertTrue(event_flag.isSet())
def test_resolve_data_decodefail(self): test_object = mock.MagicMock() callback = mock.MagicMock() event_flag = EventFlag() if IS_PYTHON3: event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "\x00\x00\x00\x00\x00\x00\x01\xc8", test_object, flag=event_flag, callback=callback) else: event_matcher = EventMatcher( EventTypes.DUT_LINE_RECEIVED, repr("\x00\x00\x00\x00\x00\x00\x01\xc8"), test_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "\x00\x00\x00\x00\x00\x00\x01\xc8") callback.assert_called_once_with(test_object, "\x00\x00\x00\x00\x00\x00\x01\xc8") self.assertTrue(event_flag.isSet()) event_flag.clear()
def test_resolve_match_data(self): test_object = mock.MagicMock() callback = mock.MagicMock() event_flag = EventFlag() event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "test", test_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "test") callback.assert_called_once_with(test_object, "test") self.assertTrue(event_flag.isSet()) event_flag.clear() callback.reset_mock() # Recreate matcher because it forgets itself once it has matched once. event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "regex:test*", test_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "nothing") self.assertFalse(event_flag.isSet()) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "test1") callback.assert_called_once_with(test_object, "test1") self.assertTrue(event_flag.isSet()) event_flag.clear() callback.reset_mock() event_matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "regex:test:[0-9]", test_object, flag=event_flag, callback=callback) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "test") self.assertFalse(event_flag.isSet()) event = Event(EventTypes.DUT_LINE_RECEIVED, test_object, "test:1") callback.assert_called_once_with(test_object, "test:1") self.assertTrue(event_flag.isSet())
def case(self): # send command "echo hello" to 1st dut by index # "hello" matcher calls callback_function when echo coming back self.command(1, "echo hello") # Alternative example: Wait data from DUT with 10s timeout # create event matcher which is trigged when "ping" is received from DUT event = Event() EventMatcher(EventTypes.DUT_LINE_RECEIVED, # event id "ping", # match string or regex (see documentation) self.get_dut(1), # dut which data want to follow event) # simulate "ping" by sending echo command self.command(1, "echo ping") # waits until event is set - "ping" is received if not event.wait(10): # if wait timeouts raise Error raise TestStepError("ping did not arrive") self.logger.info("Pong!")
def init_wait_register(self): """ Initialize EventMatcher to wait for certain cli_ready_trigger to arrive from this Dut. :return: None """ app = self.config.get("application") if app: bef_init_cmds = app.get("cli_ready_trigger") if bef_init_cmds: self.init_done.clear() self.init_event_matcher = EventMatcher( EventTypes.DUT_LINE_RECEIVED, bef_init_cmds, self, self.init_done) self.init_wait_timeout = app.get("cli_ready_trigger_timeout", 30) return self.init_done.set() return
def sync_cli(self, dut, generator_function=None, generator_function_args=None, retries=None, command_timeout=None): """ Synchronize cli for a dut using custom function. :param dut: str or int :param generator_function: callable :param generator_function_args: list of arguments for generator_function :param retries: int, if set to 0 will skip command entirely (for unit testing purposes) :param command_timeout: int :raises: TestStepError: if synchronization fails. :raises: AttributeError: if retries is set to 0. Unit testing reasons. """ # pylint: disable=too-many-locals if dut == "*": for dut_index in self._resources.dut_indexes: self.sync_cli(dut_index, generator_function, generator_function_args, retries, command_timeout) return None dut = self._resources.get_dut(dut) generator_args = generator_function_args if generator_function_args else [] init_done_flag = Event() matcher = None cmd_timeout = command_timeout if command_timeout else None retry_count = retries if retries is not None else None generator_function = generator_function if generator_function else None app = dut.config.get("application") if not app: if cmd_timeout is None: cmd_timeout = 5 if retry_count is None: retry_count = 5 else: sync_cli = app.get("sync_cli", dict()) if cmd_timeout is None: cmd_timeout = sync_cli.get("command_timeout", 5) if retry_count is None: retry_count = sync_cli.get("retries", 5) if generator_function is None: generator_function = self.get_echo_uuid for _ in range(0, retry_count): cmd_tuple = generator_function(generator_args) command = cmd_tuple[0] retval = cmd_tuple[1] self._logger.debug("Sending %s. Expecting %s", command, retval) matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, retval, dut, init_done_flag) self.command(dut.index, command, timeout=cmd_timeout, asynchronous=True, wait=False, report_cmd_fail=False) if init_done_flag.wait(cmd_timeout): self._logger.info("Synchronization complete.") break else: self._logger.debug("Retrying...") matcher.forget() if not init_done_flag.isSet(): self._logger.error( "Command line interface synchronization failed.") dut.close_dut() dut.close_connection() matcher.forget() raise TestStepError("Synchronization for dut {} failed.".format( dut.index)) matcher.forget() return None
def setup(self): # Start an EventMatcher to follow line received events, looking for the echoed hello. self.matcher = EventMatcher(EventTypes.DUT_LINE_RECEIVED, "hello", callback=self.callback_function)