Simplify the logic of validate_network_port
osl.utils provides method is_valid_port to check port, we can leverage it to make code more clear. Note that the code here was incorrect in accepting 1-65535, and this change fixes it to also include 0. Change-Id: I60cb36a042fd808edca66b07d7248213debd4dff
This commit is contained in:
parent
32846e9987
commit
d3da9dec0e
@ -472,18 +472,13 @@ def validate_network_port(port, port_name="Port"):
|
|||||||
:returns: An integer port number.
|
:returns: An integer port number.
|
||||||
:raises: InvalidParameterValue, if the port is invalid.
|
:raises: InvalidParameterValue, if the port is invalid.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
port = int(port)
|
if netutils.is_valid_port(port):
|
||||||
except ValueError:
|
return int(port)
|
||||||
raise exception.InvalidParameterValue(_(
|
|
||||||
'%(port_name)s "%(port)s" is not a valid integer.') %
|
raise exception.InvalidParameterValue(_(
|
||||||
{'port_name': port_name, 'port': port})
|
'%(port_name)s "%(port)s" is not a valid port.') %
|
||||||
if port < 1 or port > 65535:
|
{'port_name': port_name, 'port': port})
|
||||||
raise exception.InvalidParameterValue(_(
|
|
||||||
'%(port_name)s "%(port)s" is out of range. Valid port '
|
|
||||||
'numbers must be between 1 and 65535.') %
|
|
||||||
{'port_name': port_name, 'port': port})
|
|
||||||
return port
|
|
||||||
|
|
||||||
|
|
||||||
def render_template(template, params, is_file=True):
|
def render_template(template, params, is_file=True):
|
||||||
|
@ -545,23 +545,23 @@ class GetUpdatedCapabilitiesTestCase(base.TestCase):
|
|||||||
self.assertIsInstance(cap_returned, str)
|
self.assertIsInstance(cap_returned, str)
|
||||||
|
|
||||||
def test_validate_network_port(self):
|
def test_validate_network_port(self):
|
||||||
port = utils.validate_network_port('1', 'message')
|
port = utils.validate_network_port('0', 'message')
|
||||||
self.assertEqual(1, port)
|
self.assertEqual(0, port)
|
||||||
port = utils.validate_network_port('65535')
|
port = utils.validate_network_port('65535')
|
||||||
self.assertEqual(65535, port)
|
self.assertEqual(65535, port)
|
||||||
|
|
||||||
def test_validate_network_port_fail(self):
|
def test_validate_network_port_fail(self):
|
||||||
self.assertRaisesRegex(exception.InvalidParameterValue,
|
self.assertRaisesRegex(exception.InvalidParameterValue,
|
||||||
'Port "65536" is out of range.',
|
'Port "65536" is not a valid port.',
|
||||||
utils.validate_network_port,
|
utils.validate_network_port,
|
||||||
'65536')
|
'65536')
|
||||||
self.assertRaisesRegex(exception.InvalidParameterValue,
|
self.assertRaisesRegex(exception.InvalidParameterValue,
|
||||||
'fake_port "-1" is out of range.',
|
'fake_port "-1" is not a valid port.',
|
||||||
utils.validate_network_port,
|
utils.validate_network_port,
|
||||||
'-1',
|
'-1',
|
||||||
'fake_port')
|
'fake_port')
|
||||||
self.assertRaisesRegex(exception.InvalidParameterValue,
|
self.assertRaisesRegex(exception.InvalidParameterValue,
|
||||||
'Port "invalid" is not a valid integer.',
|
'Port "invalid" is not a valid port.',
|
||||||
utils.validate_network_port,
|
utils.validate_network_port,
|
||||||
'invalid')
|
'invalid')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user