def test_flash_skip_flash(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): mock_build_object = mock.MagicMock() mock_build_object.get_file = mock.MagicMock(return_value="Thisbin") mock_build.init = mock.MagicMock(return_value=mock_build_object) mock_inspect.return_value = MockArgspec(["logger"]) mock_logger_for_flasher = mock.MagicMock() mocked_logger.return_value = mock_logger_for_flasher mock_flasher_object = mock.MagicMock() mock_flasher.return_value = mock_flasher_object mock_flasher_object.flash = mock.MagicMock() mock_flasher_object.flash.return_value = 0 dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) self.assertTrue(dut.flash("try")) self.assertTrue(dut.flash("try")) self.assertTrue(dut.flash("try")) self.assertEqual(mock_flasher_object.flash.call_count, 1) self.assertTrue(dut.flash("try", forceflash=True)) self.assertEqual(mock_flasher_object.flash.call_count, 2)
def test_prepare_connection_close(self, mock_logger): ds = DutSerial() with mock.patch.object(ds, "init_cli_human") as mock_init_cli_human: with mock.patch.object(ds, "stop") as mock_stop: ds.prepare_connection_close() mock_init_cli_human.assert_called_once() mock_stop.assert_called_once()
def test_properties(self, mock_logger): ds = DutSerial() self.assertFalse(ds.ch_mode) ds.ch_mode = True self.assertTrue(ds.ch_mode) self.assertEqual(ds.ch_mode_chunk_size, 1) ds.ch_mode_chunk_size = 2 self.assertEqual(ds.ch_mode_chunk_size, 2) self.assertEqual(ds.ch_mode_ch_delay, 0.01) ds.ch_mode_ch_delay = 0.02 self.assertEqual(ds.ch_mode_ch_delay, 0.02) self.assertEqual(ds.ch_mode_start_delay, 0) ds.ch_mode_start_delay = 1 self.assertEqual(ds.ch_mode_start_delay, 1) self.assertEqual(ds.serial_baudrate, 460800) ds.serial_baudrate = 9600 self.assertEqual(ds.serial_baudrate, 9600) self.assertEqual(ds.serial_timeout, 0.01) ds.serial_timeout = 0.02 self.assertEqual(ds.serial_timeout, 0.02) self.assertFalse(ds.serial_xonxoff) ds.serial_xonxoff = True self.assertTrue(ds.serial_xonxoff) self.assertFalse(ds.serial_rtscts) ds.serial_rtscts = True self.assertTrue(ds.serial_rtscts)
def test_reset(self, mocked_time, mock_logger): ds = DutSerial() mock_port = mock.MagicMock() mock_port.safe_sendBreak = mock.MagicMock(return_value=True) mocked_time.sleep = mock.MagicMock() ds.port = mock_port self.assertIsNone(ds.reset())
def test_get_resource_id(self, mock_logger): ds = DutSerial(config={ "allocated": { "target_id": "test" }, "application": {} }) self.assertEqual(ds.get_resource_id(), "test")
def test_close_connection(self, mock_logger): ds = DutSerial() mocked_port = mock.MagicMock() mocked_port.close = mock.MagicMock() ds.port = mocked_port with mock.patch.object(ds, "stop") as mock_stop: ds.close_connection() mock_stop.assert_called_once() mocked_port.close.assert_called_once() self.assertFalse(ds.port)
def test_flash_fails(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): mock_build_object = mock.MagicMock() mock_build_object.get_file = mock.MagicMock(return_value="Thisbin") mock_build.init = mock.MagicMock(return_value=mock_build_object) mock_inspect.return_value = MockArgspec(["logger"]) mocked_logger_for_flasher = mock.MagicMock() mocked_logger.return_value = mocked_logger_for_flasher mock_flasher_object = mock.MagicMock() mock_flasher.return_value = mock_flasher_object mock_flasher_object.flash = mock.MagicMock() mock_flasher_object.flash.side_effect = [ NotImplementedError, SyntaxError, 1 ] if FlashError is not None: mock_flasher_object.flash.side_effect = [ NotImplementedError, SyntaxError, 1, FlashError("Error", 10) ] dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) with self.assertRaises(DutConnectionError): dut.flash("try") with self.assertRaises(DutConnectionError): dut.flash("try2") self.assertFalse(dut.flash("try3")) if FlashError is not None: with self.assertRaises(DutConnectionError): dut.flash("try4")
def test_peek(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) dut.port = mock.MagicMock() dut.port.peek = mock.MagicMock(return_value="test") self.assertEqual(dut.peek(), "test")
def test_readline(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) dut.port = mock.MagicMock() dut.port.readline = mock.MagicMock(side_effect=["test\n", None]) self.assertEqual(dut._readline(), "test") self.assertIsNone(dut._readline())
def test_flasher_logger_insert(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): mock_inspect.return_value = MockArgspec(["logger"]) mocked_logger_for_flasher = mock.MagicMock() mocked_logger.return_value = mocked_logger_for_flasher dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) dut.flash("this_is_not_a_binary") mock_flasher.assert_called_with(logger=mocked_logger_for_flasher) mock_flasher.reset_mock() mock_inspect.return_value = MockArgspec([]) dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) dut.flash("this_is_not_a_binary") mock_flasher.assert_called_with()
def test_flash_build_init_not_implemented(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): mock_build.init = mock.MagicMock(side_effect=[NotImplementedError]) mock_inspect.return_value = MockArgspec(["logger"]) mocked_logger_for_flasher = mock.MagicMock() mocked_logger.return_value = mocked_logger_for_flasher dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) with self.assertRaises(DutConnectionError): dut.flash("try")
def test_flash_build_get_file_fail(self, mock_build, mock_flasher, mocked_logger, mock_inspect, mock_bench_logger): mock_build_object = mock.MagicMock() mock_build_object.get_file = mock.MagicMock( side_effect=[False, "Thisbin"]) mock_build.init = mock.MagicMock(return_value=mock_build_object) mock_inspect.return_value = MockArgspec(["logger"]) mocked_logger_for_flasher = mock.MagicMock() mocked_logger.return_value = mocked_logger_for_flasher dut = DutSerial(port="test", config={ "allocated": { "target_id": "thing" }, "application": "thing" }) with self.assertRaises(DutConnectionError): dut.flash("try")
def init_generic_serial_dut(contextlist, conf, index, args): """ Initializes a local hardware dut """ port = conf['serial_port'] baudrate = (args.baudrate if args.baudrate else conf.get( "application", {}).get("baudrate", 115200)) serial_config = {} if args.serial_rtscts: serial_config["serial_rtscts"] = args.serial_rtscts elif args.serial_xonxoff: serial_config["serial_xonxoff"] = args.serial_xonxoff if args.serial_timeout: serial_config["serial_timeout"] = args.serial_timeout ch_mode_config = {} if args.serial_ch_size > 0: ch_mode_config["ch_mode"] = True ch_mode_config["ch_mode_chunk_size"] = args.serial_ch_size elif args.serial_ch_size is 0: ch_mode_config["ch_mode"] = False if args.ch_mode_ch_delay: ch_mode_config["ch_mode_ch_delay"] = args.ch_mode_ch_delay dut = DutSerial(name="D%d" % index, port=port, baudrate=baudrate, config=conf, ch_mode_config=ch_mode_config, serial_config=serial_config, params=args) dut.index = index dut.platform = conf.get("platform_name", "serial") msg = 'Use device in serial port {} as D{}' contextlist.logger.info(msg.format(port, index)) contextlist.duts.append(dut) contextlist.dutinformations.append(dut.get_info())
def test_peek(self, mock_logger): ds = DutSerial() mocked_port = mock.MagicMock() mocked_port.peek = mock.MagicMock(return_value="test") ds.port = mocked_port self.assertEqual(ds.peek(), "test") mocked_port.peek.assert_called_once() ds.port = None self.assertEqual(ds.peek(), "")
def init_hardware_dut(contextlist, conf, index, args): """ Initializes a local hardware dut """ binary = None try: binary = conf["application"]['bin'] except KeyError: pass dev = conf.get('allocated', None) if not dev: raise ResourceInitError("Allocated device not found.") port = dev['serial_port'] baudrate = (args.baudrate if args.baudrate else conf.get( "application", {}).get("baudrate", 115200)) serial_config = {} if args.serial_rtscts: serial_config["serial_rtscts"] = args.serial_rtscts elif args.serial_xonxoff: serial_config["serial_xonxoff"] = args.serial_xonxoff if args.serial_timeout: serial_config["serial_timeout"] = args.serial_timeout ch_mode_config = {} if args.serial_ch_size > 0: ch_mode_config["ch_mode"] = True ch_mode_config["ch_mode_chunk_size"] = args.serial_ch_size elif args.serial_ch_size is 0: ch_mode_config["ch_mode"] = False if args.ch_mode_ch_delay: ch_mode_config["ch_mode_ch_delay"] = args.ch_mode_ch_delay dut = DutSerial(name="D%d" % index, port=port, baudrate=baudrate, config=conf, ch_mode_config=ch_mode_config, serial_config=serial_config, params=args) dut.index = index # If something to flash, get the allocated device and flash it if binary and not args.skip_flash: if contextlist.check_flashing_need('hardware', binary, args.forceflash): if dut.flash(binary=binary, forceflash=args.forceflash): contextlist.logger.info('flash ready') else: dut.close_dut(False) dut.close_connection() dut = None raise ResourceInitError("Dut flashing failed!") dut.platform = dev["platform_name"] msg = 'Use board {} as "{}" (id: {})' contextlist.logger.info( msg.format(dev['platform_name'], dut.get_dut_name(), dev['target_id'])) contextlist.duts.append(dut) contextlist.dutinformations.append(dut.get_info())
def test_open_connection(self, mock_thread, mock_es, mock_logger): ds = DutSerial() ds.readthread = 1 ds.params = mock.MagicMock() type(ds.params).reset = mock.PropertyMock(return_value=False) with self.assertRaises(DutConnectionError): ds.open_connection() ds.readthread = None mock_es_2 = mock.MagicMock() mock_es_2.flushInput = mock.MagicMock( side_effect=[SerialException, ValueError, 1]) mock_es.return_value = mock_es_2 with self.assertRaises(DutConnectionError): ds.open_connection() with self.assertRaises(ValueError): ds.open_connection() ds.open_connection() mock_thread.assert_called_once()
def test_flash(self, mock_logger): ds = DutSerial() self.assertTrue(ds.flash())
def test_get_info(self, mock_logger): ds = DutSerial() info = ds.get_info() self.assertTrue(isinstance(info, DutInformation))
def test_writeline(self, mock_logger): ds = DutSerial() mock_port = mock.MagicMock() mock_port.write = mock.MagicMock( side_effect=[1, 1, 1, 1, SerialException]) ds.port = mock_port ds.ch_mode = False ds.writeline("data") mock_port.write.assert_called_once_with("data\n".encode()) mock_port.reset_mock() ds.ch_mode = True ds.ch_mode_chunk_size = 2 ds.writeline("data") mock_port.write.assert_has_calls([ mock.call("da".encode()), mock.call("ta".encode()), mock.call("\n".encode()) ]) with self.assertRaises(RuntimeError): ds.writeline("data")
def test_readline(self, mock_logger): ds = DutSerial() ds.input_queue.append("test1") read = ds.readline() self.assertEqual(read, "test1") self.assertIsNone(ds.readline())