Merge in RR/pyinfra from 2.0.0-input-output-file-pattern-for-download-strategy to 2.0.0
Squashed commit of the following:
commit c7ce79ebbeace6a8cb7925ed69eda2d7cd2a4783
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Jun 24 12:35:29 2022 +0200
refactor
commit 80f04e544962760adb2dc60c9dd03ccca22167d6
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Jun 24 11:06:10 2022 +0200
refactoring of component factory, callback and client-pipeline getter
commit 6c024e1a789e1d55f0739c6846e5c02e8b7c943d
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 20:04:10 2022 +0200
operations section in config cleaned up; added upload formatter
commit c85800aefc224967cea591c1ec4cf1aaa3ac8215
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 19:22:51 2022 +0200
refactoring; removed obsolete config entries and code
commit 4be125952d82dc868935c8c73ad87fd8f0bd1d6c
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 19:14:47 2022 +0200
removed obsolete code
commit ac69a5c8e3f1e2fd7e828a17eeab97984f4f9746
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 18:58:41 2022 +0200
refactoring: rm dl strat module
commit efd36d0fc4f8f36d267bfa9d35415811fe723ccc
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 18:33:51 2022 +0200
refactoring: multi dl strat -> downloader, rm single dl strat
commit afffdeb993500a6abdb6fe85a549e3d6e97e9ee7
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 16:39:22 2022 +0200
operations section in config cleaned up
commit 671129af3e343490e0fb277a2b0329aa3027fd73
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Thu Jun 23 16:09:16 2022 +0200
rename prometheus metric name to include service name
commit 932a3e314b382315492aecab95b1f02f2916f8a6
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 14:43:23 2022 +0200
cleaned up file descr mngr
commit 79350b4ce71fcd095ed6a5e1d3a598ea246fae53
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 12:26:15 2022 +0200
refactoring WIP: moving response stratgey logic into storage strategy (needs to be refactored as well, later) and file descr mngr. Here the moved code needs to be cleaned up.
commit 7e48c66f0c378b25a433a4034eefdc8a0957e775
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 12:00:48 2022 +0200
refactoring; removed operation / response folder from output path
commit 8e6cbdaf23c48f6eeb52512b7f382d5727e206d6
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 11:08:37 2022 +0200
refactoring; added operation -> file pattern mapping to file descr mngr (mainly for self-documentaton purposes)
commit 2c80d7cec0cc171e099e5b13aadd2ae0f9bf4f02
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 10:59:57 2022 +0200
refactoring: introduced input- and output-file specific methods to file descr mngr
commit ecced37150eaac3008cc1b01b235e5f7135e504b
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 10:43:26 2022 +0200
refactoring
commit 3828341e98861ff8d63035ee983309ad5064bb30
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 23 10:42:46 2022 +0200
refactoring
commit 9a7c412523d467af40feb6924823ca89e28aadfe
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jun 22 17:04:54 2022 +0200
add prometheus metric name for default operation
commit d207b2e274ba53b2a21a18c367bb130fb05ee1cd
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jun 22 17:02:55 2022 +0200
Merge config
commit d3fdf36b12d8def18810454765e731599b833bfc
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Jun 22 17:01:12 2022 +0200
added fixmes / todos
commit f49d0b9cb7764473ef9d127bc5d88525a4a16a23
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jun 22 16:28:25 2022 +0200
update script
... and 47 more commits
223 lines
6.4 KiB
Python
223 lines
6.4 KiB
Python
import json
|
|
|
|
from pyinfra.config import CONFIG as MAIN_CONFIG
|
|
from pyinfra.visitor.response_formatter.formatters.identity import IdentityResponseFormatter
|
|
from pyinfra.visitor.strategies.blob_parsing.dynamic import DynamicParsingStrategy
|
|
|
|
MAIN_CONFIG["retry"]["delay"] = 0.1
|
|
MAIN_CONFIG["retry"]["jitter"] = (0.1, 0.2)
|
|
|
|
from pyinfra.default_objects import get_component_factory
|
|
from test.config import CONFIG as TEST_CONFIG
|
|
|
|
import logging
|
|
import time
|
|
from unittest.mock import Mock
|
|
|
|
import pika
|
|
import pytest
|
|
from testcontainers.compose import DockerCompose
|
|
|
|
from pyinfra.exceptions import UnknownClient
|
|
from pyinfra.locations import TEST_DIR, COMPOSE_PATH
|
|
from pyinfra.queue.queue_manager.pika_queue_manager import PikaQueueManager, get_connection_params
|
|
from pyinfra.queue.queue_manager.queue_manager import QueueManager
|
|
from pyinfra.storage.adapters.azure import AzureStorageAdapter
|
|
from pyinfra.storage.adapters.s3 import S3StorageAdapter
|
|
from pyinfra.storage.clients.azure import get_azure_client
|
|
from pyinfra.storage.clients.s3 import get_s3_client
|
|
from pyinfra.storage.storage import Storage
|
|
from pyinfra.visitor import QueueVisitor
|
|
from pyinfra.visitor.strategies.response.forwarding import ForwardingStrategy
|
|
from pyinfra.visitor.strategies.response.storage import StorageStrategy
|
|
from test.queue.queue_manager_mock import QueueManagerMock
|
|
from test.storage.adapter_mock import StorageAdapterMock
|
|
from test.storage.client_mock import StorageClientMock
|
|
|
|
|
|
logging.basicConfig()
|
|
logger = logging.getLogger()
|
|
|
|
|
|
# TODO: refactor all fixtures into cleanly separated modules
|
|
pytest_plugins = [
|
|
"test.fixtures.consumer",
|
|
"test.fixtures.input",
|
|
"test.fixtures.pdf",
|
|
"test.fixtures.server",
|
|
"test.integration_tests.serve_test",
|
|
]
|
|
|
|
|
|
logging.getLogger("PIL.PngImagePlugin").setLevel(level=logging.CRITICAL + 1)
|
|
logging.getLogger("waitress").setLevel(level=logging.CRITICAL + 1)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mute_logger():
|
|
if not TEST_CONFIG.logging:
|
|
logger.setLevel(logging.CRITICAL + 1)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def bucket_name():
|
|
return "pyinfra-test-bucket"
|
|
|
|
|
|
@pytest.fixture
|
|
def storage_data():
|
|
with open(f"{TEST_DIR}/test_data/test_data.TEXT.json", "r") as f:
|
|
data = json.load(f)
|
|
return data
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_response(storage_data):
|
|
response = Mock(status_code=200)
|
|
response.json.return_value = storage_data
|
|
return response
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_payload():
|
|
return json.dumps({"dossierId": "test", "fileId": "test"})
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_make_load_data():
|
|
def load_data(payload):
|
|
return storage_data
|
|
|
|
return load_data
|
|
|
|
|
|
# def pytest_make_parametrize_id(config, val, argname):
|
|
# return f"\n\t{argname}={val}\n"
|
|
|
|
|
|
@pytest.fixture
|
|
def storage(client_name, bucket_name, s3_backend, docker_compose):
|
|
logger.debug("Setup for storage")
|
|
storage = Storage(get_adapter(client_name, s3_backend))
|
|
storage.make_bucket(bucket_name)
|
|
storage.clear_bucket(bucket_name)
|
|
yield storage
|
|
logger.debug("Teardown for storage")
|
|
storage.clear_bucket(bucket_name)
|
|
|
|
|
|
@pytest.fixture(params=["minio", "aws"])
|
|
def s3_backend(request):
|
|
return request.param
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def docker_compose(sleep_seconds=30):
|
|
if TEST_CONFIG.use_docker_fixture:
|
|
logger.info(f"Starting docker containers with {COMPOSE_PATH}/docker-compose.yml...")
|
|
compose = DockerCompose(COMPOSE_PATH, compose_file_name="docker-compose.yml")
|
|
compose.start()
|
|
logger.info(f"Sleeping for {sleep_seconds} seconds to wait for containers to finish startup... ")
|
|
time.sleep(sleep_seconds)
|
|
yield compose
|
|
compose.stop()
|
|
else:
|
|
yield None
|
|
|
|
|
|
def get_pika_connection_params():
|
|
params = get_connection_params()
|
|
return params
|
|
|
|
|
|
def get_s3_params(s3_backend):
|
|
params = TEST_CONFIG.storage[s3_backend]
|
|
|
|
return params
|
|
|
|
|
|
def get_adapter(client_name, s3_backend):
|
|
if client_name == "mock":
|
|
return StorageAdapterMock(StorageClientMock())
|
|
if client_name == "azure":
|
|
return AzureStorageAdapter(get_azure_client(TEST_CONFIG.storage.azure.connection_string))
|
|
if client_name == "s3":
|
|
return S3StorageAdapter(get_s3_client(get_s3_params(s3_backend)))
|
|
else:
|
|
raise UnknownClient(client_name)
|
|
|
|
|
|
def get_queue_manager(queue_manager_name) -> QueueManager:
|
|
if queue_manager_name == "mock":
|
|
return QueueManagerMock("input", "output")
|
|
if queue_manager_name == "pika":
|
|
return PikaQueueManager("input", "output", connection_params=get_pika_connection_params())
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def queue_manager(queue_manager_name, docker_compose):
|
|
def close_connections():
|
|
if queue_manager_name == "pika":
|
|
try:
|
|
queue_manager.connection.close()
|
|
except (pika.exceptions.StreamLostError, pika.exceptions.ConnectionWrongStateError, ConnectionResetError):
|
|
logger.debug("Connection was already closed when attempting to close explicitly.")
|
|
|
|
def close_channel():
|
|
if queue_manager_name == "pika":
|
|
try:
|
|
queue_manager.channel.close()
|
|
except pika.exceptions.ChannelWrongStateError:
|
|
logger.debug("Channel was already closed when attempting to close explicitly.")
|
|
|
|
queue_manager = get_queue_manager(queue_manager_name)
|
|
yield queue_manager
|
|
close_connections()
|
|
close_channel()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def callback():
|
|
def inner(request):
|
|
return [request["data"].decode() * 2]
|
|
|
|
return inner
|
|
|
|
|
|
@pytest.fixture
|
|
def analysis_callback(callback):
|
|
def inner(request):
|
|
return callback(request)
|
|
|
|
return inner
|
|
|
|
|
|
@pytest.fixture
|
|
def response_strategy(response_strategy_name, storage):
|
|
if response_strategy_name == "storage":
|
|
return StorageStrategy(storage)
|
|
if response_strategy_name == "forwarding":
|
|
return ForwardingStrategy()
|
|
|
|
|
|
@pytest.fixture()
|
|
def visitor(storage, analysis_callback, response_strategy, component_factory):
|
|
|
|
return QueueVisitor(
|
|
callback=analysis_callback,
|
|
data_loader=component_factory.get_downloader(storage),
|
|
response_strategy=response_strategy,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def file_descriptor_manager(component_factory):
|
|
return component_factory.get_file_descriptor_manager()
|
|
|
|
|
|
@pytest.fixture
|
|
def component_factory():
|
|
MAIN_CONFIG["service"]["operations"] = TEST_CONFIG.service.operations
|
|
|
|
return get_component_factory(MAIN_CONFIG)
|