Filter logs for specific logger in TestGetGcpCredentialsAndProjectId.test_disable_logging#26973
Merged
dstandish merged 3 commits intoapache:mainfrom Oct 10, 2022
Conversation
….test_disable_logging`
Contributor
Author
|
cc: @dstandish |
dstandish
approved these changes
Oct 10, 2022
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
dstandish
reviewed
Oct 10, 2022
| disable_logging=True, | ||
| ) | ||
| assert logs.output == ['DEBUG:root:nothing'] | ||
| assert_no_logs(logs.records) |
Contributor
There was a problem hiding this comment.
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
Suggested change
| 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}") | |
Contributor
There was a problem hiding this comment.
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,
)
This was referenced Dec 27, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.