This is an automated email from the ASF dual-hosted git repository. jedcunningham pushed a commit to branch v2-4-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 329f706d9e59d690bd6ff6a3c8c035172668a217 Author: Jarek Potiuk <[email protected]> AuthorDate: Thu Sep 22 22:05:34 2022 +0200 Add unit test for log retrieval url (#26603) * Add unit test for log retrieval url Added unit test to #26493 (cherry picked from commit 061caff2862d9df078336dc94efa5a6915935b7e) --- airflow/utils/log/file_task_handler.py | 13 +++++++++---- tests/utils/test_log_handlers.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/airflow/utils/log/file_task_handler.py b/airflow/utils/log/file_task_handler.py index b7ee88ef76..80addd3ded 100644 --- a/airflow/utils/log/file_task_handler.py +++ b/airflow/utils/log/file_task_handler.py @@ -197,10 +197,7 @@ class FileTaskHandler(logging.Handler): else: import httpx - url = urljoin( - f"http://{ti.hostname}:{conf.get('logging', 'WORKER_LOG_SERVER_PORT')}/log/", - log_relative_path, - ) + url = self._get_log_retrieval_url(ti, log_relative_path) log += f"*** Log file does not exist: {location}\n" log += f"*** Fetching from: {url}\n" try: @@ -244,6 +241,14 @@ class FileTaskHandler(logging.Handler): return log, {'end_of_log': True} + @staticmethod + def _get_log_retrieval_url(ti: TaskInstance, log_relative_path: str) -> str: + url = urljoin( + f"http://{ti.hostname}:{conf.get('logging', 'WORKER_LOG_SERVER_PORT')}/log/", + log_relative_path, + ) + return url + def read(self, task_instance, try_number=None, metadata=None): """ Read logs of given task instance from local machine. diff --git a/tests/utils/test_log_handlers.py b/tests/utils/test_log_handlers.py index ee78b0b3e6..6515c2f49b 100644 --- a/tests/utils/test_log_handlers.py +++ b/tests/utils/test_log_handlers.py @@ -251,3 +251,16 @@ class TestFilenameRendering: fth = FileTaskHandler("") rendered_filename = fth._render_filename(filename_rendering_ti, 42) assert expected_filename == rendered_filename + + +class TestLogUrl: + def test_log_retrieval_valid(self, create_task_instance): + log_url_ti = create_task_instance( + dag_id="dag_for_testing_filename_rendering", + task_id="task_for_testing_filename_rendering", + run_type=DagRunType.SCHEDULED, + execution_date=DEFAULT_DATE, + ) + log_url_ti.hostname = 'hostname' + url = FileTaskHandler._get_log_retrieval_url(log_url_ti, 'DYNAMIC_PATH') + assert url == "http://hostname:8793/log/DYNAMIC_PATH"
