Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_execute_raises_connection_error(self):
mock_send = Mock(name='send')
mock_send.side_effect = pyeapi.eapilib.ConnectionError('test', 'test')
instance = pyeapi.eapilib.EapiConnection()
instance.send = mock_send
with self.assertRaises(pyeapi.eapilib.ConnectionError):
instance.execute('test')
def test_execute_raises_connection_error(self):
mock_send = Mock(name='send')
mock_send.side_effect = pyeapi.eapilib.ConnectionError('test', 'test')
instance = pyeapi.eapilib.EapiConnection()
instance.send = mock_send
with self.assertRaises(pyeapi.eapilib.ConnectionError):
instance.execute('test')
Note:
If the return from this method is False, use the error property
to investigate the exception
Args:
commands (list): A list of commands to be sent to the node in
config mode
Returns:
True if the commands are executed without exception otherwise
False is returned
"""
try:
self.node.config(commands)
return True
except (CommandError, ConnectionError):
return False
raise CommandError(code, msg, command_error=err, output=out)
return decoded
# socket.error is deprecated in python 3 and replaced with OSError.
except (socket.error, OSError) as exc:
_LOGGER.exception(exc)
self.socket_error = exc
self.error = exc
error_msg = 'Socket error during eAPI connection: %s' % str(exc)
raise ConnectionError(str(self), error_msg)
except ValueError as exc:
_LOGGER.exception(exc)
self.socket_error = None
self.error = exc
raise ConnectionError(str(self), 'unable to connect to eAPI')
finally:
self.transport.close()
CommandError: A CommandError is raised that includes the error
code, error message along with the list of commands that were
sent to the node. The exception instance is also stored in
the error property and is availble until the next request is
sent
"""
if encoding not in ('json', 'text'):
raise TypeError('encoding must be one of [json, text]')
try:
self.error = None
request = self.request(commands, encoding=encoding, **kwargs)
response = self.send(request)
return response
except(ConnectionError, CommandError, TypeError) as exc:
exc.commands = commands
self.error = exc
raise
try:
connection = self.transport_class(
host=self.hostname,
username=self.username,
password=self.password,
timeout=self.timeout,
**self.eapi_kwargs
)
if self.device is None:
self.device = pyeapi.client.Node(connection, enablepwd=self.enablepwd)
# does not raise an Exception if unusable
# let's try to run a very simple command
self.device.run_commands(["show clock"], encoding="text")
except ConnectionError as ce:
# and this is raised either if device not avaiable
# either if HTTP(S) agent is not enabled
# show management api http-commands
raise ConnectionException(str(ce))
if match:
auto_msg = ('%s parameter is not supported in this'
' version of EOS.' % match.group(1))
_LOGGER.error(auto_msg)
msg = msg + '. ' + auto_msg
raise CommandError(code, msg, command_error=err, output=out)
return decoded
# socket.error is deprecated in python 3 and replaced with OSError.
except (socket.error, OSError) as exc:
_LOGGER.exception(exc)
self.socket_error = exc
self.error = exc
error_msg = 'Socket error during eAPI connection: %s' % str(exc)
raise ConnectionError(str(self), error_msg)
except ValueError as exc:
_LOGGER.exception(exc)
self.socket_error = None
self.error = exc
raise ConnectionError(str(self), 'unable to connect to eAPI')
finally:
self.transport.close()
password=self.password,
port=self.port,
timeout=self.timeout
)
elif self.transport == 'socket':
connection = pyeapi.client.connect(transport=self.transport)
else:
raise ConnectionException("Unknown transport: {}".format(self.transport))
if self.device is None:
self.device = pyeapi.client.Node(connection, enablepwd=self.enablepwd)
# does not raise an Exception if unusable
# let's try to run a very simple command
self.device.run_commands(['show clock'], encoding='text')
except ConnectionError as ce:
# and this is raised either if device not avaiable
# either if HTTP(S) agent is not enabled
# show management api http-commands
raise ConnectionException(ce.message)