Skip to content

Commit

Permalink
Yet another attempt to fix flakey test_disable_logging test (#36455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis authored Dec 27, 2023
1 parent 949fc57 commit 71314b8
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions tests/providers/google/cloud/utils/test_credentials_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import logging
import os
import re
from contextlib import contextmanager
from io import StringIO
from tempfile import NamedTemporaryFile
from unittest import mock
Expand Down Expand Up @@ -55,6 +56,31 @@
CRED_PROVIDER_LOGGER_NAME = "airflow.providers.google.cloud.utils.credentials_provider._CredentialProvider"


@pytest.fixture
def assert_no_logs(caplog):
"""
Helper fixture for assert if any log message for the specific logger captured.
This is workaround for fix issue with asyncio in ``test_disable_logging``, see:
- https://github.com/apache/airflow/pull/26871
- https://github.com/apache/airflow/pull/26973
- https://github.com/apache/airflow/pull/36453
"""

@contextmanager
def wrapper(level: str, logger: str):
with caplog.at_level(level=level, logger=logger):
caplog.clear()
yield
if records := list(filter(lambda lr: lr[0] == logger, caplog.record_tuples)):
msg = f"Did not expect any log message from logger={logger!r} but got:"
for log_record in records:
msg += f"\n * logger name: {log_record[0]!r}, level: {log_record[1]}, msg: {log_record[2]!r}"
raise AssertionError(msg)

return wrapper


class TestHelper:
def test_build_gcp_conn_path(self):
value = "test"
Expand Down Expand Up @@ -364,32 +390,26 @@ def test_get_credentials_and_project_id_with_mutually_exclusive_configuration(se
@mock.patch(
"google.oauth2.service_account.Credentials.from_service_account_file",
)
def test_disable_logging(self, mock_default, mock_info, mock_file, caplog):
def test_disable_logging(self, mock_default, mock_info, mock_file, assert_no_logs):
"""Test disable logging in ``get_credentials_and_project_id``"""

# assert no logs
with caplog.at_level(level=logging.DEBUG, logger=CRED_PROVIDER_LOGGER_NAME):
caplog.clear()
with assert_no_logs(level="DEBUG", logger=CRED_PROVIDER_LOGGER_NAME):
get_credentials_and_project_id(disable_logging=True)
assert not caplog.record_tuples

# assert no debug logs emitted from get_credentials_and_project_id
with caplog.at_level(level=logging.DEBUG, logger=CRED_PROVIDER_LOGGER_NAME):
caplog.clear()
with assert_no_logs(level="DEBUG", logger=CRED_PROVIDER_LOGGER_NAME):
get_credentials_and_project_id(
keyfile_dict={"private_key": "PRIVATE_KEY"},
disable_logging=True,
)
assert not caplog.record_tuples

# assert no debug logs emitted from get_credentials_and_project_id
with caplog.at_level(level=logging.DEBUG, logger=CRED_PROVIDER_LOGGER_NAME):
caplog.clear()
with assert_no_logs(level="DEBUG", logger=CRED_PROVIDER_LOGGER_NAME):
get_credentials_and_project_id(
key_path="KEY.json",
disable_logging=True,
)
assert not caplog.record_tuples


class TestGetScopes:
Expand Down

0 comments on commit 71314b8

Please sign in to comment.