Exemple #1
0
    def test_strings(self):
        msg = attributes._validate_string(None, None)
        self.assertEquals(msg, "'None' is not a valid string")

        msg = attributes._validate_string("OK", None)
        self.assertEquals(msg, None)

        msg = attributes._validate_string("123456789", 9)
        self.assertIsNone(msg)

        msg = attributes._validate_string("1234567890", 9)
        self.assertIsNotNone(msg)
Exemple #2
0
    def test_validate_string(self):
        msg = attributes._validate_string(None, None)
        self.assertEqual(msg, "'None' is not a valid string")

        # 0 == len(data) == max_len
        msg = attributes._validate_string("", 0)
        self.assertIsNone(msg)

        # 0 == len(data) < max_len
        msg = attributes._validate_string("", 9)
        self.assertIsNone(msg)

        # 0 < len(data) < max_len
        msg = attributes._validate_string("123456789", 10)
        self.assertIsNone(msg)

        # 0 < len(data) == max_len
        msg = attributes._validate_string("123456789", 9)
        self.assertIsNone(msg)

        # 0 < max_len < len(data)
        msg = attributes._validate_string("1234567890", 9)
        self.assertEqual(msg, "'1234567890' exceeds maximum length of 9")

        msg = attributes._validate_string("123456789", None)
        self.assertIsNone(msg)
Exemple #3
0
    def test_validate_string(self):
        msg = attributes._validate_string(None, None)
        self.assertEquals(msg, "'None' is not a valid string")

        # 0 == len(data) == max_len
        msg = attributes._validate_string("", 0)
        self.assertIsNone(msg)

        # 0 == len(data) < max_len
        msg = attributes._validate_string("", 9)
        self.assertIsNone(msg)

        # 0 < len(data) < max_len
        msg = attributes._validate_string("123456789", 10)
        self.assertIsNone(msg)

        # 0 < len(data) == max_len
        msg = attributes._validate_string("123456789", 9)
        self.assertIsNone(msg)

        # 0 < max_len < len(data)
        msg = attributes._validate_string("1234567890", 9)
        self.assertEquals(msg, "'1234567890' exceeds maximum length of 9")

        msg = attributes._validate_string("123456789", None)
        self.assertIsNone(msg)
Exemple #4
0
def _validate_service_defs(data, valid_values=None):
    """Validate the list of service definitions."""
    try:
        if not data:
            return _("No service type definition was provided. At least a "
                     "service type definition must be provided")
        f_name = _validate_service_defs.__name__
        for svc_def in data:
            try:
                # Do a copy of the original object so we can easily
                # pop out stuff from it
                svc_def_copy = svc_def.copy()
                try:
                    svc_name = svc_def_copy.pop(SERVICE_ATTR)
                    plugin_name = svc_def_copy.pop(PLUGIN_ATTR)
                except KeyError:
                    msg = (_("Required attributes missing in service "
                             "definition: %s") % svc_def)
                    LOG.error(_("%(f_name)s: %(msg)s"), {
                        'f_name': f_name,
                        'msg': msg
                    })
                    return msg
                # Validate 'service' attribute
                if svc_name not in constants.ALLOWED_SERVICES:
                    msg = (_("Service name '%s' unspecified "
                             "or invalid") % svc_name)
                    LOG.error(_("%(f_name)s: %(msg)s"), {
                        'f_name': f_name,
                        'msg': msg
                    })
                    return msg
                # Validate 'plugin' attribute
                if not plugin_name:
                    msg = (_("Plugin name not specified in "
                             "service definition %s") % svc_def)
                    LOG.error(_("%(f_name)s: %(msg)s"), {
                        'f_name': f_name,
                        'msg': msg
                    })
                    return msg
                # TODO(salvatore-orlando): This code will need to change when
                # multiple plugins for each adv service will be supported
                svc_plugin = manager.QuantumManager.get_service_plugins().get(
                    svc_name)
                if not svc_plugin:
                    msg = _("No plugin for service '%s'") % svc_name
                    LOG.error(_("%(f_name)s: %(msg)s"), {
                        'f_name': f_name,
                        'msg': msg
                    })
                    return msg
                if svc_plugin.get_plugin_name() != plugin_name:
                    msg = _("Plugin name '%s' is not correct ") % plugin_name
                    LOG.error(_("%(f_name)s: %(msg)s"), {
                        'f_name': f_name,
                        'msg': msg
                    })
                    return msg
                # Validate 'driver' attribute (just check it's a string)
                # FIXME(salvatore-orlando): This should be a list
                # Note: using get() instead of pop() as pop raises if the
                # key is not found, which might happen for the driver
                driver = svc_def_copy.get(DRIVER_ATTR)
                if driver:
                    msg = attributes._validate_string(driver, )
                    if msg:
                        return msg
                    del svc_def_copy[DRIVER_ATTR]
                # Anything left - it should be an error
                if svc_def_copy:
                    msg = (_("Unparseable attributes found in "
                             "service definition %s") % svc_def)
                    LOG.error(_("%(f_name)s: %(msg)s"), {
                        'f_name': f_name,
                        'msg': msg
                    })
                    return msg
            except TypeError:
                LOG.exception(
                    _("Exception while parsing service "
                      "definition:%s"), svc_def)
                msg = (_("Was expecting a dict for service definition, found "
                         "the following: %s") % svc_def)
                LOG.error(_("%(f_name)s: %(msg)s"), {
                    'f_name': f_name,
                    'msg': msg
                })
                return msg
    except TypeError:
        return (_("%s: provided data are not iterable") %
                _validate_service_defs.__name__)
Exemple #5
0
def _validate_service_defs(data, valid_values=None):
    """ Validate the list of service definitions. """
    try:
        if len(data) == 0:
            return _("No service type definition was provided. At least a "
                     "service type definition must be provided")
        f_name = _validate_service_defs.__name__
        for svc_def in data:
            try:
                # Do a copy of the original object so we can easily
                # pop out stuff from it
                svc_def_copy = svc_def.copy()
                try:
                    svc_name = svc_def_copy.pop(SERVICE_ATTR)
                    plugin_name = svc_def_copy.pop(PLUGIN_ATTR)
                except KeyError:
                    msg = (_("Required attributes missing in service "
                             "definition: %s") % svc_def)
                    LOG.error(_("%(f_name)s: %(msg)s"), locals())
                    return msg
                # Validate 'service' attribute
                if svc_name not in constants.ALLOWED_SERVICES:
                    msg = (_("Service name '%s' unspecified "
                             "or invalid") % svc_name)
                    LOG.error(_("%(f_name)s: %(msg)s"), locals())
                    return msg
                # Validate 'plugin' attribute
                if not plugin_name:
                    msg = (_("Plugin name not specified in "
                             "service definition %s") % svc_def)
                    LOG.error(_("%(f_name)s: %(msg)s"), locals())
                    return msg
                # TODO(salvatore-orlando): This code will need to change when
                # multiple plugins for each adv service will be supported
                svc_plugin = manager.QuantumManager.get_service_plugins().get(
                    svc_name)
                if not svc_plugin:
                    msg = _("No plugin for service '%s'") % svc_name
                    LOG.error(_("%(f_name)s: %(msg)s"), locals())
                    return msg
                if svc_plugin.get_plugin_name() != plugin_name:
                    msg = _("Plugin name '%s' is not correct ") % plugin_name
                    LOG.error(_("%(f_name)s: %(msg)s"), locals())
                    return msg
                # Validate 'driver' attribute (just check it's a string)
                # FIXME(salvatore-orlando): This should be a list
                # Note: using get() instead of pop() as pop raises if the
                # key is not found, which might happen for the driver
                driver = svc_def_copy.get(DRIVER_ATTR)
                if driver:
                    msg = attributes._validate_string(driver,)
                    if msg:
                        return msg
                    del svc_def_copy[DRIVER_ATTR]
                # Anything left - it should be an error
                if len(svc_def_copy):
                    msg = (_("Unparseable attributes found in "
                             "service definition %s") % svc_def)
                    LOG.error(_("%(f_name)s: %(msg)s"), locals())
                    return msg
            except TypeError:
                LOG.exception(_("Exception while parsing service "
                                "definition:%s"), svc_def)
                msg = (_("Was expecting a dict for service definition, found "
                         "the following: %s") % svc_def)
                LOG.error(_("%(f_name)s: %(msg)s"), locals())
                return msg
    except TypeError:
        return (_("%s: provided data are not iterable") %
                _validate_service_defs.__name__)