Skip to content

test: fix mTLS auto-enablement unit tests - #2792

Merged
parthea merged 1 commit into
mainfrom
fix-build
Aug 20, 2026
Merged

test: fix mTLS auto-enablement unit tests#2792
parthea merged 1 commit into
mainfrom
fix-build

Conversation

@parthea

@parthea parthea commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

This PR fixes unit test failures in tests/test_discovery.py . See the failure in #2791 as an example

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/test_discovery.py:976: in test_mtls_with_provided_client_cert_unset_environment_variable
    self.assertEqual(plus._baseUrl, base_url)
E   AssertionError: 'https://www.googleapis.com/plus/v1/' != 'https://www.mtls.googleapis.com/plus/v1/'
E   - https://www.googleapis.com/plus/v1/
E   + https://www.mtls.googleapis.com/plus/v1/
E   ? 
  • Clear CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: WhenGOOGLE_API_USE_CLIENT_CERTIFICATE is unset, google-auth falls back to CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE. Clearing it ensures pre-existing "true" values in Cloud SDK environments do not bypass the auto-discovery of the mock certificate.
  • Mock os.path.exists: Add mock.patch("os.path.exists", return_value=True) so google-auth detects the mock certificate config file before opening it.

@parthea
parthea requested a review from a team as a code owner August 20, 2026 19:14
@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Aug 20, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates mTLS tests in tests/test_discovery.py to clear the CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE environment variable, preventing it from bypassing auto-discovery of the certificate configuration. It also mocks os.path.exists to return True. The feedback suggests refining the global mock of os.path.exists to only return True for the specific configuration file path, avoiding potential side effects on other file checks within the test suite.

Comment thread tests/test_discovery.py
Comment on lines +971 to +984
with mock.patch("os.path.exists", return_value=True):
with mock.patch.dict(
"os.environ",
{"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
):
plus = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
client_options={
"client_encrypted_cert_source": self.client_encrypted_cert_source
},
)
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Mocking os.path.exists globally to always return True can cause unexpected side effects if other parts of the test or standard library rely on it to verify the existence of real files. A safer and more robust approach is to use a side_effect that only returns True for the specific mock configuration file (config_filename) and delegates to the original os.path.exists for all other paths.

                        original_exists = os.path.exists
                        with mock.patch(
                            "os.path.exists",
                            side_effect=lambda p: True if p == config_filename else original_exists(p),
                        ):
                            with mock.patch.dict(
                                "os.environ",
                                {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
                            ):
                                plus = build_from_document(
                                    discovery,
                                    credentials=self.MOCK_CREDENTIALS,
                                    client_options={
                                        "client_encrypted_cert_source": self.client_encrypted_cert_source
                                    },
                                )
                                self.assertIsNotNone(plus)
                                self.assertEqual(plus._baseUrl, base_url)

Comment thread tests/test_discovery.py
Comment on lines +1127 to +1139
with mock.patch("os.path.exists", return_value=True):
with mock.patch.dict(
"os.environ",
{"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
):
plus = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
adc_cert_path=self.ADC_CERT_PATH,
adc_key_path=self.ADC_KEY_PATH,
)
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Mocking os.path.exists globally to always return True can cause unexpected side effects if other parts of the test or standard library rely on it to verify the existence of real files. A safer and more robust approach is to use a side_effect that only returns True for the specific mock configuration file (config_filename) and delegates to the original os.path.exists for all other paths.

                        original_exists = os.path.exists
                        with mock.patch(
                            "os.path.exists",
                            side_effect=lambda p: True if p == config_filename else original_exists(p),
                        ):
                            with mock.patch.dict(
                                "os.environ",
                                {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
                            ):
                                plus = build_from_document(
                                    discovery,
                                    credentials=self.MOCK_CREDENTIALS,
                                    adc_cert_path=self.ADC_CERT_PATH,
                                    adc_key_path=self.ADC_KEY_PATH,
                                )
                                self.assertIsNotNone(plus)
                                self.assertEqual(plus._baseUrl, base_url)

@parthea
parthea merged commit 829a677 into main Aug 20, 2026
17 checks passed
@parthea
parthea deleted the fix-build branch August 20, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants