From 92e1d2d746c5e8f24c2b8d13aff1a82baada59d4 Mon Sep 17 00:00:00 2001 From: Lei Yang Date: Mon, 10 Oct 2022 16:14:14 +0800 Subject: [PATCH] utils_net.get_ip_address_by_interface:fix a regression bug A regression was introduced by ddc3e37, which results in unexcepted exceptions being raised instead of the correct one during some situations. Signed-off-by: Lei Yang --- virttest/utils_net.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/virttest/utils_net.py b/virttest/utils_net.py index 305e4adf9e..9511a5223d 100644 --- a/virttest/utils_net.py +++ b/virttest/utils_net.py @@ -3462,7 +3462,7 @@ def get_ip_address_by_interface(ifname, ip_ver="ipv4", linklocal=False): :param ifname: interface name :param ip_ver: Host IP version, ipv4 or ipv6. :param linklocal: Whether ip address is local or remote - :raise NetError: When failed to fetch IP address (ioctl raised IOError.). + :raise NetError: When failed to fetch IP address. """ if ip_ver == "ipv6": ver = netifaces.AF_INET6 @@ -3470,7 +3470,14 @@ def get_ip_address_by_interface(ifname, ip_ver="ipv4", linklocal=False): else: ver = netifaces.AF_INET linklocal_prefix = "169.254" - addr = netifaces.ifaddresses(ifname).get(ver) + try: + addr = netifaces.ifaddresses(ifname).get(ver) + # FIXME: The kind of exceptions caught should be more specific + except: + # TODO: NetError is a very general usage,it will be changed to a + # more friendly way in the future + raise NetError( + "Error while retrieving IP address from interface %s." % ifname) if addr is not None: try: