class TestConfigurationController(TestCase): def setUp(self): super(TestConfigurationController, self).setUp() self.controller = ConfigurationsController() def test_validate_create_configuration(self): body = { "configuration": { "values": {}, "name": "test", "datastore": {"type": "test_type", "version": "test_version"}, } } schema = self.controller.get_schema("create", body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_configuration_no_datastore(self): body = {"configuration": {"values": {}, "name": "test"}} schema = self.controller.get_schema("create", body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_invalid_values_param(self): body = { "configuration": { "values": "", "name": "test", "datastore": {"type": "test_type", "version": "test_version"}, } } schema = self.controller.get_schema("create", body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is not of type 'object'", error_messages) def test_validate_create_invalid_name_param(self): body = { "configuration": {"values": {}, "name": "", "datastore": {"type": "test_type", "version": "test_version"}} } schema = self.controller.get_schema("create", body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is too short", error_messages) def test_validate_edit_configuration(self): body = {"configuration": {"values": {}}} schema = self.controller.get_schema("edit", body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body))
def test_validate_long_value(self): config_val1 = MagicMock() config_val1.name = 'myisam_sort_buffer_size' config_val1.max_size = 18446744073709551615 config_val1.min_size = 4096 config_val1.data_type = 'integer' config_rules = [config_val1] ConfigurationsController._validate_configuration( {'myisam_sort_buffer_size': 18446744073709551615}, None, config_rules)
def _configurations_router(self, mapper): parameters_resource = ParametersController().create_resource() path = '/{tenant_id}/datastores/versions/{version}/parameters' mapper.connect(path, controller=parameters_resource, action='index_by_version', conditions={'method': ['GET']}) path = '/{tenant_id}/datastores/versions/{version}/parameters/{name}' mapper.connect(path, controller=parameters_resource, action='show_by_version', conditions={'method': ['GET']}) path = '/{tenant_id}/datastores/{datastore}/versions/{id}' mapper.connect(path + '/parameters', controller=parameters_resource, action='index', conditions={'method': ['GET']}) mapper.connect(path + '/parameters/{name}', controller=parameters_resource, action='show', conditions={'method': ['GET']}) configuration_resource = ConfigurationsController().create_resource() mapper.connect('/{tenant_id}/configurations', controller=configuration_resource, action='index', conditions={'method': ['GET']}) mapper.connect('/{tenant_id}/configurations', controller=configuration_resource, action='create', conditions={'method': ['POST']}) mapper.connect('/{tenant_id}/configurations/{id}', controller=configuration_resource, action='show', conditions={'method': ['GET']}) mapper.connect('/{tenant_id}/configurations/{id}/instances', controller=configuration_resource, action='instances', conditions={'method': ['GET']}) mapper.connect('/{tenant_id}/configurations/{id}', controller=configuration_resource, action='edit', conditions={'method': ['PATCH']}) mapper.connect('/{tenant_id}/configurations/{id}', controller=configuration_resource, action='update', conditions={'method': ['PUT']}) mapper.connect('/{tenant_id}/configurations/{id}', controller=configuration_resource, action='delete', conditions={'method': ['DELETE']})
def setUp(self): super(TestConfigurationController, self).setUp() self.controller = ConfigurationsController()
class TestConfigurationController(TestCase): def setUp(self): super(TestConfigurationController, self).setUp() self.controller = ConfigurationsController() def test_validate_create_configuration(self): body = { "configuration": { "values": {}, "name": "test", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_configuration_no_datastore(self): body = {"configuration": {"values": {}, "name": "test"}} schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_invalid_values_param(self): body = { "configuration": { "values": '', "name": "test", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is not of type 'object'", error_messages) def test_validate_create_invalid_name_param(self): body = { "configuration": { "values": {}, "name": "", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is too short", error_messages) def test_validate_edit_configuration(self): body = {"configuration": {"values": {}}} schema = self.controller.get_schema('edit', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def _test_validate_configuration(self, input_values, config_rules=None): if config_rules is None: config_val1 = MagicMock() config_val1.name = 'max_connections' config_val1.restart_required = 'false' config_val1.datastore_version_id = 5.5 config_val1.max = 1 config_val1.min = 0 config_val1.data_type = 'integer' config_rules = [config_val1] data_version = MagicMock() data_version.id = 42 data_version.name = 5.5 data_version.datastore_name = 'test' self.assertRaises(UnprocessableEntity, ConfigurationsController._validate_configuration, input_values, data_version, config_rules) def test_validate_configuration_with_no_rules(self): self._test_validate_configuration({'max_connections': 5}, []) def test_validate_configuration_with_invalid_param(self): self._test_validate_configuration({'test': 5}) def test_validate_configuration_with_invalid_type(self): self._test_validate_configuration({'max_connections': '1'}) def test_validate_configuration_with_invalid_max(self): self._test_validate_configuration({'max_connections': 5}) def test_validate_configuration_with_invalid_min(self): self._test_validate_configuration({'max_connections': -1}) def test_validate_long_value(self): config_val1 = MagicMock() config_val1.name = 'myisam_sort_buffer_size' config_val1.max_size = 18446744073709551615 config_val1.min_size = 4096 config_val1.data_type = 'integer' config_rules = [config_val1] ConfigurationsController._validate_configuration( {'myisam_sort_buffer_size': 18446744073709551615}, None, config_rules)
class TestConfigurationController(TestCase): def setUp(self): super(TestConfigurationController, self).setUp() self.controller = ConfigurationsController() def test_validate_create_configuration(self): body = { "configuration": { "values": {}, "name": "test", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_configuration_no_datastore(self): body = { "configuration": { "values": {}, "name": "test" } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_invalid_values_param(self): body = { "configuration": { "values": '', "name": "test", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is not of type 'object'", error_messages) def test_validate_create_invalid_name_param(self): body = { "configuration": { "values": {}, "name": "", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is too short", error_messages) def test_validate_edit_configuration(self): body = { "configuration": { "values": {} } } schema = self.controller.get_schema('edit', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body))
class TestConfigurationController(TestCase): def setUp(self): super(TestConfigurationController, self).setUp() self.controller = ConfigurationsController() def test_validate_create_configuration(self): body = { "configuration": { "values": {}, "name": "test", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_configuration_no_datastore(self): body = { "configuration": { "values": {}, "name": "test" } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def test_validate_create_invalid_values_param(self): body = { "configuration": { "values": '', "name": "test", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is not of type 'object'", error_messages) def test_validate_create_invalid_name_param(self): body = { "configuration": { "values": {}, "name": "", "datastore": { "type": "test_type", "version": "test_version" } } } schema = self.controller.get_schema('create', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertFalse(validator.is_valid(body)) errors = sorted(validator.iter_errors(body), key=lambda e: e.path) error_messages = [error.message for error in errors] self.assertIn("'' is too short", error_messages) def test_validate_edit_configuration(self): body = { "configuration": { "values": {} } } schema = self.controller.get_schema('edit', body) self.assertIsNotNone(schema) validator = jsonschema.Draft4Validator(schema) self.assertTrue(validator.is_valid(body)) def _test_validate_configuration(self, input_values, config_rules=None): if config_rules is None: config_val1 = MagicMock() config_val1.name = 'max_connections' config_val1.restart_required = 'false' config_val1.datastore_version_id = 5.5 config_val1.max = 1 config_val1.min = 0 config_val1.data_type = 'integer' config_rules = [config_val1] data_version = MagicMock() data_version.id = 42 data_version.name = 5.5 data_version.datastore_name = 'test' self.assertRaises(UnprocessableEntity, ConfigurationsController._validate_configuration, input_values, data_version, config_rules) def test_validate_configuration_with_no_rules(self): self._test_validate_configuration({'max_connections': 5}, []) def test_validate_configuration_with_invalid_param(self): self._test_validate_configuration({'test': 5}) def test_validate_configuration_with_invalid_type(self): self._test_validate_configuration({'max_connections': '1'}) def test_validate_configuration_with_invalid_max(self): self._test_validate_configuration({'max_connections': 5}) def test_validate_configuration_with_invalid_min(self): self._test_validate_configuration({'max_connections': -1}) def test_validate_long_value(self): config_val1 = MagicMock() config_val1.name = 'myisam_sort_buffer_size' config_val1.max_size = 18446744073709551615 config_val1.min_size = 4096 config_val1.data_type = 'integer' config_rules = [config_val1] ConfigurationsController._validate_configuration( {'myisam_sort_buffer_size': 18446744073709551615}, None, config_rules)
def validate_configuration(context, values, datastore_manager="mysql", instances=[], dynamic_param=False): rules = configurations.get_validation_rules(datastore_manager=datastore_manager) LOG.info(_("Validating configuration values")) result = {} for k, v in values.iteritems(): # get the validation rule dictionary, which will ensure there is a # rule for the given key name. An exception will be thrown if no # valid rule is located. rule = ConfigurationsController._get_item(k, rules["configuration-parameters"]) exception_msg = [] if rule.get("deleted_at") and not dynamic_param: output = {"parameter_name": rule.get("name"), "parameter_deleted_at": rule.get("deleted_at")} message = ( _("%(parameter_name)s parameter can no longer be " " set as of %(parameter_deleted_at)s") % output ) exception_msg.append(message) # type checking valueType = rule.get("type") if isinstance(v, basestring): v = patch_utils.convert_type(v) """ignore express type's validation when validate configuration""" if valueType != "expression" and not dynamic_param: value_type = KSC_ConfigurationsController._find_type(valueType) if isinstance(value_type, tuple): if not isinstance(v, value_type[0]) and not isinstance(v, value_type[1]): output = {"key": k, "type": str(value_type)} msg = ( _("The value provided for the configuration " "parameter %(key)s is not in type %(type)s.") % output ) exception_msg.append(msg) else: """to handle the type is float and input is integer""" if valueType == "float" and isinstance(v, int): pass elif not isinstance(v, KSC_ConfigurationsController._find_type(valueType)): output = {"key": k, "type": valueType} msg = ( _("The value provided for the configuration " "parameter %(key)s is not of type %(type)s.") % output ) exception_msg.append(msg) if valueType == "expression" and not dynamic_param: if not (isinstance(v, int) or isinstance(v, long)): msg = ( _( "The value provided for the configuration " "parameter %s is neither integer nor long integer" ) % k ) exception_msg.append(msg) # integer min/max checking if ( valueType != "expression" and (isinstance(v, int) or isinstance(v, float) or isinstance(v, long)) and not isinstance(v, bool) and not dynamic_param ): min_value = rule.get("min") second_min_value = rule.get("second_min") if not second_min_value: if valueType != "expression" and v < min_value: output = {"key": k, "min": min_value} message = ( _( "The value for the configuration parameter " "%(key)s is less than the minimum allowed: " "%(min)s" ) % output ) exception_msg.append(message) else: if valueType != "expression" and min_value < v < second_min_value: output = {"key": k, "min": min_value, "second_min": second_min_value} message = ( _( "The value for the configuration parameter " "%(key)s is greater than the minimun allowed: " "%(min)s and less than the second minimum allowed: " "%(second_min)s" ) % output ) exception_msg.append(message) try: max_value = rule.get("max") except ValueError: message = _( "Invalid or unsupported max value defined in the " "configuration-parameters configuration file. " "Expected integer." ) exception_msg.append(message) if valueType != "expression" and v > max_value: output = {"key": k, "max": max_value} message = ( _( "The value for the configuration parameter " "%(key)s is greater than the maximum " "allowed: %(max)s" ) % output ) exception_msg.append(message) if isinstance(v, basestring) and not dynamic_param: enum_itmes = rule.get("enums", None) if enum_itmes != None: valid_value = filter(lambda x: x.lower() == v.lower(), enum_itmes) if valid_value == None or len(valid_value) == 0: message = _("The string value %s is not a valid enum value. VALID ENUMS: %s" % (v, enum_itmes)) exception_msg.append(message) # step checking step = rule.get("step") if valueType != "expression" and step: min_value = rule.get("min") recommended_value = ((v - min_value) / step) * step + min_value if (v - min_value) % rule.get("step"): output = {"key": k, "step": rule.get("step"), "recommended": recommended_value} message = ( _( "The value for the configuration parameter " "%(key)s cannot be divisible by %(step)s " "%(recommended)s is recommended" ) % output ) exception_msg.append(message) """validate expression value when the configuration attached""" if valueType == "expression" and dynamic_param: try: max_value = rule.get("max") except ValueError: message = _( "Invalid or unsupported max value defined in the " "configuration-parameters configuration file. " "Expected integer." ) exception_msg.append(message) inst_msg = [] for instance in instances: value = KSC_ConfigurationsController._get_dynamic_value(context, max_value, instance) if value and float(v) > value: output = {"key": k, "max": value, "instance_name": instance.name} message = ( _( "The value for the configuration parameter " "%(key)s is greater than the maximum " "allowed: %(max)s on %(instance_name)s" ) % output ) inst_msg.append(message) try: min_value = rule.get("min") except ValueError: message = _( "Invalid or unsupported min value defined in the " "configuration-parameters configuration file. " "Expected integer." ) exception_msg.append(message) for instance in instances: value = KSC_ConfigurationsController._get_dynamic_value(context, min_value, instance) if value and float(v) < value: output = {"key": k, "min": value, "instance_name": instance.name} message = ( _( "The value for the configuration parameter " "%(key)s is less than the minimum allowed: " "%(min)s on %(instance_name)s" ) % output ) inst_msg.append(message) if inst_msg: exception_msg.append("".join(inst_msg)) else: # step checking step = rule.get("step") if step and v % step: output = {"key": k, "step": step} message = ( _("The value for the configuration parameter " "%(key)s cannot be divisible by %(step)s") % output ) exception_msg.append(message) if exception_msg: result[rule.get("name")] = "".join(exception_msg) return result