Beispiel #1
0
    def test_get_and_set_item(self):
        con1 = AllocationContext("id1", "al_id1", {"data": "data1"})
        con2 = AllocationContext("id2", "al_id2", {"data": "data2"})
        con_list = AllocationContextList(self.nulllogger)
        con_list.append(con1)
        con_list.append(con2)

        self.assertEqual(con_list[0].resource_id, "id1")
        self.assertEqual(con_list[1].alloc_id, "al_id2")
        con_list[0] = con2
        self.assertEqual(con_list[0].resource_id, "id2")

        with self.assertRaises(IndexError):
            er = con_list[2]  # pylint: disable=unused-variable,invalid-name
        with self.assertRaises(IndexError):
            er = con_list[-1]  # pylint: disable=invalid-name
        with self.assertRaises(IndexError):
            con_list[-1] = "test"
        with self.assertRaises(IndexError):
            con_list[3] = "test"

        with self.assertRaises(TypeError):
            er = con_list["test"]  # pylint: disable=unused-variable,invalid-name
        with self.assertRaises(TypeError):
            con_list["test"] = "test"
    def test_init_console_dut(self, mock_dc, mock_logging, mock_dutdetection):
        conf = {}
        conf["subtype"] = "console"
        conf["application"] = "stuff"
        con_list = AllocationContextList(self.nulllogger)
        init_process_dut(con_list, conf, 1, mock.MagicMock())

        conf["subtype"] = "other"
        con_list = AllocationContextList(self.nulllogger)
        with self.assertRaises(ResourceInitError):
            self.assertIsNone(
                init_process_dut(con_list, conf, 1, mock.MagicMock()))
Beispiel #3
0
    def allocate(self, dut_configuration_list, args=None):
        """
        Allocates resources from available local devices.

        :param dut_configuration_list: List of ResourceRequirements objects
        :param args: Not used
        :return: AllocationContextList with allocated resources
        """
        dut_config_list = dut_configuration_list.get_dut_configuration()
        # if we need one or more local hardware duts let's search attached
        # devices using DutDetection
        if not isinstance(dut_config_list, list):
            raise AllocationError("Invalid dut configuration format!")

        if next(
            (item
             for item in dut_config_list if item.get("type") == "hardware"),
                False):
            self._available_devices = DutDetection().get_available_devices()
            if len(self._available_devices) < len(dut_config_list):
                raise AllocationError(
                    "Required amount of devices not available.")

        # Enumerate all required DUT's
        try:
            for dut_config in dut_config_list:
                if not self.can_allocate(dut_config.get_requirements()):
                    raise AllocationError("Resource type is not supported")
                self._allocate(dut_config)
        except AllocationError:
            # Locally allocated don't need to be released any way for
            # now, so just re-raise the error
            raise
        alloc_list = AllocationContextList()
        res_id = None
        for conf in dut_config_list:
            if conf.get("type") == "mbed":
                res_id = conf.get("allocated").get("target_id")
            context = AllocationContext(resource_id=res_id, alloc_data=conf)
            alloc_list.append(context)

        alloc_list.set_dut_init_function("serial", init_generic_serial_dut)
        alloc_list.set_dut_init_function("process", init_process_dut)
        alloc_list.set_dut_init_function("mbed", init_mbed_dut)

        return alloc_list
Beispiel #4
0
    def test_open_dut_connections(self):
        con_list = AllocationContextList(self.nulllogger)
        # Setup mocked duts
        dut1 = mock.MagicMock()
        dut1.start_dut_thread = mock.MagicMock()
        dut1.start_dut_thread.return_value = "ok"
        dut1.open_dut = mock.MagicMock()
        dut1.open_dut.return_value = "ok"
        dut1.close_dut = mock.MagicMock()
        dut1.close_connection = mock.MagicMock()
        dut2 = mock.MagicMock()
        dut2.start_dut_thread = mock.MagicMock()
        dut2.start_dut_thread.side_effect = [DutConnectionError]
        dut2.open_dut = mock.MagicMock()
        dut2.open_dut.return_value = "ok"
        dut2.close_dut = mock.MagicMock()
        dut2.close_connection = mock.MagicMock()

        con_list.duts = [dut1]
        con_list.open_dut_connections()
        dut1.start_dut_thread.assert_called()
        dut1.open_dut.assert_called()

        con_list.duts.append(dut2)
        with self.assertRaises(DutConnectionError):
            con_list.open_dut_connections()
        dut2.start_dut_thread.assert_called()
        dut2.close_dut.assert_called()
        dut2.close_connection.assert_called()
Beispiel #5
0
 def test_append_and_len(self):
     con1 = AllocationContext("id1", "al_id1", {"data": "data1"})
     con2 = AllocationContext("id2", "al_id2", {"data": "data2"})
     con_list = AllocationContextList(self.nulllogger)
     self.assertEqual(len(con_list), 0)
     con_list.append(con1)
     self.assertEqual(len(con_list), 1)
     con_list.append(con2)
     self.assertEqual(len(con_list), 2)
    def test_init_hw_dut(self, mock_ds, mock_logging, mock_dutdetection):
        conf = {
            "allocated": {
                "serial_port": "port",
                "baud_rate": 115200,
                "platform_name": "test",
                "target_id": "id"
            },
            "application": {
                "bin": "binary"
            }
        }
        args = mock.MagicMock()
        rtscts = mock.PropertyMock(return_value=True)
        s_xonxoff = mock.PropertyMock(return_value=True)
        s_timeout = mock.PropertyMock(return_value=True)
        s_ch_size = mock.PropertyMock(return_value=1)
        s_ch_delay = mock.PropertyMock(return_value=True)
        skip_flash = mock.PropertyMock(return_value=False)
        type(args).skip_flash = skip_flash
        type(args).serial_xonxoff = s_xonxoff
        type(args).serial_rtscts = rtscts
        type(args).serial_timeout = s_timeout
        type(args).serial_ch_size = s_ch_size
        type(args).ch_mode_ch_delay = s_ch_delay

        # Setup mocked dut
        dut1 = mock.MagicMock()
        dut1.close_dut = mock.MagicMock()
        dut1.close_connection = mock.MagicMock()
        dut1.flash = mock.MagicMock()
        dut1.flash.side_effect = [True, False]
        dut1.getInfo = mock.MagicMock(return_value="test")
        type(dut1).index = mock.PropertyMock()

        con_list = AllocationContextList(self.nulllogger)
        with mock.patch.object(con_list, "check_flashing_need") as mock_cfn:
            mock_cfn = mock.MagicMock()
            mock_cfn.return_value = True
            mock_ds.return_value = dut1
            init_hardware_dut(con_list, conf, 1, args)

            with self.assertRaises(ResourceInitError):
                init_hardware_dut(con_list, conf, 1, args)
            dut1.close_dut.assert_called()
            dut1.close_connection.assert_called()
Beispiel #7
0
    def test_check_flashing_need(self, mock_get_build, mock_isfile):
        con_list = AllocationContextList(self.nulllogger)
        mock_get_build.return_value = "test_name.bin"
        mock_isfile.return_value = True
        self.assertTrue(
            con_list.check_flashing_need("hardware", "test_build", False))
        mock_get_build.return_value = "test_name.hex"
        self.assertTrue(
            con_list.check_flashing_need("hardware", "test_build", False))
        mock_get_build.return_value = "test_name"
        self.assertFalse(
            con_list.check_flashing_need("hardware", "test_build", False))

        self.assertTrue(
            con_list.check_flashing_need("hardware", "test_build", True))

        mock_isfile.return_value = False
        with self.assertRaises(ResourceInitError):
            con_list.check_flashing_need("hardware", "test_build", False)
    def test_init_hw_dut_nondefault_baud_rate(self, mock_ds, mock_logging,
                                              mock_dutdetection):
        conf = {
            "allocated": {
                "serial_port": "port",
                "baud_rate": 115200,
                "platform_name": "test",
                "target_id": "id"
            },
            "application": {
                "bin": "binary",
                "baudrate": 9600
            }
        }
        args = mock.MagicMock()

        rtscts = mock.PropertyMock(return_value=True)
        s_xonxoff = mock.PropertyMock(return_value=True)
        s_timeout = mock.PropertyMock(return_value=True)
        s_ch_size = mock.PropertyMock(return_value=1)
        s_ch_delay = mock.PropertyMock(return_value=True)
        skip_flash = mock.PropertyMock(return_value=False)
        type(args).skip_flash = skip_flash
        type(args).baudrate = mock.PropertyMock(return_value=False)
        type(args).serial_xonxoff = s_xonxoff
        type(args).serial_rtscts = rtscts
        type(args).serial_timeout = s_timeout
        type(args).serial_ch_size = s_ch_size
        type(args).ch_mode_ch_delay = s_ch_delay

        # Setup mocked dut
        dut1 = mock.MagicMock()
        dut1.close_dut = mock.MagicMock()
        dut1.closeConnection = mock.MagicMock()
        dut1.flash = mock.MagicMock()
        dut1.flash.side_effect = [True, False]
        dut1.getInfo = mock.MagicMock(return_value="test")
        type(dut1).index = mock.PropertyMock()

        con_list = AllocationContextList(self.nulllogger)
        with mock.patch.object(con_list, "check_flashing_need") as mock_cfn:
            mock_cfn = mock.MagicMock()
            mock_cfn.return_value = True
            mock_ds.return_value = dut1
            init_hardware_dut(con_list, conf, 1, args)
            mock_ds.assert_called_once_with(baudrate=9600,
                                            ch_mode_config={
                                                'ch_mode_ch_delay': True,
                                                'ch_mode': True,
                                                'ch_mode_chunk_size': 1
                                            },
                                            config={
                                                'application': {
                                                    'bin': 'binary',
                                                    'baudrate': 9600
                                                },
                                                'allocated': {
                                                    'baud_rate': 115200,
                                                    'platform_name': 'test',
                                                    'target_id': 'id',
                                                    'serial_port': 'port'
                                                }
                                            },
                                            name='D1',
                                            port='port',
                                            serial_config={
                                                'serial_timeout': True,
                                                'serial_rtscts': True
                                            },
                                            params=args)
 def allocate(self, dut_configuration_list, args=None):
     return AllocationContextList()