|
24 | 24 | import pathlib
|
25 | 25 | import sys
|
26 | 26 | import tempfile
|
27 |
| -from unittest.mock import patch |
| 27 | +from unittest.mock import call, patch |
28 | 28 |
|
29 | 29 | import pytest
|
30 | 30 |
|
|
96 | 96 | # Other settings here
|
97 | 97 | """
|
98 | 98 |
|
| 99 | +SETTINGS_FILE_WITH_REMOTE_VARS = f"""{SETTINGS_FILE_VALID} |
| 100 | +REMOTE_TASK_LOG = None |
| 101 | +DEFAULT_REMOTE_CONN_ID = "test_conn_id" |
| 102 | +""" |
| 103 | + |
99 | 104 | SETTINGS_DEFAULT_NAME = "custom_airflow_local_settings"
|
100 | 105 |
|
101 | 106 |
|
@@ -191,6 +196,15 @@ def teardown_method(self):
|
191 | 196 | importlib.reload(airflow_local_settings)
|
192 | 197 | configure_logging()
|
193 | 198 |
|
| 199 | + def _verify_basic_logging_config( |
| 200 | + self, logging_config: dict, logging_class_path: str, expected_path: str |
| 201 | + ) -> None: |
| 202 | + """Helper method to verify basic logging config structure""" |
| 203 | + assert isinstance(logging_config, dict) |
| 204 | + assert logging_config["version"] == 1 |
| 205 | + assert "airflow.task" in logging_config["loggers"] |
| 206 | + assert logging_class_path == expected_path |
| 207 | + |
194 | 208 | # When we try to load an invalid config file, we expect an error
|
195 | 209 | def test_loading_invalid_local_settings(self):
|
196 | 210 | from airflow.logging_config import configure_logging, log
|
@@ -365,3 +379,44 @@ def test_loading_remote_logging_with_hdfs_handler(self):
|
365 | 379 | airflow.logging_config.configure_logging()
|
366 | 380 |
|
367 | 381 | assert isinstance(airflow.logging_config.REMOTE_TASK_LOG, HdfsRemoteLogIO)
|
| 382 | + |
| 383 | + @pytest.mark.parametrize( |
| 384 | + "settings_content,module_structure,expected_path", |
| 385 | + [ |
| 386 | + (SETTINGS_FILE_WITH_REMOTE_VARS, None, f"{SETTINGS_DEFAULT_NAME}.LOGGING_CONFIG"), |
| 387 | + ( |
| 388 | + SETTINGS_FILE_WITH_REMOTE_VARS, |
| 389 | + "nested.config.module", |
| 390 | + f"nested.config.module.{SETTINGS_DEFAULT_NAME}.LOGGING_CONFIG", |
| 391 | + ), |
| 392 | + ], |
| 393 | + ) |
| 394 | + def test_load_logging_config_module_paths( |
| 395 | + self, settings_content: str, module_structure: str, expected_path: str |
| 396 | + ): |
| 397 | + """Test that load_logging_config works with different module path structures""" |
| 398 | + dir_structure = module_structure.replace(".", "/") if module_structure else None |
| 399 | + |
| 400 | + with settings_context(settings_content, dir_structure): |
| 401 | + from airflow.logging_config import load_logging_config |
| 402 | + |
| 403 | + logging_config, logging_class_path = load_logging_config() |
| 404 | + self._verify_basic_logging_config(logging_config, logging_class_path, expected_path) |
| 405 | + |
| 406 | + def test_load_logging_config_fallback_behavior(self): |
| 407 | + """Test that load_logging_config falls back gracefully when remote logging vars are missing""" |
| 408 | + with settings_context(SETTINGS_FILE_VALID): |
| 409 | + from airflow.logging_config import load_logging_config |
| 410 | + |
| 411 | + with patch("airflow.logging_config.log") as mock_log: |
| 412 | + logging_config, logging_class_path = load_logging_config() |
| 413 | + |
| 414 | + self._verify_basic_logging_config( |
| 415 | + logging_config, logging_class_path, f"{SETTINGS_DEFAULT_NAME}.LOGGING_CONFIG" |
| 416 | + ) |
| 417 | + |
| 418 | + mock_log.info.mock_calls = [ |
| 419 | + call( |
| 420 | + "Remote task logs will not be available due to an error: %s", |
| 421 | + ) |
| 422 | + ] |
0 commit comments