|
| 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 a2a.types import AgentSkill |
| 29 | + |
| 30 | +from vertexai.agent_engines.templates.a2a import A2aAgent as VertexA2aAgent |
| 31 | +from vertexai.agent_engines.templates.a2a import create_agent_card as vertex_create_card |
| 32 | + |
| 33 | +from agentplatform.agent_engines.templates.a2a import A2aAgent as PlatformA2aAgent |
| 34 | +from agentplatform.agent_engines.templates.a2a import create_agent_card as platform_create_card |
| 35 | + |
| 36 | + |
| 37 | +_TEST_LOCATION = "us-central1" |
| 38 | +_TEST_PROJECT = "test-project" |
| 39 | +_TEST_SKILL = AgentSkill( |
| 40 | + id="hello_world", |
| 41 | + name="Returns hello world", |
| 42 | + description="just returns hello world", |
| 43 | + tags=["hello world"], |
| 44 | + examples=["hi", "hello world"], |
| 45 | +) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def vertex_agent() -> VertexA2aAgent: |
| 50 | + card = vertex_create_card(agent_name="Test", description="Test", skills=[_TEST_SKILL]) |
| 51 | + return VertexA2aAgent(agent_card=card) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def platform_agent() -> PlatformA2aAgent: |
| 56 | + card = platform_create_card(agent_name="Test", description="Test", skills=[_TEST_SKILL]) |
| 57 | + return PlatformA2aAgent(agent_card=card) |
| 58 | + |
| 59 | + |
| 60 | +class TestA2aLocationResolution: |
| 61 | + |
| 62 | + @pytest.fixture(autouse=True) |
| 63 | + def patch_create_rest_routes(self, monkeypatch): |
| 64 | + """Patch to avoid optional dependencies not actually used in these tests.""" |
| 65 | + import sys |
| 66 | + from unittest.mock import MagicMock |
| 67 | + |
| 68 | + mock_rest_routes = MagicMock() |
| 69 | + mock_rest_routes.create_rest_routes = MagicMock() |
| 70 | + monkeypatch.setitem(sys.modules, "a2a.server.routes.rest_routes", mock_rest_routes) |
| 71 | + |
| 72 | + def setup_method(self): |
| 73 | + importlib.reload(initializer) |
| 74 | + from google.cloud import aiplatform |
| 75 | + importlib.reload(aiplatform) |
| 76 | + importlib.reload(vertexai) |
| 77 | + vertexai.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 78 | + |
| 79 | + def teardown_method(self): |
| 80 | + initializer.global_pool.shutdown(wait=True) |
| 81 | + |
| 82 | + @pytest.mark.parametrize( |
| 83 | + "agent_fixture", |
| 84 | + ["vertex_agent", "platform_agent"], |
| 85 | + ) |
| 86 | + def test_default_location_from_global_config(self, agent_fixture, request): |
| 87 | + agent = request.getfixturevalue(agent_fixture) |
| 88 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 89 | + agent.set_up() |
| 90 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == _TEST_LOCATION |
| 91 | + |
| 92 | + # Check URL in agent card |
| 93 | + if hasattr(agent.agent_card, "url"): |
| 94 | + url = agent.agent_card.url |
| 95 | + else: |
| 96 | + url = agent.agent_card.supported_interfaces[0].url |
| 97 | + assert _TEST_LOCATION in url |
| 98 | + |
| 99 | + @pytest.mark.parametrize( |
| 100 | + "agent_fixture", |
| 101 | + ["vertex_agent", "platform_agent"], |
| 102 | + ) |
| 103 | + def test_location_from_agent_engine_env_var(self, agent_fixture, request): |
| 104 | + agent = request.getfixturevalue(agent_fixture) |
| 105 | + with mock.patch.dict( |
| 106 | + os.environ, |
| 107 | + {"GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "us-east1"}, |
| 108 | + clear=True, |
| 109 | + ): |
| 110 | + agent.set_up() |
| 111 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-east1" |
| 112 | + |
| 113 | + if hasattr(agent.agent_card, "url"): |
| 114 | + url = agent.agent_card.url |
| 115 | + else: |
| 116 | + url = agent.agent_card.supported_interfaces[0].url |
| 117 | + assert "us-east1" in url |
| 118 | + |
| 119 | + @pytest.mark.parametrize( |
| 120 | + "agent_fixture", |
| 121 | + ["vertex_agent", "platform_agent"], |
| 122 | + ) |
| 123 | + def test_location_from_cloud_location_env_var(self, agent_fixture, request): |
| 124 | + agent = request.getfixturevalue(agent_fixture) |
| 125 | + with mock.patch.dict( |
| 126 | + os.environ, |
| 127 | + {"GOOGLE_CLOUD_LOCATION": "us-west1"}, |
| 128 | + clear=True, |
| 129 | + ): |
| 130 | + agent.set_up() |
| 131 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-west1" |
| 132 | + |
| 133 | + if hasattr(agent.agent_card, "url"): |
| 134 | + url = agent.agent_card.url |
| 135 | + else: |
| 136 | + url = agent.agent_card.supported_interfaces[0].url |
| 137 | + assert "us-west1" in url |
| 138 | + |
| 139 | + @pytest.mark.parametrize( |
| 140 | + "agent_fixture", |
| 141 | + ["vertex_agent", "platform_agent"], |
| 142 | + ) |
| 143 | + def test_location_env_var_precedence(self, agent_fixture, request): |
| 144 | + agent = request.getfixturevalue(agent_fixture) |
| 145 | + with mock.patch.dict( |
| 146 | + os.environ, |
| 147 | + { |
| 148 | + "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION": "us-east1", |
| 149 | + "GOOGLE_CLOUD_LOCATION": "us-west1", |
| 150 | + }, |
| 151 | + clear=True, |
| 152 | + ): |
| 153 | + agent.set_up() |
| 154 | + # Should not overwrite existing GOOGLE_CLOUD_LOCATION |
| 155 | + assert os.environ.get("GOOGLE_CLOUD_LOCATION") == "us-west1" |
| 156 | + |
| 157 | + if hasattr(agent.agent_card, "url"): |
| 158 | + url = agent.agent_card.url |
| 159 | + else: |
| 160 | + url = agent.agent_card.supported_interfaces[0].url |
| 161 | + assert "us-east1" in url |
0 commit comments