Skip to content

Conversation

@jroachgolf84
Copy link
Collaborator

This PR aims to address the issue raised in #51151. When a file failed to exist on the SFTP site, the SFTPSensor was still returning True. This change checks if the file does in fact exist. If not, it returns an empty list, resulting in the Sensor returning False and another poke being executed.

closes: #51151

@jroachgolf84
Copy link
Collaborator Author

cc: @RNHTTR, do you mind taking a look at this?

@BBQing
Copy link
Contributor

BBQing commented May 29, 2025

@jroachgolf84 I think the try expect block around hook.isfile, can be removed, because the exception is not raised from that call. Also I would suggest to update the test case for missing file - because at the moment its behavior depends on the the OSError, that never gets actually raised

@jroachgolf84
Copy link
Collaborator Author

jroachgolf84 commented May 29, 2025

@jroachgolf84 I think the try expect block around hook.isfile, can be removed, because the exception is not raised from that call. Also I would suggest to update the test case for missing file - because at the moment its behavior depends on the the OSError, that never gets actually raised

This is not the case; .isfile can raise the OSError exception. In fact, that is the only exception that can be raised. See here.

def isfile(self, path: str) -> bool:
"""
Check if the path provided is a file.
:param path: full path to the remote file to check
"""
with self.get_managed_conn() as conn:
try:
return stat.S_ISREG(conn.stat(path).st_mode) # type: ignore
except OSError:
return False

@BBQing
Copy link
Contributor

BBQing commented May 29, 2025

def test_file_absent(self, sftp_hook_mock):
I am thinking about the testcase for file absent - it depends on get_mod_time, which is called with the for loop over the actual_files_present - but since it is iteration over empty list, you shoudl never get the oserror side effect in the test.

@jroachgolf84
Copy link
Collaborator Author

I am thinking about the testcase for file absent - it depends on get_mod_time, which is called with the for loop over the actual_files_present - but since it is iteration over empty list, you shoudl never get the oserror side effect in the test.

Can you be more specific? Are you talking about the existing test?

@BBQing
Copy link
Contributor

BBQing commented May 29, 2025

For some reason I have been looking at tests for version 2.11 - the tests for 3.0.x are fine. My bad.

Maybe should there be a test case, when you receive False from the hook.isfile instead of OSError as last thing I can think of.

@jroachgolf84
Copy link
Collaborator Author

Test added!

@BBQing
Copy link
Contributor

BBQing commented May 31, 2025

One more thing - does the newly added test fail against the code without the change applied?

@jroachgolf84
Copy link
Collaborator Author

One more thing - does the newly added test fail against the code without the change applied?

This has been validated.

Copy link
Member

@jason810496 jason810496 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, I left a small nit for the files_pattern case.

jroachgolf84 and others added 2 commits June 2, 2025 13:43
@RNHTTR RNHTTR merged commit 62adb0b into apache:main Jun 3, 2025
70 checks passed
sanederchik pushed a commit to sanederchik/airflow that referenced this pull request Jun 7, 2025
…apache#51167)

* Updating SFTPSensor to properly handle scenario where file is missing

* Updating variable name to actual_files_present

* Changing list element variable name

* Adding unit-test to handle irregular files

* Updated exception handling, testing logic

* Updated exception handling, testing logic

* Update providers/sftp/src/airflow/providers/sftp/sensors/sftp.py

Co-authored-by: Ryan Hatter <25823361+RNHTTR@users.noreply.github.com>

---------

Co-authored-by: Ryan Hatter <25823361+RNHTTR@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SFTPSensor succeeds even if file is absent

6 participants