From 3f7808119cebb63de9534751c9ea03d5fef9d583 Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Mon, 13 Jun 2016 18:42:00 +0800 Subject: [PATCH] Use is_valid_ipv4 in get_ipv6_addr_by_EUI64 In netaddr module, method valid_ipv4 raise exception AddrFormatError if parameter is specified as empty string. Method is_valid_ipv4 returns False in this case. We should use it to avoid raising exception AddrFormatError to caller. Change-Id: Ic983fcb7bcb9cb957333979a6604768b921e4969 --- oslo_utils/netutils.py | 2 +- oslo_utils/tests/test_netutils.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/oslo_utils/netutils.py b/oslo_utils/netutils.py index 7e14d579..eae0d87c 100644 --- a/oslo_utils/netutils.py +++ b/oslo_utils/netutils.py @@ -151,7 +151,7 @@ def get_ipv6_addr_by_EUI64(prefix, mac): .. versionadded:: 1.4 """ # Check if the prefix is an IPv4 address - if netaddr.valid_ipv4(prefix): + if is_valid_ipv4(prefix): msg = _("Unable to generate IP address by EUI64 for IPv4 prefix") raise ValueError(msg) try: diff --git a/oslo_utils/tests/test_netutils.py b/oslo_utils/tests/test_netutils.py index 823c502d..9787d55a 100644 --- a/oslo_utils/tests/test_netutils.py +++ b/oslo_utils/tests/test_netutils.py @@ -317,6 +317,12 @@ class IPv6byEUI64TestCase(test_base.BaseTestCase): self.assertRaises(TypeError, lambda: netutils.get_ipv6_addr_by_EUI64(prefix, mac)) + def test_generate_IPv6_with_empty_prefix(self): + mac = '00:16:3e:33:44:55' + prefix = '' + self.assertRaises(ValueError, lambda: + netutils.get_ipv6_addr_by_EUI64(prefix, mac)) + @contextlib.contextmanager def mock_file_content(content):