From 289952a13f6ecf428eeabaf41020ba9ca459218e Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 9 Oct 2020 13:26:33 +0000 Subject: [PATCH] "scope" conversion only just before pyroute2 method call In "IpAddrCommand.list" method, the "scope" parameter is a string ("link", "site", "global" or "host"). This method will retrieve all devices with an IP address calling "ip_lib.get_devices_with_ip". Since [1], "ip_lib.get_devices_with_ip" makes the conversion of "scope" string parameter to pyroute2 format (see "pyroute2.netlink.rtnl.rtscopes"). The list command should skip then the previous conversion. Closes-Bug: #1899141 [1]https://review.opendev.org/#/c/747406/ Change-Id: I55a0f4341b328af52ea3bd758a72f633fbe3abcb (cherry picked from commit 0705699e6072f0862cee7749de7f7633ecc9dcfd) (cherry picked from commit f0849bb0d2a9cbd19be7ee5312ba7e5edbc18900) (cherry picked from commit cd0526dce8703bce12763c5c9bfc51f6d27f16c2) (cherry picked from commit 4ef6566b6dc3d069fccf6a7b8d7a8abbadd12d35) --- neutron/agent/linux/ip_lib.py | 2 +- .../functional/agent/linux/test_ip_lib.py | 31 +++++++++++++++++++ neutron/tests/unit/agent/linux/test_ip_lib.py | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/neutron/agent/linux/ip_lib.py b/neutron/agent/linux/ip_lib.py index 0d4b47a0ce7..701d8856783 100644 --- a/neutron/agent/linux/ip_lib.py +++ b/neutron/agent/linux/ip_lib.py @@ -535,7 +535,7 @@ class IpAddrCommand(IpDeviceCommandBase): if not common_utils.is_cidr_host(cidr): kwargs['mask'] = common_utils.cidr_mask_length(cidr) if scope: - kwargs['scope'] = IP_ADDRESS_SCOPE_NAME[scope] + kwargs['scope'] = scope if ip_version: kwargs['family'] = common_utils.get_socket_address_family( ip_version) diff --git a/neutron/tests/functional/agent/linux/test_ip_lib.py b/neutron/tests/functional/agent/linux/test_ip_lib.py index b4bb5fa40ec..55034b1fe1e 100644 --- a/neutron/tests/functional/agent/linux/test_ip_lib.py +++ b/neutron/tests/functional/agent/linux/test_ip_lib.py @@ -661,3 +661,34 @@ class NamespaceTestCase(functional_base.BaseSudoTestCase): def test_network_namespace_exists_ns_doesnt_exists_try_is_ready(self): self.assertFalse(ip_lib.network_namespace_exists('another_ns', try_is_ready=True)) + + +class IpAddrCommandTestCase(functional_base.BaseSudoTestCase): + + def setUp(self): + super(IpAddrCommandTestCase, self).setUp() + self.namespace = self.useFixture(net_helpers.NamespaceFixture()).name + ip_lib.IPWrapper(self.namespace).add_dummy('test_device') + self.device = ip_lib.IPDevice('test_device', namespace=self.namespace) + self.device.link.set_up() + + def test_list_with_scope(self): + scope_ip = [ + ('global', '192.168.100.1/24'), + ('global', '2001:db8::1/64'), + ('link', '192.168.101.1/24'), + ('link', 'fe80::1:1/64'), + ('site', 'fec0:0:0:f101::1/64'), + ('host', '192.168.102.1/24')] + for scope, _ip in scope_ip: + self.device.addr.add(_ip, scope=scope) + + devices = self.device.addr.list() + devices_cidr = {device['cidr'] for device in devices} + for scope in scope_ip: + self.assertIn(scope[1], devices_cidr) + + for scope, _ip in scope_ip: + devices_filtered = self.device.addr.list(scope=scope) + devices_cidr = {device['cidr'] for device in devices_filtered} + self.assertIn(_ip, devices_cidr) diff --git a/neutron/tests/unit/agent/linux/test_ip_lib.py b/neutron/tests/unit/agent/linux/test_ip_lib.py index a7353e83d60..29a15bb2c70 100644 --- a/neutron/tests/unit/agent/linux/test_ip_lib.py +++ b/neutron/tests/unit/agent/linux/test_ip_lib.py @@ -790,7 +790,7 @@ class TestIpAddrCommand(TestIPCmdBase): self.addr_cmd.list(scope='link') mock_get_dev_ip.assert_called_once_with('test_ns', name=self.addr_cmd.name, - scope=253) + scope='link') @mock.patch.object(ip_lib, 'get_devices_with_ip') def test_list_to(self, mock_get_dev_ip):