NSX QoS ext: RXTX factor can be decimal

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.

Cherry-picked from commit: 538a7bf3c7
Partial-Bug: #1463363

Change-Id: I983123ef7fd8f1b52b358aff3b579459fce63033
This commit is contained in:
Salvatore Orlando 2015-06-09 03:41:07 -07:00 committed by Ihar Hrachyshka
parent 5eff0019dc
commit 9f74875cf8
3 changed files with 48 additions and 1 deletions

View File

@ -566,6 +566,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'].

View 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,

View File

@ -812,6 +812,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):