@@ -273,6 +273,64 @@ def test_get_agent_identity_certificate_path_retry(
273273
274274 assert mock_sleep .call_count == len (_agent_identity_utils ._POLLING_INTERVALS )
275275
276+ @mock .patch ("time.sleep" )
277+ @mock .patch ("google.auth._agent_identity_utils._is_certificate_file_ready" )
278+ def test_get_agent_identity_certificate_path_retry_success (
279+ self , mock_is_ready , mock_sleep , tmpdir , monkeypatch
280+ ):
281+ monkeypatch .setattr (
282+ "google.auth._agent_identity_utils._WELL_KNOWN_CERT_PATH" ,
283+ str (tmpdir .join ("certificates.pem" )),
284+ )
285+ cert_path_str = str (tmpdir .join ("cert.pem" ))
286+ config_path = tmpdir .join ("config.json" )
287+ config_path .write (
288+ json .dumps ({"cert_configs" : {"workload" : {"cert_path" : cert_path_str }}})
289+ )
290+ monkeypatch .setenv (
291+ environment_vars .GOOGLE_API_CERTIFICATE_CONFIG , str (config_path )
292+ )
293+
294+ # First attempt: file missing/not ready. Second attempt: succeeds.
295+ mock_is_ready .side_effect = [False , True ]
296+
297+ result = _agent_identity_utils .get_agent_identity_certificate_path ()
298+
299+ assert result == cert_path_str
300+ assert mock_sleep .call_count == 1
301+ assert mock_is_ready .call_count == 2
302+
303+ @mock .patch ("time.sleep" )
304+ @mock .patch ("google.auth._agent_identity_utils._is_certificate_file_ready" )
305+ def test_get_agent_identity_certificate_path_config_retry_success (
306+ self , mock_is_ready , mock_sleep , tmpdir , monkeypatch
307+ ):
308+ monkeypatch .setattr (
309+ "google.auth._agent_identity_utils._WELL_KNOWN_CERT_PATH" ,
310+ str (tmpdir .join ("certificates.pem" )),
311+ )
312+ cert_path_str = str (tmpdir .join ("cert.pem" ))
313+ config_path = tmpdir .join ("config.json" )
314+ monkeypatch .setenv (
315+ environment_vars .GOOGLE_API_CERTIFICATE_CONFIG , str (config_path )
316+ )
317+
318+ mock_is_ready .return_value = True
319+
320+ def write_config (* args , ** kwargs ):
321+ config_path .write (
322+ json .dumps ({"cert_configs" : {"workload" : {"cert_path" : cert_path_str }}})
323+ )
324+
325+ # First attempt: config file missing. Sleep side effect creates it.
326+ mock_sleep .side_effect = write_config
327+
328+ result = _agent_identity_utils .get_agent_identity_certificate_path ()
329+
330+ assert result == cert_path_str
331+ assert mock_sleep .call_count == 1
332+ assert mock_is_ready .call_count == 1
333+
276334 @mock .patch ("time.sleep" )
277335 @mock .patch ("google.auth._agent_identity_utils.os.path.exists" )
278336 def test_get_agent_identity_certificate_path_failure (
0 commit comments