Skip to content

tests(toolbox-code): add e2e tests #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
5f1b2b0
feat: add authenticated parameters support
kurtisvg Mar 30, 2025
34bec37
chore: add asyncio dep
kurtisvg Mar 30, 2025
f0024bf
chore: run itest
kurtisvg Mar 30, 2025
a646285
chore: add type hint
kurtisvg Mar 30, 2025
0dc7034
fix: call tool instead of client
kurtisvg Mar 30, 2025
61d32aa
chore: correct arg name
kurtisvg Mar 31, 2025
744ade9
feat: add support for bound parameters
kurtisvg Mar 31, 2025
b875771
chore: add tests for bound parameters
kurtisvg Mar 31, 2025
138d8d9
docs: update syntax error on readme (#121)
Yuan325 Mar 31, 2025
e2f5b4d
ci: added release please config (#112)
twishabansal Apr 1, 2025
280a79f
added basic e2e tests
twishabansal Apr 1, 2025
63808ce
change license year
twishabansal Apr 1, 2025
55f5ca0
add test deps
twishabansal Apr 1, 2025
ed16488
fix tests
twishabansal Apr 1, 2025
f0991ab
fix tests
twishabansal Apr 1, 2025
cc6d568
fix tests
twishabansal Apr 1, 2025
1f63b0d
add new test case
twishabansal Apr 1, 2025
66e88ab
fix docstring
twishabansal Apr 1, 2025
1fa1b7a
added todo
twishabansal Apr 1, 2025
12af5fa
cleanup
twishabansal Apr 1, 2025
9c3ba38
add bind param test case
twishabansal Apr 1, 2025
d75828e
make bind params dynamic
twishabansal Apr 1, 2025
fef1f7f
try fix test errors
twishabansal Apr 1, 2025
cd701b7
lint
twishabansal Apr 1, 2025
7a5a1bd
remove redundant test
twishabansal Apr 1, 2025
e343e78
test fix
twishabansal Apr 1, 2025
11d62b4
fix docstring
twishabansal Apr 1, 2025
87e00cc
feat: add authenticated parameters support
kurtisvg Mar 30, 2025
6b263ad
chore: add asyncio dep
kurtisvg Mar 30, 2025
5fe541f
chore: run itest
kurtisvg Mar 30, 2025
e9d7a31
chore: add type hint
kurtisvg Mar 30, 2025
58c55cf
fix: call tool instead of client
kurtisvg Mar 30, 2025
c1a482a
chore: correct arg name
kurtisvg Mar 31, 2025
c1ac2cd
chore: address feedback
kurtisvg Apr 2, 2025
bcba462
chore: address more feedback
kurtisvg Apr 2, 2025
c8491a9
feat: add support for bound parameters
kurtisvg Mar 31, 2025
c26c453
chore: add tests for bound parameters
kurtisvg Mar 31, 2025
3abe9e4
chore: address feedback
kurtisvg Apr 2, 2025
3200f00
Merge branch 'kvg-bound-params' into twisha-core-e2e
twishabansal Apr 2, 2025
9482f37
revert package file changes
twishabansal Apr 2, 2025
6dca7f9
fix error message
twishabansal Apr 2, 2025
4d562bd
revert package files
twishabansal Apr 2, 2025
cc7ccad
lint
twishabansal Apr 2, 2025
84e66a6
fix error message
twishabansal Apr 2, 2025
fe2a332
Update packages/toolbox-core/tests/test_e2e.py
twishabansal Apr 2, 2025
b05ea39
add new test case
twishabansal Apr 2, 2025
7d3b77a
change docstring to reflect new test cases
twishabansal Apr 2, 2025
16e38e9
Merge branch 'main' into twisha-core-e2e
twishabansal Apr 3, 2025
755d3e9
clean up docstring
twishabansal Apr 3, 2025
738b984
lint
twishabansal Apr 3, 2025
7683063
Move tests to different classes
twishabansal Apr 3, 2025
1ab6ae7
Merge branch 'main' into twisha-core-e2e
twishabansal Apr 3, 2025
49203ac
add timeout
twishabansal Apr 3, 2025
2f758ca
Merge remote-tracking branch 'origin/twisha-core-e2e' into twisha-cor…
twishabansal Apr 3, 2025
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
2 changes: 2 additions & 0 deletions packages/toolbox-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ test = [
"pytest==8.3.5",
"pytest-aioresponses==0.3.0",
"pytest-asyncio==0.25.3",
"google-cloud-secret-manager==2.23.2",
"google-cloud-storage==3.1.0",
]
[build-system]
requires = ["setuptools"]
Expand Down
166 changes: 166 additions & 0 deletions packages/toolbox-core/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Copyright 2025 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.

"""Contains pytest fixtures that are accessible from all
files present in the same directory."""

from __future__ import annotations

import os
import platform
import subprocess
import tempfile
import time
from typing import Generator

import google
import pytest_asyncio
from google.auth import compute_engine
from google.cloud import secretmanager, storage


#### Define Utility Functions
def get_env_var(key: str) -> str:
"""Gets environment variables."""
value = os.environ.get(key)
if value is None:
raise ValueError(f"Must set env var {key}")
return value


def access_secret_version(
project_id: str, secret_id: str, version_id: str = "latest"
) -> str:
"""Accesses the payload of a given secret version from Secret Manager."""
client = secretmanager.SecretManagerServiceClient()
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
response = client.access_secret_version(request={"name": name})
return response.payload.data.decode("UTF-8")


def create_tmpfile(content: str) -> str:
"""Creates a temporary file with the given content."""
with tempfile.NamedTemporaryFile(delete=False, mode="w") as tmpfile:
tmpfile.write(content)
return tmpfile.name


def download_blob(
bucket_name: str, source_blob_name: str, destination_file_name: str
) -> None:
"""Downloads a blob from a GCS bucket."""
storage_client = storage.Client()

bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)

print(f"Blob {source_blob_name} downloaded to {destination_file_name}.")


def get_toolbox_binary_url(toolbox_version: str) -> str:
"""Constructs the GCS path to the toolbox binary."""
os_system = platform.system().lower()
arch = (
"arm64" if os_system == "darwin" and platform.machine() == "arm64" else "amd64"
)
return f"v{toolbox_version}/{os_system}/{arch}/toolbox"


def get_auth_token(client_id: str) -> str:
"""Retrieves an authentication token"""
request = google.auth.transport.requests.Request()
credentials = compute_engine.IDTokenCredentials(
request=request,
target_audience=client_id,
use_metadata_identity_endpoint=True,
)
if not credentials.valid:
credentials.refresh(request)
return credentials.token


#### Define Fixtures
@pytest_asyncio.fixture(scope="session")
def project_id() -> str:
return get_env_var("GOOGLE_CLOUD_PROJECT")


@pytest_asyncio.fixture(scope="session")
def toolbox_version() -> str:
return get_env_var("TOOLBOX_VERSION")


@pytest_asyncio.fixture(scope="session")
def tools_file_path(project_id: str) -> Generator[str]:
"""Provides a temporary file path containing the tools manifest."""
tools_manifest = access_secret_version(
project_id=project_id, secret_id="sdk_testing_tools"
)
tools_file_path = create_tmpfile(tools_manifest)
yield tools_file_path
os.remove(tools_file_path)


@pytest_asyncio.fixture(scope="session")
def auth_token1(project_id: str) -> str:
client_id = access_secret_version(
project_id=project_id, secret_id="sdk_testing_client1"
)
return get_auth_token(client_id)


@pytest_asyncio.fixture(scope="session")
def auth_token2(project_id: str) -> str:
client_id = access_secret_version(
project_id=project_id, secret_id="sdk_testing_client2"
)
return get_auth_token(client_id)


@pytest_asyncio.fixture(scope="session")
def toolbox_server(toolbox_version: str, tools_file_path: str) -> Generator[None]:
"""Starts the toolbox server as a subprocess."""
print("Downloading toolbox binary from gcs bucket...")
source_blob_name = get_toolbox_binary_url(toolbox_version)
download_blob("genai-toolbox", source_blob_name, "toolbox")
print("Toolbox binary downloaded successfully.")
try:
print("Opening toolbox server process...")
# Make toolbox executable
os.chmod("toolbox", 0o700)
# Run toolbox binary
toolbox_server = subprocess.Popen(
["./toolbox", "--tools_file", tools_file_path]
)

# Wait for server to start
# Retry logic with a timeout
for _ in range(5): # retries
time.sleep(2)
print("Checking if toolbox is successfully started...")
if toolbox_server.poll() is None:
print("Toolbox server started successfully.")
break
else:
raise RuntimeError("Toolbox server failed to start after 5 retries.")
except subprocess.CalledProcessError as e:
print(e.stderr.decode("utf-8"))
print(e.stdout.decode("utf-8"))
raise RuntimeError(f"{e}\n\n{e.stderr.decode('utf-8')}") from e
yield

# Clean up toolbox server
toolbox_server.terminate()
toolbox_server.wait(timeout=5)
186 changes: 186 additions & 0 deletions packages/toolbox-core/tests/test_e2e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Copyright 2025 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.
import pytest
import pytest_asyncio

from toolbox_core.client import ToolboxClient
from toolbox_core.tool import ToolboxTool


# --- Shared Fixtures Defined at Module Level ---
@pytest_asyncio.fixture(scope="function")
async def toolbox():
"""Creates a ToolboxClient instance shared by all tests in this module."""
toolbox = ToolboxClient("http://localhost:5000")
try:
yield toolbox
finally:
await toolbox.close()


@pytest_asyncio.fixture(scope="function")
async def get_n_rows_tool(toolbox: ToolboxClient) -> ToolboxTool:
"""Load the 'get-n-rows' tool using the shared toolbox client."""
tool = await toolbox.load_tool("get-n-rows")
assert tool.__name__ == "get-n-rows"
return tool


@pytest.mark.asyncio
@pytest.mark.usefixtures("toolbox_server")
class TestBasicE2E:
@pytest.mark.parametrize(
"toolset_name, expected_length, expected_tools",
[
("my-toolset", 1, ["get-row-by-id"]),
("my-toolset-2", 2, ["get-n-rows", "get-row-by-id"]),
],
)
async def test_load_toolset_specific(
self,
toolbox: ToolboxClient,
toolset_name: str,
expected_length: int,
expected_tools: list[str],
):
"""Load a specific toolset"""
toolset = await toolbox.load_toolset(toolset_name)
assert len(toolset) == expected_length
tool_names = {tool.__name__ for tool in toolset}
assert tool_names == set(expected_tools)

async def test_run_tool(self, get_n_rows_tool: ToolboxTool):
"""Invoke a tool."""
response = await get_n_rows_tool(num_rows="2")

assert isinstance(response, str)
assert "row1" in response
assert "row2" in response
assert "row3" not in response

async def test_run_tool_missing_params(self, get_n_rows_tool: ToolboxTool):
"""Invoke a tool with missing params."""
with pytest.raises(TypeError, match="missing a required argument: 'num_rows'"):
await get_n_rows_tool()

async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool):
"""Invoke a tool with wrong param type."""
with pytest.raises(
Exception,
match='provided parameters were invalid: unable to parse value for "num_rows": .* not type "string"',
):
await get_n_rows_tool(num_rows=2)


@pytest.mark.asyncio
@pytest.mark.usefixtures("toolbox_server")
class TestBindParams:
async def test_bind_params(
self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool
):
"""Bind a param to an existing tool."""
new_tool = get_n_rows_tool.bind_parameters({"num_rows": "3"})
response = await new_tool()
assert isinstance(response, str)
assert "row1" in response
assert "row2" in response
assert "row3" in response
assert "row4" not in response

async def test_bind_params_callable(
self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool
):
"""Bind a callable param to an existing tool."""
new_tool = get_n_rows_tool.bind_parameters({"num_rows": lambda: "3"})
response = await new_tool()
assert isinstance(response, str)
assert "row1" in response
assert "row2" in response
assert "row3" in response
assert "row4" not in response


@pytest.mark.asyncio
@pytest.mark.usefixtures("toolbox_server")
class TestAuth:
async def test_run_tool_unauth_with_auth(
self, toolbox: ToolboxClient, auth_token2: str
):
"""Tests running a tool that doesn't require auth, with auth provided."""
tool = await toolbox.load_tool(
"get-row-by-id", auth_token_getters={"my-test-auth": lambda: auth_token2}
)
response = await tool(id="2")
assert "row2" in response

async def test_run_tool_no_auth(self, toolbox: ToolboxClient):
"""Tests running a tool requiring auth without providing auth."""
tool = await toolbox.load_tool("get-row-by-id-auth")
with pytest.raises(
Exception,
match="tool invocation not authorized. Please make sure your specify correct auth headers",
):
await tool(id="2")

async def test_run_tool_wrong_auth(self, toolbox: ToolboxClient, auth_token2: str):
"""Tests running a tool with incorrect auth. The tool
requires a different authentication than the one provided."""
tool = await toolbox.load_tool("get-row-by-id-auth")
auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token2})
with pytest.raises(
Exception,
match="tool invocation not authorized",
):
await auth_tool(id="2")

async def test_run_tool_auth(self, toolbox: ToolboxClient, auth_token1: str):
"""Tests running a tool with correct auth."""
tool = await toolbox.load_tool("get-row-by-id-auth")
auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token1})
response = await auth_tool(id="2")
assert "row2" in response

async def test_run_tool_param_auth_no_auth(self, toolbox: ToolboxClient):
"""Tests running a tool with a param requiring auth, without auth."""
tool = await toolbox.load_tool("get-row-by-email-auth")
with pytest.raises(
Exception,
match="One or more of the following authn services are required to invoke this tool: my-test-auth",
):
await tool()

async def test_run_tool_param_auth(self, toolbox: ToolboxClient, auth_token1: str):
"""Tests running a tool with a param requiring auth, with correct auth."""
tool = await toolbox.load_tool(
"get-row-by-email-auth",
auth_token_getters={"my-test-auth": lambda: auth_token1},
)
response = await tool()
assert "row4" in response
assert "row5" in response
assert "row6" in response

async def test_run_tool_param_auth_no_field(
self, toolbox: ToolboxClient, auth_token1: str
):
"""Tests running a tool with a param requiring auth, with insufficient auth."""
tool = await toolbox.load_tool(
"get-row-by-content-auth",
auth_token_getters={"my-test-auth": lambda: auth_token1},
)
with pytest.raises(
Exception,
match="no field named row_data in claims",
):
await tool()