updated to use server db for testing
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
"""Integration test configurations."""
|
"""Integration test configurations."""
|
||||||
|
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
@@ -8,65 +7,25 @@ from io import BytesIO
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from dotenv import load_dotenv
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from testcontainers.core.container import inside_container
|
|
||||||
from testcontainers.minio import MinioContainer
|
|
||||||
|
|
||||||
|
|
||||||
class FixedMinioContainer(MinioContainer):
|
|
||||||
"""Fixed Minio Container to allow running inside CI pipeline."""
|
|
||||||
|
|
||||||
def get_container_host_ip(self) -> str:
|
|
||||||
if inside_container() and Path('/var/run/docker.sock').exists():
|
|
||||||
return self.get_docker_client().gateway_ip(self._container.id)
|
|
||||||
return super().get_container_host_ip()
|
|
||||||
|
|
||||||
|
|
||||||
env_var_map = {
|
env_var_map = {
|
||||||
'MINIO_ENDPOINT': 'localhost:9000',
|
|
||||||
'MINIO_ACCESS_KEY': 'test-access-key',
|
|
||||||
'MINIO_SECRET_KEY': 'test-secret-key',
|
|
||||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
'MINIO_OBJECT_NAME': '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX',
|
'MINIO_OBJECT_NAME': '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX',
|
||||||
}
|
}
|
||||||
container_map = {
|
|
||||||
'minio': FixedMinioContainer(
|
|
||||||
port=env_var_map['MINIO_ENDPOINT'].rsplit(':', maxsplit=1)[-1],
|
|
||||||
access_key=env_var_map['MINIO_ACCESS_KEY'],
|
|
||||||
secret_key=env_var_map['MINIO_SECRET_KEY'],
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session', autouse=True)
|
|
||||||
def setup_infrastructure(request: pytest.FixtureRequest) -> None:
|
|
||||||
"""Prepare infrastructure for integration test."""
|
|
||||||
# prepare infrastructure
|
|
||||||
for container in container_map.values():
|
|
||||||
container.start()
|
|
||||||
# update env var map
|
|
||||||
minio_host = container_map['minio'].get_container_host_ip()
|
|
||||||
minio_port = container_map['minio'].get_exposed_port(
|
|
||||||
port=env_var_map['MINIO_ENDPOINT'].rsplit(':', maxsplit=1)[-1],
|
|
||||||
)
|
|
||||||
env_var_map['MINIO_ENDPOINT'] = f'{minio_host}:{minio_port}'
|
|
||||||
logging.error('MINIO_ENDPOINT: %s', env_var_map['MINIO_ENDPOINT'])
|
|
||||||
|
|
||||||
# ensure cleanup
|
|
||||||
def cleanup_infrastructure():
|
|
||||||
for container in container_map.values():
|
|
||||||
container.stop()
|
|
||||||
|
|
||||||
request.addfinalizer(cleanup_infrastructure)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session', autouse=True)
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
def populate_env(
|
def populate_env(
|
||||||
request: pytest.FixtureRequest,
|
request: pytest.FixtureRequest,
|
||||||
setup_infrastructure,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Populate environment with variables used for testing."""
|
"""Populate environment with variables used for testing."""
|
||||||
|
# read env-file for local testing
|
||||||
|
load_dotenv(
|
||||||
|
dotenv_path=Path(__file__).parent.parent.parent.parent.parent / 'server.env',
|
||||||
|
)
|
||||||
# update env
|
# update env
|
||||||
for key, val in env_var_map.items():
|
for key, val in env_var_map.items():
|
||||||
os.environ[key] = val
|
os.environ[key] = val
|
||||||
@@ -81,7 +40,6 @@ def populate_env(
|
|||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def minio_client(
|
def minio_client(
|
||||||
setup_infrastructure,
|
|
||||||
populate_env,
|
populate_env,
|
||||||
) -> Iterator[Minio]:
|
) -> Iterator[Minio]:
|
||||||
# prepare arguments
|
# prepare arguments
|
||||||
@@ -101,6 +59,15 @@ def minio_client(
|
|||||||
client.make_bucket(bucket_name=minio_bucket_name)
|
client.make_bucket(bucket_name=minio_bucket_name)
|
||||||
# expose client
|
# expose client
|
||||||
yield client
|
yield client
|
||||||
|
# remove objects left behind by tests
|
||||||
|
for obj_name in client.list_objects(bucket_name=minio_bucket_name, recursive=True):
|
||||||
|
client.remove_object(
|
||||||
|
bucket_name=minio_bucket_name,
|
||||||
|
object_name=obj_name,
|
||||||
|
)
|
||||||
|
# remove bucket
|
||||||
|
client.remove_bucket(bucket_name=minio_bucket_name)
|
||||||
|
assert not client.bucket_exists(bucket_name=minio_bucket_name)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -123,7 +90,7 @@ def data_in_minio(
|
|||||||
# convert data
|
# convert data
|
||||||
buffer = BytesIO(data)
|
buffer = BytesIO(data)
|
||||||
# prepare for saving
|
# prepare for saving
|
||||||
num_bytes = buffer.tell()
|
num_bytes = len(buffer.getvalue())
|
||||||
buffer.seek(0)
|
buffer.seek(0)
|
||||||
# send data to bucket
|
# send data to bucket
|
||||||
minio_client.put_object(
|
minio_client.put_object(
|
||||||
|
|||||||
Reference in New Issue
Block a user