Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b3374d451 | ||
|
|
b17c1745ab | ||
|
|
db4ce6b425 | ||
|
|
aa6a097e4f | ||
|
|
150a213ae2 |
@@ -38,5 +38,7 @@ def get(
|
||||
print_exc()
|
||||
raise exc
|
||||
finally:
|
||||
response.close()
|
||||
response.release_conn()
|
||||
# close connection if established
|
||||
if 'response' in locals():
|
||||
response.close()
|
||||
response.release_conn()
|
||||
|
||||
@@ -20,7 +20,7 @@ def put(
|
||||
assert isinstance(object_name, str)
|
||||
assert len(object_name) > 0
|
||||
# prepare for saving
|
||||
num_bytes = buffer.tell()
|
||||
num_bytes = len(buffer.getvalue())
|
||||
buffer.seek(0)
|
||||
# send data to bucket
|
||||
try:
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
"""Integration tests related to base CRUD functions."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
import minio
|
||||
import pytest
|
||||
|
||||
# from shared.datastore import connect_minio, delete, get, put
|
||||
from shared.datastore import delete, get, put
|
||||
|
||||
|
||||
def same_data(
|
||||
@@ -22,85 +24,94 @@ def same_data(
|
||||
data_a_bytes = data_a.read()
|
||||
data_b_bytes = data_b.read()
|
||||
# compare size
|
||||
logging.error(len(data_a_bytes))
|
||||
logging.error(len(data_b_bytes))
|
||||
if len(data_a_bytes) != len(data_b_bytes):
|
||||
logging.error(
|
||||
'data has different length: %s and %s',
|
||||
len(data_a_bytes),
|
||||
len(data_b_bytes),
|
||||
)
|
||||
return False
|
||||
# compare content
|
||||
if data_a_bytes != data_b_bytes:
|
||||
logging.error('data has different bytes')
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# def test_should_get_data(
|
||||
# minio_client,
|
||||
# data_in_minio,
|
||||
# ):
|
||||
# data, bucket_name, object_name = data_in_minio
|
||||
# received_data = get(
|
||||
# client=minio_client,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
def test_should_get_data(
|
||||
minio_client,
|
||||
data_in_minio,
|
||||
):
|
||||
data, bucket_name, object_name = data_in_minio
|
||||
received_data = get(
|
||||
client=minio_client,
|
||||
bucket_name=bucket_name,
|
||||
object_name=object_name,
|
||||
)
|
||||
|
||||
# assert isinstance(data, BytesIO)
|
||||
# assert same_data(data, received_data)
|
||||
assert isinstance(data, BytesIO)
|
||||
assert same_data(data, received_data)
|
||||
|
||||
|
||||
# def test_should_delete_data(
|
||||
# data_in_minio,
|
||||
# ):
|
||||
# _, bucket_name, object_name = data_in_minio
|
||||
# client = connect_minio()
|
||||
# delete(
|
||||
# client=client,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
# with pytest.raises(ValueError):
|
||||
# _ = get(
|
||||
# client=client,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
def test_should_delete_data(
|
||||
minio_client,
|
||||
data_in_minio,
|
||||
):
|
||||
_, bucket_name, object_name = data_in_minio
|
||||
delete(
|
||||
client=minio_client,
|
||||
bucket_name=bucket_name,
|
||||
object_name=object_name,
|
||||
)
|
||||
with pytest.raises(minio.error.S3Error):
|
||||
_ = get(
|
||||
client=minio_client,
|
||||
bucket_name=bucket_name,
|
||||
object_name=object_name,
|
||||
)
|
||||
|
||||
|
||||
# def test_should_put_data(
|
||||
# data,
|
||||
# ):
|
||||
# buffer, bucket_name, object_name = data
|
||||
# client = connect_minio()
|
||||
# put(
|
||||
# client=client,
|
||||
# buffer=buffer,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
# received_data = get(
|
||||
# client=client,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
# assert received_data == data
|
||||
def test_should_put_data(
|
||||
minio_client,
|
||||
data,
|
||||
):
|
||||
# prepare variables
|
||||
minio_bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||
minio_object_name = str(os.getenv('MINIO_OBJECT_NAME'))
|
||||
buffer = BytesIO(data)
|
||||
put(
|
||||
client=minio_client,
|
||||
buffer=buffer,
|
||||
bucket_name=minio_bucket_name,
|
||||
object_name=minio_object_name,
|
||||
)
|
||||
received_data = get(
|
||||
client=minio_client,
|
||||
bucket_name=minio_bucket_name,
|
||||
object_name=minio_object_name,
|
||||
)
|
||||
assert isinstance(received_data, BytesIO)
|
||||
assert same_data(received_data, buffer)
|
||||
|
||||
|
||||
# def test_should_update_data(
|
||||
# data_in_minio,
|
||||
# ):
|
||||
# buffer, bucket_name, object_name = data_in_minio
|
||||
# client = connect_minio()
|
||||
# put(
|
||||
# client=client,
|
||||
# buffer=buffer,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
# received_data = get(
|
||||
# client=client,
|
||||
# bucket_name=bucket_name,
|
||||
# object_name=object_name,
|
||||
# )
|
||||
# assert received_data == buffer
|
||||
def test_should_update_data(
|
||||
minio_client,
|
||||
data_in_minio,
|
||||
):
|
||||
buffer, bucket_name, object_name = data_in_minio
|
||||
put(
|
||||
client=minio_client,
|
||||
buffer=buffer,
|
||||
bucket_name=bucket_name,
|
||||
object_name=object_name,
|
||||
)
|
||||
received_data = get(
|
||||
client=minio_client,
|
||||
bucket_name=bucket_name,
|
||||
object_name=object_name,
|
||||
)
|
||||
assert same_data(received_data, buffer)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"""Integration test configurations."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
from collections.abc import Iterator
|
||||
@@ -8,65 +7,25 @@ from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dotenv import load_dotenv
|
||||
from minio import Minio
|
||||
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 = {
|
||||
'MINIO_ENDPOINT': 'localhost:9000',
|
||||
'MINIO_ACCESS_KEY': 'test-access-key',
|
||||
'MINIO_SECRET_KEY': 'test-secret-key',
|
||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||
'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)
|
||||
def populate_env(
|
||||
request: pytest.FixtureRequest,
|
||||
setup_infrastructure,
|
||||
) -> None:
|
||||
"""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
|
||||
for key, val in env_var_map.items():
|
||||
os.environ[key] = val
|
||||
@@ -81,7 +40,6 @@ def populate_env(
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def minio_client(
|
||||
setup_infrastructure,
|
||||
populate_env,
|
||||
) -> Iterator[Minio]:
|
||||
# prepare arguments
|
||||
@@ -101,6 +59,15 @@ def minio_client(
|
||||
client.make_bucket(bucket_name=minio_bucket_name)
|
||||
# expose client
|
||||
yield client
|
||||
# remove objects left behind by tests
|
||||
for obj in client.list_objects(bucket_name=minio_bucket_name, recursive=True):
|
||||
client.remove_object(
|
||||
bucket_name=obj.bucket_name,
|
||||
object_name=obj.object_name,
|
||||
)
|
||||
# remove bucket
|
||||
client.remove_bucket(bucket_name=minio_bucket_name)
|
||||
assert not client.bucket_exists(bucket_name=minio_bucket_name)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -123,7 +90,7 @@ def data_in_minio(
|
||||
# convert data
|
||||
buffer = BytesIO(data)
|
||||
# prepare for saving
|
||||
num_bytes = buffer.tell()
|
||||
num_bytes = len(buffer.getvalue())
|
||||
buffer.seek(0)
|
||||
# send data to bucket
|
||||
minio_client.put_object(
|
||||
|
||||
Reference in New Issue
Block a user