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


##########
providers/microsoft/azure/tests/unit/microsoft/azure/log/test_wasb_task_handler.py:
##########
@@ -314,22 +315,18 @@ def test_wasb_read_raises(self, mock_hook, caplog):
         assert rec.exc_info is not None
 
     @mock.patch("airflow.providers.microsoft.azure.hooks.wasb.WasbHook")

Review Comment:
   **[nit]** The tests rewritten or added here could patch `WasbHook` with 
`autospec=True`.
   
   These tests now set `check_for_blob` and `read_file` directly on 
`mock_hook.return_value`. Without a spec, a mistyped method name on the mock 
would still pass, and the test would then exercise nothing. 
`@mock.patch("airflow.providers.microsoft.azure.hooks.wasb.WasbHook", 
autospec=True)` ties the mock to the real hook's methods and signatures. 
[AGENTS.md](https://github.com/apache/airflow/blob/main/AGENTS.md) asks for 
`spec`/`autospec` when mocking. This applies to `test_write_log`, the two 
`test_write_on_existing_log*` tests, 
`test_write_refuses_to_overwrite_when_existing_log_cannot_be_read` and 
`test_write_when_remote_log_does_not_exist`. The other tests in the file can 
stay as they are.



##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/log/wasb_task_handler.py:
##########
@@ -229,10 +229,24 @@ def write(self, log: str, remote_log_location: str, 
append: bool = True) -> bool
         :param log: the log to write to the remote_log_location
         :param remote_log_location: the log's location in remote storage
         :param append: if False, any existing log file is overwritten. If True,
-            the new log is appended to any existing logs.
+            the new log is appended to any existing logs; if the existing log 
cannot be
+            read, nothing is written and ``False`` is returned.
         """
-        if append and self.wasb_log_exists(remote_log_location):
-            old_log = self.wasb_read(remote_log_location)
+        if append:
+            try:
+                old_log = ""
+                if self.hook.check_for_blob(self.wasb_container, 
remote_log_location):

Review Comment:
   **[nit]** One GET that treats "not found" as an empty log would do the job 
of the HEAD plus GET.
   
   `check_for_blob` turns `ResourceNotFoundError` into `False` and lets 
everything else through. Catching that same exception around `read_file` gives 
the same contract with one request per upload instead of two. It also drops the 
window where the blob changes between the existence check and the read. The GCS 
handler is built the same way: one download, with `no_log_found` deciding which 
exception counts as "no log yet".
   
   ```python
   try:
       old_log = self.hook.read_file(self.wasb_container, remote_log_location)
   except ResourceNotFoundError:
       old_log = ""
   except Exception as e:
       self.log.warning(...)
       return False
   ```
   
   The `test_write_when_remote_log_does_not_exist` control would then set 
`read_file.side_effect = ResourceNotFoundError()` instead of checking that 
`read_file` isn't called. Fine to leave as is if you prefer to keep the hook's 
existence check.



-- 
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