Beispiel #1
0
 def test_dut_counts_invalid_fields(self):
     self.resconfig._dut_requirements = [
         ResourceRequirements({}),
         ResourceRequirements({"type": "process"}),
         ResourceRequirements({"type": "hardware"})
     ]
     self.assertRaises(ValueError, self.resconfig._resolve_dut_count)
Beispiel #2
0
    def test_dut_counts(self):
        self.resconfig._dut_requirements = [
            ResourceRequirements({"type": "process"}),
            ResourceRequirements({"type": "hardware"})
        ]
        self.resconfig._resolve_dut_count()
        self.assertEqual(self.resconfig.count_duts(), 2)
        self.assertEqual(self.resconfig.count_hardware(), 1)
        self.assertEqual(self.resconfig.count_process(), 1)

        self.resconfig._dut_requirements = [
            ResourceRequirements({"type": "process"}),
            ResourceRequirements({"type": "process"}),
            ResourceRequirements({"type": "hardware"})
        ]
        self.resconfig._resolve_dut_count()
        self.assertEqual(self.resconfig.count_duts(), 3)
        self.assertEqual(self.resconfig.count_hardware(), 1)
        self.assertEqual(self.resconfig.count_process(), 2)

        self.resconfig._dut_requirements = []
        self.resconfig._resolve_dut_count()
        self.assertEqual(self.resconfig.count_duts(), 0)
        self.assertEqual(self.resconfig.count_hardware(), 0)
        self.assertEqual(self.resconfig.count_process(), 0)
 def test_set_tags(self):
     dutreq = ResourceRequirements(self.simple_testreqs)
     dutreq._set_tag(tag="test", value=False)
     dutreq._set_tag(tag="test2", value=True)
     self.assertDictEqual(dutreq._requirements["tags"], {
         "test": False,
         "test2": True
     })
Beispiel #4
0
    def test_set(self):
        dutreq = ResourceRequirements(self.simple_testreqs)
        dutreq.set("test_key", "test_val")
        self.assertEqual(dutreq._requirements["test_key"], "test_val")
        # Test override
        dutreq.set("test_key", "test_val2")
        self.assertEqual(dutreq._requirements["test_key"], "test_val2")

        # test tags merging. Also a test for set_tag(tags=stuff)
        dutreq.set("tags", {"test": False, "test2": True})
        self.assertEqual(dutreq._requirements["tags"], {"test": False, "test2": True})
        dutreq.set("tags", {"test2": False})
        self.assertEqual(dutreq._requirements["tags"], {"test": False, "test2": False})
    def test_alloc_twice_suc_when_two_dev_available(self, mock_logging,
                                                    mock_dutdetection):
        # Test with two devices, both are allocatable, but the serial port for first is unusable
        dutdetect = mock.Mock()  # DutDetection instance mock
        mock_dutdetection.return_value = dutdetect
        mock_dutdetection.is_port_usable = mock.MagicMock(return_value=True)

        devices = [{
            "state": "unknown",
            "platform_name": "K64F",
            "target_id": "1234",
            "serial_port": "/dev/serial1"
        }, {
            "state": "unknown",
            "platform_name": "K64F",
            "target_id": "5678",
            "serial_port": "/dev/serial2"
        }]
        dutdetect.get_available_devices = mock.MagicMock(return_value=devices)

        alloc = LocalAllocator()
        duts = [
            ResourceRequirements({
                "type": "hardware",
                "allowed_platforms": ["K64F"]
            }),
            ResourceRequirements({
                "type": "hardware",
                "allowed_platforms": ["K64F"]
            })
        ]
        mfunc = mock.MagicMock()
        mfunc.get_dut_configuration = mock.MagicMock()
        mfunc.get_dut_configuration.return_value = duts
        self.assertEqual(len(alloc.allocate(mfunc)), 2)

        # Result of first dut allocation
        resultingdut1 = duts[0]
        resultingdevice1 = devices[0]
        resultingdevice1["state"] = "allocated"
        resultingdut1.set("allocated", resultingdevice1)
        # Result of second dut allocation
        resultingdut2 = duts[1]
        resultingdevice2 = devices[1]
        resultingdevice2["state"] = "allocated"
        resultingdut2.set("allocated", resultingdevice2)
        mock_dutdetection.is_port_usable.assert_has_calls([
            mock.call(resultingdevice1["serial_port"]),
            mock.call(resultingdevice2["serial_port"])
        ])
    def test_alloc_fail_w_two_hw_allocatable_dev_both_already_allocated(
            self, mock_logging, mock_dutdetection):
        # Allocation should raise AllocationError if no unallocated devices
        dutdetect = mock.Mock()  # DutDetection instance mock
        mock_dutdetection.return_value = dutdetect
        mfunc = mock.MagicMock()
        mfunc.get_dut_configuration = mock.MagicMock()
        mfunc.get_dut_configuration.return_value = [
            ResourceRequirements({"type": "hardware"})
        ]
        devices = [{
            "state": "allocated",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial"
        }, {
            "state": "allocated",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial"
        }]
        dutdetect.get_available_devices = mock.MagicMock(return_value=devices)

        alloc = LocalAllocator()
        self.assertRaises(AllocationError, alloc.allocate, mfunc)
    def test_inter_alloc_suc_w_two_hw_allocatable_dev_one_has_unusable_serial(
            self, mock_logging, mock_dutdetection):
        # Test with two devices, both are allocatable, but the serial port for first is unusable
        dutdetect = mock.Mock()  # DutDetection instance mock
        mock_dutdetection.return_value = dutdetect
        mock_dutdetection.is_port_usable = mock.MagicMock(
            side_effect=iter([False, True]))

        devices = [{
            "state": "unknown",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial"
        }, {
            "state": "unknown",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial"
        }]
        dutdetect.get_available_devices = mock.MagicMock(return_value=devices)

        alloc = LocalAllocator()
        dut = ResourceRequirements({"type": "hardware"})
        mfunc = mock.MagicMock()
        mfunc.get_dut_configuration = mock.MagicMock()
        mfunc.get_dut_configuration.return_value = [dut]
        self.assertEqual(len(alloc.allocate(mfunc)), 1)
        self.assertEqual(mock_dutdetection.is_port_usable.call_count, 2)
    def test_inter_alloc_suc_one_hardware_device_with_undef_allowed_platf(
            self, mock_logging, mock_dutdetection):
        # Allocation should succeed if no allowed_platform defined in dut configuration,
        # and devices are available
        dutdetect = mock.Mock()  # DutDetection instance mock
        mock_dutdetection.return_value = dutdetect
        mock_dutdetection.is_port_usable = mock.MagicMock(return_value=True)

        device = {
            "state": "unknown",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial"
        }
        dutdetect.get_available_devices = mock.MagicMock(return_value=[device])

        alloc = LocalAllocator()
        dut = ResourceRequirements({"type": "hardware"})
        mfunc = mock.MagicMock()
        mfunc.get_dut_configuration = mock.MagicMock()
        mfunc.get_dut_configuration.return_value = [dut]
        self.assertEqual(len(alloc.allocate(mfunc)), 1)

        dutdetect.get_available_devices.assert_called_once_with()

        # Test correct format of resulting dut configuration
        mock_dutdetection.is_port_usable.assert_called_once_with(
            device["serial_port"])
 def test_internal_allocate_non_hardware_types_success_without_dutfactory(
         self, mock_logging, mock_dutdetection):
     alloc = LocalAllocator()
     dut = ResourceRequirements({"type": "process"})
     mfunc = mock.MagicMock()
     mfunc.get_dut_configuration = mock.MagicMock()
     mfunc.get_dut_configuration.return_value = [dut]
     self.assertTrue(alloc.allocate(mfunc))
Beispiel #10
0
 def test_empty_tags(self):
     dutreq = ResourceRequirements(self.simple_testreqs)
     dutreq._set_tag("test", value=None)
     dutreq.remove_empty_tags()
     self.assertEqual(dutreq._requirements["tags"], {})
     self.assertEqual(dutreq.remove_empty_tags(tags={"test1": True, "test2": None}),
                      {"test1": True})
Beispiel #11
0
    def test_get(self):
        dutreq = ResourceRequirements(self.simple_testreqs)
        self.assertEqual(dutreq.get("type"), "process")

        dutreq = ResourceRequirements(self.recursion_testreqs)
        self.assertEqual(dutreq.get("application.bin"), "test_binary")
        self.assertIsNone(dutreq.get("application.bin.not_exist"))
Beispiel #12
0
    def set_dut_configuration(self, ident, config):
        """
        Set requirements for dut ident.

        :param ident: Identity of dut.
        :param config: If ResourceRequirements object, add object as requirements for resource
        ident. If dictionary, create new ResourceRequirements object from dictionary.
        :return: Nothing
        """
        if hasattr(config, "get_requirements"):
            self._dut_requirements[ident] = config
        elif isinstance(config, dict):
            self._dut_requirements[ident] = ResourceRequirements(config)
    def test_internal_allocate_non_hardware_types_success(
            self, mock_logging, mock_dutdetection):
        dutdetect = mock.Mock()
        mock_dutdetection.return_value = dutdetect
        dutdetect.get_available_devices = mock.MagicMock(return_value=None)

        alloc = LocalAllocator()
        dut = ResourceRequirements({"type": "process"})
        mfunc = mock.MagicMock()
        mfunc.get_dut_configuration = mock.MagicMock()
        mfunc.get_dut_configuration.return_value = [dut]
        self.assertTrue(alloc.allocate(mfunc))

        mock_dutdetection.assert_not_called()
        dutdetect.get_available_devices.assert_not_called()
    def test_inter_alloc_suc_w_two_hw_allocatabl_dev_w_match_platf_one_alloc(
            self, mock_logging, mock_dutdetection):
        # Test with two devices, both are allocatable, but the serial port for first is unusable
        dutdetect = mock.Mock()  # DutDetection instance mock
        mock_dutdetection.return_value = dutdetect
        mock_dutdetection.is_port_usable = mock.MagicMock(return_value=True)

        dut = ResourceRequirements({
            "type": "hardware",
            "allowed_platforms": ["K64F"]
        })
        devices = [{
            "state": "allocated",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial1"
        }, {
            "state": "unknown",
            "platform_name": "K64F",
            "target_id": "ABCDEFG12345",
            "serial_port": "/dev/serial2"
        }]
        dutdetect.get_available_devices = mock.MagicMock(return_value=devices)

        alloc = LocalAllocator()
        # Test correct format of resulting dut configuration
        mfunc = mock.MagicMock()
        mfunc.get_dut_configuration = mock.MagicMock()
        mfunc.get_dut_configuration.return_value = [dut]
        self.assertEqual(len(alloc.allocate(mfunc)), 1)

        resultingdut = dut
        resultingdevice = devices[1]
        resultingdevice["state"] = "allocated"
        resultingdut.set("allocated", resultingdevice)

        mock_dutdetection.is_port_usable.assert_called_once_with(
            resultingdevice["serial_port"])
Beispiel #15
0
    def __generate_indexed_requirements(dut_count, basekeys, requirements):
        """
        Generate indexed requirements from general requirements.

        :param dut_count: Amount of duts
        :param basekeys: base keys as dict
        :param requirements: requirements
        :return: Indexed requirements as dict.
        """
        dut_requirements = []
        for i in range(1, dut_count + 1):
            dut_requirement = ResourceRequirements(basekeys.copy())
            if i in requirements["duts"]:
                for k in requirements["duts"][i]:
                    dut_requirement.set(k, requirements["duts"][i][k])
            elif str(i) in requirements["duts"]:
                i = str(i)
                for k in requirements["duts"][i]:
                    dut_requirement.set(k, requirements["duts"][i][k])
            dut_requirements.append(dut_requirement)
        return dut_requirements