Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b3374d451 | ||
|
|
b17c1745ab | ||
|
|
db4ce6b425 | ||
|
|
aa6a097e4f | ||
|
|
150a213ae2 |
@@ -38,5 +38,7 @@ def get(
|
|||||||
print_exc()
|
print_exc()
|
||||||
raise exc
|
raise exc
|
||||||
finally:
|
finally:
|
||||||
|
# close connection if established
|
||||||
|
if 'response' in locals():
|
||||||
response.close()
|
response.close()
|
||||||
response.release_conn()
|
response.release_conn()
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ def put(
|
|||||||
assert isinstance(object_name, str)
|
assert isinstance(object_name, str)
|
||||||
assert len(object_name) > 0
|
assert len(object_name) > 0
|
||||||
# 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
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
"""Integration tests related to base CRUD functions."""
|
"""Integration tests related to base CRUD functions."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
|
import minio
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
# from shared.datastore import connect_minio, delete, get, put
|
from shared.datastore import delete, get, put
|
||||||
|
|
||||||
|
|
||||||
def same_data(
|
def same_data(
|
||||||
@@ -22,85 +24,94 @@ def same_data(
|
|||||||
data_a_bytes = data_a.read()
|
data_a_bytes = data_a.read()
|
||||||
data_b_bytes = data_b.read()
|
data_b_bytes = data_b.read()
|
||||||
# compare size
|
# compare size
|
||||||
logging.error(len(data_a_bytes))
|
|
||||||
logging.error(len(data_b_bytes))
|
|
||||||
if len(data_a_bytes) != 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
|
return False
|
||||||
# compare content
|
# compare content
|
||||||
if data_a_bytes != data_b_bytes:
|
if data_a_bytes != data_b_bytes:
|
||||||
|
logging.error('data has different bytes')
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# def test_should_get_data(
|
def test_should_get_data(
|
||||||
# minio_client,
|
minio_client,
|
||||||
# data_in_minio,
|
data_in_minio,
|
||||||
# ):
|
):
|
||||||
# data, bucket_name, object_name = data_in_minio
|
data, bucket_name, object_name = data_in_minio
|
||||||
# received_data = get(
|
received_data = get(
|
||||||
# client=minio_client,
|
client=minio_client,
|
||||||
# bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
# object_name=object_name,
|
object_name=object_name,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# assert isinstance(data, BytesIO)
|
assert isinstance(data, BytesIO)
|
||||||
# assert same_data(data, received_data)
|
assert same_data(data, received_data)
|
||||||
|
|
||||||
|
|
||||||
# def test_should_delete_data(
|
def test_should_delete_data(
|
||||||
# data_in_minio,
|
minio_client,
|
||||||
# ):
|
data_in_minio,
|
||||||
# _, bucket_name, object_name = data_in_minio
|
):
|
||||||
# client = connect_minio()
|
_, bucket_name, object_name = data_in_minio
|
||||||
# delete(
|
delete(
|
||||||
# client=client,
|
client=minio_client,
|
||||||
# bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
# object_name=object_name,
|
object_name=object_name,
|
||||||
# )
|
)
|
||||||
# with pytest.raises(ValueError):
|
with pytest.raises(minio.error.S3Error):
|
||||||
# _ = get(
|
_ = get(
|
||||||
# client=client,
|
client=minio_client,
|
||||||
# bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
# object_name=object_name,
|
object_name=object_name,
|
||||||
# )
|
)
|
||||||
|
|
||||||
|
|
||||||
# def test_should_put_data(
|
def test_should_put_data(
|
||||||
# data,
|
minio_client,
|
||||||
# ):
|
data,
|
||||||
# buffer, bucket_name, object_name = data
|
):
|
||||||
# client = connect_minio()
|
# prepare variables
|
||||||
# put(
|
minio_bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||||
# client=client,
|
minio_object_name = str(os.getenv('MINIO_OBJECT_NAME'))
|
||||||
# buffer=buffer,
|
buffer = BytesIO(data)
|
||||||
# bucket_name=bucket_name,
|
put(
|
||||||
# object_name=object_name,
|
client=minio_client,
|
||||||
# )
|
buffer=buffer,
|
||||||
# received_data = get(
|
bucket_name=minio_bucket_name,
|
||||||
# client=client,
|
object_name=minio_object_name,
|
||||||
# bucket_name=bucket_name,
|
)
|
||||||
# object_name=object_name,
|
received_data = get(
|
||||||
# )
|
client=minio_client,
|
||||||
# assert received_data == data
|
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(
|
def test_should_update_data(
|
||||||
# data_in_minio,
|
minio_client,
|
||||||
# ):
|
data_in_minio,
|
||||||
# buffer, bucket_name, object_name = data_in_minio
|
):
|
||||||
# client = connect_minio()
|
buffer, bucket_name, object_name = data_in_minio
|
||||||
# put(
|
put(
|
||||||
# client=client,
|
client=minio_client,
|
||||||
# buffer=buffer,
|
buffer=buffer,
|
||||||
# bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
# object_name=object_name,
|
object_name=object_name,
|
||||||
# )
|
)
|
||||||
# received_data = get(
|
received_data = get(
|
||||||
# client=client,
|
client=minio_client,
|
||||||
# bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
# object_name=object_name,
|
object_name=object_name,
|
||||||
# )
|
)
|
||||||
# assert received_data == buffer
|
assert same_data(received_data, buffer)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pytest.main()
|
pytest.main()
|
||||||
|
|||||||
@@ -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 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
|
@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