|
| 1 | +from typing import Callable, Dict |
| 2 | +import pytest |
| 3 | +import os |
| 4 | +from _pytest.fixtures import FixtureRequest |
| 5 | + |
| 6 | +S3_BUCKET_NAME = os.getenv("LLS_FILES_S3_BUCKET_NAME", "opendatathub-tests-llama-stack") |
| 7 | +S3_BUCKET_REGION = os.getenv("LLS_FILES_S3_REGION", "us-east-1") |
| 8 | +S3_BUCKET_ENDPOINT_URL = os.getenv("AWS_DEFAULT_ENDPOINT", "https://s3.us-east-1.amazonaws.com") |
| 9 | +S3_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "minio") |
| 10 | +S3_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "minio123") |
| 11 | +S3_AUTO_CREATE_BUCKET = os.getenv("LLS_FILES_S3_AUTO_CREATE_BUCKET", "true") |
| 12 | + |
| 13 | +@pytest.fixture(scope="class") |
| 14 | +def files_provider_config_factory( |
| 15 | + request: FixtureRequest, |
| 16 | +) -> Callable[[str], list[Dict[str, str]]]: |
| 17 | + """ |
| 18 | + Factory fixture for configuring external files providers and returning their configuration. |
| 19 | +
|
| 20 | + This fixture returns a factory function that can configure additional files storage providers |
| 21 | + (such as S3/minio) and return the necessary environment variables |
| 22 | + for configuring the LlamaStack server to use these providers. |
| 23 | +
|
| 24 | + Args: |
| 25 | + request: Pytest fixture request object for accessing other fixtures |
| 26 | +
|
| 27 | + Returns: |
| 28 | + Callable[[str], list[Dict[str, str]]]: Factory function that takes a provider name |
| 29 | + and returns a list of environment variable dictionaries |
| 30 | +
|
| 31 | + Supported Providers: |
| 32 | + - "local": defaults to using just local filesystem |
| 33 | + - "s3": a remote S3/Minio storage provider |
| 34 | +
|
| 35 | + Environment Variables by Provider: |
| 36 | + - "s3": |
| 37 | + * S3_BUCKET_NAME: Name of the S3/Minio bucket |
| 38 | + * AWS_DEFAULT_REGION: Region of the S3/Minio bucket |
| 39 | + * S3_ENDPOINT_URL: Endpoint URL of the S3/Minio bucket |
| 40 | + * AWS_ACCESS_KEY_ID: Access key ID for the S3/Minio bucket |
| 41 | + * AWS_SECRET_ACCESS_KEY: Secret access key for the S3/Minio bucket |
| 42 | + * S3_AUTO_CREATE_BUCKET: Whether to automatically create the S3/Minio bucket if it doesn't exist |
| 43 | +
|
| 44 | + Example: |
| 45 | + def test_with_s3(files_provider_config_factory): |
| 46 | + env_vars = files_provider_config_factory("s3") |
| 47 | + # env_vars contains S3_BUCKET_NAME, S3_BUCKET_ENDPOINT_URL, etc. |
| 48 | + """ |
| 49 | + |
| 50 | + def _factory(provider_name: str) -> list[Dict[str, str]]: |
| 51 | + env_vars: list[dict[str, str]] = [] |
| 52 | + |
| 53 | + if provider_name is "local" or provider_name is None: |
| 54 | + # Default case - no additional environment variables needed |
| 55 | + pass |
| 56 | + elif provider_name == "s3": |
| 57 | + env_vars.append({"name": "ENABLE_S3", "value": "s3"}) |
| 58 | + env_vars.append({"name": "S3_BUCKET_NAME", "value": S3_BUCKET_NAME}) |
| 59 | + env_vars.append({"name": "AWS_DEFAULT_REGION", "value": S3_BUCKET_REGION}) |
| 60 | + env_vars.append({"name": "S3_ENDPOINT_URL", "value": S3_BUCKET_ENDPOINT_URL}) |
| 61 | + env_vars.append({"name": "AWS_ACCESS_KEY_ID", "value": S3_ACCESS_KEY_ID}) |
| 62 | + env_vars.append({"name": "AWS_SECRET_ACCESS_KEY", "value": S3_SECRET_ACCESS_KEY}) |
| 63 | + env_vars.append({"name": "S3_AUTO_CREATE_BUCKET", "value": S3_AUTO_CREATE_BUCKET}) |
| 64 | + |
| 65 | + return env_vars |
| 66 | + |
| 67 | + return _factory |
0 commit comments