dabla commented on code in PR #76887:
URL: https://github.com/apache/airflow/pull/76887#discussion_r4639911247


##########
providers/microsoft/azure/tests/unit/microsoft/azure/transfers/test_s3_to_wasb.py:
##########
@@ -118,6 +118,35 @@ def 
test__execute__prefix_without_replace_empty_destination(
         s3_mock_hook.assert_called_once_with(aws_conn_id="aws_default", 
verify=None)
         wasb_mock_hook.assert_called_once_with(wasb_conn_id="wasb_default")
 
+    
@mock.patch("airflow.providers.microsoft.azure.transfers.s3_to_wasb.S3Hook")

Review Comment:
   **[nit]** The new test patches `S3Hook`, `WasbHook` and `NamedTemporaryFile` 
without `autospec=True`.
   
   [AGENTS.md](https://github.com/apache/airflow/blob/main/AGENTS.md) asks for 
`spec`/`autospec` when mocking. Without it, 
`s3_mock_hook.return_value.download_file.return_value = ...` sets a value 
nothing reads (`move_file()` calls `get_conn().download_file`), and a renamed 
hook method would still pass. The other tests in this file have the same 
pattern, but a new test is a good place to stop copying it.



##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/transfers/s3_to_wasb.py:
##########
@@ -257,9 +257,18 @@ def get_files_to_move(self) -> list[str]:
                     azure_blob_files.append(self.blob_name.split("/")[-1])
 
             elif self.blob_prefix:
-                azure_blob_files += self.wasb_hook.get_blobs_list_recursive(
+                # move_file writes f"{blob_prefix}/{file}", so a prefix ending 
in "/" gives "prefix//file"
+                written_prefix = f"{self.blob_prefix}/"
+                clean_prefix = f"{self.blob_prefix.rstrip('/')}/"
+                for blob in self.wasb_hook.get_blobs_list_recursive(
                     container_name=self.container_name, prefix=self.blob_prefix
-                )
+                ):
+                    name = blob

Review Comment:
   **[warning]** A listed blob that does not start with either prefix is kept 
under its full name, so it can still match a relative file name and skip a file 
that was never written.
   
   `get_blobs_list_recursive(prefix=self.blob_prefix)` is a plain string-prefix 
listing, so with `blob_prefix="out"` it also returns blobs such as `outside/x`. 
It does not start with `out/` (both prefixes are `out/` here), so `name = blob` 
keeps `outside/x`, and an S3 key `pre/outside/x` (with `s3_prefix="pre"`, so 
file name `outside/x`) is dropped from `files_to_move`, although the operator 
would write `out/outside/x`, which does not exist. Checked with the loop copied 
as-is:
   
   ```
   blob_prefix='out', file 'outside/x', existing blob 'outside/x'
     skip 'outside/x': writes 'out/outside/x', exists=False
   ```
   
   This is the same issue as the `out/` case @aoelvp94 raised, one level down: 
the listing is converted back into relative names, and every conversion rule 
has inputs where it gets the wrong answer. Building the names `move_file()` 
writes and comparing those against the listing removes the guessing completely, 
and reuses the function that does the writing instead of a second copy of its 
rule ([DRY, The Pragmatic Programmer](https://pragprog.com/tips/)):
   
   ```python
   existing = 
set(self.wasb_hook.get_blobs_list_recursive(container_name=self.container_name, 
prefix=self.blob_prefix))
   files_to_move = [f for f in files_to_move if self._create_key(None, 
self.blob_prefix, f) not in existing]
   ```



##########
providers/microsoft/azure/tests/unit/microsoft/azure/transfers/test_s3_to_wasb.py:
##########
@@ -130,6 +159,38 @@ def 
test__execute__prefix_without_replace_empty_destination(
             (MOCK_FILES, [], MOCK_FILES, PREFIX, PREFIX, False),  # Task 1 
from above
             (MOCK_FILES, MOCK_FILES[1:], [MOCK_FILES[0]], PREFIX, PREFIX, 
False),  # Task 2 from above
             (MOCK_FILES, MOCK_FILES[1:], MOCK_FILES, PREFIX, PREFIX, True),  # 
Task 3 from above
+            (
+                MOCK_FILES,
+                [f"{PREFIX}/{f}" for f in MOCK_FILES[1:]],
+                [MOCK_FILES[0]],
+                PREFIX,
+                PREFIX,
+                False,
+            ),
+            (
+                MOCK_FILES,
+                [f"{PREFIX}/{f}" for f in MOCK_FILES[1:]],
+                [MOCK_FILES[0]],
+                PREFIX,
+                f"{PREFIX}/",

Review Comment:
   **[warning]** This row asserts the wrong-skip case @aoelvp94 described as 
correct behaviour.
   
   With `blob_prefix="TEST/"` the operator writes `TEST//<file>`, so existing 
`TEST/<file>` blobs are not the operator's output, and `MOCK_FILES[1:]` should 
still be transferred. The row expects only `MOCK_FILES[0]`, so any fix of that 
case will fail here first. Expected value should be `MOCK_FILES` (the 
`TEST//<file>` row below already covers the real "already written" case).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to