|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +import os |
| 17 | +import sys |
| 18 | +import tempfile |
| 19 | +from unittest import mock |
| 20 | +import pytest |
| 21 | +import cloudpickle |
| 22 | +import pydantic |
| 23 | + |
| 24 | +from google import auth |
| 25 | +from google.api_core import operation as ga_operation |
| 26 | +from google.auth import credentials as auth_credentials |
| 27 | +from google.cloud import storage |
| 28 | +from google.cloud import aiplatform |
| 29 | +from google.cloud.aiplatform import base |
| 30 | + |
| 31 | +from google.cloud.aiplatform_v1 import types |
| 32 | +from google.cloud.aiplatform_v1.services import reasoning_engine_service |
| 33 | +from vertexai import agent_engines |
| 34 | +from vertexai.agent_engines import _agent_engines |
| 35 | +from vertexai.agent_engines import _utils |
| 36 | +from google.protobuf import struct_pb2 |
| 37 | + |
| 38 | + |
| 39 | +class CapitalizeEngine: |
| 40 | + """A sample Agent Engine.""" |
| 41 | + |
| 42 | + def query(self, unused_arbitrary_string_name: str) -> str: |
| 43 | + """Runs the engine.""" |
| 44 | + return unused_arbitrary_string_name.upper() |
| 45 | + |
| 46 | + |
| 47 | +class CapitalizeEngineWithCard(CapitalizeEngine): |
| 48 | + |
| 49 | + def __init__(self, card): |
| 50 | + self.agent_card = card |
| 51 | + |
| 52 | + def __getstate__(self): |
| 53 | + state = self.__dict__.copy() |
| 54 | + if hasattr(self.agent_card, "DESCRIPTOR"): |
| 55 | + state["agent_card"] = None |
| 56 | + return state |
| 57 | + |
| 58 | + def __setstate__(self, state): |
| 59 | + self.__dict__.update(state) |
| 60 | + |
| 61 | + |
| 62 | +class DummyPydanticCard(pydantic.BaseModel): |
| 63 | + name: str = "test_pydantic_card" |
| 64 | + |
| 65 | + |
| 66 | +def _create_empty_fake_package(package_name: str) -> str: |
| 67 | + temp_dir = tempfile.mkdtemp() |
| 68 | + package_dir = os.path.join(temp_dir, package_name) |
| 69 | + os.makedirs(package_dir) |
| 70 | + init_path = os.path.join(package_dir, "__init__.py") |
| 71 | + open(init_path, "w").close() |
| 72 | + return temp_dir |
| 73 | + |
| 74 | + |
| 75 | +_TEST_CREDENTIALS = mock.Mock(spec=auth_credentials.AnonymousCredentials()) |
| 76 | +_TEST_STAGING_BUCKET = "gs://test-bucket" |
| 77 | +_TEST_LOCATION = "us-central1" |
| 78 | +_TEST_PROJECT = "test-project" |
| 79 | +_TEST_RESOURCE_ID = "1028944691210842416" |
| 80 | +_TEST_PARENT = f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}" |
| 81 | +_TEST_AGENT_ENGINE_RESOURCE_NAME = ( |
| 82 | + f"{_TEST_PARENT}/reasoningEngines/{_TEST_RESOURCE_ID}" |
| 83 | +) |
| 84 | +_TEST_AGENT_ENGINE_DISPLAY_NAME = "Agent Engine Display Name" |
| 85 | +_TEST_GCS_DIR_NAME = _agent_engines._DEFAULT_GCS_DIR_NAME |
| 86 | +_TEST_BLOB_FILENAME = _agent_engines._BLOB_FILENAME |
| 87 | +_TEST_REQUIREMENTS_FILE = _agent_engines._REQUIREMENTS_FILE |
| 88 | +_TEST_EXTRA_PACKAGES_FILE = _agent_engines._EXTRA_PACKAGES_FILE |
| 89 | +_TEST_STANDARD_API_MODE = _agent_engines._STANDARD_API_MODE |
| 90 | +_TEST_DEFAULT_METHOD_NAME = _agent_engines._DEFAULT_METHOD_NAME |
| 91 | +_TEST_MODE_KEY_IN_SCHEMA = _agent_engines._MODE_KEY_IN_SCHEMA |
| 92 | + |
| 93 | +_TEST_AGENT_ENGINE_EXTRA_PACKAGE = "fake.py" |
| 94 | + |
| 95 | +_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH = _create_empty_fake_package( |
| 96 | + _TEST_AGENT_ENGINE_EXTRA_PACKAGE |
| 97 | +) |
| 98 | + |
| 99 | +_TEST_AGENT_ENGINE_REQUIREMENTS = [ |
| 100 | + "google-cloud-aiplatform==1.29.0", |
| 101 | + "langchain", |
| 102 | +] |
| 103 | + |
| 104 | +_TEST_AGENT_ENGINE_GCS_URI = "{}/{}/{}".format( |
| 105 | + _TEST_STAGING_BUCKET, |
| 106 | + _TEST_GCS_DIR_NAME, |
| 107 | + _TEST_BLOB_FILENAME, |
| 108 | +) |
| 109 | +_TEST_AGENT_ENGINE_DEPENDENCY_FILES_GCS_URI = "{}/{}/{}".format( |
| 110 | + _TEST_STAGING_BUCKET, |
| 111 | + _TEST_GCS_DIR_NAME, |
| 112 | + _TEST_EXTRA_PACKAGES_FILE, |
| 113 | +) |
| 114 | +_TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI = "{}/{}/{}".format( |
| 115 | + _TEST_STAGING_BUCKET, |
| 116 | + _TEST_GCS_DIR_NAME, |
| 117 | + _TEST_REQUIREMENTS_FILE, |
| 118 | +) |
| 119 | + |
| 120 | +_TEST_AGENT_ENGINE_QUERY_SCHEMA = _utils.to_proto( |
| 121 | + _utils.generate_schema( |
| 122 | + CapitalizeEngine().query, |
| 123 | + schema_name=_TEST_DEFAULT_METHOD_NAME, |
| 124 | + ) |
| 125 | +) |
| 126 | +_TEST_AGENT_ENGINE_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = _TEST_STANDARD_API_MODE |
| 127 | + |
| 128 | +_TEST_AGENT_ENGINE_PACKAGE_SPEC = types.ReasoningEngineSpec.PackageSpec( |
| 129 | + python_version=f"{sys.version_info.major}.{sys.version_info.minor}", |
| 130 | + pickle_object_gcs_uri=_TEST_AGENT_ENGINE_GCS_URI, |
| 131 | + dependency_files_gcs_uri=_TEST_AGENT_ENGINE_DEPENDENCY_FILES_GCS_URI, |
| 132 | + requirements_gcs_uri=_TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, |
| 133 | +) |
| 134 | + |
| 135 | +_TEST_AGENT_ENGINE_OBJ = types.ReasoningEngine( |
| 136 | + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, |
| 137 | + spec=types.ReasoningEngineSpec( |
| 138 | + package_spec=_TEST_AGENT_ENGINE_PACKAGE_SPEC, |
| 139 | + agent_framework=_agent_engines._DEFAULT_AGENT_FRAMEWORK, |
| 140 | + ), |
| 141 | +) |
| 142 | +_TEST_AGENT_ENGINE_OBJ.spec.class_methods.append(_TEST_AGENT_ENGINE_QUERY_SCHEMA) |
| 143 | + |
| 144 | + |
| 145 | +@pytest.fixture(scope="module") |
| 146 | +def google_auth_mock(): |
| 147 | + with mock.patch.object(auth, "default") as google_auth_mock: |
| 148 | + google_auth_mock.return_value = ( |
| 149 | + auth_credentials.AnonymousCredentials(), |
| 150 | + _TEST_PROJECT, |
| 151 | + ) |
| 152 | + yield google_auth_mock |
| 153 | + |
| 154 | + |
| 155 | +@pytest.fixture(scope="module") |
| 156 | +def cloud_storage_create_bucket_mock(): |
| 157 | + with mock.patch.object(storage, "Client") as cloud_storage_mock: |
| 158 | + bucket_mock = mock.Mock(spec=storage.Bucket) |
| 159 | + bucket_mock.blob.return_value.open.return_value = "blob_file" |
| 160 | + bucket_mock.blob.return_value.upload_from_filename.return_value = None |
| 161 | + bucket_mock.blob.return_value.upload_from_string.return_value = None |
| 162 | + |
| 163 | + cloud_storage_mock.get_bucket = mock.Mock( |
| 164 | + side_effect=ValueError("bucket not found") |
| 165 | + ) |
| 166 | + cloud_storage_mock.bucket.return_value = bucket_mock |
| 167 | + cloud_storage_mock.create_bucket.return_value = bucket_mock |
| 168 | + |
| 169 | + yield cloud_storage_mock |
| 170 | + |
| 171 | + |
| 172 | +@pytest.fixture(scope="module") |
| 173 | +def cloudpickle_load_mock(): |
| 174 | + with mock.patch.object(cloudpickle, "load") as cloudpickle_load_mock: |
| 175 | + yield cloudpickle_load_mock |
| 176 | + |
| 177 | + |
| 178 | +@pytest.fixture(scope="module") |
| 179 | +def create_agent_engine_mock(): |
| 180 | + with mock.patch.object( |
| 181 | + reasoning_engine_service.ReasoningEngineServiceClient, |
| 182 | + "create_reasoning_engine", |
| 183 | + ) as create_agent_engine_mock: |
| 184 | + create_agent_engine_lro_mock = mock.Mock(spec=ga_operation.Operation) |
| 185 | + create_agent_engine_lro_mock.result.return_value = _TEST_AGENT_ENGINE_OBJ |
| 186 | + create_agent_engine_mock.return_value = create_agent_engine_lro_mock |
| 187 | + yield create_agent_engine_mock |
| 188 | + |
| 189 | + |
| 190 | +@pytest.fixture(scope="function") |
| 191 | +def get_gca_resource_mock(): |
| 192 | + with mock.patch.object( |
| 193 | + base.VertexAiResourceNoun, |
| 194 | + "_get_gca_resource", |
| 195 | + ) as get_gca_resource_mock: |
| 196 | + get_gca_resource_mock.return_value = _TEST_AGENT_ENGINE_OBJ |
| 197 | + yield get_gca_resource_mock |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.usefixtures("google_auth_mock") |
| 201 | +class TestAgentEngineA2A: |
| 202 | + def setup_method(self): |
| 203 | + aiplatform.init( |
| 204 | + project=_TEST_PROJECT, |
| 205 | + location=_TEST_LOCATION, |
| 206 | + credentials=_TEST_CREDENTIALS, |
| 207 | + staging_bucket=_TEST_STAGING_BUCKET, |
| 208 | + ) |
| 209 | + |
| 210 | + def test_create_agent_engine_with_protobuf_agent_card( |
| 211 | + self, |
| 212 | + create_agent_engine_mock, |
| 213 | + cloud_storage_create_bucket_mock, |
| 214 | + cloudpickle_load_mock, |
| 215 | + get_gca_resource_mock, |
| 216 | + ): |
| 217 | + from a2a.grpc import a2a_pb2 |
| 218 | + |
| 219 | + card = a2a_pb2.AgentCard(name="test_agent_card") |
| 220 | + agent = CapitalizeEngineWithCard(card) |
| 221 | + |
| 222 | + agent_engines.create( |
| 223 | + agent, |
| 224 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, |
| 225 | + requirements=_TEST_AGENT_ENGINE_REQUIREMENTS, |
| 226 | + extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], |
| 227 | + ) |
| 228 | + |
| 229 | + expected_reasoning_engine = types.ReasoningEngine( |
| 230 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, |
| 231 | + spec=types.ReasoningEngineSpec( |
| 232 | + package_spec=_TEST_AGENT_ENGINE_PACKAGE_SPEC, |
| 233 | + agent_framework=_agent_engines._DEFAULT_AGENT_FRAMEWORK, |
| 234 | + ), |
| 235 | + ) |
| 236 | + from google.protobuf import json_format |
| 237 | + |
| 238 | + expected_class_method = struct_pb2.Struct() |
| 239 | + expected_class_method.CopyFrom(_TEST_AGENT_ENGINE_QUERY_SCHEMA) |
| 240 | + expected_class_method["a2a_agent_card"] = json_format.MessageToJson(card) |
| 241 | + expected_reasoning_engine.spec.class_methods.append(expected_class_method) |
| 242 | + |
| 243 | + create_agent_engine_mock.assert_called_with( |
| 244 | + parent=_TEST_PARENT, |
| 245 | + reasoning_engine=expected_reasoning_engine, |
| 246 | + ) |
| 247 | + |
| 248 | + def test_create_agent_engine_with_pydantic_agent_card( |
| 249 | + self, |
| 250 | + create_agent_engine_mock, |
| 251 | + cloud_storage_create_bucket_mock, |
| 252 | + cloudpickle_load_mock, |
| 253 | + get_gca_resource_mock, |
| 254 | + ): |
| 255 | + card = DummyPydanticCard() |
| 256 | + agent = CapitalizeEngineWithCard(card) |
| 257 | + |
| 258 | + agent_engines.create( |
| 259 | + agent, |
| 260 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, |
| 261 | + requirements=_TEST_AGENT_ENGINE_REQUIREMENTS, |
| 262 | + extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], |
| 263 | + ) |
| 264 | + |
| 265 | + expected_reasoning_engine = types.ReasoningEngine( |
| 266 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, |
| 267 | + spec=types.ReasoningEngineSpec( |
| 268 | + package_spec=_TEST_AGENT_ENGINE_PACKAGE_SPEC, |
| 269 | + agent_framework=_agent_engines._DEFAULT_AGENT_FRAMEWORK, |
| 270 | + ), |
| 271 | + ) |
| 272 | + |
| 273 | + expected_class_method = struct_pb2.Struct() |
| 274 | + expected_class_method.CopyFrom(_TEST_AGENT_ENGINE_QUERY_SCHEMA) |
| 275 | + expected_class_method["a2a_agent_card"] = card.model_dump_json() |
| 276 | + expected_reasoning_engine.spec.class_methods.append(expected_class_method) |
| 277 | + |
| 278 | + create_agent_engine_mock.assert_called_with( |
| 279 | + parent=_TEST_PARENT, |
| 280 | + reasoning_engine=expected_reasoning_engine, |
| 281 | + ) |
| 282 | + |
| 283 | + def test_create_agent_engine_with_invalid_agent_card( |
| 284 | + self, |
| 285 | + create_agent_engine_mock, |
| 286 | + cloud_storage_create_bucket_mock, |
| 287 | + cloudpickle_load_mock, |
| 288 | + get_gca_resource_mock, |
| 289 | + ): |
| 290 | + agent = CapitalizeEngineWithCard(card="invalid_card_type_string") |
| 291 | + |
| 292 | + with pytest.raises( |
| 293 | + TypeError, |
| 294 | + match="Unsupported AgentCard type", |
| 295 | + ): |
| 296 | + agent_engines.create( |
| 297 | + agent, |
| 298 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, |
| 299 | + requirements=_TEST_AGENT_ENGINE_REQUIREMENTS, |
| 300 | + extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], |
| 301 | + ) |
0 commit comments