]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
NSX QoS ext: RXTX factor can be decimal
authorSalvatore Orlando <salv.orlando@gmail.com>
Tue, 9 Jun 2015 10:41:07 +0000 (03:41 -0700)
committerSalvatore Orlando <salv.orlando@gmail.com>
Fri, 19 Jun 2015 11:02:12 +0000 (04:02 -0700)
In Nova flavors it is ok to specify a decimal RXTX factor.
For this reason when applying QoS to a port Neutron should not
convert this factor to an integer value, but simply ensure
it's a valid float number and positive.

Partial-Bug: #1463363

Change-Id: I983123ef7fd8f1b52b358aff3b579459fce63033

neutron/api/v2/attributes.py
neutron/plugins/vmware/extensions/qos.py
neutron/tests/unit/api/v2/test_attributes.py

index bfc056010fc431dc781e660c96ad066316883d36..7bd61fa73aa4f74e845cd21fdae12f47acb61109 100644 (file)
@@ -518,6 +518,24 @@ def convert_to_int_if_not_none(data):
     return data
 
 
+def convert_to_positive_float_or_none(val):
+    # NOTE(salv-orlando): This conversion function is currently used by
+    # a vendor specific extension only at the moment  It is used for
+    # port's RXTX factor in neutron.plugins.vmware.extensions.qos.
+    # It is deemed however generic enough to be in this module as it
+    # might be used in future for other API attributes.
+    if val is None:
+        return
+    try:
+        val = float(val)
+        if val < 0:
+            raise ValueError()
+    except (ValueError, TypeError):
+        msg = _("'%s' must be a non negative decimal.") % val
+        raise n_exc.InvalidInput(error_message=msg)
+    return val
+
+
 def convert_kvp_str_to_list(data):
     """Convert a value of the form 'key=value' to ['key', 'value'].
 
index 30e6f8c7d023acc095774ccfa4027dfc776394e1..94272466d1b47cbd063b611a804a0e8ca9656c56 100644 (file)
@@ -145,7 +145,7 @@ EXTENDED_ATTRIBUTES_2_0 = {
                       'is_visible': False,
                       'default': 1,
                       'enforce_policy': True,
-                      'convert_to': convert_to_unsigned_int_or_none},
+                      'convert_to': attr.convert_to_positive_float_or_none},
 
         QUEUE: {'allow_post': False,
                 'allow_put': False,
index 28b8d6621b28f62f2d276f25e31efc9412098c37..512fc3022e7e861d69709beff69eb568d83b55b0 100644 (file)
@@ -799,6 +799,35 @@ class TestConvertToInt(base.BaseTestCase):
                 value, attributes.convert_none_to_empty_list(value))
 
 
+class TestConvertToFloat(base.BaseTestCase):
+    # NOTE: the routine being tested here is a plugin-specific extension
+    # module. As the plugin split proceed towards its second phase this
+    # test should either be remove, or the validation routine moved into
+    # neutron.api.v2.attributes
+
+    def test_convert_to_float_positve_value(self):
+        self.assertEqual(
+            1.111, attributes.convert_to_positive_float_or_none(1.111))
+        self.assertEqual(1, attributes.convert_to_positive_float_or_none(1))
+        self.assertEqual(0, attributes.convert_to_positive_float_or_none(0))
+
+    def test_convert_to_float_negative_value(self):
+        self.assertRaises(n_exc.InvalidInput,
+                          attributes.convert_to_positive_float_or_none,
+                          -1.11)
+
+    def test_convert_to_float_string(self):
+        self.assertEqual(4, attributes.convert_to_positive_float_or_none('4'))
+        self.assertEqual(
+            4.44, attributes.convert_to_positive_float_or_none('4.44'))
+        self.assertRaises(n_exc.InvalidInput,
+                          attributes.convert_to_positive_float_or_none,
+                          'garbage')
+
+    def test_convert_to_float_none_value(self):
+        self.assertIsNone(attributes.convert_to_positive_float_or_none(None))
+
+
 class TestConvertKvp(base.BaseTestCase):
 
     def test_convert_kvp_list_to_dict_succeeds_for_missing_values(self):