def test_store_traces(self, mock_log): dut = Dut("test") self.assertTrue(dut.store_traces) dut.store_traces = False self.assertFalse(dut.store_traces) dut.store_traces = True self.assertTrue(dut.store_traces)
def __init__(self, type='process', name='process', config=None, params=None): Dut.__init__(self, name=name, params=params) GenericProcess.__init__(self, self.name, logger=self.logger) self.disable_io_prints() # because those are printed in Dut object self.proc = False self.type = type self.config = config if config else {} self.dutinformation = DutInformation(self.type, self.resource_id if self.resource_id else "", index=None, build=None) self.command = None
def run(self): """ Read lines while keep_reading is True. Calls process_dut for each received line. :return: Nothing """ self.keep_reading = True while self.keep_reading: line = self._readline() if line: self.input_queue.appendleft(line) Dut.process_dut(self)
def __init__(self, name='serial', port=None, baudrate=460800, config=None, ch_mode_config=None, serial_config=None, params=None): Dut.__init__(self, name=name, params=params) ch_mode_config = ch_mode_config if ch_mode_config is not None else {} serial_config = serial_config if serial_config is not None else {} self.readthread = None self.port = False self.comport = port self.type = 'serial' self.name = port self.platform = '' self.serialparams = SerialParams( timeout=serial_config.get("serial_timeout", 0.01), xonxoff=serial_config.get("serial_xonxoff", False), rtscts=serial_config.get("serial_rtscts", False), baudrate=baudrate) self.chunkmodeparams = ChunkModeParams( on=ch_mode_config.get("ch_mode", False), size=ch_mode_config.get("ch_mode_chunk_size", 1), chunk_delay=ch_mode_config.get("ch_mode_ch_delay", 0.01), start_delay=ch_mode_config.get("ch_mode_start_delay", 0)) self.input_queue = deque() # Input queue self.daemon = True # Allow Python to stop us self.keep_reading = False if config: self.config.update(config) self.device = config.get("allocated", None) init_cli_cmds = None if "init_cli_cmds" in config["application"]: init_cli_cmds = config["application"]["init_cli_cmds"] if init_cli_cmds is not None: self.set_init_cli_cmds(init_cli_cmds) post_cli_cmds = None if "post_cli_cmds" in config["application"]: post_cli_cmds = config["application"]["post_cli_cmds"] if post_cli_cmds is not None: self.set_post_cli_cmds(post_cli_cmds) tid = self.config.get('allocated', {}).get('target_id', "unknown") self.dutinformation = DutInformation("serial", tid, index=self.index, build=self.build)
def open_connection(self): """ Open connection by starting the process. :raises: DutConnectionError """ self.logger.debug("Open CLI Process '%s'", (self.comport), extra={'type': '<->'}) self.cmd = self.comport if isinstance(self.comport, list) else [self.comport] if not self.comport: raise DutConnectionError("Process not defined!") try: self.build = Build.init(self.cmd[0]) except NotImplementedError as error: self.logger.error( "Build initialization failed. Check your build location.") self.logger.debug(error) raise DutConnectionError(error) # Start process&reader thread. Call Dut.process_dut() when new data is coming app = self.config.get("application") if app and app.get("bin_args"): self.cmd = self.cmd + app.get("bin_args") try: self.start_process( self.cmd, processing_callback=lambda: Dut.process_dut(self)) except KeyboardInterrupt: raise except Exception as error: raise DutConnectionError( "Couldn't start DUT target process {}".format(error))
def test_read_reasponse(self, mock_log): dut = Dut("test") with mock.patch.object(dut, "readline") as mocked_readline: mocked_readline.side_effect = ["retcode: 0", RuntimeError, "retcode: 0", "test"] resp = dut._read_response() self.assertTrue(isinstance(resp, CliResponse)) self.assertEqual(dut._read_response(), -1) prior_len = len(dut.traces) dut.store_traces = False dut._read_response() self.assertEqual(len(dut.traces), prior_len) dut.store_traces = True resp = dut._read_response() self.assertEqual(len(dut.traces), prior_len + 1) self.assertIsNone(resp)
def test_executionready_wait_skip(self, mock_log): # pylint: disable=unused-argument dut = Dut("test_dut") with mock.patch.object(dut, "response_received") as mock_r: mock_r.wait = mock.MagicMock() mock_r.wait.return_value = False dut.query_timeout = 0 dut.response_coming_in = -1 with self.assertRaises(TestStepError): dut._wait_for_exec_ready() # pylint: disable=protected-access mock_r.wait.return_value = True dut.query_timeout = 1 with self.assertRaises(TestStepError): dut._wait_for_exec_ready() # pylint: disable=protected-access
def test_wait_for_exec_ready(self, mock_log): dut_obj = Dut("test") dut_obj.get_time = mock.MagicMock(return_value=10) dut_obj.query_timeout = 9 mocked_response = mock.MagicMock() type(dut_obj).response_received = mock.PropertyMock(return_value=mocked_response) mocked_response.wait = mock.MagicMock(return_value=False) with self.assertRaises(TestStepTimeout): dut_obj._wait_for_exec_ready()
def test_dut_run(self, mock_log): dut = Dut("test_Dut") type(dut)._logger = mock.MagicMock() dut.logger = mock.MagicMock() semaphore = mock.MagicMock() semaphore.acquire = mock.MagicMock(side_effect=[True, Exception]) type(dut)._sem = semaphore type(dut)._signalled_duts = [dut] type(dut)._run = mock.MagicMock(side_effect=[True, False]) type(dut).waiting_for_response = mock.PropertyMock(return_value=None) query = mock.MagicMock() query.cmd = mock.MagicMock() dut.query = query dut.writeline = mock.MagicMock(side_effect=[Exception]) dut.readline = mock.MagicMock(return_value=False) dut.response_received = mock.MagicMock() with self.assertRaises(Exception): dut.run()
def __init__(self, name='tcp'): Dut.__init__(self, name=name) self.port = None self.type = 'socket'
def test_initclihuman(self, mock_log): # pylint: disable=unused-argument dut = Dut("test_Dut") with mock.patch.object(dut, "execute_command") as m_com: dut.post_cli_cmds = None dut.init_cli_human() self.assertListEqual(dut.post_cli_cmds, dut.set_default_init_cli_human_cmds()) self.assertEqual(len(m_com.mock_calls), len(dut.post_cli_cmds)) m_com.reset_mock() dut.post_cli_cmds = ["com1", "com2"] dut.init_cli_human() self.assertListEqual(dut.post_cli_cmds, ["com1", "com2"]) self.assertEqual(len(m_com.mock_calls), 2) m_com.reset_mock() dut.post_cli_cmds = [["com1", True, False]] dut.init_cli_human() self.assertListEqual(dut.post_cli_cmds, [["com1", True, False]]) self.assertEqual(len(m_com.mock_calls), 1) m_com.assert_called_once_with("com1", wait=False, asynchronous=True)