File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed
Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 5858except ValueError : # pragma: NO COVER
5959 _METADATA_DEFAULT_TIMEOUT = 3
6060
61+ # The number of tries to perform when waiting for the GCE metadata server
62+ # when detecting the GCE environment.
63+ try :
64+ _METADATA_DETECT_RETRIES = int (
65+ os .getenv (environment_vars .GCE_METADATA_DETECT_RETRIES , "3" )
66+ )
67+ except ValueError : # pragma: NO COVER
68+ _METADATA_DETECT_RETRIES = 3
69+
6170# Detect GCE Residency
6271_GOOGLE = "Google"
6372_GCE_PRODUCT_NAME_FILE = "/sys/class/dmi/id/product_name"
@@ -100,7 +109,9 @@ def detect_gce_residency_linux():
100109 return content .startswith (_GOOGLE )
101110
102111
103- def ping (request , timeout = _METADATA_DEFAULT_TIMEOUT , retry_count = 3 ):
112+ def ping (
113+ request , timeout = _METADATA_DEFAULT_TIMEOUT , retry_count = _METADATA_DETECT_RETRIES
114+ ):
104115 """Checks to see if the metadata server is available.
105116
106117 Args:
Original file line number Diff line number Diff line change 6060"""Environment variable providing an alternate ip:port to be used for ip-only
6161GCE metadata requests."""
6262
63+ GCE_METADATA_DETECT_RETRIES = "GCE_METADATA_DETECT_RETRIES"
64+ """Environment variable representing the number of retries that should be
65+ attempted on metadata lookup.
66+ """
67+
6368GOOGLE_API_USE_CLIENT_CERTIFICATE = "GOOGLE_API_USE_CLIENT_CERTIFICATE"
6469"""Environment variable controlling whether to use client certificate or not.
6570
Original file line number Diff line number Diff line change @@ -160,6 +160,23 @@ def test_ping_success_custom_root(mock_metrics_header_value):
160160 )
161161
162162
163+ @mock .patch ("google.auth.metrics.mds_ping" , return_value = MDS_PING_METRICS_HEADER_VALUE )
164+ def test_ping_failure_custom_retry (mock_metrics_header_value ):
165+ request = make_request ("" )
166+ request .side_effect = exceptions .TransportError ()
167+
168+ os .environ [environment_vars .GCE_METADATA_DETECT_RETRIES ] = "10"
169+ importlib .reload (_metadata )
170+
171+ try :
172+ _metadata .ping (request )
173+ finally :
174+ del os .environ [environment_vars .GCE_METADATA_DETECT_RETRIES ]
175+ importlib .reload (_metadata )
176+
177+ assert request .call_count == 10
178+
179+
163180def test_get_success_json ():
164181 key , value = "foo" , "bar"
165182
You can’t perform that action at this time.
0 commit comments