amoghrajesh commented on code in PR #69877:
URL: https://github.com/apache/airflow/pull/69877#discussion_r3613683616


##########
task-sdk/tests/task_sdk/execution_time/test_email_backend.py:
##########
@@ -0,0 +1,60 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+
+from airflow.sdk.configuration import conf
+from airflow.sdk.exceptions import AirflowConfigException
+from airflow.sdk.execution_time.email_backend import LegacyEmailBackendNotifier
+
+
+class TestLegacyEmailBackendNotifier:
+    def test_notify_calls_configured_backend(self):
+        """notify() loads the legacy backend from config and calls it with the 
standard fields."""
+        backend = mock.MagicMock()

Review Comment:
   Can we please use a spec or autospec here?



##########
task-sdk/src/airflow/sdk/execution_time/email_backend.py:
##########
@@ -0,0 +1,75 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Any
+
+from airflow.sdk.bases.notifier import BaseNotifier
+from airflow.sdk.configuration import conf
+from airflow.sdk.exceptions import AirflowConfigException
+
+if TYPE_CHECKING:
+    from collections.abc import Iterable
+
+    from airflow.sdk.definitions.context import Context
+
+DEFAULT_EMAIL_BACKEND = "airflow.utils.email.send_email_smtp"
+
+
+class LegacyEmailBackendNotifier(BaseNotifier):

Review Comment:
   There is no legitimate use case for a Dag author or provider to construct 
`LegacyEmailBackendNotifier` directly, I'd keep this class internal / private



##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -4008,6 +4013,76 @@ def execute(self, context):
                 )
                 assert kwargs["from_email"] == self.FROM
 
+    def test_custom_email_backend_is_used(self, create_runtime_ti, 
mock_supervisor_comms):
+        """A custom ``[email] email_backend`` is wrapped and invoked with 
rendered fields."""
+        from airflow.sdk.exceptions import AirflowFailException
+        from airflow.sdk.execution_time.task_runner import finalize, run
+
+        backend = mock.MagicMock()
+
+        class FailingOperator(BaseOperator):
+            def execute(self, context):
+                raise AirflowFailException("Task failed on purpose")
+
+        task = FailingOperator(
+            task_id="legacy_backend_task",
+            email=["[email protected]"],
+            email_on_failure=True,
+        )
+
+        runtime_ti = create_runtime_ti(task=task)
+        context = runtime_ti.get_template_context()
+        log = mock.MagicMock()
+
+        with conf_vars(
+            {
+                ("email", "email_backend"): 
f"{__name__}._recording_email_backend",
+                ("email", "email_conn_id"): "my_smtp",
+                ("email", "from_email"): self.FROM,
+            }
+        ):
+            with mock.patch(f"{__name__}._recording_email_backend", backend):
+                with mock.patch(
+                    "airflow.providers.smtp.notifications.smtp.SmtpNotifier"
+                ) as mock_smtp_notifier:
+                    state, _, error = run(runtime_ti, context, log)
+                    finalize(runtime_ti, state, context, log, error)
+
+        # The default SMTP notifier must not be used when a custom backend is 
configured.
+        mock_smtp_notifier.assert_not_called()
+        backend.assert_called_once()
+        args, kwargs = backend.call_args
+        # send_email(to, subject, html_content, conn_id=..., from_email=...)
+        assert args[0] == ["[email protected]"]
+        assert kwargs["conn_id"] == "my_smtp"
+        assert kwargs["from_email"] == self.FROM
+
+    def test_unresolvable_email_backend_is_logged(self, create_runtime_ti, 
mock_supervisor_comms):
+        """A custom backend that cannot be imported is logged and does not 
raise."""
+        from airflow.sdk.exceptions import AirflowFailException
+        from airflow.sdk.execution_time.task_runner import finalize, run
+
+        class FailingOperator(BaseOperator):
+            def execute(self, context):
+                raise AirflowFailException("Task failed on purpose")
+
+        task = FailingOperator(
+            task_id="bad_backend_task",
+            email=["[email protected]"],
+            email_on_failure=True,
+        )
+
+        runtime_ti = create_runtime_ti(task=task)
+        context = runtime_ti.get_template_context()
+        log = mock.MagicMock()
+
+        with conf_vars({("email", "email_backend"): "airflow.does.not.Exist"}):
+            state, _, error = run(runtime_ti, context, log)
+            # Must not raise even though the backend cannot be loaded.
+            finalize(runtime_ti, state, context, log, error)
+
+        log.exception.assert_called()

Review Comment:
   This may not be a real regression test. It sets email_backend to 
"airflow.does.not.Exist" and asserts `log.exception.assert_called()`, but 
doesn't mock SmtpNotifier. 
   
   Before this PR's fix, the code never reads `email_backend` at all, it always 
constructs a real SmtpNotifier unconditionally. In the test environment, that 
real `SmtpNotifier()` call is quite likely to also fail and also hit the same 
exception block at the call site. If thats the case, this test would pass 
identically with or without the PR's change meaning it isn't actually verifying 
"unresolvable custom backend is handled gracefully," it's just asserting 
"something failed and got logged," which was already true. Worth checking 
whether this test would fail if you reverted just the fix and kept the test and 
if it still passes, it should either mock SmtpNotifier to succeed (isolating 
the assertion to the backend-resolution path) or be dropped in favor of relying 
on test_custom_email_backend_is_used (which does correctly assert 
mock_smtp_notifier.assert_not_called()) for the meaningful coverage.



##########
task-sdk/tests/task_sdk/execution_time/test_email_backend.py:
##########
@@ -0,0 +1,60 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+
+from airflow.sdk.configuration import conf
+from airflow.sdk.exceptions import AirflowConfigException
+from airflow.sdk.execution_time.email_backend import LegacyEmailBackendNotifier
+
+
+class TestLegacyEmailBackendNotifier:
+    def test_notify_calls_configured_backend(self):
+        """notify() loads the legacy backend from config and calls it with the 
standard fields."""
+        backend = mock.MagicMock()

Review Comment:
   This and other occurences.



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