Skip to content

Commit

Permalink
Merge pull request #1060 from rahkumar651991/cli_return_exception
Browse files Browse the repository at this point in the history
Raise exception when dev.cli fails with exception
  • Loading branch information
Nitin Kr authored Jul 16, 2020
2 parents 2f3e271 + b27b012 commit 3ca08f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
16 changes: 11 additions & 5 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,18 @@ def cli(self, command, format="text", warning=True):
if rsp.tag == "rpc":
return rsp[0]
return rsp
except EzErrors.ConnectClosedError as ex:
except (
EzErrors.ConnectClosedError,
EzErrors.RpcError,
EzErrors.RpcTimeoutError,
) as ex:
raise ex
except EzErrors.RpcError as ex:
return "invalid command: %s: %s" % (command, ex)
except Exception as ex:
return "invalid command: " + command
warnings.warn(
"An unknown exception occurred : %s - please report." % ex,
RuntimeWarning,
)
raise ex

# ------------------------------------------------------------------------
# execute
Expand Down Expand Up @@ -828,7 +834,7 @@ def execute(self, rpc_cmd, ignore_warning=False, **kvargs):
# Something unexpected happened - raise it up
except Exception as err:
warnings.warn(
"An unknown exception occured - please report.", RuntimeWarning
"An unknown exception occurred - please report.", RuntimeWarning
)
raise

Expand Down
22 changes: 16 additions & 6 deletions tests/unit/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,16 +710,26 @@ def test_device_cli_rpc(self, mock_execute):
"get-system-uptime-information",
)

def test_device_cli_exception(self):
self.dev.rpc.cli = MagicMock(side_effect=AttributeError)
val = self.dev.cli("show version")
self.assertEqual(val, "invalid command: show version")
def test_device_cli_connection_exception(self):
self.dev.connected = False
self.assertRaises(EzErrors.ConnectClosedError, self.dev.cli, "foo")

@patch("jnpr.junos.Device.execute")
def test_device_cli_rpc_exception(self, mock_execute):
mock_execute.side_effect = self._mock_manager
val = self.dev.cli("foo")
self.assertEqual(val, "invalid command: foo: RpcError")
self.assertRaises(EzErrors.RpcError, self.dev.cli, "foo")

def test_device_cli_timeout_exception(self):
self.dev._conn.rpc = MagicMock(side_effect=TimeoutExpiredError)
self.assertRaises(EzErrors.RpcTimeoutError, self.dev.cli, "foo")

@patch("jnpr.junos.device.warnings")
def test_device_cli_unknown_exception(self, mock_warnings):
class MyException(Exception):
pass

self.dev._conn.rpc = MagicMock(side_effect=MyException)
self.assertRaises(MyException, self.dev.cli, "foo")

@patch("jnpr.junos.Device.execute")
def test_device_display_xml_rpc(self, mock_execute):
Expand Down

0 comments on commit 3ca08f8

Please sign in to comment.