summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Nemec <bnemec@redhat.com>2015-01-08 17:58:40 +0000
committerBen Nemec <bnemec@redhat.com>2015-01-08 18:04:35 +0000
commit208988b0e935836c76042d6f42829ec23135049b (patch)
tree5f337a7e9079acdbe1cfe6234a914b141bd6ebc0
parent6e0b86164b955a7930cd39dd3c12574f25f1a14e (diff)
downloadoslo-utils-1.2.1.tar.gz
Return LOCALHOST if no default interface1.2.1
If no default interface is available in netutils._get_my_ipv4_address(), return LOCALHOST immediately. Without this change, an UnboundLocalError exception will be raised instead. Co-Authored-By: Ruby Loo <rloo@yahoo-inc.com> Change-Id: I098450c32ef8ad467298c845dd0122da3e3cda3e Closes-Bug: #1405217 Closes-Bug: #1408701
-rw-r--r--oslo_utils/netutils.py2
-rw-r--r--oslo_utils/tests/test_netutils.py9
2 files changed, 11 insertions, 0 deletions
diff --git a/oslo_utils/netutils.py b/oslo_utils/netutils.py
index 7849ca6..41587f2 100644
--- a/oslo_utils/netutils.py
+++ b/oslo_utils/netutils.py
@@ -152,6 +152,8 @@ def _get_my_ipv4_address():
except (KeyError, IndexError):
LOG.info(_LI('Could not determine default network interface, '
'using 127.0.0.1 for IPv4 address'))
+ return LOCALHOST
+
try:
return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
except (KeyError, IndexError):
diff --git a/oslo_utils/tests/test_netutils.py b/oslo_utils/tests/test_netutils.py
index 790104f..7e1b545 100644
--- a/oslo_utils/tests/test_netutils.py
+++ b/oslo_utils/tests/test_netutils.py
@@ -222,3 +222,12 @@ class NetworkUtilsTest(test_base.BaseTestCase):
ifaddr.return_value = {}
addr = netutils._get_my_ipv4_address()
self.assertEqual('127.0.0.1', addr)
+
+ @mock.patch('netifaces.gateways')
+ @mock.patch('netifaces.ifaddresses')
+ def test_get_my_ipv4_address_without_default_interface(
+ self, ifaddr, gateways):
+ gateways.return_value = {}
+ addr = netutils._get_my_ipv4_address()
+ self.assertEqual('127.0.0.1', addr)
+ self.assertFalse(ifaddr.called)