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
This commit is contained in:
Arata Notsu 2014-01-10 19:54:10 +09:00
parent f21dad0579
commit 789c3f5ef9
2 changed files with 16 additions and 4 deletions

View 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

View 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(),