|
| 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 | +import importlib |
| 16 | +import os |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +import pytest |
| 20 | +import vertexai |
| 21 | +from google.cloud.aiplatform import initializer |
| 22 | + |
| 23 | +# Skip A2A tests if a2a-sdk is not installed |
| 24 | +pytest.importorskip( |
| 25 | + "a2a.types", reason="a2a-sdk not installed, skipping A2A Agent tests" |
| 26 | +) |
| 27 | + |
| 28 | +from vertexai.preview.reasoning_engines.templates.a2a import A2aAgent as PreviewA2aAgent |
| 29 | +from vertexai.preview.reasoning_engines.templates.a2a import create_agent_card as preview_create_card |
| 30 | + |
| 31 | +from vertexai.agent_engines.templates.a2a import A2aAgent as VertexA2aAgent |
| 32 | +from vertexai.agent_engines.templates.a2a import create_agent_card as vertex_create_card |
| 33 | + |
| 34 | +from agentplatform.agent_engines.templates.a2a import A2aAgent as PlatformA2aAgent |
| 35 | +from agentplatform.agent_engines.templates.a2a import create_agent_card as platform_create_card |
| 36 | + |
| 37 | + |
| 38 | +_TEST_LOCATION = "us-central1" |
| 39 | +_TEST_PROJECT = "test-project" |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def preview_agent() -> "PreviewA2aAgent": |
| 44 | + from a2a.types import AgentSkill |
| 45 | + skill = AgentSkill( |
| 46 | + id="hello_world", |
| 47 | + name="Returns hello world", |
| 48 | + description="just returns hello world", |
| 49 | + ) |
| 50 | + try: |
| 51 | + card = preview_create_card(agent_name="Test", description="Test", skills=[skill]) |
| 52 | + except ImportError as e: |
| 53 | + pytest.skip(f"Legacy preview A2A template is not compatible with the installed a2a-sdk version: {e}") |
| 54 | + return PreviewA2aAgent(agent_card=card) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def vertex_agent() -> "VertexA2aAgent": |
| 59 | + from a2a.types import AgentSkill |
| 60 | + skill = AgentSkill( |
| 61 | + id="hello_world", |
| 62 | + name="Returns hello world", |
| 63 | + description="just returns hello world", |
| 64 | + ) |
| 65 | + card = vertex_create_card(agent_name="Test", description="Test", skills=[skill]) |
| 66 | + return VertexA2aAgent(agent_card=card) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.fixture |
| 70 | +def platform_agent() -> "PlatformA2aAgent": |
| 71 | + from a2a.types import AgentSkill |
| 72 | + skill = AgentSkill( |
| 73 | + id="hello_world", |
| 74 | + name="Returns hello world", |
| 75 | + description="just returns hello world", |
| 76 | + ) |
| 77 | + card = platform_create_card(agent_name="Test", description="Test", skills=[skill]) |
| 78 | + return PlatformA2aAgent(agent_card=card) |
| 79 | + |
| 80 | + |
| 81 | +class TestA2aLocationResolution: |
| 82 | + |
| 83 | + def setup_method(self): |
| 84 | + importlib.reload(initializer) |
| 85 | + importlib.reload(vertexai) |
| 86 | + vertexai.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 87 | + |
| 88 | + def teardown_method(self): |
| 89 | + initializer.global_pool.shutdown(wait=True) |
| 90 | + |
| 91 | + @pytest.mark.parametrize( |
| 92 | + "agent_fixture", |
| 93 | + ["preview_agent", "vertex_agent", "platform_agent"], |
| 94 | + ) |
| 95 | + def test_default_location_from_global_config(self, agent_fixture, request): |
| 96 | + agent = request.getfixturevalue(agent_fixture) |
| 97 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 98 | + agent.set_up() |
| 99 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == _TEST_LOCATION |
| 100 | + |
| 101 | + # Check URL in agent card |
| 102 | + if hasattr(agent.agent_card, "url"): |
| 103 | + url = agent.agent_card.url |
| 104 | + else: |
| 105 | + url = agent.agent_card.supported_interfaces[0].url |
| 106 | + assert _TEST_LOCATION in url |
| 107 | + |
| 108 | + @pytest.mark.parametrize( |
| 109 | + "agent_fixture", |
| 110 | + ["preview_agent", "vertex_agent", "platform_agent"], |
| 111 | + ) |
| 112 | + def test_location_from_agent_engine_env_var(self, agent_fixture, request): |
| 113 | + agent = request.getfixturevalue(agent_fixture) |
| 114 | + with mock.patch.dict( |
| 115 | + os.environ, |
| 116 | + {"GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "us-east1"}, |
| 117 | + clear=True, |
| 118 | + ): |
| 119 | + agent.set_up() |
| 120 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-east1" |
| 121 | + |
| 122 | + if hasattr(agent.agent_card, "url"): |
| 123 | + url = agent.agent_card.url |
| 124 | + else: |
| 125 | + url = agent.agent_card.supported_interfaces[0].url |
| 126 | + assert "us-east1" in url |
| 127 | + |
| 128 | + @pytest.mark.parametrize( |
| 129 | + "agent_fixture", |
| 130 | + ["preview_agent", "vertex_agent", "platform_agent"], |
| 131 | + ) |
| 132 | + def test_location_from_cloud_location_env_var(self, agent_fixture, request): |
| 133 | + agent = request.getfixturevalue(agent_fixture) |
| 134 | + with mock.patch.dict( |
| 135 | + os.environ, |
| 136 | + {"GOOGLE_CLOUD_LOCATION": "us-west1"}, |
| 137 | + clear=True, |
| 138 | + ): |
| 139 | + agent.set_up() |
| 140 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-west1" |
| 141 | + |
| 142 | + if hasattr(agent.agent_card, "url"): |
| 143 | + url = agent.agent_card.url |
| 144 | + else: |
| 145 | + url = agent.agent_card.supported_interfaces[0].url |
| 146 | + assert "us-west1" in url |
| 147 | + |
| 148 | + @pytest.mark.parametrize( |
| 149 | + "agent_fixture", |
| 150 | + ["preview_agent", "vertex_agent", "platform_agent"], |
| 151 | + ) |
| 152 | + def test_location_env_var_precedence(self, agent_fixture, request): |
| 153 | + agent = request.getfixturevalue(agent_fixture) |
| 154 | + with mock.patch.dict( |
| 155 | + os.environ, |
| 156 | + { |
| 157 | + "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "us-east1", |
| 158 | + "GOOGLE_CLOUD_LOCATION": "us-west1", |
| 159 | + }, |
| 160 | + clear=True, |
| 161 | + ): |
| 162 | + agent.set_up() |
| 163 | + # Should not overwrite existing GOOGLE_CLOUD_LOCATION |
| 164 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-west1" |
| 165 | + |
| 166 | + if hasattr(agent.agent_card, "url"): |
| 167 | + url = agent.agent_card.url |
| 168 | + else: |
| 169 | + url = agent.agent_card.supported_interfaces[0].url |
| 170 | + assert "us-east1" in url |
0 commit comments