-
Notifications
You must be signed in to change notification settings - Fork 2.6k
test: fix mTLS auto-enablement unit tests #2792
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -957,23 +957,31 @@ def test_mtls_with_provided_client_cert_unset_environment_variable( | |
| with mock.patch.dict( | ||
| "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} | ||
| ): | ||
| # Clear CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE so its fallback in | ||
| # google-auth (often "true" in Cloud SDK environments) does not bypass | ||
| # certificate_config.json auto-discovery. | ||
| with mock.patch.dict( | ||
| "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} | ||
| "os.environ", | ||
| { | ||
| "GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert, | ||
| "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "", | ||
| }, | ||
| ): | ||
| with mock.patch("builtins.open", m): | ||
| 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) | ||
| 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) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
|
|
@@ -1105,22 +1113,30 @@ def test_mtls_with_default_client_cert_with_unset_environment_variable( | |
| with mock.patch.dict( | ||
| "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} | ||
| ): | ||
| # Clear CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE so its fallback in | ||
| # google-auth (often "true" in Cloud SDK environments) does not bypass | ||
| # certificate_config.json auto-discovery. | ||
| with mock.patch.dict( | ||
| "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} | ||
| "os.environ", | ||
| { | ||
| "GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert, | ||
| "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "", | ||
| }, | ||
| ): | ||
| with mock.patch("builtins.open", m): | ||
| 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) | ||
| 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) | ||
|
Comment on lines
+1127
to
+1139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mocking 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) |
||
|
|
||
| @parameterized.expand( | ||
| [ | ||
|
|
||
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.
Mocking
os.path.existsglobally to always returnTruecan 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 aside_effectthat only returnsTruefor the specific mock configuration file (config_filename) and delegates to the originalos.path.existsfor all other paths.