Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions airflow/providers/sftp/hooks/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,12 @@ def append_matching_path_callback(list_):
)

return files, dirs, unknowns

def test_connection(self) -> Tuple[bool, str]:
"""Test the SFTP connection by checking if remote entity '/some/path' exists"""
try:
conn = self.get_conn()
conn.pwd
return True, "Connection successfully tested"
except Exception as e:
return False, str(e)
31 changes: 31 additions & 0 deletions tests/providers/sftp/hooks/test_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,37 @@ def test_get_tree_map(self):
assert dirs == [os.path.join(TMP_PATH, TMP_DIR_FOR_TESTS, SUB_DIR)]
assert unknowns == []

@mock.patch('airflow.providers.sftp.hooks.sftp.SFTPHook.get_connection')
@mock.patch(
'airflow.providers.sftp.hooks.sftp.SFTPHook.get_conn.pwd', side_effect=Exception('Connection Error')
)
def test_connection_failure(self, mock_get_connection, mock_pwd):
connection = Connection(
login='login',
host='host',
)
mock_get_connection.return_value = connection

hook = SFTPHook()
status, msg = hook.test_connection()
assert status is False
assert msg == 'Connection Error'

@mock.patch('airflow.providers.sftp.hooks.sftp.SFTPHook.get_connection')
@mock.patch('airflow.providers.sftp.hooks.sftp.SFTPHook.get_conn.pwd')
def test_connection_success(self, mock_get_connection, mock_pwd):
connection = Connection(
login='login',
host='host',
)
mock_get_connection.return_value = connection
mock_pwd.return_value = '/home/some_user'

hook = SFTPHook()
status, msg = hook.test_connection()
assert status is True
assert msg == 'Connection successfully tested'

def tearDown(self):
shutil.rmtree(os.path.join(TMP_PATH, TMP_DIR_FOR_TESTS))
os.remove(os.path.join(TMP_PATH, TMP_FILE_FOR_TESTS))
Expand Down