diff --git a/neutron/agent/linux/ip_lib.py b/neutron/agent/linux/ip_lib.py index aa44b1e299..297c566f8e 100644 --- a/neutron/agent/linux/ip_lib.py +++ b/neutron/agent/linux/ip_lib.py @@ -393,9 +393,8 @@ class IpRouteCommand(IpDeviceCommandBase): gateway_index = 2 parts = default_route_line.split() retval = dict(gateway=parts[gateway_index]) - metric_index = 4 - parts_has_metric = (len(parts) > metric_index) - if parts_has_metric: + if 'metric' in parts: + metric_index = parts.index('metric') + 1 retval.update(metric=int(parts[metric_index])) return retval diff --git a/neutron/tests/unit/test_linux_ip_lib.py b/neutron/tests/unit/test_linux_ip_lib.py index 7ae10670bf..499153c3a3 100644 --- a/neutron/tests/unit/test_linux_ip_lib.py +++ b/neutron/tests/unit/test_linux_ip_lib.py @@ -127,6 +127,14 @@ GATEWAY_SAMPLE4 = (""" default via 10.35.19.254 """) +GATEWAY_SAMPLE5 = (""" +default via 192.168.99.1 proto static +""") + +GATEWAY_SAMPLE6 = (""" +default via 192.168.99.1 proto static metric 100 +""") + DEVICE_ROUTE_SAMPLE = ("10.0.0.0/24 scope link src 10.0.0.2") SUBNET_SAMPLE1 = ("10.0.0.0/24 dev qr-23380d11-d2 scope link src 10.0.0.1\n" @@ -647,7 +655,12 @@ class TestIpRouteCommand(TestIPCmdBase): {'sample': GATEWAY_SAMPLE3, 'expected': None}, {'sample': GATEWAY_SAMPLE4, - 'expected': {'gateway': '10.35.19.254'}}] + 'expected': {'gateway': '10.35.19.254'}}, + {'sample': GATEWAY_SAMPLE5, + 'expected': {'gateway': '192.168.99.1'}}, + {'sample': GATEWAY_SAMPLE6, + 'expected': {'gateway': '192.168.99.1', + 'metric': 100}}] for test_case in test_cases: self.parent._run = mock.Mock(return_value=test_case['sample']) self.assertEqual(self.route_cmd.get_gateway(),