]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Ignore possible suffix in iproute commands.
authorJohn Schwarz <jschwarz@redhat.com>
Tue, 8 Dec 2015 14:17:46 +0000 (16:17 +0200)
committerJohn Schwarz <jschwarz@redhat.com>
Thu, 10 Dec 2015 09:16:12 +0000 (11:16 +0200)
Closes-Bug: #1522199
Change-Id: I14815abd9345edb079e3331cbe2465ad22a0d4c3

neutron/agent/linux/ip_lib.py
neutron/agent/linux/ip_monitor.py
neutron/tests/unit/agent/linux/test_ip_lib.py

index 89bc5776f27815546f87a64adf0abb0f22ae4582..db538ecd1337d5fbd7057cf8a32ca5285b714059 100644 (file)
@@ -46,6 +46,17 @@ METRIC_PATTERN = re.compile(r"metric (\S+)")
 DEVICE_NAME_PATTERN = re.compile(r"(\d+?): (\S+?):.*")
 
 
+def remove_interface_suffix(interface):
+    """Remove a possible "<if>@<endpoint>" suffix from an interface' name.
+
+    This suffix can appear in some kernel versions, and intends on specifying,
+    for example, a veth's pair. However, this interface name is useless to us
+    as further 'ip' commands require that the suffix be removed.
+    """
+    # If '@' is not present, this will do nothing.
+    return interface.partition("@")[0]
+
+
 class AddressNotReady(exceptions.NeutronException):
     message = _("Failure waiting for address %(address)s to "
                 "become ready: %(reason)s")
@@ -556,7 +567,7 @@ class IpAddrCommand(IpDeviceCommandBase):
             if match:
                 # Found a match for a device name, but its' addresses will
                 # only appear in following lines, so we may as well continue.
-                device_name = match.group(2)
+                device_name = remove_interface_suffix(match.group(2))
                 continue
             elif not line.startswith('inet'):
                 continue
index 97bd7ffa7df5413900e00b96c541483804bdcc5c..4e36c813b8d921675630adcaefcc9375525577f5 100644 (file)
@@ -18,6 +18,7 @@ from oslo_utils import excutils
 
 from neutron._i18n import _LE
 from neutron.agent.linux import async_process
+from neutron.agent.linux import ip_lib
 
 LOG = logging.getLogger(__name__)
 
@@ -47,7 +48,7 @@ class IPMonitorEvent(object):
             route = route[1:]
 
         try:
-            interface = route[1]
+            interface = ip_lib.remove_interface_suffix(route[1])
             cidr = route[3]
         except IndexError:
             with excutils.save_and_reraise_exception():
index b7f0d1b294beb911974adb0fafa53fe0096ac412..d8584995bccedaafcf7a953a589038b3b1c6cf66 100644 (file)
@@ -125,6 +125,13 @@ ADDR_SAMPLE2 = ("""
        valid_lft forever preferred_lft forever
 """)
 
+
+ADDR_SAMPLE3 = ("""
+2: eth0@NONE: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP
+    link/ether dd:cc:aa:b9:76:ce brd ff:ff:ff:ff:ff:ff
+    inet 172.16.77.240/24 brd 172.16.77.255 scope global eth0
+""")
+
 GATEWAY_SAMPLE1 = ("""
 default via 10.35.19.254  metric 100
 10.35.16.0/22  proto kernel  scope link  src 10.35.17.97
@@ -852,6 +859,12 @@ class TestIpAddrCommand(TestIPCmdBase):
             self._assert_call([], ('show', 'tap0', 'permanent', 'scope',
                               'global'))
 
+    def test_get_devices_with_ip(self):
+        self.parent._run.return_value = ADDR_SAMPLE3
+        devices = self.addr_cmd.get_devices_with_ip('172.16.77.240/24')
+        self.assertEqual(1, len(devices))
+        self.assertEqual('eth0', devices[0]['name'])
+
 
 class TestIpRouteCommand(TestIPCmdBase):
     def setUp(self):