added minio adapter and tests
This commit is contained in:
@@ -6,8 +6,14 @@ import pytest
|
||||
import redis
|
||||
import structlog
|
||||
import logging
|
||||
from minio import Minio
|
||||
|
||||
from testcontainers.redis import RedisContainer
|
||||
from testcontainers.minio import MinioContainer
|
||||
|
||||
MINIO_ACCESS_KEY = "minioadmin"
|
||||
MINIO_SECRET_KEY = "minioadmin"
|
||||
MINIO_BUCKET = "test-bucket"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
@@ -16,6 +22,10 @@ def configure_logging() -> None:
|
||||
# Configure structlog
|
||||
structlog.configure(
|
||||
processors=[
|
||||
structlog.stdlib.filter_by_level,
|
||||
structlog.stdlib.add_logger_name,
|
||||
structlog.stdlib.add_log_level,
|
||||
structlog.processors.TimeStamper(fmt="iso"),
|
||||
structlog.processors.JSONRenderer(),
|
||||
],
|
||||
logger_factory=structlog.stdlib.LoggerFactory(),
|
||||
@@ -27,12 +37,11 @@ def configure_logging() -> None:
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def redis_container() -> Generator[str]:
|
||||
def redis_container() -> Generator[str, None, None]:
|
||||
"""Set up a Redis container for testing and yield the Redis URI."""
|
||||
# Start container
|
||||
container = RedisContainer(
|
||||
image="redis/redis-stack:7.2.0-v0",
|
||||
port=6379,
|
||||
)
|
||||
container.start()
|
||||
# Set environment variable for Redis URI
|
||||
@@ -46,13 +55,42 @@ def redis_container() -> Generator[str]:
|
||||
container.stop()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def minio_container() -> Generator[dict[str, str], None, None]:
|
||||
"""Set up a Minio container for testing and yield the Minio URI."""
|
||||
# Start container
|
||||
container = MinioContainer(
|
||||
image="minio/minio:latest",
|
||||
access_key=MINIO_ACCESS_KEY,
|
||||
secret_key=MINIO_SECRET_KEY,
|
||||
)
|
||||
container.start()
|
||||
# Build environment variables dictionary
|
||||
minio_host = container.get_container_host_ip()
|
||||
minio_port = container.get_exposed_port(9000)
|
||||
minio_endpoint = f"{minio_host}:{minio_port}"
|
||||
env_vars = {
|
||||
"MINIO_ENDPOINT": minio_endpoint,
|
||||
"MINIO_ACCESS_KEY": MINIO_ACCESS_KEY,
|
||||
"MINIO_SECRET_KEY": MINIO_SECRET_KEY,
|
||||
"MINIO_BUCKET": MINIO_BUCKET,
|
||||
}
|
||||
|
||||
yield env_vars
|
||||
|
||||
# Stop container
|
||||
container.stop()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def set_environment_variables(
|
||||
redis_container: str,
|
||||
minio_container: dict[str, str],
|
||||
) -> Generator[dict[str, str]]:
|
||||
"""Set environment variables needed for tests."""
|
||||
# Build environment variables dictionary
|
||||
env_vars = {"REDIS_URI": redis_container}
|
||||
env_vars.update(minio_container)
|
||||
# Set environment variables
|
||||
for key, value in env_vars.items():
|
||||
os.environ[key] = value
|
||||
@@ -78,3 +116,26 @@ def raw_redis_client(redis_container: str) -> Generator[redis.Redis]:
|
||||
# Cleanup
|
||||
client.flushall()
|
||||
client.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def raw_minio_client(minio_container: dict[str, str]) -> Generator[Minio]:
|
||||
"""Provide a raw Minio client connected to the test Minio container."""
|
||||
# Connect client
|
||||
client = Minio(
|
||||
endpoint=minio_container["MINIO_ENDPOINT"],
|
||||
access_key=minio_container["MINIO_ACCESS_KEY"],
|
||||
secret_key=minio_container["MINIO_SECRET_KEY"],
|
||||
secure=False,
|
||||
)
|
||||
# Ensure bucket exists
|
||||
bucket_name = minio_container["MINIO_BUCKET"]
|
||||
if not client.bucket_exists(bucket_name):
|
||||
client.make_bucket(bucket_name)
|
||||
|
||||
yield client
|
||||
|
||||
# Cleanup
|
||||
objects = client.list_objects(bucket_name, recursive=True)
|
||||
for obj in objects:
|
||||
client.remove_object(bucket_name, obj.object_name)
|
||||
Reference in New Issue
Block a user