|
28 | 28 | from google.adk.events.event_actions import EventActions |
29 | 29 | from google.adk.sessions.base_session_service import BaseSessionService |
30 | 30 | from google.adk.sessions.in_memory_session_service import InMemorySessionService |
| 31 | +from google.api_core import exceptions as api_exceptions |
31 | 32 | from google.api_core import operation as ga_operation |
32 | 33 | from google.auth import credentials as auth_credentials |
33 | 34 | from google.auth.transport import mtls |
@@ -1195,6 +1196,37 @@ def test_project_id_without_project( |
1195 | 1196 | assert app.project_id() is None |
1196 | 1197 | get_project_id_mock.assert_not_called() |
1197 | 1198 |
|
| 1199 | + @pytest.mark.parametrize( |
| 1200 | + "error", |
| 1201 | + [ |
| 1202 | + api_exceptions.PermissionDenied("denied"), |
| 1203 | + api_exceptions.Unauthenticated("unauthenticated"), |
| 1204 | + # b/561814776: gRPC UNKNOWN, seen when an Agent Gateway terminates |
| 1205 | + # the HTTP/2 stream to cloudresourcemanager. |
| 1206 | + api_exceptions.Unknown("Stream removed"), |
| 1207 | + api_exceptions.DeadlineExceeded("deadline exceeded"), |
| 1208 | + RuntimeError("something unexpected"), |
| 1209 | + ], |
| 1210 | + ) |
| 1211 | + def test_project_id_fails_open_on_lookup_error( |
| 1212 | + self, |
| 1213 | + get_project_id_mock: mock.Mock, |
| 1214 | + error: Exception, |
| 1215 | + ): |
| 1216 | + """A failed lookup falls back to the project number instead of raising. |
| 1217 | +
|
| 1218 | + project_id() runs from set_up() on the cold-start path, so an escaping |
| 1219 | + exception kills the worker over a lookup nothing depends on. |
| 1220 | + """ |
| 1221 | + app = agent_engines.AdkApp(agent=_TEST_AGENT) |
| 1222 | + app._tmpl_attrs["project"] = _TEST_PROJECT_NUMBER |
| 1223 | + # Discard the lookup that vertexai.init() made in setup_method. |
| 1224 | + get_project_id_mock.reset_mock() |
| 1225 | + get_project_id_mock.side_effect = error |
| 1226 | + |
| 1227 | + assert app.project_id() == _TEST_PROJECT_NUMBER |
| 1228 | + get_project_id_mock.assert_called_once_with(_TEST_PROJECT_NUMBER) |
| 1229 | + |
1198 | 1230 | @pytest.mark.usefixtures("caplog") |
1199 | 1231 | def test_enable_tracing( |
1200 | 1232 | self, |
|
0 commit comments