chore: import upstream snapshot with attribution
Continuous Integration / Pre-commit Linter (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.10) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.11) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.12) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.12) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.14) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Has been cancelled
Copybara PR Handler / close-imported-pr (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Has been cancelled
Continuous Integration / Pre-commit Linter (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.10) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.11) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.12) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.12) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.14) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Has been cancelled
Copybara PR Handler / close-imported-pr (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
@@ -0,0 +1,56 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from google.adk.integrations.gcs import client
|
||||
from google.auth.credentials import Credentials
|
||||
from google.cloud import storage
|
||||
|
||||
|
||||
def test_get_gcs_client():
|
||||
"""Test get_gcs_client function."""
|
||||
with mock.patch.object(storage, "Client", autospec=True) as MockGCSClient:
|
||||
mock_creds = mock.create_autospec(Credentials, instance=True)
|
||||
client.get_gcs_client(project="test-project", credentials=mock_creds)
|
||||
MockGCSClient.assert_called_once_with(
|
||||
project="test-project",
|
||||
credentials=mock_creds,
|
||||
client_info=mock.ANY,
|
||||
)
|
||||
|
||||
|
||||
def test_get_gcs_client_cache():
|
||||
"""Test get_gcs_client caches and reuses the client instance."""
|
||||
client._client_cache.clear() # pylint: disable=protected-access
|
||||
|
||||
with mock.patch.object(storage, "Client", autospec=True) as MockGCSClient:
|
||||
mock_creds = mock.create_autospec(Credentials, instance=True)
|
||||
|
||||
# First call - cache miss
|
||||
client1 = client.get_gcs_client(
|
||||
project="test-project", credentials=mock_creds
|
||||
)
|
||||
|
||||
# Second call - cache hit
|
||||
client2 = client.get_gcs_client(
|
||||
project="test-project", credentials=mock_creds
|
||||
)
|
||||
|
||||
assert client1 is client2
|
||||
MockGCSClient.assert_called_once_with(
|
||||
project="test-project",
|
||||
credentials=mock_creds,
|
||||
client_info=mock.ANY,
|
||||
)
|
||||
@@ -0,0 +1,140 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from google.adk.integrations.gcs import admin_tool
|
||||
from google.adk.integrations.gcs import client
|
||||
from google.auth.credentials import Credentials
|
||||
|
||||
|
||||
def test_list_buckets():
|
||||
"""Test list_buckets function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_bucket.name = "test-bucket"
|
||||
mock_client.list_buckets.return_value = [mock_bucket]
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = admin_tool.list_buckets(
|
||||
project_id="test-project", credentials=creds
|
||||
)
|
||||
assert result == {
|
||||
"status": "SUCCESS",
|
||||
"results": ["test-bucket"],
|
||||
}
|
||||
|
||||
|
||||
def test_list_buckets_pagination():
|
||||
"""Test list_buckets function with pagination."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_bucket.name = "test-bucket"
|
||||
mock_buckets = mock.MagicMock()
|
||||
mock_buckets.pages = iter([[mock_bucket]])
|
||||
mock_buckets.next_page_token = "next-page-token"
|
||||
mock_client.list_buckets.return_value = mock_buckets
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = admin_tool.list_buckets(
|
||||
project_id="test-project",
|
||||
credentials=creds,
|
||||
page_size=1,
|
||||
page_token="token",
|
||||
)
|
||||
assert result == {
|
||||
"status": "SUCCESS",
|
||||
"results": ["test-bucket"],
|
||||
"next_page_token": "next-page-token",
|
||||
}
|
||||
mock_client.list_buckets.assert_called_once_with(
|
||||
max_results=1, page_token="token"
|
||||
)
|
||||
|
||||
|
||||
def test_create_bucket():
|
||||
"""Test create_bucket function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket_obj = mock.MagicMock()
|
||||
mock_client.bucket.return_value = mock_bucket_obj
|
||||
mock_new_bucket = mock.MagicMock()
|
||||
mock_new_bucket.name = "test-bucket"
|
||||
mock_client.create_bucket.return_value = mock_new_bucket
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = admin_tool.create_bucket(
|
||||
project_id="test-project", bucket_name="test-bucket", credentials=creds
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
mock_client.create_bucket.assert_called_once_with(
|
||||
mock_bucket_obj, location=None
|
||||
)
|
||||
|
||||
|
||||
def test_update_bucket():
|
||||
"""Test update_bucket function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_bucket.name = "test-bucket"
|
||||
mock_bucket.iam_configuration = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = admin_tool.update_bucket(
|
||||
bucket_name="test-bucket",
|
||||
credentials=creds,
|
||||
versioning_enabled=True,
|
||||
uniform_bucket_level_access_enabled=True,
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
assert mock_bucket.versioning_enabled is True
|
||||
assert (
|
||||
mock_bucket.iam_configuration.uniform_bucket_level_access_enabled
|
||||
is True
|
||||
)
|
||||
mock_bucket.patch.assert_called_once()
|
||||
|
||||
|
||||
def test_delete_bucket():
|
||||
"""Test delete_bucket function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = admin_tool.delete_bucket(
|
||||
bucket_name="test-bucket", credentials=creds
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
mock_bucket.delete.assert_called_once()
|
||||
@@ -0,0 +1,67 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
# # Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from google.adk.integrations.gcs.gcs_credentials import GCS_DEFAULT_SCOPE
|
||||
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
|
||||
from google.auth.credentials import Credentials
|
||||
import google.oauth2.credentials
|
||||
import pytest
|
||||
|
||||
|
||||
class TestGCSCredentials:
|
||||
"""Test suite for GCS credentials configuration validation."""
|
||||
|
||||
def test_gcs_credentials_config_client_id_secret(self):
|
||||
"""Test GCSCredentialsConfig with client_id and client_secret."""
|
||||
config = GCSCredentialsConfig(client_id="abc", client_secret="def")
|
||||
assert config.client_id == "abc"
|
||||
assert config.client_secret == "def"
|
||||
assert config.scopes == GCS_DEFAULT_SCOPE
|
||||
assert config.credentials is None
|
||||
|
||||
def test_gcs_credentials_config_existing_creds(self):
|
||||
"""Test GCSCredentialsConfig with existing generic credentials."""
|
||||
mock_creds = mock.create_autospec(Credentials, instance=True)
|
||||
config = GCSCredentialsConfig(credentials=mock_creds)
|
||||
assert config.credentials == mock_creds
|
||||
assert config.client_id is None
|
||||
assert config.client_secret is None
|
||||
|
||||
def test_gcs_credentials_config_oauth2_creds(self):
|
||||
"""Test GCSCredentialsConfig with existing OAuth2 credentials."""
|
||||
mock_creds = mock.create_autospec(
|
||||
google.oauth2.credentials.Credentials, instance=True
|
||||
)
|
||||
mock_creds.client_id = "oauth_client_id"
|
||||
mock_creds.client_secret = "oauth_client_secret"
|
||||
mock_creds.scopes = ["fake_scope"]
|
||||
config = GCSCredentialsConfig(credentials=mock_creds)
|
||||
assert config.client_id == "oauth_client_id"
|
||||
assert config.client_secret == "oauth_client_secret"
|
||||
assert config.scopes == ["fake_scope"]
|
||||
|
||||
def test_gcs_credentials_config_validation_errors(self):
|
||||
"""Test GCSCredentialsConfig validation errors."""
|
||||
with pytest.raises(ValueError):
|
||||
GCSCredentialsConfig()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
GCSCredentialsConfig(client_id="abc")
|
||||
|
||||
mock_creds = mock.create_autospec(Credentials, instance=True)
|
||||
with pytest.raises(ValueError):
|
||||
GCSCredentialsConfig(
|
||||
credentials=mock_creds, client_id="abc", client_secret="def"
|
||||
)
|
||||
@@ -0,0 +1,373 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from google.adk.integrations.gcs import client
|
||||
from google.adk.integrations.gcs import storage_tool
|
||||
from google.auth.credentials import Credentials
|
||||
|
||||
|
||||
def test_get_bucket():
|
||||
"""Test get_bucket function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
setattr(
|
||||
mock_bucket,
|
||||
"_properties",
|
||||
{
|
||||
"bucket_id": "test-bucket-id",
|
||||
"bucket_name": "test-bucket",
|
||||
"location": "US",
|
||||
"storage_class": "STANDARD",
|
||||
"time_created": "2024-01-01",
|
||||
"updated": "2024-01-02",
|
||||
"labels": {"env": "test"},
|
||||
},
|
||||
)
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_bucket(
|
||||
bucket_name="test-bucket", credentials=creds
|
||||
)
|
||||
expected_result = getattr(mock_bucket, "_properties", {}).copy()
|
||||
assert result == {"status": "SUCCESS", "results": expected_result}
|
||||
|
||||
|
||||
def test_get_bucket_with_properties():
|
||||
"""Test get_bucket function when bucket has raw _properties populated."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
setattr(
|
||||
mock_bucket,
|
||||
"_properties",
|
||||
{
|
||||
"kind": "storage#bucket",
|
||||
"id": "test-bucket-id",
|
||||
"name": "test-bucket",
|
||||
"location": "US",
|
||||
"storageClass": "STANDARD",
|
||||
"timeCreated": "2024-01-01",
|
||||
"updated": "2024-01-02",
|
||||
"labels": {"env": "test"},
|
||||
"locationType": "region",
|
||||
"etag": "etag-val",
|
||||
"metageneration": 2,
|
||||
"versioning": {"enabled": True},
|
||||
"iamConfiguration": {"uniformBucketLevelAccess": {"enabled": True}},
|
||||
},
|
||||
)
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_bucket(
|
||||
bucket_name="test-bucket", credentials=creds
|
||||
)
|
||||
expected_result = getattr(mock_bucket, "_properties", {}).copy()
|
||||
assert result == {"status": "SUCCESS", "results": expected_result}
|
||||
|
||||
|
||||
def test_list_objects():
|
||||
"""Test list_objects function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_blob.name = "test-object"
|
||||
mock_bucket.list_blobs.return_value = [mock_blob]
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.list_objects(
|
||||
bucket_name="test-bucket", credentials=creds
|
||||
)
|
||||
assert result == {
|
||||
"status": "SUCCESS",
|
||||
"results": ["test-object"],
|
||||
}
|
||||
|
||||
|
||||
def test_list_objects_pagination():
|
||||
"""Test list_objects function with pagination."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_blob.name = "test-object"
|
||||
mock_blobs = mock.MagicMock()
|
||||
mock_blobs.pages = iter([[mock_blob]])
|
||||
mock_blobs.next_page_token = "next-page-token"
|
||||
mock_bucket.list_blobs.return_value = mock_blobs
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.list_objects(
|
||||
bucket_name="test-bucket",
|
||||
credentials=creds,
|
||||
page_size=1,
|
||||
page_token="token",
|
||||
)
|
||||
assert result == {
|
||||
"status": "SUCCESS",
|
||||
"results": ["test-object"],
|
||||
"next_page_token": "next-page-token",
|
||||
}
|
||||
mock_bucket.list_blobs.assert_called_once_with(
|
||||
max_results=1, page_token="token"
|
||||
)
|
||||
|
||||
|
||||
def test_get_object_metadata():
|
||||
"""Test get_object_metadata function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.get_blob.return_value = mock_blob
|
||||
setattr(
|
||||
mock_blob,
|
||||
"_properties",
|
||||
{
|
||||
"kind": "storage#object",
|
||||
"id": "test-bucket/test-object/1",
|
||||
"name": "test-object",
|
||||
"bucket": "test-bucket",
|
||||
"size": "1024",
|
||||
"contentType": "text/plain",
|
||||
"timeCreated": "2024-01-01",
|
||||
"updated": "2024-01-02",
|
||||
"md5Hash": "hash",
|
||||
"metadata": {"key": "value"},
|
||||
},
|
||||
)
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_object_metadata(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
credentials=creds,
|
||||
generation=1,
|
||||
)
|
||||
expected_result = getattr(mock_blob, "_properties", {}).copy()
|
||||
assert result == {"status": "SUCCESS", "results": expected_result}
|
||||
mock_bucket.get_blob.assert_called_once_with("test-object", generation=1)
|
||||
|
||||
|
||||
def test_get_object_metadata_not_found():
|
||||
"""Test get_object_metadata function when object is not found."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_bucket.get_blob.return_value = None
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_object_metadata(
|
||||
bucket_name="test-bucket",
|
||||
object_name="non-existent",
|
||||
credentials=creds,
|
||||
)
|
||||
assert result["status"] == "ERROR"
|
||||
assert "not found" in result["error_details"]
|
||||
mock_bucket.get_blob.assert_called_once_with("non-existent")
|
||||
|
||||
|
||||
def test_create_object():
|
||||
"""Test create_object function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.blob.return_value = mock_blob
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.create_object(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
data="data",
|
||||
credentials=creds,
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
mock_blob.upload_from_string.assert_called_once_with("data")
|
||||
|
||||
|
||||
def test_create_object_from_file():
|
||||
"""Test create_object function using source_file_path."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.blob.return_value = mock_blob
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.create_object(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
source_file_path="path/to/file.txt",
|
||||
credentials=creds,
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
mock_blob.upload_from_filename.assert_called_once_with("path/to/file.txt")
|
||||
|
||||
|
||||
def test_create_object_no_data():
|
||||
"""Test create_object function when neither data nor source_file_path is provided."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.blob.return_value = mock_blob
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.create_object(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
credentials=creds,
|
||||
)
|
||||
assert result["status"] == "ERROR"
|
||||
assert "must be provided" in result["error_details"]
|
||||
|
||||
|
||||
def test_get_object_data():
|
||||
"""Test get_object_data function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.get_blob.return_value = mock_blob
|
||||
mock_blob.download_as_bytes.return_value = b"content"
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_object_data(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
credentials=creds,
|
||||
generation=1,
|
||||
)
|
||||
assert result == {
|
||||
"status": "SUCCESS",
|
||||
"results": "content",
|
||||
"encoding": "text",
|
||||
}
|
||||
mock_bucket.get_blob.assert_called_once_with("test-object", generation=1)
|
||||
|
||||
|
||||
def test_get_object_data_no_generation():
|
||||
"""Test get_object_data function without generation parameter."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.get_blob.return_value = mock_blob
|
||||
mock_blob.download_as_bytes.return_value = b"\xff\xff"
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_object_data(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
credentials=creds,
|
||||
)
|
||||
assert result == {
|
||||
"status": "SUCCESS",
|
||||
"results": "//8=",
|
||||
"encoding": "base64",
|
||||
}
|
||||
mock_bucket.get_blob.assert_called_once_with("test-object")
|
||||
|
||||
|
||||
def test_get_object_data_to_file():
|
||||
"""Test get_object_data function downloading directly to destination_file_path."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
mock_blob = mock.MagicMock()
|
||||
mock_bucket.get_blob.return_value = mock_blob
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.get_object_data(
|
||||
bucket_name="test-bucket",
|
||||
object_name="test-object",
|
||||
destination_file_path="path/to/download.txt",
|
||||
credentials=creds,
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
mock_blob.download_to_filename.assert_called_once_with(
|
||||
"path/to/download.txt"
|
||||
)
|
||||
|
||||
|
||||
def test_delete_objects():
|
||||
"""Test delete_objects function."""
|
||||
with mock.patch.object(
|
||||
client, "get_gcs_client", autospec=True
|
||||
) as mock_get_client:
|
||||
mock_client = mock.MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
mock_bucket = mock.MagicMock()
|
||||
mock_client.get_bucket.return_value = mock_bucket
|
||||
|
||||
creds = mock.create_autospec(Credentials, instance=True)
|
||||
result = storage_tool.delete_objects(
|
||||
bucket_name="test-bucket",
|
||||
object_names=["test-object"],
|
||||
credentials=creds,
|
||||
)
|
||||
assert result["status"] == "SUCCESS"
|
||||
mock_bucket.delete_blobs.assert_called_once_with(blobs=["test-object"])
|
||||
@@ -0,0 +1,111 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from google.adk.integrations.gcs import GCSCredentialsConfig
|
||||
from google.adk.integrations.gcs.admin_toolset import GCSAdminToolset
|
||||
from google.adk.integrations.gcs.storage_toolset import DEFAULT_GCS_TOOL_NAME_PREFIX
|
||||
from google.adk.integrations.gcs.storage_toolset import GCSToolset
|
||||
from google.adk.tools.google_tool import GoogleTool
|
||||
import pytest
|
||||
|
||||
|
||||
def test_gcs_toolset_name_prefix():
|
||||
"""Test GCS toolset name prefix."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSToolset(credentials_config=credentials_config)
|
||||
assert toolset.tool_name_prefix == DEFAULT_GCS_TOOL_NAME_PREFIX
|
||||
|
||||
admin_toolset = GCSAdminToolset(credentials_config=credentials_config)
|
||||
assert admin_toolset.tool_name_prefix == DEFAULT_GCS_TOOL_NAME_PREFIX
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_toolset_tools_default():
|
||||
"""Test default GCS toolset."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSToolset(credentials_config=credentials_config)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
|
||||
assert len(tools) == 4
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
expected_tool_names = set([
|
||||
"get_bucket",
|
||||
"get_object_data",
|
||||
"get_object_metadata",
|
||||
"list_objects",
|
||||
])
|
||||
actual_tool_names = set([tool.name for tool in tools])
|
||||
assert actual_tool_names == expected_tool_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_admin_toolset_tools_default():
|
||||
"""Test default GCS admin toolset."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSAdminToolset(credentials_config=credentials_config)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
|
||||
assert len(tools) == 1
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
expected_tool_names = set([
|
||||
"list_buckets",
|
||||
])
|
||||
actual_tool_names = set([tool.name for tool in tools])
|
||||
assert actual_tool_names == expected_tool_names
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"selected_tools, expected_count",
|
||||
[
|
||||
pytest.param(None, 4, id="None"),
|
||||
pytest.param(["get_bucket"], 1, id="bucket-get"),
|
||||
pytest.param(
|
||||
["list_objects", "get_object_metadata"], 2, id="object-metadata"
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_toolset_tools_selective(selected_tools, expected_count):
|
||||
"""Test GCS toolset with filter."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSToolset(
|
||||
credentials_config=credentials_config, tool_filter=selected_tools
|
||||
)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
|
||||
assert len(tools) == expected_count
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
if selected_tools is not None:
|
||||
expected_tool_names = set(selected_tools)
|
||||
actual_tool_names = set([tool.name for tool in tools])
|
||||
assert actual_tool_names == expected_tool_names
|
||||
@@ -0,0 +1,153 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from google.adk.integrations.gcs import GCSAdminToolset
|
||||
from google.adk.integrations.gcs import GCSCredentialsConfig
|
||||
from google.adk.integrations.gcs import GCSToolset
|
||||
from google.adk.integrations.gcs.settings import Capabilities
|
||||
from google.adk.integrations.gcs.settings import GCSToolSettings
|
||||
from google.adk.tools.google_tool import GoogleTool
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_toolset_tools_default():
|
||||
"""Test default GCS toolset (READ_ONLY)."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSToolset(
|
||||
credentials_config=credentials_config, gcs_tool_settings=None
|
||||
)
|
||||
assert isinstance(toolset._tool_settings, GCSToolSettings)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
assert len(tools) == 4
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
expected_tool_names = {
|
||||
"get_bucket",
|
||||
"get_object_data",
|
||||
"get_object_metadata",
|
||||
"list_objects",
|
||||
}
|
||||
actual_tool_names = {tool.name for tool in tools}
|
||||
assert actual_tool_names == expected_tool_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_toolset_tools_read_write():
|
||||
"""Test GCS toolset with READ_WRITE capability."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
settings = GCSToolSettings(capabilities=[Capabilities.READ_WRITE])
|
||||
toolset = GCSToolset(
|
||||
credentials_config=credentials_config, gcs_tool_settings=settings
|
||||
)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
assert len(tools) == 6
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
expected_tool_names = {
|
||||
"get_bucket",
|
||||
"get_object_data",
|
||||
"get_object_metadata",
|
||||
"list_objects",
|
||||
"create_object",
|
||||
"delete_objects",
|
||||
}
|
||||
actual_tool_names = {tool.name for tool in tools}
|
||||
assert actual_tool_names == expected_tool_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_admin_toolset_tools_default():
|
||||
"""Test default GCS admin toolset (READ_ONLY)."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSAdminToolset(
|
||||
credentials_config=credentials_config, gcs_tool_settings=None
|
||||
)
|
||||
assert isinstance(toolset._tool_settings, GCSToolSettings)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
assert len(tools) == 1
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
expected_tool_names = {
|
||||
"list_buckets",
|
||||
}
|
||||
actual_tool_names = {tool.name for tool in tools}
|
||||
assert actual_tool_names == expected_tool_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_admin_toolset_tools_read_write():
|
||||
"""Test GCS admin toolset with READ_WRITE capability."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
settings = GCSToolSettings(capabilities=[Capabilities.READ_WRITE])
|
||||
toolset = GCSAdminToolset(
|
||||
credentials_config=credentials_config, gcs_tool_settings=settings
|
||||
)
|
||||
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
assert len(tools) == 4
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
expected_tool_names = {
|
||||
"list_buckets",
|
||||
"create_bucket",
|
||||
"update_bucket",
|
||||
"delete_bucket",
|
||||
}
|
||||
actual_tool_names = {tool.name for tool in tools}
|
||||
assert actual_tool_names == expected_tool_names
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"selected_tools, expected_count",
|
||||
[
|
||||
pytest.param(None, 4, id="None"),
|
||||
pytest.param(["get_bucket", "list_objects"], 2, id="read-subset"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_gcs_toolset_tools_selective(selected_tools, expected_count):
|
||||
"""Test GCS toolset with filter."""
|
||||
credentials_config = GCSCredentialsConfig(
|
||||
client_id="abc", client_secret="def"
|
||||
)
|
||||
toolset = GCSToolset(
|
||||
credentials_config=credentials_config, tool_filter=selected_tools
|
||||
)
|
||||
tools = await toolset.get_tools()
|
||||
assert tools is not None
|
||||
assert len(tools) == expected_count
|
||||
assert all([isinstance(tool, GoogleTool) for tool in tools])
|
||||
|
||||
if selected_tools is not None:
|
||||
expected_tool_names = set(selected_tools)
|
||||
actual_tool_names = {tool.name for tool in tools}
|
||||
assert actual_tool_names == expected_tool_names
|
||||
Reference in New Issue
Block a user