Return LOCALHOST if no default interface

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
This commit is contained in:
Ben Nemec 2015-01-08 17:58:40 +00:00
parent 6e0b86164b
commit 208988b0e9
2 changed files with 11 additions and 0 deletions

View File

@ -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):

View File

@ -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)