From 5b5ba869aa4680bcfa7544c4e718cb5a5dc9c168 Mon Sep 17 00:00:00 2001 From: Arata Notsu Date: Fri, 10 Jan 2014 19:54:10 +0900 Subject: [PATCH] Fix ValueError in ip_lib.IpRouteCommand.get_gateway() As metric is not necessarily the 5th word of the gateway line, the method should search the string 'metric' in the line and pick the next word as the metric value. Change-Id: I2663ddbae82f80b912b364c07f9ab92c5b90b718 Closes-Bug: #1267790 --- neutron/agent/linux/ip_lib.py | 5 ++--- neutron/tests/unit/test_linux_ip_lib.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/neutron/agent/linux/ip_lib.py b/neutron/agent/linux/ip_lib.py index aa44b1e29..297c566f8 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 7ae10670b..499153c3a 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(), -- 2.45.2