diff --git a/nova/netconf.py b/nova/netconf.py index e74080a78fb1..6d43693afc2d 100644 --- a/nova/netconf.py +++ b/nova/netconf.py @@ -18,33 +18,14 @@ import socket from oslo.config import cfg - -from nova import utils +from oslo.utils import netutils CONF = cfg.CONF -def _get_my_ip(): - """Returns the actual ip of the local machine. - - This code figures out what source address would be used if some traffic - were to be sent out to some well known address on the Internet. In this - case, a Google DNS server is used, but the specific address does not - matter much. No traffic is actually sent. - """ - try: - csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - csock.connect(('8.8.8.8', 80)) - (addr, port) = csock.getsockname() - csock.close() - return addr - except socket.error: - return utils.get_my_ipv4_address() - - netconf_opts = [ cfg.StrOpt('my_ip', - default=_get_my_ip(), + default=netutils.get_my_ipv4(), help='IP address of this host'), cfg.StrOpt('my_block_storage_ip', default='$my_ip', diff --git a/nova/tests/unit/api/openstack/fakes.py b/nova/tests/unit/api/openstack/fakes.py index e48d9da8d3c5..5592bb6ecb9e 100644 --- a/nova/tests/unit/api/openstack/fakes.py +++ b/nova/tests/unit/api/openstack/fakes.py @@ -17,6 +17,7 @@ import datetime import uuid from oslo.serialization import jsonutils +from oslo.utils import netutils from oslo.utils import timeutils import routes import six @@ -176,7 +177,7 @@ def stub_out_instance_quota(stubs, allowed, quota, resource='instances'): def stub_out_networking(stubs): def get_my_ip(): return '127.0.0.1' - stubs.Set(nova.netconf, '_get_my_ip', get_my_ip) + stubs.Set(netutils, 'get_my_ipv4', get_my_ip) def stub_out_compute_api_snapshot(stubs): diff --git a/nova/tests/unit/test_utils.py b/nova/tests/unit/test_utils.py index 8e07c985f3d4..f818786fbefd 100644 --- a/nova/tests/unit/test_utils.py +++ b/nova/tests/unit/test_utils.py @@ -36,92 +36,6 @@ from nova import utils CONF = cfg.CONF -class GetMyIP4AddressTestCase(test.NoDBTestCase): - def test_get_my_ipv4_address_with_no_ipv4(self): - response = """172.16.0.0/16 via 172.16.251.13 dev tun1 -172.16.251.1 via 172.16.251.13 dev tun1 -172.16.251.13 dev tun1 proto kernel scope link src 172.16.251.14 -172.24.0.0/16 via 172.16.251.13 dev tun1 -192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1""" - - def fake_execute(*args, **kwargs): - return response, None - - self.stubs.Set(utils, 'execute', fake_execute) - address = utils.get_my_ipv4_address() - self.assertEqual(address, '127.0.0.1') - - def test_get_my_ipv4_address_bad_process(self): - def fake_execute(*args, **kwargs): - raise processutils.ProcessExecutionError() - - self.stubs.Set(utils, 'execute', fake_execute) - address = utils.get_my_ipv4_address() - self.assertEqual(address, '127.0.0.1') - - def test_get_my_ipv4_address_with_single_interface(self): - response_route = """default via 192.168.1.1 dev wlan0 proto static -192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.137 metric 9 -""" - response_addr = """ -1: lo inet 127.0.0.1/8 scope host lo -3: wlan0 inet 192.168.1.137/24 brd 192.168.1.255 scope global wlan0 -""" - - def fake_execute(*args, **kwargs): - if 'route' in args: - return response_route, None - return response_addr, None - - self.stubs.Set(utils, 'execute', fake_execute) - address = utils.get_my_ipv4_address() - self.assertEqual(address, '192.168.1.137') - - def test_get_my_ipv4_address_with_multi_ipv4_on_single_interface(self): - response_route = """ -172.18.56.0/24 dev customer proto kernel scope link src 172.18.56.22 -169.254.0.0/16 dev customer scope link metric 1031 -default via 172.18.56.1 dev customer -""" - response_addr = ("" -"31: customer inet 172.18.56.22/24 brd 172.18.56.255 scope global" -" customer\n" -"31: customer inet 172.18.56.32/24 brd 172.18.56.255 scope global " -"secondary customer") - - def fake_execute(*args, **kwargs): - if 'route' in args: - return response_route, None - return response_addr, None - - self.stubs.Set(utils, 'execute', fake_execute) - address = utils.get_my_ipv4_address() - self.assertEqual(address, '172.18.56.22') - - def test_get_my_ipv4_address_with_multiple_interfaces(self): - response_route = """ -169.1.9.0/24 dev eth1 proto kernel scope link src 169.1.9.10 -172.17.248.0/21 dev eth0 proto kernel scope link src 172.17.255.9 -169.254.0.0/16 dev eth0 scope link metric 1002 -169.254.0.0/16 dev eth1 scope link metric 1003 -default via 172.17.248.1 dev eth0 proto static -""" - response_addr = """ -1: lo inet 127.0.0.1/8 scope host lo -2: eth0 inet 172.17.255.9/21 brd 172.17.255.255 scope global eth0 -3: eth1 inet 169.1.9.10/24 scope global eth1 -""" - - def fake_execute(*args, **kwargs): - if 'route' in args: - return response_route, None - return response_addr, None - - self.stubs.Set(utils, 'execute', fake_execute) - address = utils.get_my_ipv4_address() - self.assertEqual(address, '172.17.255.9') - - class GenericUtilsTestCase(test.NoDBTestCase): def test_parse_server_string(self): result = utils.parse_server_string('::1')