Reimplement is_valid_ipv4().
This patch reimplements the is_valid_ipv4() function is nova.utils. Instead of open-coding the validity check, just make use of the netaddr module, which is already used elsewhere in nova.utils. Also add a unit test for this code. Part of indigoprint bored-on-an-airplane. Change-Id: Ia89cbbd94a0ac4631d794d658b53c244237e1ca2
This commit is contained in:
parent
795d1bcfd1
commit
ed45c0b95d
@ -476,6 +476,11 @@ class GenericUtilsTestCase(test.TestCase):
|
|||||||
self.assertFalse(utils.is_valid_boolstr('maybe'))
|
self.assertFalse(utils.is_valid_boolstr('maybe'))
|
||||||
self.assertFalse(utils.is_valid_boolstr('only on tuesdays'))
|
self.assertFalse(utils.is_valid_boolstr('only on tuesdays'))
|
||||||
|
|
||||||
|
def test_is_valid_ipv4(self):
|
||||||
|
self.assertTrue(utils.is_valid_ipv4('127.0.0.1'))
|
||||||
|
self.assertFalse(utils.is_valid_ipv4('::1'))
|
||||||
|
self.assertFalse(utils.is_valid_ipv4('bacon'))
|
||||||
|
|
||||||
|
|
||||||
class MonkeyPatchTestCase(test.TestCase):
|
class MonkeyPatchTestCase(test.TestCase):
|
||||||
"""Unit test for utils.monkey_patch()."""
|
"""Unit test for utils.monkey_patch()."""
|
||||||
|
@ -881,19 +881,12 @@ def is_valid_boolstr(val):
|
|||||||
|
|
||||||
|
|
||||||
def is_valid_ipv4(address):
|
def is_valid_ipv4(address):
|
||||||
"""valid the address strictly as per format xxx.xxx.xxx.xxx.
|
"""Verify that address represents a valid IPv4 address."""
|
||||||
where xxx is a value between 0 and 255.
|
try:
|
||||||
"""
|
addr = netaddr.IPAddress(address)
|
||||||
parts = address.split(".")
|
return addr.version == 4
|
||||||
if len(parts) != 4:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
for item in parts:
|
|
||||||
try:
|
|
||||||
if not 0 <= int(item) <= 255:
|
|
||||||
return False
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def is_valid_cidr(address):
|
def is_valid_cidr(address):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user