-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Filter logs for specific logger in TestGetGcpCredentialsAndProjectId.test_disable_logging
#26973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filter logs for specific logger in TestGetGcpCredentialsAndProjectId.test_disable_logging
#26973
Conversation
….test_disable_logging`
|
cc: @dstandish |
tests/providers/google/cloud/utils/test_credentials_provider.py
Outdated
Show resolved
Hide resolved
tests/providers/google/cloud/utils/test_credentials_provider.py
Outdated
Show resolved
Hide resolved
3fbe839 to
c32979f
Compare
| disable_logging=True, | ||
| ) | ||
| assert logs.output == ['DEBUG:root:nothing'] | ||
| assert_no_logs(logs.records) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Taragolis , building off your work, i got inspired, and figured out we could wrap this logic in a context mgr and make it all even cleaner.
No obligation to accept it but here is the code
| assert_no_logs(logs.records) | |
| assert_no_logs(logs.records) | |
| @contextmanager | |
| def assert_no_logs(self, name, level): | |
| with self.assertLogs(level=level) as logs: | |
| logging.log(level=level, msg='nothing') | |
| yield | |
| records = [log_record for log_record in logs.records if log_record.name == name] | |
| if not records: | |
| return | |
| raise AssertionError(f"Did not expect any log message from logger={name!r}, but got: {records}") | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then the code becomes this:
# assert no logs
with self.assert_no_logs(name=logger_name, level="DEBUG") as logs:
get_credentials_and_project_id(disable_logging=True)
# assert no debug logs emitted from get_credentials_and_project_id
with self.assert_no_logs(name=logger_name, level="DEBUG") as logs:
get_credentials_and_project_id(
keyfile_dict={'private_key': 'PRIVATE_KEY'},
disable_logging=True,
)
# assert no debug logs emitted from get_credentials_and_project_id
with self.assert_no_logs(name=logger_name, level="DEBUG") as logs:
get_credentials_and_project_id(
key_path='KEY.json',
disable_logging=True,
)
TestGetGcpCredentialsAndProjectId.test_disable_loggingtime to time failed with selected errorDue to the code of
airflow.providers.google.cloud.utils.credentials_provider._CredentialProviderlogging should disable only within the class, soasyncioerror log not related to disabling log.Add some workaround for check only logs with specific logger.