Add validation for parameter_groups

This fix adds validation to raise error when the parameter_groups
is not a list and parameters in a parameter_group is not a list.

Change-Id: I35cdca9ce07522b5dbcfcf02d2c30393a4c15d1f
Partial-Bug: #1454559
This commit is contained in:
Rabi Mishra
2015-05-15 13:25:41 +05:30
parent 5b2d0fe1e0
commit d3d4f62395
2 changed files with 79 additions and 12 deletions

View File

@@ -43,28 +43,36 @@ class ParameterGroups(object):
''' '''
LOG.debug('Validating Parameter Groups.') LOG.debug('Validating Parameter Groups.')
LOG.debug(self.parameter_names) LOG.debug(self.parameter_names)
if self.parameter_groups is not None: if self.parameter_groups:
if not isinstance(self.parameter_groups, list):
raise exception.StackValidationFailed(message=_(
'The %s should be a list.') % PARAMETER_GROUPS)
# Loop through groups and validate parameters # Loop through groups and validate parameters
grouped_parameters = [] grouped_parameters = []
for group in self.parameter_groups: for group in self.parameter_groups:
parameters = group.get(PARAMETERS) parameters = group.get(PARAMETERS)
if parameters is None: if parameters is None:
raise exception.StackValidationFailed(message=_( raise exception.StackValidationFailed(message=_(
'Parameters must be provided for ' 'The %s must be provided for '
'each Parameter Group.')) 'each parameter group.') % PARAMETERS)
if not isinstance(parameters, list):
raise exception.StackValidationFailed(message=_(
'The %s of parameter group '
'should be a list.') % PARAMETERS)
for param in parameters: for param in parameters:
# Check if param has been added to a previous group # Check if param has been added to a previous group
if param in grouped_parameters: if param in grouped_parameters:
raise exception.StackValidationFailed(message=_( raise exception.StackValidationFailed(message=_(
'The %s parameter must be assigned to one ' 'The %s parameter must be assigned to one '
'Parameter Group only.') % param) 'parameter group only.') % param)
else: else:
grouped_parameters.append(param) grouped_parameters.append(param)
# Check that grouped parameter references a valid Parameter # Check that grouped parameter references a valid Parameter
if param not in self.parameter_names: if param not in self.parameter_names:
raise exception.StackValidationFailed(message=_( raise exception.StackValidationFailed(message=_(
'The Parameter name (%s) does not reference ' 'The grouped parameter %s does not reference '
'an existing parameter.') % param) 'a valid parameter.') % param)

View File

@@ -734,6 +734,45 @@ resources:
type: OS::Nova::Server type: OS::Nova::Server
''' '''
test_template_parameter_groups_not_list = '''
heat_template_version: 2013-05-23
description: >
Hello world HOT template that just defines a single compute instance.
Contains just base features to verify base HOT support.
parameter_groups:
label: Server Group
description: A group of parameters for the server
parameters:
key_name: heat_key
label: Database Group
description: A group of parameters for the database
parameters:
public_net: public
resources:
server:
type: OS::Nova::Server
'''
test_template_parameters_not_list = '''
heat_template_version: 2013-05-23
description: >
Hello world HOT template that just defines a single compute instance.
Contains just base features to verify base HOT support.
parameter_groups:
- label: Server Group
description: A group of parameters for the server
parameters:
key_name: heat_key
public_net: public
resources:
server:
type: OS::Nova::Server
'''
test_template_allowed_integers = ''' test_template_allowed_integers = '''
heat_template_version: 2013-05-23 heat_template_version: 2013-05-23
@@ -1368,7 +1407,7 @@ class validateTest(common.HeatTestCase):
stack.validate) stack.validate)
self.assertEqual(_('The InstanceType parameter must be assigned to ' self.assertEqual(_('The InstanceType parameter must be assigned to '
'one Parameter Group only.'), six.text_type(exc)) 'one parameter group only.'), six.text_type(exc))
def test_validate_invalid_parameter_in_group(self): def test_validate_invalid_parameter_in_group(self):
t = template_format.parse(test_template_invalid_parameter_name) t = template_format.parse(test_template_invalid_parameter_name)
@@ -1382,8 +1421,8 @@ class validateTest(common.HeatTestCase):
exc = self.assertRaises(exception.StackValidationFailed, exc = self.assertRaises(exception.StackValidationFailed,
stack.validate) stack.validate)
self.assertEqual(_('The Parameter name (SomethingNotHere) does not ' self.assertEqual(_('The grouped parameter SomethingNotHere does not '
'reference an existing parameter.'), 'reference a valid parameter.'),
six.text_type(exc)) six.text_type(exc))
def test_validate_no_parameters_in_group(self): def test_validate_no_parameters_in_group(self):
@@ -1393,8 +1432,28 @@ class validateTest(common.HeatTestCase):
exc = self.assertRaises(exception.StackValidationFailed, exc = self.assertRaises(exception.StackValidationFailed,
stack.validate) stack.validate)
self.assertEqual(_('Parameters must be provided for each Parameter ' self.assertEqual(_('The parameters must be provided for each '
'Group.'), six.text_type(exc)) 'parameter group.'), six.text_type(exc))
def test_validate_parameter_groups_not_list(self):
t = template_format.parse(test_template_parameter_groups_not_list)
template = hot_tmpl.HOTemplate20130523(t)
stack = parser.Stack(self.ctx, 'test_stack', template)
exc = self.assertRaises(exception.StackValidationFailed,
stack.validate)
self.assertEqual(_('The parameter_groups should be '
'a list.'), six.text_type(exc))
def test_validate_parameters_not_list(self):
t = template_format.parse(test_template_parameters_not_list)
template = hot_tmpl.HOTemplate20130523(t)
stack = parser.Stack(self.ctx, 'test_stack', template)
exc = self.assertRaises(exception.StackValidationFailed,
stack.validate)
self.assertEqual(_('The parameters of parameter group should be '
'a list.'), six.text_type(exc))
def test_validate_allowed_values_integer(self): def test_validate_allowed_values_integer(self):
t = template_format.parse(test_template_allowed_integers) t = template_format.parse(test_template_allowed_integers)