|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2025 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +"""System tests for reasoning engines.""" |
| 18 | +import pytest |
| 19 | +import time |
| 20 | +import sys |
| 21 | +import datetime |
| 22 | +from google import auth |
| 23 | +import google.cloud.aiplatform |
| 24 | +import vertexai |
| 25 | +from google.cloud import trace_v1 |
| 26 | +from google.adk.sessions import in_memory_session_service |
| 27 | +from google.protobuf.timestamp_pb2 import Timestamp |
| 28 | +from tests.system.aiplatform import e2e_base |
| 29 | +from vertexai import agent_engines |
| 30 | + |
| 31 | +_BLOB_FILENAME = agent_engines._agent_engines._BLOB_FILENAME |
| 32 | +_CANNED_AGENT_RESPONSE = "Hello agent" |
| 33 | + |
| 34 | + |
| 35 | +# Class definition needs to be in a function, to not pull in entire testing module as a dependency, when pickling this class. |
| 36 | +def adk_agent_no_dependencies(): |
| 37 | + from google.adk.agents import base_agent |
| 38 | + |
| 39 | + class AdkAgentNoDependencies(base_agent.BaseAgent): |
| 40 | + async def _run_async_impl(self, ctx): |
| 41 | + from google.adk.events import event |
| 42 | + from google.genai import types |
| 43 | + |
| 44 | + yield event.Event( |
| 45 | + invocation_id=ctx.invocation_id, |
| 46 | + author="agent", |
| 47 | + content=types.Content( |
| 48 | + role="agent", parts=[types.Part(text=_CANNED_AGENT_RESPONSE)] |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + return AdkAgentNoDependencies(name="test_agent") |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.usefixtures( |
| 56 | + "prepare_staging_bucket", "delete_staging_bucket", "tear_down_resources" |
| 57 | +) |
| 58 | +class TestAgentEngines(e2e_base.TestEndToEnd): |
| 59 | + """System tests for reasoning engines.""" |
| 60 | + |
| 61 | + _temp_prefix = "test-reasoning-engine" |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_adk_template(self, shared_state): |
| 65 | + # Avoid import errors template when pickling the template. |
| 66 | + sys.modules["google.cloud.aiplatform.aiplatform"] = google.cloud.aiplatform |
| 67 | + super().setup_method() |
| 68 | + credentials, _ = auth.default( |
| 69 | + scopes=["https://www.googleapis.com/auth/cloud-platform"] |
| 70 | + ) |
| 71 | + vertexai.init( |
| 72 | + project=e2e_base._PROJECT, |
| 73 | + location=e2e_base._LOCATION, |
| 74 | + staging_bucket=f"gs://{shared_state['staging_bucket_name']}", |
| 75 | + credentials=credentials, |
| 76 | + ) |
| 77 | + |
| 78 | + app = agent_engines.AdkApp( |
| 79 | + agent=adk_agent_no_dependencies(), |
| 80 | + enable_tracing=True, |
| 81 | + session_service_builder=in_memory_session_service.InMemorySessionService, |
| 82 | + ) |
| 83 | + agent = agent_engines.AgentEngine.create( |
| 84 | + agent_engine=app, |
| 85 | + requirements=["google-cloud-aiplatform[agent_engines,adk]"], |
| 86 | + display_name="test-display-name", |
| 87 | + description="test-description", |
| 88 | + gcs_dir_name="test-gcs-dir-name", |
| 89 | + ) |
| 90 | + shared_state.setdefault("resources", []) |
| 91 | + shared_state["resources"].append(agent) # Deletion at teardown. |
| 92 | + |
| 93 | + resp = await agent.async_stream_query( |
| 94 | + message="Hello", user_id="test-user" |
| 95 | + ).__anext__() |
| 96 | + assert resp["content"]["parts"][0]["text"] == _CANNED_AGENT_RESPONSE |
| 97 | + |
| 98 | + traces = [] |
| 99 | + trace_query_attempts = 10 |
| 100 | + trace_query_end_time = datetime.datetime.now( |
| 101 | + datetime.timezone.utc |
| 102 | + ) + datetime.timedelta(minutes=1) |
| 103 | + trace_query_start_time = trace_query_end_time - datetime.timedelta(minutes=2) |
| 104 | + trace_client = trace_v1.TraceServiceClient() |
| 105 | + for _ in range(trace_query_attempts): |
| 106 | + traces = trace_client.list_traces( |
| 107 | + request=trace_v1.ListTracesRequest( |
| 108 | + project_id=e2e_base._PROJECT, |
| 109 | + start_time=Timestamp().FromDatetime(dt=trace_query_start_time), |
| 110 | + end_time=Timestamp().FromDatetime(dt=trace_query_end_time), |
| 111 | + ) |
| 112 | + ) |
| 113 | + traces = list(traces) |
| 114 | + if len(traces) > 0: |
| 115 | + break |
| 116 | + time.sleep(5) |
| 117 | + |
| 118 | + assert len(traces) > 0 |
0 commit comments