]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix ip_lib get_gateway for default gateway on an iface
authorsridhargaddam <sridhar.gaddam@enovance.com>
Mon, 22 Jun 2015 14:13:56 +0000 (14:13 +0000)
committersridhargaddam <sridhar.gaddam@enovance.com>
Fri, 26 Jun 2015 12:35:35 +0000 (12:35 +0000)
Currently get_gateway() of IpRouteCommand class always assumes
the presence of gateway_ip when default_route is seen.
Since we can also have interface routes without the gw_ip,
this patch fixes the issue by parsing the output accordingly.

Closes-Bug: #1467531
Change-Id: Icf988994b61cbdeb1261c5a0887f29ced41ada07

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

index 084a67d4171033a9dffac6b1a2697f9e7b63127b..0de9a21b6d99983635dfd75afa0e7d414426775b 100644 (file)
@@ -911,7 +911,7 @@ class DeviceManager(object):
         device = ip_lib.IPDevice(device_name, namespace=network.namespace)
         gateway = device.route.get_gateway()
         if gateway:
-            gateway = gateway['gateway']
+            gateway = gateway.get('gateway')
 
         for subnet in network.subnets:
             skip_subnet = (
index 6e317947af4a0caa4c8f90ab4892736d6e7a2668..dbb6e60a488912d11a735e1e018fb58d0086aa9d 100644 (file)
@@ -19,6 +19,7 @@ import os
 from oslo_config import cfg
 from oslo_log import log as logging
 from oslo_utils import excutils
+import re
 
 from neutron.agent.common import utils
 from neutron.common import exceptions
@@ -36,6 +37,8 @@ OPTS = [
 LOOPBACK_DEVNAME = 'lo'
 
 SYS_NET_PATH = '/sys/class/net'
+DEFAULT_GW_PATTERN = re.compile(r"via (\S+)")
+METRIC_PATTERN = re.compile(r"metric (\S+)")
 
 
 class AddressNotReady(exceptions.NeutronException):
@@ -531,12 +534,13 @@ class IpRouteCommand(IpDeviceCommandBase):
                                    route_list_lines if
                                    x.strip().startswith('default')), None)
         if default_route_line:
-            gateway_index = 2
-            parts = default_route_line.split()
-            retval = dict(gateway=parts[gateway_index])
-            if 'metric' in parts:
-                metric_index = parts.index('metric') + 1
-                retval.update(metric=int(parts[metric_index]))
+            retval = dict()
+            gateway = DEFAULT_GW_PATTERN.search(default_route_line)
+            if gateway:
+                retval.update(gateway=gateway.group(1))
+            metric = METRIC_PATTERN.search(default_route_line)
+            if metric:
+                retval.update(metric=int(metric.group(1)))
 
         return retval
 
index 51ac34cfe95ef3bff2bf65e1eff62b5d55ae02d3..f2f879d702d7e88d4556c434eb6d5291606b11de 100644 (file)
@@ -144,6 +144,10 @@ GATEWAY_SAMPLE6 = ("""
 default via 192.168.99.1 proto static metric 100
 """)
 
+GATEWAY_SAMPLE7 = ("""
+default dev qg-31cd36 metric 1
+""")
+
 IPv6_GATEWAY_SAMPLE1 = ("""
 default via 2001:470:9:1224:4508:b885:5fb:740b metric 100
 2001:db8::/64 proto kernel scope link src 2001:470:9:1224:dfcc:aaff:feb9:76ce
@@ -782,7 +786,9 @@ class TestIpRouteCommand(TestIPCmdBase):
                             'expected': {'gateway': '192.168.99.1'}},
                            {'sample': GATEWAY_SAMPLE6,
                             'expected': {'gateway': '192.168.99.1',
-                                         'metric': 100}}]
+                                         'metric': 100}},
+                           {'sample': GATEWAY_SAMPLE7,
+                            'expected': {'metric': 1}}]
 
     def test_add_gateway(self):
         self.route_cmd.add_gateway(self.gateway, self.metric, self.table)