From: Carl Baldwin Date: Mon, 31 Aug 2015 21:33:53 +0000 (+0000) Subject: Turn device not found errors in to exceptions X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=606e346b803101345a991b6183ee16201222984a;p=openstack-build%2Fneutron-build.git Turn device not found errors in to exceptions Change-Id: If697c4c4c09d32abd410ac1fce4d8a6a0d337da1 Partially-implements: blueprint address-scopes --- diff --git a/neutron/agent/linux/ip_lib.py b/neutron/agent/linux/ip_lib.py index 551341a18..0a27d4a15 100644 --- a/neutron/agent/linux/ip_lib.py +++ b/neutron/agent/linux/ip_lib.py @@ -583,20 +583,22 @@ class IpRouteCommand(IpDeviceCommandBase): args += self._table_args(table) self._as_root([ip_version], tuple(args)) + def _run_as_root_detect_device_not_found(self, *args, **kwargs): + try: + return self._as_root(*args, **kwargs) + except RuntimeError as rte: + with excutils.save_and_reraise_exception() as ctx: + if "Cannot find device" in str(rte): + ctx.reraise = False + raise exceptions.DeviceNotFoundError(device_name=self.name) + def delete_gateway(self, gateway, table=None): ip_version = get_ip_version(gateway) args = ['del', 'default', 'via', gateway] args += self._dev_args() args += self._table_args(table) - try: - self._as_root([ip_version], tuple(args)) - except RuntimeError as rte: - with (excutils.save_and_reraise_exception()) as ctx: - if "Cannot find device" in str(rte): - ctx.reraise = False - raise exceptions.DeviceNotFoundError( - device_name=self.name) + self._run_as_root_detect_device_not_found([ip_version], tuple(args)) def _parse_routes(self, ip_version, output, **kwargs): for line in output.splitlines(): @@ -729,7 +731,7 @@ class IpRouteCommand(IpDeviceCommandBase): args += self._table_args(table) for k, v in kwargs.items(): args += [k, v] - self._as_root([ip_version], tuple(args)) + self._run_as_root_detect_device_not_found([ip_version], tuple(args)) def delete_route(self, cidr, via=None, table=None, **kwargs): ip_version = get_ip_version(cidr) @@ -740,7 +742,7 @@ class IpRouteCommand(IpDeviceCommandBase): args += self._table_args(table) for k, v in kwargs.items(): args += [k, v] - self._as_root([ip_version], tuple(args)) + self._run_as_root_detect_device_not_found([ip_version], tuple(args)) class IPRoute(SubProcessBase): diff --git a/neutron/tests/unit/agent/linux/test_ip_lib.py b/neutron/tests/unit/agent/linux/test_ip_lib.py index 2de408d76..af205002d 100644 --- a/neutron/tests/unit/agent/linux/test_ip_lib.py +++ b/neutron/tests/unit/agent/linux/test_ip_lib.py @@ -965,6 +965,12 @@ class TestIpRouteCommand(TestIPCmdBase): 'dev', self.parent.name, 'scope', 'link')) + def test_add_route_no_device(self): + self.parent._as_root.side_effect = RuntimeError("Cannot find device") + self.assertRaises(exceptions.DeviceNotFoundError, + self.route_cmd.add_route, + self.cidr, self.ip, self.table) + def test_delete_route(self): self.route_cmd.delete_route(self.cidr, self.ip, self.table) self._assert_sudo([self.ip_version], @@ -987,6 +993,12 @@ class TestIpRouteCommand(TestIPCmdBase): 'dev', self.parent.name, 'scope', 'link')) + def test_delete_route_no_device(self): + self.parent._as_root.side_effect = RuntimeError("Cannot find device") + self.assertRaises(exceptions.DeviceNotFoundError, + self.route_cmd.delete_route, + self.cidr, self.ip, self.table) + def test_list_routes(self): self.parent._run.return_value = ( "default via 172.124.4.1 dev eth0 metric 100\n"