|
| 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 os |
| 16 | +import subprocess |
| 17 | +import time |
| 18 | +from typing import Iterator |
| 19 | +import uuid |
| 20 | + |
| 21 | +from google.api_core import exceptions |
| 22 | +from google.cloud import parametermanager_v1 |
| 23 | +import pytest |
| 24 | + |
| 25 | +CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 26 | +PARENT_DIR = os.path.dirname(CURRENT_DIR) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture() |
| 30 | +def client() -> parametermanager_v1.ParameterManagerClient: |
| 31 | + return parametermanager_v1.ParameterManagerClient() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture() |
| 35 | +def project_id() -> str: |
| 36 | + return os.environ["GOOGLE_CLOUD_PROJECT"] |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture() |
| 40 | +def parameter_id( |
| 41 | + client: parametermanager_v1.ParameterManagerClient, project_id: str |
| 42 | +) -> Iterator[str]: |
| 43 | + parameter_id = f"python-adk-test-{uuid.uuid4()}" |
| 44 | + yield parameter_id |
| 45 | + |
| 46 | + # Teardown: delete the version then the parameter |
| 47 | + version_path = client.parameter_version_path(project_id, "global", parameter_id, "1") |
| 48 | + parameter_path = client.parameter_path(project_id, "global", parameter_id) |
| 49 | + |
| 50 | + print("Deleting parameter version 1...") |
| 51 | + try: |
| 52 | + time.sleep(1) |
| 53 | + client.delete_parameter_version(request={"name": version_path}) |
| 54 | + except exceptions.NotFound: |
| 55 | + pass |
| 56 | + |
| 57 | + print(f"Deleting parameter {parameter_id}...") |
| 58 | + try: |
| 59 | + time.sleep(2) |
| 60 | + client.delete_parameter(name=parameter_path) |
| 61 | + except exceptions.NotFound: |
| 62 | + pass |
| 63 | + |
| 64 | + |
| 65 | +def test_agent_global( |
| 66 | + client: parametermanager_v1.ParameterManagerClient, project_id: str, parameter_id: str |
| 67 | +) -> None: |
| 68 | + # Create the parameter |
| 69 | + parent = client.common_location_path(project_id, "global") |
| 70 | + print(f"Creating parameter {parameter_id}...") |
| 71 | + client.create_parameter( |
| 72 | + request={ |
| 73 | + "parent": parent, |
| 74 | + "parameter_id": parameter_id, |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + # Add a version |
| 79 | + parameter_path = client.parameter_path(project_id, "global", parameter_id) |
| 80 | + print(f"Adding version to {parameter_id}...") |
| 81 | + client.create_parameter_version( |
| 82 | + request={ |
| 83 | + "parent": parameter_path, |
| 84 | + "parameter_version_id": "1", |
| 85 | + "parameter_version": { |
| 86 | + "payload": {"data": b"test-payload"} |
| 87 | + }, |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + # Set environment variables required by the agent |
| 92 | + os.environ["ADK_TEST_PARAMETER_ID"] = parameter_id |
| 93 | + os.environ["ADK_TEST_PARAMETER_VERSION"] = "1" |
| 94 | + |
| 95 | + print(f"Running adk run agent_global from {PARENT_DIR}...") |
| 96 | + # Run the sample using adk run |
| 97 | + result = subprocess.run( |
| 98 | + ["adk", "run", "agent_global"], |
| 99 | + input="exit\n", |
| 100 | + capture_output=True, |
| 101 | + text=True, |
| 102 | + cwd=PARENT_DIR, |
| 103 | + ) |
| 104 | + |
| 105 | + print("STDOUT:") |
| 106 | + print(result.stdout) |
| 107 | + print("STDERR:") |
| 108 | + print(result.stderr) |
| 109 | + |
| 110 | + assert result.returncode == 0 |
| 111 | + assert "Successfully fetched parameter" in result.stdout |
| 112 | + assert "Agent initialized successfully" in result.stdout |
0 commit comments