Esempio n. 1
0
    def test_validate_dict_without_constraints(self):
        msg = attributes._validate_dict({})
        self.assertIsNone(msg)

        # Validate a dictionary without constraints.
        msg = attributes._validate_dict({'key': 'value'})
        self.assertIsNone(msg)
    def test_validate_dict_without_constraints(self):
        msg = attributes._validate_dict({})
        self.assertIsNone(msg)

        # Validate a dictionary without constraints.
        msg = attributes._validate_dict({'key': 'value'})
        self.assertIsNone(msg)
Esempio n. 3
0
def _validate_device_list(data, valid_values=None):
    """Validate the list of service definitions."""
    if not data:
        # Devices must be provided
        msg = _("Cannot create a gateway with an empty device list")
        return msg
    try:
        for device in data:
            key_specs = {DEVICE_ID_ATTR:
                         {'type:regex': attributes.UUID_PATTERN,
                          'required': True},
                         IFACE_NAME_ATTR:
                         {'type:string': None,
                          'required': False}}
            err_msg = attributes._validate_dict(
                device, key_specs=key_specs)
            if err_msg:
                return err_msg
            unexpected_keys = [key for key in device if key not in key_specs]
            if unexpected_keys:
                err_msg = (_("Unexpected keys found in device description:%s")
                           % ",".join(unexpected_keys))
                return err_msg
    except TypeError:
        return (_("%s: provided data are not iterable") %
                _validate_device_list.__name__)
Esempio n. 4
0
def _validate_device_list(data, valid_values=None):
    """Validate the list of service definitions."""
    if not data:
        # Devices must be provided
        msg = _("Cannot create a gateway with an empty device list")
        return msg
    try:
        for device in data:
            key_specs = {DEVICE_ID_ATTR:
                         {'type:regex': attributes.UUID_PATTERN,
                          'required': True},
                         IFACE_NAME_ATTR:
                         {'type:string': None,
                          'required': False}}
            err_msg = attributes._validate_dict(
                device, key_specs=key_specs)
            if err_msg:
                return err_msg
            unexpected_keys = [key for key in device if key not in key_specs]
            if unexpected_keys:
                err_msg = (_("Unexpected keys found in device description:%s")
                           % ",".join(unexpected_keys))
                return err_msg
    except TypeError:
        return (_("%s: provided data are not iterable") %
                _validate_device_list.__name__)
Esempio n. 5
0
    def test_subdictionary(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary["key3"]["k4"]
        dictionary["key3"]["k5"] = "a string value"
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIn("Expected keys:", msg)
    def test_validate_dict_not_required_keys(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary['key2']
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg, 'Field that was not required by the specs was'
                               'required by the validator.')
    def test_subdictionary(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary['key3']['k4']
        dictionary['key3']['k5'] = 'a string value'
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIn('Expected keys:', msg)
Esempio n. 8
0
    def test_validate_dict_not_required_keys(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary['key2']
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg, 'Field that was not required by the specs was'
                               'required by the validator.')
Esempio n. 9
0
    def test_subdictionary(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary['key3']['k4']
        dictionary['key3']['k5'] = 'a string value'
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIn('Expected keys:', msg)
def validate_gwdevice_list(data, valid_values=None):
    """Validate the list of devices."""

    if not data:
        # Devices must be provided
        msg = _("Cannot create a gateway with an empty device list")
        return msg

    if len(data) > 1:
        # The number of devices must be exactly one
        msg = _("Exactly one device must be specified to create a gateway")
        return msg

    try:
        for device in data:
            err_msg = attributes._validate_dict(device, None)
            if err_msg:
                return err_msg

            device_id = device.get('device_id')
            if not device_id:
                msg = _("Cannot create a gateway with an empty device_id")
                return msg

            # Don't accept any interface.  However, when supporting HW VTEPs
            # this must be supported.
            # TODO(RYU): Allow setting segmentation ID in some way
            if device.get('interfaces'):
                msg = _("Interfaces are not allowed in MidoNet L2GW")
                return msg
            device['interfaces'] = []
    except TypeError:
        return (_("%s: provided data are not iterable") %
                validate_gwdevice_list.__name__)
Esempio n. 11
0
def _validate_list_of_dict_or_none(data, key_specs=None):
    if data is not None:
        if not isinstance(data, list):
            raise ExtraDhcpOptBadData(data=data)
        for d in data:
            msg = attr._validate_dict(d, key_specs)
            if msg:
                raise ExtraDhcpOptBadData(data=msg)
Esempio n. 12
0
def _validate_list_of_dict_or_none(data, key_specs=None):
    if data is not None:
        if not isinstance(data, list):
            raise ExtraDhcpOptBadData(data=data)
        for d in data:
            msg = attr._validate_dict(d, key_specs)
            if msg:
                raise ExtraDhcpOptBadData(data=msg)
Esempio n. 13
0
    def test_validate_dict_convert_boolean(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        constraints["key_bool"] = {"type:boolean": None, "required": False, "convert_to": attributes.convert_to_boolean}
        dictionary["key_bool"] = "true"
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg)
        # Explicitly comparing with literal 'True' as assertTrue
        # succeeds also for 'true'
        self.assertIs(True, dictionary["key_bool"])
Esempio n. 14
0
def _validate_extra_dhcp_opt(data, key_specs=None):
    if data is not None:
        if not isinstance(data, list):
            raise ExtraDhcpOptBadData(data=data)
        for d in data:
            if d["opt_name"] in VALID_BLANK_EXTRA_DHCP_OPTS:
                msg = attr._validate_string_or_none(d["opt_value"], DHCP_OPT_VALUE_MAX_LEN)
            else:
                msg = attr._validate_dict(d, key_specs)
            if msg:
                raise ExtraDhcpOptBadData(data=msg)
Esempio n. 15
0
def _validate_extra_dhcp_opt(data, key_specs=None):
    if data is not None:
        if not isinstance(data, list):
            raise ExtraDhcpOptBadData(data=data)
        for d in data:
            if d['opt_name'] in VALID_BLANK_EXTRA_DHCP_OPTS:
                msg = attr._validate_string_or_none(d['opt_value'],
                                                    DHCP_OPT_VALUE_MAX_LEN)
            else:
                msg = attr._validate_dict(d, key_specs)
            if msg:
                raise ExtraDhcpOptBadData(data=msg)
def validate_gwdevice_list(data, valid_values=None):
    """Validate the list of devices."""
    if not data:
        # Devices must be provided
        msg = _("Cannot create a gateway with an empty device list")
        return msg
    try:
        for device in data:
            interface_data = device.get(constants.IFACE_NAME_ATTR)
            device_name = device.get(constants.DEVICE_ID_ATTR)
            device_ip = device.get(constants.DEVICE_IP_ATTR)
            if device_ip and not netaddr.valid_ipv4(device_ip):
                msg = _("Cannot create a gateway with an invalid device IP")
                return msg
            if not device_ip:
                msg = _("Cannot create a gateway with an empty device IP")
                return msg
            if not device_name:
                msg = _("Cannot create a gateway with an empty device_name")
                return msg
            if not interface_data:
                msg = _("Cannot create a gateway with an empty interface")
                return msg
            for int_dict in interface_data:
                err_msg = attributes._validate_dict(int_dict, None)
                if not int_dict.get('name'):
                    msg = _("Cannot create a gateway with an empty"
                            "interface name")
                    return msg
                if not constants.SEG_ID in int_dict:
                    msg = _("Cannot create a gateway with an empty "
                            "segmentation ID")
                    return msg
                else:
                    seg_id_list = int_dict.get(constants.SEG_ID)
                    if seg_id_list and type(seg_id_list) is not list:
                        msg = _("segmentation_id type should be of list type")
                        return msg
                    if len(seg_id_list) > 1:
                        msg = _("Only one segmentation_id per interface is "
                                "allowed")
                        return msg
                    if not seg_id_list:
                        msg = _("segmentation_id_list should not be empty")
                        return msg
                    for seg_id in seg_id_list:
                        is_valid_vlan_id(seg_id)
                    if err_msg:
                        return err_msg
    except TypeError:
        return (_("%s: provided data are not iterable") %
                validate_gwdevice_list.__name__)
Esempio n. 17
0
    def test_validate_dict_convert_boolean(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        constraints['key_bool'] = {
            'type:boolean': None,
            'required': False,
            'convert_to': attributes.convert_to_boolean}
        dictionary['key_bool'] = 'true'
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg)
        # Explicitly comparing with literal 'True' as assertTrue
        # succeeds also for 'true'
        self.assertIs(True, dictionary['key_bool'])
Esempio n. 18
0
    def test_validate_dict_convert_boolean(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        constraints['key_bool'] = {
            'type:boolean': None,
            'required': False,
            'convert_to': attributes.convert_to_boolean}
        dictionary['key_bool'] = 'true'
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg)
        # Explicitly comparing with literal 'True' as assertTrue
        # succeeds also for 'true'
        self.assertIs(True, dictionary['key_bool'])
Esempio n. 19
0
def _validate_service_type_list(data, valid_values=None):
    if not isinstance(data, list):
        msg = _("invalid data format for service list: '%s'") % data
        LOG.debug(msg)
        return msg
    if not data:
        msg = _("empty list is not allowed for service list. '%s'") % data
        LOG.debug(msg)
        return msg
    key_specs = {
        'service_type': {
            'type:string': None,
        }
    }
    for service in data:
        msg = attr._validate_dict(service, key_specs)
        if msg:
            LOG.debug(msg)
            return msg
Esempio n. 20
0
def _validate_service_type_list(data, valid_values=None):
    if not isinstance(data, list):
        msg = _("invalid data format for service list: '%s'") % data
        LOG.debug(msg)
        return msg
    if not data:
        msg = _("empty list is not allowed for service list. '%s'") % data
        LOG.debug(msg)
        return msg
    key_specs = {
        'service_type': {
            'type:string': None,
        }
    }
    for service in data:
        msg = attr._validate_dict(service, key_specs)
        if msg:
            LOG.debug(msg)
            return msg
Esempio n. 21
0
def validate_gwdevice_list(data, valid_values=None):
    """Validate the list of devices."""
    if not data:
        # Devices must be provided
        msg = _("Cannot create a gateway with an empty device list")
        return msg
    try:
        for device in data:
            interface_data = device.get(constants.IFACE_NAME_ATTR)
            device_name = device.get(constants.DEVICE_ID_ATTR)
            if not device_name:
                msg = _("Cannot create a gateway with an empty device_name")
                return msg
            if not interface_data:
                msg = _("Cannot create a gateway with an empty interfaces")
                return msg
            for int_dict in interface_data:
                err_msg = attributes._validate_dict(int_dict, None)
                if not int_dict.get('name'):
                    msg = _("Cannot create a gateway with an empty"
                            "interface name")
                    return msg
                if constants.SEG_ID in int_dict:
                    seg_id_list = int_dict.get(constants.SEG_ID)
                    if seg_id_list and type(seg_id_list) is not list:
                        msg = _("segmentation_id type should be of list type ")
                        return msg
                    if not seg_id_list:
                        msg = _("segmentation_id_list should not be empty")
                        return msg
                    for seg_id in seg_id_list:
                        is_valid_vlan_id(seg_id)
                    if err_msg:
                        return err_msg
    except TypeError:
        return (_("%s: provided data are not iterable") %
                validate_gwdevice_list.__name__)
Esempio n. 22
0
    def test_validate_dict_wrong_values(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        dictionary['key1'] = 'UNSUPPORTED'
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNotNone(msg)
Esempio n. 23
0
    def test_validate_dict_required_keys(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary['key1']
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIn('Expected keys:', msg)
Esempio n. 24
0
    def test_validate_dict_with_invalid_validator(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        constraints['key1'] = {'type:unsupported': None, 'required': True}
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertEqual("Validator 'type:unsupported' does not exist.", msg)
Esempio n. 25
0
    def test_validate_a_valid_dict_with_constraints(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
Esempio n. 26
0
    def test_validate_a_valid_dict_with_constraints(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
Esempio n. 27
0
 def test_validate_dict_type(self):
     for value in (None, True, '1', []):
         self.assertEqual("'%s' is not a dictionary" % value,
                          attributes._validate_dict(value))
Esempio n. 28
0
    def test_validate_dict_wrong_values(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        dictionary['key1'] = 'UNSUPPORTED'
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIsNotNone(msg)
Esempio n. 29
0
    def test_validate_dict_required_keys(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        del dictionary['key1']
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertIn('Expected keys:', msg)
Esempio n. 30
0
    def test_validate_dict_with_invalid_validator(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        constraints["key1"] = {"type:unsupported": None, "required": True}
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertEqual(msg, "Validator 'type:unsupported' does not exist.")
Esempio n. 31
0
    def test_validate_dict_with_invalid_validator(self):
        dictionary, constraints = self._construct_dict_and_constraints()

        constraints['key1'] = {'type:unsupported': None, 'required': True}
        msg = attributes._validate_dict(dictionary, constraints)
        self.assertEqual("Validator 'type:unsupported' does not exist.", msg)
Esempio n. 32
0
 def test_validate_dict_type(self):
     for value in (None, True, '1', []):
         self.assertEqual("'%s' is not a dictionary" % value,
                          attributes._validate_dict(value))