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 = (
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
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):
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
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
'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)