1515# limitations under the License.
1616#
1717
18- import os
19-
2018import mock
2119import pytest # type: ignore
2220import requests
@@ -35,23 +33,21 @@ def mock_mds_mtls_config():
3533@mock .patch ("os.name" , "nt" )
3634def test__MdsMtlsConfig_windows_defaults ():
3735 config = _mtls .MdsMtlsConfig ()
38- assert config .ca_cert_path == os .path .join (
39- "C:\\ " , "ProgramData" , "Google" , "ComputeEngine" , "mds-mtls-root.crt"
36+ assert (
37+ str (config .ca_cert_path )
38+ == "C:/ProgramData/Google/ComputeEngine/mds-mtls-root.crt"
4039 )
41- assert config .client_combined_cert_path == os .path .join (
42- "C:\\ " , "ProgramData" , "Google" , "ComputeEngine" , "mds-mtls-client.key"
40+ assert (
41+ str (config .client_combined_cert_path )
42+ == "C:/ProgramData/Google/ComputeEngine/mds-mtls-client.key"
4343 )
4444
4545
4646@mock .patch ("os.name" , "posix" )
4747def test__MdsMtlsConfig_non_windows_defaults ():
4848 config = _mtls .MdsMtlsConfig ()
49- assert config .ca_cert_path == os .path .join (
50- "/" , "run" , "google-mds-mtls" , "root.crt"
51- )
52- assert config .client_combined_cert_path == os .path .join (
53- "/" , "run" , "google-mds-mtls" , "client.key"
54- )
49+ assert str (config .ca_cert_path ) == "/run/google-mds-mtls/root.crt"
50+ assert str (config .client_combined_cert_path ) == "/run/google-mds-mtls/client.key"
5551
5652
5753def test__parse_mds_mode_default (monkeypatch ):
@@ -157,7 +153,7 @@ def test_mds_mtls_adapter_session_request(mock_ssl_context, mock_mds_mtls_config
157153 session = requests .Session ()
158154 session .mount ("https://" , adapter )
159155
160- # Mock the adapter's send method to avoid actual network requests
156+ # Mock the adapter\ 's send method to avoid actual network requests
161157 adapter .send = mock .Mock ()
162158 response = requests .Response ()
163159 response .status_code = 200
@@ -169,3 +165,67 @@ def test_mds_mtls_adapter_session_request(mock_ssl_context, mock_mds_mtls_config
169165 # Assert that the request was successful
170166 assert response .status_code == 200
171167 adapter .send .assert_called_once ()
168+
169+
170+ @mock .patch ("google.auth.compute_engine._mtls.HTTPAdapter" )
171+ @mock .patch ("google.auth.compute_engine._mtls._parse_mds_mode" )
172+ @mock .patch ("ssl.create_default_context" )
173+ def test_mds_mtls_adapter_send_fallback_default_mode (
174+ mock_ssl_context , mock_parse_mds_mode , mock_http_adapter_class , mock_mds_mtls_config
175+ ):
176+ mock_parse_mds_mode .return_value = _mtls .MdsMtlsMode .DEFAULT
177+ adapter = _mtls .MdsMtlsAdapter (mock_mds_mtls_config )
178+
179+ mock_fallback_send = mock .Mock ()
180+ mock_http_adapter_class .return_value .send = mock_fallback_send
181+
182+ # Simulate SSLError on the super().send() call
183+ with mock .patch (
184+ "requests.adapters.HTTPAdapter.send" , side_effect = requests .exceptions .SSLError
185+ ):
186+ request = requests .Request (method = "GET" , url = "https://example.com" ).prepare ()
187+ adapter .send (request )
188+
189+ # Check that fallback to HTTPAdapter.send occurred
190+ mock_http_adapter_class .assert_called_once ()
191+ mock_fallback_send .assert_called_once ()
192+ fallback_request = mock_fallback_send .call_args [0 ][0 ]
193+ assert fallback_request .url == "http://example.com/"
194+
195+
196+ @mock .patch ("google.auth.compute_engine._mtls._parse_mds_mode" )
197+ @mock .patch ("ssl.create_default_context" )
198+ def test_mds_mtls_adapter_send_no_fallback_strict_mode (
199+ mock_ssl_context , mock_parse_mds_mode , mock_mds_mtls_config
200+ ):
201+ mock_parse_mds_mode .return_value = _mtls .MdsMtlsMode .STRICT
202+ adapter = _mtls .MdsMtlsAdapter (mock_mds_mtls_config )
203+
204+ # Simulate SSLError on the super().send() call
205+ with mock .patch (
206+ "requests.adapters.HTTPAdapter.send" , side_effect = requests .exceptions .SSLError
207+ ):
208+ request = requests .Request (method = "GET" , url = "https://example.com" ).prepare ()
209+ with pytest .raises (requests .exceptions .SSLError ):
210+ adapter .send (request )
211+
212+
213+ @mock .patch ("requests.adapters.HTTPAdapter.send" )
214+ @mock .patch ("google.auth.compute_engine._mtls._parse_mds_mode" )
215+ @mock .patch ("ssl.create_default_context" )
216+ def test_mds_mtls_adapter_send_no_fallback_other_exception (
217+ mock_ssl_context , mock_parse_mds_mode , mock_http_adapter_send , mock_mds_mtls_config
218+ ):
219+ mock_parse_mds_mode .return_value = _mtls .MdsMtlsMode .DEFAULT
220+ adapter = _mtls .MdsMtlsAdapter (mock_mds_mtls_config )
221+
222+ # Simulate a different exception
223+ with mock .patch (
224+ "requests.adapters.HTTPAdapter.send" ,
225+ side_effect = requests .exceptions .ConnectionError ,
226+ ):
227+ request = requests .Request (method = "GET" , url = "https://example.com" ).prepare ()
228+ with pytest .raises (requests .exceptions .ConnectionError ):
229+ adapter .send (request )
230+
231+ mock_http_adapter_send .assert_not_called ()
0 commit comments