support pyroute2 0.5.13

This change modifes os-vif add interface to account
for the new behavior of link_lookup.
In 0.5.13 if a link is not found link_lookup returns an
empty list. In previous releases it raised ipexc.NetlinkError.

Closes-bug: #1890353

Change-Id: I567afb544425c1b91d98968a0b597be718869089
This commit is contained in:
Sean Mooney 2020-08-05 00:07:11 +00:00
parent 989f5a7d92
commit c8703df185
2 changed files with 25 additions and 20 deletions

View File

@ -41,11 +41,7 @@ class PyRoute2(ip_command.IpCommand):
address=None, promisc=None, master=None):
check_exit_code = check_exit_code or []
with iproute.IPRoute() as ip:
idx = ip.link_lookup(ifname=device)
if not idx:
raise exception.NetworkInterfaceNotFound(interface=device)
idx = idx[0]
idx = self.lookup_interface(ip, device)
args = {'index': idx}
if state:
args['state'] = state
@ -59,13 +55,24 @@ class PyRoute2(ip_command.IpCommand):
if promisc is True else
utils.unset_mask(flags, ifinfmsg.IFF_PROMISC))
if master:
args['master'] = ip.link_lookup(ifname=master)
args['master'] = self.lookup_interface(ip, master)
if isinstance(check_exit_code, int):
check_exit_code = [check_exit_code]
return self._ip_link(ip, 'set', check_exit_code, **args)
def lookup_interface(self, ip, link):
# TODO(sean-k-mooney): remove try block after we raise
# the min pyroute2 version above 0.5.12
try:
idx = ip.link_lookup(ifname=link)
except ipexc.NetlinkError:
raise exception.NetworkInterfaceNotFound(interface=link)
if not len(idx):
raise exception.NetworkInterfaceNotFound(interface=link)
return idx[0]
def add(self, device, dev_type, check_exit_code=None, peer=None, link=None,
vlan_id=None, ageing=None):
check_exit_code = check_exit_code or []
@ -74,10 +81,7 @@ class PyRoute2(ip_command.IpCommand):
'kind': dev_type}
if self.TYPE_VLAN == dev_type:
args['vlan_id'] = vlan_id
idx = ip.link_lookup(ifname=link)
if 0 == len(idx):
raise exception.NetworkInterfaceNotFound(interface=link)
args['link'] = idx[0]
args['link'] = self.lookup_interface(ip, link)
elif self.TYPE_VETH == dev_type:
args['peer'] = peer
elif self.TYPE_BRIDGE == dev_type:
@ -106,15 +110,14 @@ class PyRoute2(ip_command.IpCommand):
def delete(self, device, check_exit_code=None):
check_exit_code = check_exit_code or []
with iproute.IPRoute() as ip:
idx = ip.link_lookup(ifname=device)
if len(idx) == 0:
raise exception.NetworkInterfaceNotFound(interface=device)
idx = idx[0]
idx = self.lookup_interface(ip, device)
return self._ip_link(ip, 'del', check_exit_code, **{'index': idx})
def exists(self, device):
"""Return True if the device exists."""
with iproute.IPRoute() as ip:
idx = ip.link_lookup(ifname=device)
return True if idx else False
try:
self.lookup_interface(ip, device)
return True
except Exception:
return False

View File

@ -134,7 +134,9 @@ class TestIpCommand(base.TestCase):
self.ip_link.assert_called_once_with(
'add', ifname=self.DEVICE, kind=self.TYPE_VETH, peer='peer')
self.assertRaises(ipexc.NetlinkError, self.ip.add, self.DEVICE,
self.assertRaises(
exception.NetworkInterfaceNotFound,
self.ip.add, self.DEVICE,
self.TYPE_VLAN, peer='peer',
check_exit_code=[self.OTHER_ERROR_CODE])