Skip to content

Commit

Permalink
refactor (sdk:tests): add and integrated fixtures for programmatic ac…
Browse files Browse the repository at this point in the history
…cess to sdk (management, sdk routing & cli commands)
  • Loading branch information
aybruhm committed Dec 25, 2024
1 parent 59829b8 commit e492d70
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 48 deletions.
5 changes: 5 additions & 0 deletions agenta-cli/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# rename [environ] to the environment you're working/testing on
# for local, rename it to local --- .env.local
_SECRET_KEY=xxxxxxx
AWS_PROFILE_NAME=xxxxxxxxxx
AGENTA_AUTH_KEY_SECRET_ARN=xxxxxxxxxxxxx
114 changes: 112 additions & 2 deletions agenta-cli/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions agenta-cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pytest-asyncio = "^0.24.0"
mypy = "^1.13.0"
pytest-xdist = "^3.6.1"
uvicorn = "^0.34.0"
requests = "^2.32.3"
pexpect = "^4.9.0"
boto3 = "^1.35.87"

[tool.pytest.ini_options]
asyncio_default_fixture_loop_scope = "function"
Expand Down
Empty file.
Empty file.
29 changes: 19 additions & 10 deletions agenta-cli/tests/cli/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import os
import toml
import shutil
Expand All @@ -8,10 +9,7 @@
import httpx
import pytest


AGENTA_API_KEY = os.environ.get("AGENTA_API_KEY")
AGENTA_HOST = os.environ.get("AGENTA_HOST", "http://localhost")
API_BASE_URL = f"{AGENTA_HOST}/api/"
from tests.conftest import get_admin_user_credentials, API_BASE_URL


def agenta_executable():
Expand All @@ -29,7 +27,9 @@ def get_assets_folder(example_folder: str):
return assets_folder


def retrieve_app_id_from_path_and_remove_application(assets_dir):
def retrieve_app_id_from_path_and_remove_application(
assets_dir: Path, access_token: str
):
"""
Retrieve the app_id from the config.toml file and remove the application
"""
Expand All @@ -42,7 +42,7 @@ def retrieve_app_id_from_path_and_remove_application(assets_dir):
# Delete application
response = httpx.delete(
f"{API_BASE_URL}apps/{app_id}",
headers={"Authorization": f"ApiKey {AGENTA_API_KEY}"},
headers={"Authorization": f"ApiKey {access_token}"},
timeout=httpx.Timeout(timeout=6, read=None, write=5),
)
response.raise_for_status()
Expand All @@ -67,15 +67,24 @@ def cleanup_created_test_files(assets_dir: Path):
print(f"Removed: {config_file}")


def get_programmatic_access_credentials():
"""
Retrieve the admin user's credentials for API testing.
"""

user_credentials = get_admin_user_credentials()
return str(user_credentials).strip("ApiKey ")


@pytest.fixture
def cleanup_application_and_files():
"""
Factory fixture to ensure the application and test files are cleaned up after each test class, with support for dynamic folder input.
"""

def _cleanup_application_and_files(folder_name):
def _cleanup_application_and_files(folder_name, access_token):
assets_dir = get_assets_folder(folder_name)
retrieve_app_id_from_path_and_remove_application(assets_dir)
retrieve_app_id_from_path_and_remove_application(assets_dir, access_token)
cleanup_created_test_files(assets_dir)

yield "ok"
Expand All @@ -95,7 +104,7 @@ def run_agenta_init(user_inputs: List[str], example_folder: str):
# Construct the command with the provided inputs
executable_path = agenta_executable()
child = pexpect.spawn(
command=f"{executable_path} init", encoding="utf-8", timeout=5
command=f"{executable_path} init", encoding="utf-8", timeout=10
)

for input in user_inputs:
Expand Down Expand Up @@ -123,7 +132,7 @@ def run_variant_serve(user_inputs: List[str], example_folder: str):
# Construct the command with the provided inputs
executable_path = agenta_executable()
child = pexpect.spawn(
command=f"{executable_path} variant serve app.py", encoding="utf-8", timeout=5
command=f"{executable_path} variant serve app.py", encoding="utf-8", timeout=10
)

for input in user_inputs:
Expand Down
29 changes: 17 additions & 12 deletions agenta-cli/tests/cli/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@


class TestAgentaInitCommand:
@pytest.fixture(autouse=True)
def _setup(self):
self.asset_example_folder = "greetings"
self.assets_folder = str(
get_assets_folder(example_folder=self.asset_example_folder)
@pytest.fixture(scope="class", autouse=True)
def _setup(self, request):
request.cls.asset_example_folder = "greetings"
request.cls.assets_folder = str(
get_assets_folder(example_folder=request.cls.asset_example_folder)
)
request.cls.api_key = get_programmatic_access_credentials()

@pytest.mark.cli_testing
def test_cloud_blank_app_success(self, cleanup_application_and_files):
# ARRANGE: Prepare test data
app_name = f"greetings_{uuid.uuid4().hex[:6]}"
where_to_run_agenta = "\n"
use_this_key = "n"
provide_api_key = os.environ.get("AGENTA_API_KEY")
provide_api_key = self.api_key

# ACT: Add configuration
inputs = [
f"{app_name}\n",
where_to_run_agenta,
use_this_key,
provide_api_key,
f"{provide_api_key}\n",
]
result = run_agenta_init(inputs, self.asset_example_folder)
cli_output = next(result)
Expand All @@ -50,7 +51,9 @@ def test_cloud_blank_app_success(self, cleanup_application_and_files):
assert agentaignore_path.exists()

# CLEANUP: Remove application from backend, db and local filesystem
cleanup = cleanup_application_and_files(self.asset_example_folder)
cleanup = cleanup_application_and_files(
self.asset_example_folder, provide_api_key
)
assert next(cleanup) == "ok"

@pytest.mark.cli_testing
Expand All @@ -59,14 +62,14 @@ def test_cloud_blank_app_already_exists(self, cleanup_application_and_files):
app_name = f"greetings_{uuid.uuid4().hex[:6]}"
where_to_run_agenta = "\n"
use_this_key = "N"
provide_api_key = os.environ.get("AGENTA_API_KEY")
provide_api_key = self.api_key

# ACT: Add configuration
inputs = [
f"{app_name}\n",
where_to_run_agenta,
use_this_key,
provide_api_key,
f"{provide_api_key}\n",
]
result_1 = run_agenta_init(
inputs, self.asset_example_folder
Expand All @@ -82,7 +85,9 @@ def test_cloud_blank_app_already_exists(self, cleanup_application_and_files):
assert "App with the same name already exists" in cli_output["output"]

# CLEANUP: Remove application from backend, db and local filesystem
cleanup = cleanup_application_and_files(self.asset_example_folder)
cleanup = cleanup_application_and_files(
self.asset_example_folder, provide_api_key
)
assert next(cleanup) == "ok"

@pytest.mark.cli_testing
Expand All @@ -98,7 +103,7 @@ def test_cloud_blank_app_with_invalid_credential(self):
inputs = [
f"{app_name}\n",
where_to_run_agenta,
provide_api_key,
f"{provide_api_key}\n",
]
result = run_agenta_init(inputs, self.asset_example_folder)
cli_output = next(result)
Expand Down
Loading

0 comments on commit e492d70

Please sign in to comment.