]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add length validation to properties of type LIST and MAP.
authorVijendar Komalla <vijendar.komalla@RACKSPACE.COM>
Tue, 9 Jul 2013 14:12:43 +0000 (09:12 -0500)
committerVijendar Komalla <vijendar.komalla@RACKSPACE.COM>
Tue, 9 Jul 2013 14:13:20 +0000 (09:13 -0500)
Fixing bug #1194981

Change-Id: I0537f105a61bba82e85b6b67d2354b77d8096eb1

heat/engine/properties.py
heat/tests/test_properties.py

index e71a54da53d5e39dc525cfaa1a293441905b5eeb..810ddaaad84b084e5117fbff588adc6492a2ada5 100644 (file)
@@ -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):
index 4bdf9f46b509787973f09d31cb2d26f6a95abd75..076ee000b8674b1a32c3b29cf18c983b42e10862 100644 (file)
@@ -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})