From a433b2dbef61ffcafb5f793a12a15af46995edbc Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Tue, 4 Oct 2022 15:05:42 -0700 Subject: [PATCH 1/2] Fix flakey test test_disable_logging We expected an AssertionError arising from `self.assertLogs`, which means we expected that no logs would be emitted. The problem is, when this fails, we don't know why -- we don't know what was logged that we did not expect to be logged. We can improve this test by capturing the logs, then asserting that the logs captured are only the ones emitted explicitly in the test code (assertLogs will fail if we don't insert a dummy log record). --- .../cloud/utils/test_credentials_provider.py | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/providers/google/cloud/utils/test_credentials_provider.py b/tests/providers/google/cloud/utils/test_credentials_provider.py index 7a67af9a0e080..7a3cdd2457ac3 100644 --- a/tests/providers/google/cloud/utils/test_credentials_provider.py +++ b/tests/providers/google/cloud/utils/test_credentials_provider.py @@ -17,6 +17,7 @@ from __future__ import annotations import json +import logging import os import re import unittest @@ -327,22 +328,37 @@ def test_get_credentials_and_project_id_with_mutually_exclusive_configuration( ) def test_disable_logging(self, mock_default, mock_info, mock_file): # assert not logs - with pytest.raises(AssertionError), self.assertLogs(level="DEBUG"): + with self.assertLogs(level="DEBUG") as logs: + logging.debug('nothing') get_credentials_and_project_id(disable_logging=True) + assert logs.output == ['DEBUG:root:nothing'] - # assert not logs - with pytest.raises(AssertionError), self.assertLogs(level="DEBUG"): + # assert no debug logs emitted from get_credentials_and_project_id + with self.assertLogs(level="DEBUG") as logs: + logging.debug('nothing') get_credentials_and_project_id( keyfile_dict={'private_key': 'PRIVATE_KEY'}, disable_logging=True, ) + assert logs.output == ['DEBUG:root:nothing'] - # assert not logs - with pytest.raises(AssertionError), self.assertLogs(level="DEBUG"): + # assert no debug logs emitted from get_credentials_and_project_id + with self.assertLogs(level="DEBUG") as logs: + logging.debug('nothing') + get_credentials_and_project_id( + key_path='KEY.json', + disable_logging=True, + ) + assert logs.output == ['DEBUG:root:nothing'] + + # assert no debug logs emitted from get_credentials_and_project_id + with self.assertLogs(level="DEBUG") as logs: + logging.debug('nothing') get_credentials_and_project_id( key_path='KEY.json', disable_logging=True, ) + assert logs.output == ['DEBUG:root:nothing'] class TestGetScopes(unittest.TestCase): From ad2c1701c793edc261454d2f70b0579f46bf82f3 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Tue, 4 Oct 2022 15:15:17 -0700 Subject: [PATCH 2/2] Update tests/providers/google/cloud/utils/test_credentials_provider.py Co-authored-by: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> --- .../google/cloud/utils/test_credentials_provider.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/providers/google/cloud/utils/test_credentials_provider.py b/tests/providers/google/cloud/utils/test_credentials_provider.py index 7a3cdd2457ac3..58e97d3926e26 100644 --- a/tests/providers/google/cloud/utils/test_credentials_provider.py +++ b/tests/providers/google/cloud/utils/test_credentials_provider.py @@ -351,15 +351,6 @@ def test_disable_logging(self, mock_default, mock_info, mock_file): ) assert logs.output == ['DEBUG:root:nothing'] - # assert no debug logs emitted from get_credentials_and_project_id - with self.assertLogs(level="DEBUG") as logs: - logging.debug('nothing') - get_credentials_and_project_id( - key_path='KEY.json', - disable_logging=True, - ) - assert logs.output == ['DEBUG:root:nothing'] - class TestGetScopes(unittest.TestCase): def test_get_scopes_with_default(self):