Skip to content

Commit

Permalink
Add parameter type validation before stack-create
Browse files Browse the repository at this point in the history
If a number parameter accept a string that can't convert to number,
it will break heat functions and heat cli can't get any stack
information but a ValuerError message.
Add Number and Boolean parameter type validation before stack
create, so we can avoid above situation.

Change-Id: I25a41543027f929201a9b053d123d8e2b16e2725
Closes-Bug: #1329152
  • Loading branch information
Ethan Lynn committed Jun 12, 2014
1 parent 79bf311 commit 7bcae31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions heat/engine/parameters.py
Expand Up @@ -278,6 +278,10 @@ def __float__(self):
return float(super(NumberParam, self).value())

def _validate(self, val, context):
try:
Schema.str_to_num(val)
except ValueError as ex:
raise exception.StackValidationFailed(message=six.text_type(ex))
self.schema.validate_value(self.name, val, context)

def value(self):
Expand All @@ -288,6 +292,10 @@ class BooleanParam(Parameter):
'''A template parameter of type "Boolean".'''

def _validate(self, val, context):
try:
strutils.bool_from_string(val, strict=True)
except ValueError as ex:
raise exception.StackValidationFailed(message=six.text_type(ex))
self.schema.validate_value(self.name, val, context)

def value(self):
Expand Down
10 changes: 8 additions & 2 deletions heat/tests/test_parameters.py
Expand Up @@ -210,6 +210,12 @@ def test_number_high(self):
self.new_parameter, 'p', schema, '3')
self.assertIn('wibble', str(err))

def test_number_bad(self):
schema = {'Type': 'Number'}
err = self.assertRaises(exception.StackValidationFailed,
self.new_parameter, 'p', schema, 'str')
self.assertIn('float', str(err))

def test_number_value_list_good(self):
schema = {'Type': 'Number',
'AllowedValues': ['1', '3', '5']}
Expand Down Expand Up @@ -318,8 +324,8 @@ def test_bool_value_false(self):

def test_bool_value_invalid(self):
schema = {'Type': 'Boolean'}
bo = self.new_parameter('bo', schema, 'foo')
err = self.assertRaises(ValueError, bo.value)
err = self.assertRaises(exception.StackValidationFailed,
self.new_parameter, 'bo', schema, 'foo')
self.assertIn("Unrecognized value 'foo'", unicode(err))

def test_missing_param(self):
Expand Down

0 comments on commit 7bcae31

Please sign in to comment.