From: Vijendar Komalla Date: Tue, 9 Jul 2013 14:12:43 +0000 (-0500) Subject: Add length validation to properties of type LIST and MAP. X-Git-Tag: 2014.1~378^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=fc47b7f670dfd85276eaef2ba51020bb5de5ec44;p=openstack-build%2Fheat-build.git Add length validation to properties of type LIST and MAP. Fixing bug #1194981 Change-Id: I0537f105a61bba82e85b6b67d2354b77d8096eb1 --- diff --git a/heat/engine/properties.py b/heat/engine/properties.py index e71a54da..810ddaaa 100644 --- a/heat/engine/properties.py +++ b/heat/engine/properties.py @@ -116,18 +116,21 @@ class Property(object): raise ValueError('"%s" does not match pattern "%s"' % (value, pattern)) + self._validate_min_max_length(value, STRING) + return value + + def _validate_min_max_length(self, value, value_type): if MIN_LENGTH in self.schema: min_length = int(self.schema[MIN_LENGTH]) if len(value) < min_length: - raise ValueError('Minimum string length is %d characters.' % - min_length) + raise ValueError('Minimum %s length is %d' % + (value_type, min_length)) if MAX_LENGTH in self.schema: max_length = int(self.schema[MAX_LENGTH]) if len(value) > max_length: - raise ValueError('Maximum string length is %d characters.' % - max_length) - return value + raise ValueError('Maximum %s length is %d' % + (value_type, max_length)) def _validate_map(self, value): if value is None: @@ -141,6 +144,7 @@ class Property(object): else: children = value + self._validate_min_max_length(value, MAP) return children def _validate_list(self, value): @@ -159,6 +163,7 @@ class Property(object): else: children = value + self._validate_min_max_length(value, LIST) return children def _validate_bool(self, value): diff --git a/heat/tests/test_properties.py b/heat/tests/test_properties.py index 4bdf9f46..076ee000 100644 --- a/heat/tests/test_properties.py +++ b/heat/tests/test_properties.py @@ -233,6 +233,37 @@ class PropertyTest(testtools.TestCase): p = properties.Property(schema) self.assertEqual(p.validate_data(['bar', 'foo']), ['bar', 'foo']) + def test_list_maxlength_good(self): + schema = {'Type': 'List', + 'MaxLength': '3'} + p = properties.Property(schema) + self.assertEqual(p.validate_data(['1', '2']), ['1', '2']) + + def test_list_exceeded_maxlength(self): + schema = {'Type': 'List', + 'MaxLength': '2'} + p = properties.Property(schema) + self.assertRaises(ValueError, p.validate_data, ['1', '2', '3']) + + def test_list_length_in_range(self): + schema = {'Type': 'List', + 'MinLength': '2', + 'MaxLength': '4'} + p = properties.Property(schema) + self.assertEqual(p.validate_data(['1', '2', '3']), ['1', '2', '3']) + + def test_list_minlength_good(self): + schema = {'Type': 'List', + 'MinLength': '3'} + p = properties.Property(schema) + self.assertEqual(p.validate_data(['1', '2', '3']), ['1', '2', '3']) + + def test_list_smaller_than_minlength(self): + schema = {'Type': 'List', + 'MinLength': '4'} + p = properties.Property(schema) + self.assertRaises(ValueError, p.validate_data, ['1', '2', '3']) + def test_map_string(self): p = properties.Property({'Type': 'Map'}) self.assertRaises(TypeError, p.validate_data, 'foo') @@ -241,6 +272,44 @@ class PropertyTest(testtools.TestCase): p = properties.Property({'Type': 'Map'}) self.assertRaises(TypeError, p.validate_data, ['foo']) + def test_map_maxlength_good(self): + schema = {'Type': 'Map', + 'MaxLength': '4'} + p = properties.Property(schema) + self.assertEqual( + p.validate_data({'1': 'one', '2': 'two', '3': 'three'}), + {'1': 'one', '2': 'two', '3': 'three'}) + + def test_map_exceeded_maxlength(self): + schema = {'Type': 'Map', + 'MaxLength': '2'} + p = properties.Property(schema) + self.assertRaises(ValueError, + p.validate_data, + {'1': 'one', '2': 'two', '3': 'three'}) + + def test_map_length_in_range(self): + schema = {'Type': 'Map', + 'MinLength': '2', + 'MaxLength': '4'} + p = properties.Property(schema) + self.assertEqual( + p.validate_data({'1': 'one', '2': 'two', '3': 'three'}), + {'1': 'one', '2': 'two', '3': 'three'}) + + def test_map_minlength_good(self): + schema = {'Type': 'Map', + 'MinLength': '2'} + p = properties.Property(schema) + self.assertEqual(p.validate_data({'1': 'one', '2': 'two'}), + {'1': 'one', '2': 'two'}) + + def test_map_smaller_than_minlength(self): + schema = {'Type': 'Map', + 'MinLength': '3'} + p = properties.Property(schema) + self.assertRaises(ValueError, p.validate_data, {'1': 'one'}) + def test_map_schema_good(self): map_schema = {'valid': {'Type': 'Boolean'}} p = properties.Property({'Type': 'Map', 'Schema': map_schema})