def test_socket_closed(self): conn_params = { 'hostname': 'dummy.host.org', 'username': '******', 'password': '******', 'timeout': '600' } ssh_client = ParamikoSSHClient(**conn_params) # Make sure .close() doesn't actually call anything real ssh_client.client = Mock() ssh_client.sftp_client = Mock() ssh_client.bastion_client = Mock() ssh_client.socket = Mock() ssh_client.bastion_socket = Mock() # Make sure we havent called any close methods at this point # TODO: Replace these with .assert_not_called() once it's Python 3.6+ only self.assertEqual(ssh_client.socket.close.call_count, 0) self.assertEqual(ssh_client.client.close.call_count, 0) self.assertEqual(ssh_client.sftp_client.close.call_count, 0) self.assertEqual(ssh_client.bastion_socket.close.call_count, 0) self.assertEqual(ssh_client.bastion_client.close.call_count, 0) # Call the function that has changed ssh_client.close() # TODO: Replace these with .assert_called_once() once it's Python 3.6+ only self.assertEqual(ssh_client.socket.close.call_count, 1) self.assertEqual(ssh_client.client.close.call_count, 1) self.assertEqual(ssh_client.sftp_client.close.call_count, 1) self.assertEqual(ssh_client.bastion_socket.close.call_count, 1) self.assertEqual(ssh_client.bastion_client.close.call_count, 1)
def test_sftp_connection_is_only_established_if_required(self): # Verify that SFTP connection is lazily established only if and when needed. conn_params = { 'hostname': 'dummy.host.org', 'username': '******', 'password': '******' } # Verify sftp connection and client hasn't been established yet client = ParamikoSSHClient(**conn_params) client.connect() self.assertIsNone(client.sftp_client) # run method doesn't require sftp access so it shouldn't establish connection client = ParamikoSSHClient(**conn_params) client.connect() client.run(cmd='whoami') self.assertIsNone(client.sftp_client) # Methods below require SFTP access so they should cause SFTP connection to be established # put client = ParamikoSSHClient(**conn_params) client.connect() path = '/root/random_script.sh' client.put(path, path, mirror_local_mode=False) self.assertIsNotNone(client.sftp_client) # exists client = ParamikoSSHClient(**conn_params) client.connect() client.exists('/root/somepath.txt') self.assertIsNotNone(client.sftp_client) # mkdir client = ParamikoSSHClient(**conn_params) client.connect() client.mkdir('/root/somedirfoo') self.assertIsNotNone(client.sftp_client) # Verify close doesn't throw if SFTP connection is not established client = ParamikoSSHClient(**conn_params) client.connect() self.assertIsNone(client.sftp_client) client.close() # Verify SFTP connection is closed if it's opened client = ParamikoSSHClient(**conn_params) client.connect() client.mkdir('/root/somedirfoo') self.assertIsNotNone(client.sftp_client) client.close() self.assertEqual(client.sftp_client.close.call_count, 1)
def test_sftp_connection_is_only_established_if_required(self): # Verify that SFTP connection is lazily established only if and when needed. conn_params = {'hostname': 'dummy.host.org', 'username': '******'} # Verify sftp connection and client hasn't been established yet client = ParamikoSSHClient(**conn_params) client.connect() self.assertTrue(client.sftp_client is None) # run method doesn't require sftp access so it shouldn't establish connection client = ParamikoSSHClient(**conn_params) client.connect() client.run(cmd='whoami') self.assertTrue(client.sftp_client is None) # Methods bellow require SFTP access so they should cause SFTP connection to be established # put client = ParamikoSSHClient(**conn_params) client.connect() path = '/root/random_script.sh' client.put(path, path, mirror_local_mode=False) self.assertTrue(client.sftp_client is not None) # exists client = ParamikoSSHClient(**conn_params) client.connect() client.exists('/root/somepath.txt') self.assertTrue(client.sftp_client is not None) # mkdir client = ParamikoSSHClient(**conn_params) client.connect() client.mkdir('/root/somedirfoo') self.assertTrue(client.sftp_client is not None) # Verify close doesn't throw if SFTP connection is not established client = ParamikoSSHClient(**conn_params) client.connect() self.assertTrue(client.sftp_client is None) client.close() # Verify SFTP connection is closed if it's opened client = ParamikoSSHClient(**conn_params) client.connect() client.mkdir('/root/somedirfoo') self.assertTrue(client.sftp_client is not None) client.close() self.assertEqual(client.sftp_client.close.call_count, 1)
def test_socket_not_closed_if_none(self): conn_params = { 'hostname': 'dummy.host.org', 'username': '******', 'password': '******', 'timeout': '600' } ssh_client = ParamikoSSHClient(**conn_params) # Make sure .close() doesn't actually call anything real ssh_client.client = None ssh_client.sftp_client = None ssh_client.bastion_client = None ssh_client.socket = None ssh_client.bastion_socket = None # Call the function, this should not throw an exception ssh_client.close()
def test_socket_not_closed_if_none(self): conn_params = { "hostname": "dummy.host.org", "username": "******", "password": "******", "timeout": "600", } ssh_client = ParamikoSSHClient(**conn_params) # Make sure .close() doesn't actually call anything real ssh_client.client = None ssh_client.sftp_client = None ssh_client.bastion_client = None ssh_client.socket = None ssh_client.bastion_socket = None # Call the function, this should not throw an exception ssh_client.close()