potiuk commented on code in PR #69877: URL: https://github.com/apache/airflow/pull/69877#discussion_r3613756750
########## 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: Done. Both test files now use `create_autospec` / `autospec=True`. The spec function mirrors the real `send_email` signature rather than a bare `*args, **kwargs` stub, so the autospec actually enforces something — it rejects a call missing `subject`. Duplicated rather than imported since task-sdk can't depend on airflow-core. Left the three pre-existing `SmtpNotifier` patches alone as they predate this PR. --- Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting ########## 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: You were right. Ran your experiment — with the fix reverted: - `test_custom_email_backend_is_used` → FAILED - `test_unresolvable_email_backend_is_logged` → **PASSED** Exactly your diagnosis: the real `SmtpNotifier()` also blew up and hit the same `except`. Took your first option — `SmtpNotifier` is now patched to succeed plus an explicit `assert_not_called()`, so the logged exception can only come from backend resolution. Re-ran the experiment; it fails with the fix disabled now. Kept rather than dropped because the no-silent-fallback-to-SMTP case is what a stale `email_backend` actually hits on upgrade. --- Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting ########## 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: Agreed — renamed to `_LegacyEmailBackendNotifier`. --- Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting -- 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]
