Merge "Do not use exceptions in get_link_id() to control the code flow"
This commit is contained in:
commit
b7c3a99473
|
@ -241,11 +241,11 @@ def _translate_ip_device_exception(e, device=None, namespace=None):
|
|||
|
||||
|
||||
def get_link_id(device, namespace):
|
||||
try:
|
||||
with get_iproute(namespace) as ip:
|
||||
return ip.link_lookup(ifname=device)[0]
|
||||
except IndexError:
|
||||
with get_iproute(namespace) as ip:
|
||||
link_id = ip.link_lookup(ifname=device)
|
||||
if not link_id or len(link_id) < 1:
|
||||
raise NetworkInterfaceNotFound(device=device, namespace=namespace)
|
||||
return link_id[0]
|
||||
|
||||
|
||||
def _run_iproute_link(command, device, namespace=None, **kwargs):
|
||||
|
|
|
@ -47,11 +47,24 @@ class IpLibTestCase(base.BaseTestCase):
|
|||
def test_run_iproute_link_interface_not_exists(self):
|
||||
with mock.patch.object(pyroute2, "IPRoute") as iproute_mock:
|
||||
ip_mock = iproute_mock()
|
||||
ip_mock.__enter__().link_lookup.return_value = []
|
||||
self.assertRaises(
|
||||
priv_lib.NetworkInterfaceNotFound,
|
||||
priv_lib._run_iproute_link,
|
||||
"test_cmd", "eth0", None, test_param="test_value")
|
||||
ret_values = [
|
||||
[], # No interface found.
|
||||
None, # Unexpected output but also handled.
|
||||
]
|
||||
for ret_val in ret_values:
|
||||
ip_mock.__enter__().link_lookup.return_value = ret_val
|
||||
self.assertRaises(
|
||||
priv_lib.NetworkInterfaceNotFound,
|
||||
priv_lib._run_iproute_link,
|
||||
"test_cmd", "eth0", None, test_param="test_value")
|
||||
|
||||
@mock.patch.object(priv_lib, 'get_iproute')
|
||||
def test_get_link_id(self, mock_iproute):
|
||||
mock_ip = mock.Mock()
|
||||
mock_ip.link_lookup.return_value = ['interface_id']
|
||||
mock_iproute.return_value.__enter__.return_value = mock_ip
|
||||
self.assertEqual('interface_id',
|
||||
priv_lib.get_link_id('device', 'namespace'))
|
||||
|
||||
def test_run_iproute_link_interface_removed_during_call(self):
|
||||
with mock.patch.object(pyroute2, "IPRoute") as iproute_mock:
|
||||
|
|
Loading…
Reference in New Issue