From b083d39a8375a4b6fe56544f707d1fb9b2a67e78 Mon Sep 17 00:00:00 2001 From: Brian Haley Date: Thu, 24 Jan 2019 15:42:42 -0500 Subject: [PATCH] Change agents to use get_devices_with_ip() Instead of instantiating an IPDevice object just to get the list of IPs, call get_devices_with_ip() instead since that's what it's doing anyways. Trivialfix Change-Id: I5055d24a40d45f3f3b13b05249d353ea67acf4d5 --- neutron/agent/l3/dvr_edge_router.py | 6 +++--- neutron/agent/l3/ha_router.py | 6 ++---- neutron/agent/linux/dhcp.py | 6 ++---- neutron/agent/linux/interface.py | 10 ++++++---- neutron/tests/unit/agent/linux/test_dhcp.py | 13 ++++++------- neutron/tests/unit/agent/linux/test_interface.py | 15 ++++++++++----- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/neutron/agent/l3/dvr_edge_router.py b/neutron/agent/l3/dvr_edge_router.py index c94fbd3714f..744c3333fd8 100644 --- a/neutron/agent/l3/dvr_edge_router.py +++ b/neutron/agent/l3/dvr_edge_router.py @@ -277,9 +277,9 @@ class DvrEdgeRouter(dvr_local_router.DvrLocalRouter): return set() interface_name = self.get_snat_external_device_interface_name( ex_gw_port) - device = ip_lib.IPDevice( - interface_name, namespace=self.snat_namespace.name) - return set([addr['cidr'] for addr in device.addr.list()]) + return set([addr['cidr'] for addr in ip_lib.get_devices_with_ip( + self.snat_namespace.name, + name=interface_name)]) def get_router_cidrs(self, device): """Over-ride the get_router_cidrs function to return the list. diff --git a/neutron/agent/l3/ha_router.py b/neutron/agent/l3/ha_router.py index 8606e9e6c23..6a8ce531453 100644 --- a/neutron/agent/l3/ha_router.py +++ b/neutron/agent/l3/ha_router.py @@ -401,10 +401,8 @@ class HaRouter(router.RouterInfo): pm.disable(sig=str(int(signal.SIGKILL))) def update_initial_state(self, callback): - ha_device = ip_lib.IPDevice( - self.get_ha_device_name(), - self.ha_namespace) - addresses = ha_device.addr.list() + addresses = ip_lib.get_devices_with_ip(self.ha_namespace, + name=self.get_ha_device_name()) cidrs = (address['cidr'] for address in addresses) ha_cidr = self._get_primary_vip() state = 'master' if ha_cidr in cidrs else 'backup' diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 1aa9cf01c4b..5e0acb7b634 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -1066,9 +1066,6 @@ class Dnsmasq(DhcpLocalProcess): return options def _make_subnet_interface_ip_map(self): - ip_dev = ip_lib.IPDevice(self.interface_name, - namespace=self.network.namespace) - subnet_lookup = dict( (netaddr.IPNetwork(subnet.cidr), subnet.id) for subnet in self.network.subnets @@ -1076,7 +1073,8 @@ class Dnsmasq(DhcpLocalProcess): retval = {} - for addr in ip_dev.addr.list(): + for addr in ip_lib.get_devices_with_ip(self.network.namespace, + name=self.interface_name): ip_net = netaddr.IPNetwork(addr['cidr']) if ip_net in subnet_lookup: diff --git a/neutron/agent/linux/interface.py b/neutron/agent/linux/interface.py index 901d705c84a..418b6988de1 100644 --- a/neutron/agent/linux/interface.py +++ b/neutron/agent/linux/interface.py @@ -26,6 +26,7 @@ import six from neutron.agent.common import ovs_lib from neutron.agent.linux import ip_lib from neutron.common import constants as n_const +from neutron.common import utils LOG = logging.getLogger(__name__) @@ -224,10 +225,11 @@ class LinuxInterfaceDriver(object): break def get_ipv6_llas(self, device_name, namespace): - device = ip_lib.IPDevice(device_name, - namespace=namespace) - - return device.addr.list(scope='link', ip_version=6) + kwargs = {'family': utils.get_socket_address_family( + constants.IP_VERSION_6), + 'scope': 'link'} + return ip_lib.get_devices_with_ip(namespace, name=device_name, + **kwargs) def check_bridge_exists(self, bridge): if not ip_lib.device_exists(bridge): diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index b2869c6ed00..3cf0d70186d 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -2502,10 +2502,9 @@ class TestDnsmasq(TestBase): self._test_read_leases_file_leases(None, True) def test_make_subnet_interface_ip_map(self): - with mock.patch('neutron.agent.linux.ip_lib.IPDevice') as ip_dev: - ip_dev.return_value.addr.list.return_value = [ - {'cidr': '192.168.0.1/24'} - ] + with mock.patch('neutron.agent.linux.ip_lib.' + 'get_devices_with_ip') as list_mock: + list_mock.return_value = [{'cidr': '192.168.0.1/24'}] dm = self._get_dnsmasq(FakeDualNetwork()) @@ -2705,9 +2704,9 @@ class TestDnsmasq(TestBase): for key, value in config_opts.items(): self.conf.set_override(key, value) dm = self._get_dnsmasq(network_class()) - with mock.patch('neutron.agent.linux.ip_lib.IPDevice') as ipdev_mock: - list_addr = ipdev_mock.return_value.addr.list - list_addr.return_value = [{'cidr': alloc.ip_address + '/24'} + with mock.patch('neutron.agent.linux.ip_lib.' + 'get_devices_with_ip') as list_mock: + list_mock.return_value = [{'cidr': alloc.ip_address + '/24'} for alloc in FakeDhcpPort().fixed_ips] options, idx_map = dm._generate_opts_per_subnet() diff --git a/neutron/tests/unit/agent/linux/test_interface.py b/neutron/tests/unit/agent/linux/test_interface.py index 5306ac908c2..0f29074e263 100644 --- a/neutron/tests/unit/agent/linux/test_interface.py +++ b/neutron/tests/unit/agent/linux/test_interface.py @@ -21,6 +21,7 @@ from oslo_utils import excutils from neutron.agent.common import ovs_lib from neutron.agent.linux import interface from neutron.agent.linux import ip_lib +from neutron.common import utils from neutron.conf.agent import common as config from neutron.tests import base @@ -66,6 +67,9 @@ class TestBase(base.BaseTestCase): self.ip = self.ip_p.start() self.device_exists_p = mock.patch.object(ip_lib, 'device_exists') self.device_exists = self.device_exists_p.start() + self.get_devices_with_ip_p = mock.patch.object(ip_lib, + 'get_devices_with_ip') + self.get_devices_with_ip = self.get_devices_with_ip_p.start() class TestABCDriver(TestBase): @@ -352,17 +356,18 @@ class TestABCDriver(TestBase): addresses = [dict(scope='link', dynamic=False, cidr='fe80:cafe::/64')] - self.ip_dev().addr.list = mock.Mock(return_value=addresses) + self.get_devices_with_ip.return_value = addresses device_name = self.ip_dev().name bc = BaseChild(self.conf) llas = bc.get_ipv6_llas(device_name, ns) self.assertEqual(addresses, llas) - self.ip_dev.assert_has_calls( - [mock.call(device_name, namespace=ns), - mock.call().addr.list( - scope='link', ip_version=constants.IP_VERSION_6)]) + kwargs = {'family': utils.get_socket_address_family( + constants.IP_VERSION_6), + 'scope': 'link'} + self.get_devices_with_ip.assert_called_with( + ns, name=device_name, **kwargs) def test_set_mtu_logs_once(self): bc = BaseChild(self.conf)