Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 97 additions & 97 deletions tests/unit/vertex_adk/test_agent_engine_templates_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from google.auth import credentials as auth_credentials
from google.cloud import storage
import vertexai
from google.cloud import aiplatform
from google.cloud.aiplatform_v1 import types as aip_types
from google.cloud.aiplatform_v1.services import reasoning_engine_service
from google.cloud.aiplatform import base
Expand Down Expand Up @@ -1012,99 +1011,100 @@ def update_agent_engine_mock():
yield update_agent_engine_mock


@pytest.mark.usefixtures("google_auth_mock")
class TestAgentEngines:
def setup_method(self):
importlib.reload(initializer)
importlib.reload(aiplatform)
aiplatform.init(
project=_TEST_PROJECT,
location=_TEST_LOCATION,
credentials=_TEST_CREDENTIALS,
staging_bucket=_TEST_STAGING_BUCKET,
)

def teardown_method(self):
initializer.global_pool.shutdown(wait=True)

@pytest.mark.parametrize(
"env_vars,expected_env_vars",
[
({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}),
(None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}),
(
{"some_env": "some_val"},
{
"some_env": "some_val",
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true",
},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
),
],
)
def test_create_default_telemetry_enablement(
self,
create_agent_engine_mock: mock.Mock,
cloud_storage_create_bucket_mock: mock.Mock,
cloudpickle_dump_mock: mock.Mock,
cloudpickle_load_mock: mock.Mock,
get_gca_resource_mock: mock.Mock,
env_vars: dict[str, str],
expected_env_vars: dict[str, str],
):
agent_engines.create(
agent_engine=agent_engines.AdkApp(agent=_TEST_AGENT),
env_vars=env_vars,
)
create_agent_engine_mock.assert_called_once()
deployment_spec = create_agent_engine_mock.call_args.kwargs[
"reasoning_engine"
].spec.deployment_spec
assert _utils.to_dict(deployment_spec)["env"] == [
{"name": key, "value": value} for key, value in expected_env_vars.items()
]

@pytest.mark.parametrize(
"env_vars,expected_env_vars",
[
({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}),
(None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}),
(
{"some_env": "some_val"},
{
"some_env": "some_val",
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true",
},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
),
],
)
def test_update_default_telemetry_enablement(
self,
update_agent_engine_mock: mock.Mock,
cloud_storage_create_bucket_mock: mock.Mock,
cloudpickle_dump_mock: mock.Mock,
cloudpickle_load_mock: mock.Mock,
get_gca_resource_mock: mock.Mock,
get_agent_engine_mock: mock.Mock,
env_vars: dict[str, str],
expected_env_vars: dict[str, str],
):
agent_engines.update(
resource_name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
description="foobar", # avoid "At least one of ... must be specified" errors.
env_vars=env_vars,
)
update_agent_engine_mock.assert_called_once()
deployment_spec = update_agent_engine_mock.call_args.kwargs[
"request"
].reasoning_engine.spec.deployment_spec
assert _utils.to_dict(deployment_spec)["env"] == [
{"name": key, "value": value} for key, value in expected_env_vars.items()
]
# TODO(jawoszek): Uncomment once we're ready for default-on.
# @pytest.mark.usefixtures("google_auth_mock")
# class TestAgentEngines:
# def setup_method(self):
# importlib.reload(initializer)
# importlib.reload(aiplatform)
# aiplatform.init(
# project=_TEST_PROJECT,
# location=_TEST_LOCATION,
# credentials=_TEST_CREDENTIALS,
# staging_bucket=_TEST_STAGING_BUCKET,
# )

# def teardown_method(self):
# initializer.global_pool.shutdown(wait=True)

# @pytest.mark.parametrize(
# "env_vars,expected_env_vars",
# [
# ({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (
# {"some_env": "some_val"},
# {
# "some_env": "some_val",
# GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false",
# },
# ),
# (
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# ),
# ],
# )
# def test_create_default_telemetry_enablement(
# self,
# create_agent_engine_mock: mock.Mock,
# cloud_storage_create_bucket_mock: mock.Mock,
# cloudpickle_dump_mock: mock.Mock,
# cloudpickle_load_mock: mock.Mock,
# get_gca_resource_mock: mock.Mock,
# env_vars: dict[str, str],
# expected_env_vars: dict[str, str],
# ):
# agent_engines.create(
# agent_engine=agent_engines.AdkApp(agent=_TEST_AGENT),
# env_vars=env_vars,
# )
# create_agent_engine_mock.assert_called_once()
# deployment_spec = create_agent_engine_mock.call_args.kwargs[
# "reasoning_engine"
# ].spec.deployment_spec
# assert _utils.to_dict(deployment_spec)["env"] == [
# {"name": key, "value": value} for key, value in expected_env_vars.items()
# ]

# @pytest.mark.parametrize(
# "env_vars,expected_env_vars",
# [
# ({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (
# {"some_env": "some_val"},
# {
# "some_env": "some_val",
# GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false",
# },
# ),
# (
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# ),
# ],
# )
# def test_update_default_telemetry_enablement(
# self,
# update_agent_engine_mock: mock.Mock,
# cloud_storage_create_bucket_mock: mock.Mock,
# cloudpickle_dump_mock: mock.Mock,
# cloudpickle_load_mock: mock.Mock,
# get_gca_resource_mock: mock.Mock,
# get_agent_engine_mock: mock.Mock,
# env_vars: dict[str, str],
# expected_env_vars: dict[str, str],
# ):
# agent_engines.update(
# resource_name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
# description="foobar", # avoid "At least one of ... must be specified" errors.
# env_vars=env_vars,
# )
# update_agent_engine_mock.assert_called_once()
# deployment_spec = update_agent_engine_mock.call_args.kwargs[
# "request"
# ].reasoning_engine.spec.deployment_spec
# assert _utils.to_dict(deployment_spec)["env"] == [
# {"name": key, "value": value} for key, value in expected_env_vars.items()
# ]
86 changes: 43 additions & 43 deletions tests/unit/vertexai/genai/test_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from google.cloud import aiplatform
import vertexai
from google.cloud.aiplatform import initializer
from vertexai.agent_engines.templates import adk
from vertexai._genai import _agent_engines_utils
from vertexai._genai import agent_engines
from vertexai._genai import types as _genai_types
Expand Down Expand Up @@ -856,48 +855,49 @@ def test_create_agent_engine_config_lightweight(self, mock_prepare):
"description": _TEST_AGENT_ENGINE_DESCRIPTION,
}

@mock.patch.object(_agent_engines_utils, "_prepare")
@pytest.mark.parametrize(
"env_vars,expected_env_vars",
[
({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}),
(None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}),
(
{"some_env": "some_val"},
{
"some_env": "some_val",
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true",
},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
),
],
)
def test_agent_engine_adk_telemetry_enablement(
self,
mock_prepare: mock.Mock,
env_vars: dict[str, str],
expected_env_vars: dict[str, str],
):
agent = mock.Mock(spec=adk.AdkApp)
agent.clone = lambda: agent
agent.register_operations = lambda: {}

config = self.client.agent_engines._create_config(
mode="create",
agent=agent,
staging_bucket=_TEST_STAGING_BUCKET,
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
env_vars=env_vars,
)
assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
assert config["spec"]["deployment_spec"]["env"] == [
{"name": key, "value": value} for key, value in expected_env_vars.items()
]
# TODO(jawoszek): Uncomment once we're ready for default-on.
# @mock.patch.object(_agent_engines_utils, "_prepare")
# @pytest.mark.parametrize(
# "env_vars,expected_env_vars",
# [
# ({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (
# {"some_env": "some_val"},
# {
# "some_env": "some_val",
# GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false",
# },
# ),
# (
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# ),
# ],
# )
# def test_agent_engine_adk_telemetry_enablement(
# self,
# mock_prepare: mock.Mock,
# env_vars: dict[str, str],
# expected_env_vars: dict[str, str],
# ):
# agent = mock.Mock(spec=adk.AdkApp)
# agent.clone = lambda: agent
# agent.register_operations = lambda: {}

# config = self.client.agent_engines._create_config(
# mode="create",
# agent=agent,
# staging_bucket=_TEST_STAGING_BUCKET,
# display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
# description=_TEST_AGENT_ENGINE_DESCRIPTION,
# env_vars=env_vars,
# )
# assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
# assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
# assert config["spec"]["deployment_spec"]["env"] == [
# {"name": key, "value": value} for key, value in expected_env_vars.items()
# ]

@mock.patch.object(_agent_engines_utils, "_prepare")
def test_create_agent_engine_config_full(self, mock_prepare):
Expand Down
5 changes: 3 additions & 2 deletions vertexai/_genai/agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,9 @@ def _create_config(
raise ValueError("location must be set using `vertexai.Client`.")
gcs_dir_name = gcs_dir_name or _agent_engines_utils._DEFAULT_GCS_DIR_NAME
agent = _agent_engines_utils._validate_agent_or_raise(agent=agent)
if _agent_engines_utils._is_adk_agent(agent):
env_vars = _agent_engines_utils._add_telemetry_enablement_env(env_vars)
# TODO(jawoszek): Uncomment once we're ready for default-on.
# if _agent_engines_utils._is_adk_agent(agent):
# env_vars = _agent_engines_utils._add_telemetry_enablement_env(env_vars)
staging_bucket = _agent_engines_utils._validate_staging_bucket_or_raise(
staging_bucket=staging_bucket,
)
Expand Down
5 changes: 3 additions & 2 deletions vertexai/agent_engines/_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,9 @@ def create(
if agent_engine is not None:
agent_engine = _validate_agent_engine_or_raise(agent_engine)
staging_bucket = _validate_staging_bucket_or_raise(staging_bucket)
if _is_adk_agent(None, agent_engine):
env_vars = _add_telemetry_enablement_env(env_vars=env_vars)
# TODO(jawoszek): Uncomment once we're ready for default-on.
# if _is_adk_agent(None, agent_engine):
# env_vars = _add_telemetry_enablement_env(env_vars=env_vars)

if agent_engine is None:
if requirements is not None:
Expand Down
Loading