|
| 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 | +# pylint: disable=protected-access,bad-continuation,missing-function-docstring |
| 16 | + |
| 17 | +import contextlib |
| 18 | +import os |
| 19 | +from unittest import mock |
| 20 | +from tests.unit.agentplatform.genai.replays import pytest_helper |
| 21 | +from agentplatform._genai import types |
| 22 | +from agentplatform.agent_engines.templates.a2a import default_a2a_agent |
| 23 | +import pytest |
| 24 | + |
| 25 | + |
| 26 | +pytest.importorskip( |
| 27 | + "a2a.types", reason="a2a-sdk not installed, skipping A2A Agent tests" |
| 28 | +) |
| 29 | +from a2a.types import AgentInterface |
| 30 | +from a2a.utils.constants import TransportProtocol |
| 31 | + |
| 32 | + |
| 33 | +def test_create_a2a_agent(client, is_replay_mode): |
| 34 | + my_agent = default_a2a_agent() |
| 35 | + |
| 36 | + my_agent.agent_card.supported_interfaces.append( |
| 37 | + AgentInterface( |
| 38 | + url="http://localhost:8888/", |
| 39 | + protocol_binding=TransportProtocol.HTTP_JSON, |
| 40 | + protocol_version="1.0", |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | + staging_bucket = os.environ["GCS_BUCKET"] |
| 46 | + |
| 47 | + # In replay mode, GCS operations are mocked and blob.open("rb") returns a mock |
| 48 | + # that fails when cloudpickle.load expects bytes. We mock _upload_agent_engine |
| 49 | + # to skip this verification step, which is not needed when replaying API calls. |
| 50 | + upload_patch = ( |
| 51 | + mock.patch("agentplatform._genai._agent_engines_utils._upload_agent_engine") |
| 52 | + if is_replay_mode |
| 53 | + else contextlib.nullcontext() |
| 54 | + ) |
| 55 | + |
| 56 | + with upload_patch: |
| 57 | + agent_engine = client.agent_engines.create( |
| 58 | + agent=my_agent, |
| 59 | + config={ |
| 60 | + "staging_bucket": staging_bucket, |
| 61 | + "display_name": "test-a2a-agent", |
| 62 | + "http_options": {"api_version": "v1beta1"}, |
| 63 | + "requirements": [ |
| 64 | + "google-cloud-aiplatform[agent_engines]", |
| 65 | + "a2a-sdk", |
| 66 | + "sse-starlette", |
| 67 | + ], |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | + assert isinstance(agent_engine, types.AgentEngine) |
| 73 | + assert agent_engine.api_resource.display_name == "test-a2a-agent" |
| 74 | + |
| 75 | + # Clean up resources. |
| 76 | + client.agent_engines.delete(name=agent_engine.api_resource.name, force=True) |
| 77 | + |
| 78 | + |
| 79 | +pytestmark = pytest_helper.setup( |
| 80 | + file=__file__, |
| 81 | + globals_for_file=globals(), |
| 82 | +) |
| 83 | + |
| 84 | +pytest_plugins = ("pytest_asyncio",) |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments