Merge "Raise TypeError when a bool property has int value"

This commit is contained in:
Jenkins 2016-10-24 05:03:34 +00:00 committed by Gerrit Code Review
commit f5291d5781
2 changed files with 16 additions and 5 deletions

View File

@ -313,15 +313,22 @@ class Property(object):
validate)]
def _get_bool(self, value):
"""Get value for boolean property.
Explicitly checking for bool, or string with lower value
"true" or "false", to avoid integer values.
"""
if value is None:
value = self.has_default() and self.default() or False
if isinstance(value, bool):
return value
normalised = value.lower()
if normalised not in ['true', 'false']:
raise ValueError(_('"%s" is not a valid boolean') % normalised)
if isinstance(value, six.string_types):
normalised = value.lower()
if normalised not in ['true', 'false']:
raise ValueError(_('"%s" is not a valid boolean') % normalised)
return normalised == 'true'
return normalised == 'true'
raise TypeError(_('"%s" is not a valid boolean') % value)
def get_value(self, value, validate=False, template=None):
"""Get value from raw value and sanitize according to data type."""

View File

@ -887,10 +887,14 @@ class PropertyTest(common.HeatTestCase):
self.assertIs(False, p.get_value('false'))
self.assertIs(False, p.get_value(False))
def test_boolean_invalid(self):
def test_boolean_invalid_string(self):
p = properties.Property({'Type': 'Boolean'})
self.assertRaises(ValueError, p.get_value, 'fish')
def test_boolean_invalid_int(self):
p = properties.Property({'Type': 'Boolean'})
self.assertRaises(TypeError, p.get_value, 5)
def test_list_string(self):
p = properties.Property({'Type': 'List'})
self.assertRaises(TypeError, p.get_value, 'foo')