Skip to content
Open
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
146 changes: 143 additions & 3 deletions tests/unit/vertex_langchain/test_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@ def register_operations(self) -> Dict[str, List[str]]:
_TEST_INSTALLATION_SCRIPT_PATH = f"{_TEST_INSTALLATION_SUBDIR}/install_package.sh"

_TEST_CUSTOM_SERVICE_ACCOUNT = "test-custom-service-account"
_TEST_AGENT_IDENTITY_TYPE = types.ReasoningEngineSpec.IdentityType.AGENT_IDENTITY
_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE = (
types.ReasoningEngineSpec.IdentityType.SERVICE_ACCOUNT
)
_TEST_AGENT_ENGINE_PSC_INTERFACE_CONFIG = {
"network_attachment": "test-network-attachment",
"dns_peering_configs": [
Expand Down Expand Up @@ -1284,6 +1288,72 @@ def test_create_agent_engine_with_service_account(
retry=_TEST_RETRY,
)

def test_create_agent_engine_with_agent_identity(
self,
create_agent_engine_mock,
cloud_storage_create_bucket_mock,
tarfile_open_mock,
cloudpickle_dump_mock,
cloudpickle_load_mock,
importlib_metadata_version_mock,
get_agent_engine_mock,
get_gca_resource_mock,
):
agent_engines.create(
self.test_agent,
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
requirements=_TEST_AGENT_ENGINE_REQUIREMENTS,
extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH],
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)
test_spec = types.ReasoningEngineSpec(
package_spec=_TEST_AGENT_ENGINE_PACKAGE_SPEC,
agent_framework=_agent_engines._DEFAULT_AGENT_FRAMEWORK,
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)
test_spec.class_methods.append(_TEST_AGENT_ENGINE_QUERY_SCHEMA)
create_agent_engine_mock.assert_called_with(
parent=_TEST_PARENT,
reasoning_engine=types.ReasoningEngine(
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
spec=test_spec,
),
)

def test_create_agent_engine_with_service_account_identity_type(
self,
create_agent_engine_mock,
cloud_storage_create_bucket_mock,
tarfile_open_mock,
cloudpickle_dump_mock,
cloudpickle_load_mock,
importlib_metadata_version_mock,
get_agent_engine_mock,
get_gca_resource_mock,
):
agent_engines.create(
self.test_agent,
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
requirements=_TEST_AGENT_ENGINE_REQUIREMENTS,
extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH],
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
)
test_spec = types.ReasoningEngineSpec(
package_spec=_TEST_AGENT_ENGINE_PACKAGE_SPEC,
agent_framework=_agent_engines._DEFAULT_AGENT_FRAMEWORK,
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
)
test_spec.class_methods.append(_TEST_AGENT_ENGINE_QUERY_SCHEMA)
create_agent_engine_mock.assert_called_with(
parent=_TEST_PARENT,
reasoning_engine=types.ReasoningEngine(
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
spec=test_spec,
),
)

def test_create_agent_engine_with_psc_interface_config(
self,
create_agent_engine_mock,
Expand Down Expand Up @@ -1809,6 +1879,45 @@ def test_get_agent_framework(
),
),
),
(
"Update the agent_engine with identity_type attribute",
{"identity_type": _TEST_AGENT_IDENTITY_TYPE},
types.reasoning_engine_service.UpdateReasoningEngineRequest(
reasoning_engine=types.ReasoningEngine(
name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
spec=types.ReasoningEngineSpec(
identity_type=_TEST_AGENT_IDENTITY_TYPE,
),
),
update_mask=field_mask_pb2.FieldMask(
paths=[
"spec.identity_type",
],
),
),
),
(
"Update the agent_engine with service_account and identity_type",
{
"service_account": _TEST_CUSTOM_SERVICE_ACCOUNT,
"identity_type": _TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
},
types.reasoning_engine_service.UpdateReasoningEngineRequest(
reasoning_engine=types.ReasoningEngine(
name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
spec=types.ReasoningEngineSpec(
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
),
),
update_mask=field_mask_pb2.FieldMask(
paths=[
"spec.service_account",
"spec.identity_type",
],
),
),
),
(
"Update the agent_engine with psc_interface_config attribute",
{"psc_interface_config": _TEST_AGENT_ENGINE_PSC_INTERFACE_CONFIG},
Expand Down Expand Up @@ -3406,14 +3515,45 @@ def test_update_agent_engine_with_no_updates(
"At least one of `agent_engine`, `requirements`, "
"`extra_packages`, `display_name`, `description`, "
"`env_vars`, `build_options`, `service_account`, "
"`psc_interface_config`, `min_instances`, `max_instances`, "
"`resource_limits`, `container_concurrency`, or "
"`encryption_spec` must be specified."
"`identity_type`, `psc_interface_config`, `min_instances`, "
"`max_instances`, `resource_limits`, `container_concurrency`, "
"or `encryption_spec` must be specified."
),
):
test_agent_engine = _generate_agent_engine_to_update()
test_agent_engine.update()

def test_create_agent_engine_with_agent_identity_and_service_account(self):
with pytest.raises(
ValueError,
match=(
"`service_account` must not be specified when `identity_type` "
"is `AGENT_IDENTITY`"
),
):
agent_engines.create(
CapitalizeEngine(),
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)

def test_update_agent_engine_with_agent_identity_and_service_account(
self,
update_agent_engine_mock,
):
with pytest.raises(
ValueError,
match=(
"`service_account` must not be specified when `identity_type` "
"is `AGENT_IDENTITY`"
),
):
test_agent_engine = _generate_agent_engine_to_update()
test_agent_engine.update(
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)

def test_create_class_methods_spec_with_registered_operation_not_found(self):
with pytest.raises(
ValueError,
Expand Down
164 changes: 162 additions & 2 deletions tests/unit/vertex_langchain/test_reasoning_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,42 @@ class ListClass:
_TEST_INPUT_REASONING_ENGINE_OBJ.spec.class_methods.append(
_TEST_REASONING_ENGINE_QUERY_SCHEMA
)
_TEST_CUSTOM_SERVICE_ACCOUNT = "test-custom-service-account"
_TEST_AGENT_IDENTITY_TYPE = types.ReasoningEngineSpec.IdentityType.AGENT_IDENTITY
_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE = (
types.ReasoningEngineSpec.IdentityType.SERVICE_ACCOUNT
)
_TEST_INPUT_REASONING_ENGINE_OBJ_WITH_SERVICE_ACCOUNT = types.ReasoningEngine(
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
spec=types.ReasoningEngineSpec(
package_spec=types.ReasoningEngineSpec.PackageSpec(
python_version=f"{sys.version_info.major}.{sys.version_info.minor}",
pickle_object_gcs_uri=_TEST_REASONING_ENGINE_GCS_URI,
dependency_files_gcs_uri=_TEST_REASONING_ENGINE_DEPENDENCY_FILES_GCS_URI,
requirements_gcs_uri=_TEST_REASONING_ENGINE_REQUIREMENTS_GCS_URI,
),
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
),
)
_TEST_INPUT_REASONING_ENGINE_OBJ_WITH_SERVICE_ACCOUNT.spec.class_methods.append(
_TEST_REASONING_ENGINE_QUERY_SCHEMA
)
_TEST_INPUT_REASONING_ENGINE_OBJ_WITH_AGENT_IDENTITY = types.ReasoningEngine(
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
spec=types.ReasoningEngineSpec(
package_spec=types.ReasoningEngineSpec.PackageSpec(
python_version=f"{sys.version_info.major}.{sys.version_info.minor}",
pickle_object_gcs_uri=_TEST_REASONING_ENGINE_GCS_URI,
dependency_files_gcs_uri=_TEST_REASONING_ENGINE_DEPENDENCY_FILES_GCS_URI,
requirements_gcs_uri=_TEST_REASONING_ENGINE_REQUIREMENTS_GCS_URI,
),
identity_type=_TEST_AGENT_IDENTITY_TYPE,
),
)
_TEST_INPUT_REASONING_ENGINE_OBJ_WITH_AGENT_IDENTITY.spec.class_methods.append(
_TEST_REASONING_ENGINE_QUERY_SCHEMA
)
_TEST_REASONING_ENGINE_OBJ = types.ReasoningEngine(
name=_TEST_REASONING_ENGINE_RESOURCE_NAME,
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
Expand Down Expand Up @@ -794,6 +830,49 @@ def test_create_reasoning_engine(
retry=_TEST_RETRY,
)

def test_create_reasoning_engine_with_service_account(
self,
create_reasoning_engine_mock,
cloud_storage_create_bucket_mock,
tarfile_open_mock,
cloudpickle_dump_mock,
get_reasoning_engine_mock,
get_gca_resource_mock,
):
reasoning_engines.ReasoningEngine.create(
self.test_app,
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
requirements=_TEST_REASONING_ENGINE_REQUIREMENTS,
extra_packages=[_TEST_REASONING_ENGINE_EXTRA_PACKAGE_PATH],
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
)
create_reasoning_engine_mock.assert_called_with(
parent=_TEST_PARENT,
reasoning_engine=_TEST_INPUT_REASONING_ENGINE_OBJ_WITH_SERVICE_ACCOUNT,
)

def test_create_reasoning_engine_with_agent_identity(
self,
create_reasoning_engine_mock,
cloud_storage_create_bucket_mock,
tarfile_open_mock,
cloudpickle_dump_mock,
get_reasoning_engine_mock,
get_gca_resource_mock,
):
reasoning_engines.ReasoningEngine.create(
self.test_app,
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
requirements=_TEST_REASONING_ENGINE_REQUIREMENTS,
extra_packages=[_TEST_REASONING_ENGINE_EXTRA_PACKAGE_PATH],
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)
create_reasoning_engine_mock.assert_called_with(
parent=_TEST_PARENT,
reasoning_engine=_TEST_INPUT_REASONING_ENGINE_OBJ_WITH_AGENT_IDENTITY,
)

@pytest.mark.usefixtures("caplog")
def test_create_reasoning_engine_warn_resource_name(
self,
Expand Down Expand Up @@ -990,6 +1069,56 @@ def test_create_reasoning_engine_requirements_from_file(
update_mask=field_mask_pb2.FieldMask(paths=["description"]),
),
),
(
"Update the service_account",
{"service_account": _TEST_CUSTOM_SERVICE_ACCOUNT},
types.reasoning_engine_service.UpdateReasoningEngineRequest(
reasoning_engine=types.ReasoningEngine(
name=_TEST_REASONING_ENGINE_RESOURCE_NAME,
spec=types.ReasoningEngineSpec(
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
),
),
update_mask=field_mask_pb2.FieldMask(
paths=["spec.service_account"]
),
),
),
(
"Update the identity_type",
{"identity_type": _TEST_AGENT_IDENTITY_TYPE},
types.reasoning_engine_service.UpdateReasoningEngineRequest(
reasoning_engine=types.ReasoningEngine(
name=_TEST_REASONING_ENGINE_RESOURCE_NAME,
spec=types.ReasoningEngineSpec(
identity_type=_TEST_AGENT_IDENTITY_TYPE,
),
),
update_mask=field_mask_pb2.FieldMask(paths=["spec.identity_type"]),
),
),
(
"Update the service_account and identity_type",
{
"service_account": _TEST_CUSTOM_SERVICE_ACCOUNT,
"identity_type": _TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
},
types.reasoning_engine_service.UpdateReasoningEngineRequest(
reasoning_engine=types.ReasoningEngine(
name=_TEST_REASONING_ENGINE_RESOURCE_NAME,
spec=types.ReasoningEngineSpec(
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_SERVICE_ACCOUNT_IDENTITY_TYPE,
),
),
update_mask=field_mask_pb2.FieldMask(
paths=[
"spec.service_account",
"spec.identity_type",
]
),
),
),
],
)
def test_update_reasoning_engine(
Expand Down Expand Up @@ -2010,13 +2139,44 @@ def test_update_reasoning_engine_with_no_updates(
ValueError,
match=(
"At least one of `reasoning_engine`, `requirements`, "
"`extra_packages`, `display_name`, or `description` "
"must be specified."
"`extra_packages`, `display_name`, `description`, "
"`service_account`, or `identity_type` must be specified."
),
):
test_reasoning_engine = _generate_reasoning_engine_to_update()
test_reasoning_engine.update()

def test_create_reasoning_engine_with_agent_identity_and_service_account(self):
with pytest.raises(
ValueError,
match=(
"`service_account` must not be specified when `identity_type` "
"is `AGENT_IDENTITY`"
),
):
reasoning_engines.ReasoningEngine.create(
CapitalizeEngine(),
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)

def test_update_reasoning_engine_with_agent_identity_and_service_account(
self,
update_reasoning_engine_mock,
):
with pytest.raises(
ValueError,
match=(
"`service_account` must not be specified when `identity_type` "
"is `AGENT_IDENTITY`"
),
):
test_reasoning_engine = _generate_reasoning_engine_to_update()
test_reasoning_engine.update(
service_account=_TEST_CUSTOM_SERVICE_ACCOUNT,
identity_type=_TEST_AGENT_IDENTITY_TYPE,
)

def test_create_class_methods_spec_with_registered_operation_not_found(self):
with pytest.raises(
ValueError,
Expand Down
Loading
Loading