]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix ValueError in ip_lib.IpRouteCommand.get_gateway()
authorArata Notsu <notsu@virtualtech.jp>
Fri, 10 Jan 2014 10:54:10 +0000 (19:54 +0900)
committerArata Notsu <notsu@virtualtech.jp>
Wed, 12 Feb 2014 06:43:52 +0000 (15:43 +0900)
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
neutron/tests/unit/test_linux_ip_lib.py

index aa44b1e2994bc435a2f2a589ccc105045d23ee8a..297c566f8e892caddbf7557060978b310f4e98d9 100644 (file)
@@ -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
index 7ae10670bfc638c94f1037b453961fc9cbd49de5..499153c3a3bc6d8e2c81fd444c8da0c4cf1c62b1 100644 (file)
@@ -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(),