Skip to content

Commit

Permalink
fixup! Add info about JSON Connection format for AWS SSM Parameter St…
Browse files Browse the repository at this point in the history
…ore Secrets Backend (apache#27134)
  • Loading branch information
ashb committed Nov 10, 2022
1 parent 116e380 commit d7f50d8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
4 changes: 3 additions & 1 deletion airflow/utils/log/logging_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def set_context(logger, value):
:param value: value to set
"""
while logger:
orig_propagate = logger.propagate
for handler in logger.handlers:
# Not all handlers need to have context passed in so we ignore
# the error when handlers do not have set_context defined.
Expand All @@ -214,7 +215,8 @@ def set_context(logger, value):
# explicitly asks us to keep it on.
if flag is not SetContextPropagate.MAINTAIN_PROPAGATE:
logger.propagate = False
if logger.propagate is True:
if orig_propagate is True:
# If we were set to propagate before we turned if off, then keep passing set_context up
logger = logger.parent
else:
break
74 changes: 46 additions & 28 deletions tests/utils/test_logging_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,54 @@
from airflow.utils.log.logging_mixin import SetContextPropagate, StreamLogWriter, set_context


@pytest.fixture
def logger():
parent = logging.getLogger(__name__)
parent.propagate = False
yield parent

parent.propagate = True


@pytest.fixture
def child_logger(logger):
yield logger.getChild("child")


@pytest.fixture
def parent_child_handlers(child_logger):
parent_handler = logging.NullHandler()
parent_handler.handle = mock.MagicMock(name="parent_handler.handle")

child_handler = logging.NullHandler()
child_handler.handle = mock.MagicMock(name="handler.handle")

logger = child_logger.parent
logger.addHandler(parent_handler)

child_logger.addHandler(child_handler),
child_logger.propagate = True

yield parent_handler, child_handler

logger.removeHandler(parent_handler)
child_logger.removeHandler(child_handler)


class TestLoggingMixin:
def setup_method(self):
warnings.filterwarnings(action="always")

def test_set_context(self):
handler1 = mock.MagicMock()
handler2 = mock.MagicMock()
parent = mock.MagicMock()
def test_set_context(self, child_logger, parent_child_handlers):
handler1, handler2 = parent_child_handlers
handler1.set_context = mock.MagicMock()
handler2.set_context = mock.MagicMock()

parent = logging.getLogger(__name__)
parent.propagate = False
parent.handlers = [
handler1,
]
log = mock.MagicMock()
log.handlers = [
handler2,
]
log.parent = parent
parent.addHandler(handler1)
log = parent.getChild("child")
log.addHandler(handler2),
log.propagate = True

value = "test"
Expand Down Expand Up @@ -112,24 +143,11 @@ def test_iobase_compatibility(self):


@pytest.mark.parametrize(["maintain_propagate"], [[SetContextPropagate.MAINTAIN_PROPAGATE], [None]])
def test_set_context_propagation(maintain_propagate):
def test_set_context_propagation(parent_child_handlers, child_logger, maintain_propagate):
# Test the behaviour of set_context and logger propagation and the MAINTAIN_PROPAGATE return
class CustomHandler(logging.Handler):
def set_context(self, context):
return maintain_propagate

parent_handler = logging.NullHandler()
parent_handler.handle = mock.MagicMock(name="parent_handler.handle")

handler = CustomHandler()
handler.handle = mock.MagicMock(name="handler.handle")

parent_logger = logging.getLogger(__name__)
parent_logger.addHandler(parent_handler)

child_logger = parent_logger.getChild("child")
child_logger.propagate = True
child_logger.addHandler(handler)
parent_handler, handler = parent_child_handlers
handler.set_context = mock.MagicMock(return_value=maintain_propagate)

# Before settting_context, ensure logs make it to the parent
line = sys._getframe().f_lineno + 1
Expand Down

0 comments on commit d7f50d8

Please sign in to comment.