From 72ce40a4f5f17d634ce511b6c32b7dcecc993835 Mon Sep 17 00:00:00 2001 From: Tomer Nosrati Date: Thu, 12 Oct 2023 00:59:21 +0300 Subject: [PATCH] Completely refactored the defaults --- src/pytest_celery/api/backend.py | 10 +- src/pytest_celery/api/broker.py | 10 +- src/pytest_celery/api/setup.py | 7 +- src/pytest_celery/defaults.py | 160 ++---------------- src/pytest_celery/fixtures/backend.py | 7 +- src/pytest_celery/fixtures/broker.py | 7 +- src/pytest_celery/fixtures/worker.py | 4 +- .../vendors/rabbitmq/container.py | 10 +- .../vendors/rabbitmq/defaults.py | 6 + .../vendors/rabbitmq/fixtures.py | 4 +- .../vendors/redis/backend/defaults.py | 2 + .../vendors/redis/backend/fixtures.py | 4 +- .../vendors/redis/broker/defaults.py | 2 + .../vendors/redis/broker/fixtures.py | 4 +- src/pytest_celery/vendors/redis/container.py | 10 +- src/pytest_celery/vendors/redis/defaults.py | 4 + src/pytest_celery/vendors/worker/container.py | 16 +- src/pytest_celery/vendors/worker/defaults.py | 26 +++ src/pytest_celery/vendors/worker/fixtures.py | 10 +- tests/defaults.py | 41 +++++ .../integration/api/custom_setup/conftest.py | 16 +- .../api/custom_setup/test_custom_setup.py | 6 +- tests/integration/api/test_backend.py | 7 +- tests/integration/api/test_broker.py | 7 +- tests/integration/api/test_cluster.py | 7 +- tests/integration/api/test_container.py | 4 +- tests/integration/api/test_setup.py | 6 +- tests/integration/api/test_worker.py | 7 +- tests/integration/conftest.py | 10 +- tests/integration/vendors/test_rabbitmq.py | 7 +- tests/integration/vendors/test_redis.py | 10 +- tests/integration/vendors/test_worker.py | 7 +- tests/smoke/conftest.py | 16 +- tests/smoke/test_canvas.py | 20 +-- tests/smoke/test_failover.py | 7 +- tests/smoke/test_signals.py | 12 +- tests/unit/conftest.py | 49 +++--- tests/unit/vendors/test_rabbitmq.py | 7 +- tests/unit/vendors/test_redis.py | 7 +- tests/unit/vendors/test_worker.py | 7 +- 40 files changed, 280 insertions(+), 283 deletions(-) create mode 100644 src/pytest_celery/vendors/rabbitmq/defaults.py create mode 100644 src/pytest_celery/vendors/redis/backend/defaults.py create mode 100644 src/pytest_celery/vendors/redis/broker/defaults.py create mode 100644 src/pytest_celery/vendors/redis/defaults.py create mode 100644 src/pytest_celery/vendors/worker/defaults.py create mode 100644 tests/defaults.py diff --git a/src/pytest_celery/api/backend.py b/src/pytest_celery/api/backend.py index 9658ed1e..e81608f2 100644 --- a/src/pytest_celery/api/backend.py +++ b/src/pytest_celery/api/backend.py @@ -2,18 +2,18 @@ from typing import Type from typing import Union -from pytest_celery import defaults from pytest_celery.api.base import CeleryTestCluster from pytest_celery.api.base import CeleryTestNode from pytest_celery.api.container import CeleryTestContainer +from pytest_celery.defaults import WORKER_ENV class CeleryTestBackend(CeleryTestNode): @classmethod def default_config(cls) -> dict: return { - "url": defaults.WORKER_ENV["CELERY_RESULT_BACKEND"], - "local_url": defaults.WORKER_ENV["CELERY_RESULT_BACKEND"], + "url": WORKER_ENV["CELERY_RESULT_BACKEND"], + "local_url": WORKER_ENV["CELERY_RESULT_BACKEND"], } @@ -31,6 +31,6 @@ def _set_nodes( @classmethod def default_config(cls) -> dict: return { - "urls": [defaults.WORKER_ENV["CELERY_RESULT_BACKEND"]], - "local_urls": [defaults.WORKER_ENV["CELERY_RESULT_BACKEND"]], + "urls": [WORKER_ENV["CELERY_RESULT_BACKEND"]], + "local_urls": [WORKER_ENV["CELERY_RESULT_BACKEND"]], } diff --git a/src/pytest_celery/api/broker.py b/src/pytest_celery/api/broker.py index 803dddad..b1c5cec4 100644 --- a/src/pytest_celery/api/broker.py +++ b/src/pytest_celery/api/broker.py @@ -2,18 +2,18 @@ from typing import Type from typing import Union -from pytest_celery import defaults from pytest_celery.api.base import CeleryTestCluster from pytest_celery.api.base import CeleryTestNode from pytest_celery.api.container import CeleryTestContainer +from pytest_celery.defaults import WORKER_ENV class CeleryTestBroker(CeleryTestNode): @classmethod def default_config(cls) -> dict: return { - "url": defaults.WORKER_ENV["CELERY_BROKER_URL"], - "local_url": defaults.WORKER_ENV["CELERY_BROKER_URL"], + "url": WORKER_ENV["CELERY_BROKER_URL"], + "local_url": WORKER_ENV["CELERY_BROKER_URL"], } @@ -31,6 +31,6 @@ def _set_nodes( @classmethod def default_config(cls) -> dict: return { - "urls": [defaults.WORKER_ENV["CELERY_BROKER_URL"]], - "local_urls": [defaults.WORKER_ENV["CELERY_BROKER_URL"]], + "urls": [WORKER_ENV["CELERY_BROKER_URL"]], + "local_urls": [WORKER_ENV["CELERY_BROKER_URL"]], } diff --git a/src/pytest_celery/api/setup.py b/src/pytest_celery/api/setup.py index bc492975..3e4549f5 100644 --- a/src/pytest_celery/api/setup.py +++ b/src/pytest_celery/api/setup.py @@ -1,10 +1,11 @@ from celery import Celery -from pytest_celery import defaults from pytest_celery.api.backend import CeleryBackendCluster from pytest_celery.api.broker import CeleryBrokerCluster from pytest_celery.api.worker import CeleryTestWorker from pytest_celery.api.worker import CeleryWorkerCluster +from pytest_celery.defaults import DEFAULT_WORKER_APP_NAME +from pytest_celery.defaults import RESULT_TIMEOUT from pytest_celery.vendors.worker.tasks import ping @@ -56,13 +57,13 @@ def ready(self, ping: bool = False) -> bool: worker: CeleryTestWorker for worker in self.worker_cluster: # type: ignore res = self.ping.s().apply_async(queue=worker.worker_queue) - ready = ready and res.get(timeout=defaults.RESULT_TIMEOUT) == "pong" + ready = ready and res.get(timeout=RESULT_TIMEOUT) == "pong" return ready @classmethod def name(cls) -> str: - return defaults.DEFAULT_WORKER_APP_NAME + return DEFAULT_WORKER_APP_NAME @classmethod def config(cls, celery_worker_cluster_config: dict) -> dict: diff --git a/src/pytest_celery/defaults.py b/src/pytest_celery/defaults.py index 878a4649..ef6eb7b7 100644 --- a/src/pytest_celery/defaults.py +++ b/src/pytest_celery/defaults.py @@ -1,33 +1,21 @@ -""" -This module contains all the default values and settings. -These values are used by the pytest-celery plugin to configure the -components fixtures. You can override these values by hooking to the -matchin fixture and returning your own value. -""" +# flake8: noqa from pytest_docker_tools import network -########## -# Docker -########## - -CONTAINER_TIMEOUT = 60 -RESULT_TIMEOUT = 60 - - -default_pytest_celery_network = network() +from pytest_celery.vendors.rabbitmq.defaults import CELERY_RABBITMQ_BROKER +from pytest_celery.vendors.rabbitmq.defaults import * +from pytest_celery.vendors.redis.backend.defaults import CELERY_REDIS_BACKEND +from pytest_celery.vendors.redis.backend.defaults import * +from pytest_celery.vendors.redis.broker.defaults import CELERY_REDIS_BROKER +from pytest_celery.vendors.redis.broker.defaults import * +from pytest_celery.vendors.redis.defaults import * +from pytest_celery.vendors.worker.defaults import CELERY_SETUP_WORKER +from pytest_celery.vendors.worker.defaults import * ########## # Fixtures ########## -# These are the names of the fixtures that are used by the plugin. -# They define preconfigured components fixtures for docker containers - -# Fixtures names -################ - -# Generic components fixtures CELERY_SETUP = "celery_setup" CELERY_WORKER = "celery_worker" CELERY_WORKER_CLUSTER = "celery_worker_cluster" @@ -36,49 +24,10 @@ CELERY_BROKER = "celery_broker" CELERY_BROKER_CLUSTER = "celery_broker_cluster" -# Components fixtures -CELERY_SETUP_WORKER = "celery_setup_worker" -CELERY_REDIS_BACKEND = "celery_redis_backend" -CELERY_REDIS_BROKER = "celery_redis_broker" -CELERY_RABBITMQ_BROKER = "celery_rabbitmq_broker" -DEFAULT_WORKER = "default_worker_container" -DEFAULT_REDIS_BACKEND = "default_redis_backend" -DEFAULT_RABBITMQ_BROKER = "default_rabbitmq_broker" -DEFAULT_REDIS_BROKER = "default_redis_broker" - +###################### # Fixtures collections ###################### -DEFAULT_WORKERS = (DEFAULT_WORKER,) -DEFAULT_BACKENDS = (DEFAULT_REDIS_BACKEND,) -DEFAULT_BROKERS = ( - DEFAULT_RABBITMQ_BROKER, - DEFAULT_REDIS_BROKER, -) - -ALL_REDIS_FIXTURES = ( - DEFAULT_REDIS_BACKEND, - DEFAULT_REDIS_BROKER, -) -ALL_RABBITMQ_FIXTURES = (DEFAULT_RABBITMQ_BROKER,) -ALL_WORKERS_FIXTURES = (*DEFAULT_WORKERS,) -ALL_BACKENDS_FIXTURES = (*DEFAULT_BACKENDS,) -ALL_BROKERS_FIXTURES = (*DEFAULT_BROKERS,) -ALL_COMPONENTS_FIXTURES = ( - *ALL_WORKERS_FIXTURES, - *ALL_BACKENDS_FIXTURES, - *ALL_BROKERS_FIXTURES, -) -ALL_NODES_FIXTURES = ( - CELERY_WORKER, - CELERY_BACKEND, - CELERY_BROKER, -) -ALL_CLUSTERS_FIXTURES = ( - CELERY_WORKER_CLUSTER, - CELERY_BACKEND_CLUSTER, - CELERY_BROKER_CLUSTER, -) ALL_CELERY_WORKERS = (CELERY_SETUP_WORKER,) ALL_CELERY_BACKENDS = (CELERY_REDIS_BACKEND,) ALL_CELERY_BROKERS = ( @@ -86,87 +35,12 @@ CELERY_RABBITMQ_BROKER, ) -########################## -# Worker Container Settings -########################## - -WORKER_DOCKERFILE_ROOTDIR = "src/pytest_celery/vendors/worker" - -# Default container settings for all worker container fixtures -WORKER_CELERY_APP_NAME = "celery_test_app" -WORKER_CELERY_VERSION = "" # latest from pypi -WORKER_LOG_LEVEL = "INFO" -WORKER_NAME = CELERY_SETUP_WORKER -WORKER_QUEUE = "celery" -WORKER_ENV = { - "CELERY_BROKER_URL": "memory://", - "CELERY_RESULT_BACKEND": "cache+memory://", - "PYTHONUNBUFFERED": "1", - "PYTHONDONTWRITEBYTECODE": "1", -} -WORKER_VOLUME = { - "bind": "/app", - "mode": "rw", -} - -# Docker containers settings -################################################# - -# Default Worker # -################### -DEFAULT_WORKER_APP_NAME = WORKER_CELERY_APP_NAME -DEFAULT_WORKER_VERSION = WORKER_CELERY_VERSION -DEFAULT_WORKER_LOG_LEVEL = WORKER_LOG_LEVEL -DEFAULT_WORKER_NAME = WORKER_NAME -DEFAULT_WORKER_ENV = WORKER_ENV -DEFAULT_WORKER_QUEUE = WORKER_QUEUE -DEFAULT_WORKER_CONTAINER_TIMEOUT = CONTAINER_TIMEOUT -DEFAULT_WORKER_VOLUME = WORKER_VOLUME - -########################## -# Redis Container Settings -########################## - -# Default container settings for all redis container fixtures - -REDIS_IMAGE = "redis:latest" -REDIS_PORTS = {"6379/tcp": None} -REDIS_ENV: dict = {} -REDIS_CONTAINER_TIMEOUT = CONTAINER_TIMEOUT - -# Docker containers settings -################################################# - -# Default Backend # -#################### -DEFAULT_REDIS_BACKEND_ENV = REDIS_ENV -DEFAULT_REDIS_BACKEND_IMAGE = REDIS_IMAGE -DEFAULT_REDIS_BACKEND_PORTS = REDIS_PORTS - - -# Default Broker # -################### -DEFAULT_REDIS_BROKER_ENV = REDIS_ENV -DEFAULT_REDIS_BROKER_IMAGE = REDIS_IMAGE -DEFAULT_REDIS_BROKER_PORTS = REDIS_PORTS - - -############################# -# RabbitMQ Container Settings -############################# - -# Default container settings for all rabbitmq container fixtures +########## +# Docker +########## -RABBITMQ_IMAGE = "rabbitmq:latest" -RABBITMQ_PORTS = {"5672/tcp": None} -RABBITMQ_ENV: dict = {} -RABBITMQ_CONTAINER_TIMEOUT = CONTAINER_TIMEOUT * 2 +CONTAINER_TIMEOUT = 60 +RESULT_TIMEOUT = 60 -# Docker containers settings -################################################# -# Default Broker # -################### -DEFAULT_RABBITMQ_BROKER_ENV = RABBITMQ_ENV -DEFAULT_RABBITMQ_BROKER_IMAGE = RABBITMQ_IMAGE -DEFAULT_RABBITMQ_BROKER_PORTS = RABBITMQ_PORTS +default_pytest_celery_network = network() diff --git a/src/pytest_celery/fixtures/backend.py b/src/pytest_celery/fixtures/backend.py index 0c87fa5e..d6984b91 100644 --- a/src/pytest_celery/fixtures/backend.py +++ b/src/pytest_celery/fixtures/backend.py @@ -2,12 +2,13 @@ import pytest -from pytest_celery import defaults from pytest_celery.api.backend import CeleryBackendCluster from pytest_celery.api.backend import CeleryTestBackend +from pytest_celery.defaults import ALL_CELERY_BACKENDS +from pytest_celery.defaults import CELERY_BACKEND_CLUSTER -@pytest.fixture(params=defaults.ALL_CELERY_BACKENDS) +@pytest.fixture(params=ALL_CELERY_BACKENDS) def celery_backend(request: pytest.FixtureRequest) -> CeleryTestBackend: # type: ignore backend: CeleryTestBackend = request.getfixturevalue(request.param) yield backend @@ -25,7 +26,7 @@ def celery_backend_cluster(celery_backend: CeleryTestBackend) -> CeleryBackendCl def celery_backend_cluster_config(request: pytest.FixtureRequest) -> dict: try: use_default_config = pytest.fail.Exception - cluster: CeleryBackendCluster = request.getfixturevalue(defaults.CELERY_BACKEND_CLUSTER) + cluster: CeleryBackendCluster = request.getfixturevalue(CELERY_BACKEND_CLUSTER) return cluster.config() except use_default_config: return CeleryBackendCluster.default_config() diff --git a/src/pytest_celery/fixtures/broker.py b/src/pytest_celery/fixtures/broker.py index 1ec13ef6..86710abf 100644 --- a/src/pytest_celery/fixtures/broker.py +++ b/src/pytest_celery/fixtures/broker.py @@ -2,12 +2,13 @@ import pytest -from pytest_celery import defaults from pytest_celery.api.broker import CeleryBrokerCluster from pytest_celery.api.broker import CeleryTestBroker +from pytest_celery.defaults import ALL_CELERY_BROKERS +from pytest_celery.defaults import CELERY_BROKER_CLUSTER -@pytest.fixture(params=defaults.ALL_CELERY_BROKERS) +@pytest.fixture(params=ALL_CELERY_BROKERS) def celery_broker(request: pytest.FixtureRequest) -> CeleryTestBroker: # type: ignore broker: CeleryTestBroker = request.getfixturevalue(request.param) yield broker @@ -25,7 +26,7 @@ def celery_broker_cluster(celery_broker: CeleryTestBroker) -> CeleryBrokerCluste def celery_broker_cluster_config(request: pytest.FixtureRequest) -> dict: try: use_default_config = pytest.fail.Exception - cluster: CeleryBrokerCluster = request.getfixturevalue(defaults.CELERY_BROKER_CLUSTER) + cluster: CeleryBrokerCluster = request.getfixturevalue(CELERY_BROKER_CLUSTER) return cluster.config() except use_default_config: return CeleryBrokerCluster.default_config() diff --git a/src/pytest_celery/fixtures/worker.py b/src/pytest_celery/fixtures/worker.py index 9d1d0cc1..dcb38f9f 100644 --- a/src/pytest_celery/fixtures/worker.py +++ b/src/pytest_celery/fixtures/worker.py @@ -2,12 +2,12 @@ import pytest -from pytest_celery import defaults from pytest_celery.api.worker import CeleryTestWorker from pytest_celery.api.worker import CeleryWorkerCluster +from pytest_celery.defaults import ALL_CELERY_WORKERS -@pytest.fixture(params=defaults.ALL_CELERY_WORKERS) +@pytest.fixture(params=ALL_CELERY_WORKERS) def celery_worker(request: pytest.FixtureRequest) -> CeleryTestWorker: # type: ignore worker: CeleryTestWorker = request.getfixturevalue(request.param) yield worker diff --git a/src/pytest_celery/vendors/rabbitmq/container.py b/src/pytest_celery/vendors/rabbitmq/container.py index 90846ef5..0799c463 100644 --- a/src/pytest_celery/vendors/rabbitmq/container.py +++ b/src/pytest_celery/vendors/rabbitmq/container.py @@ -2,8 +2,10 @@ from kombu import Connection -from pytest_celery import defaults from pytest_celery.api.container import CeleryTestContainer +from pytest_celery.vendors.rabbitmq.defaults import RABBITMQ_ENV +from pytest_celery.vendors.rabbitmq.defaults import RABBITMQ_IMAGE +from pytest_celery.vendors.rabbitmq.defaults import RABBITMQ_PORTS class RabbitMQContainer(CeleryTestContainer): @@ -51,15 +53,15 @@ def version(cls) -> str: @classmethod def env(cls) -> dict: - return defaults.DEFAULT_RABBITMQ_BROKER_ENV + return RABBITMQ_ENV @classmethod def image(cls) -> str: - return defaults.DEFAULT_RABBITMQ_BROKER_IMAGE + return RABBITMQ_IMAGE @classmethod def ports(cls) -> dict: - return defaults.DEFAULT_RABBITMQ_BROKER_PORTS + return RABBITMQ_PORTS @property def ready_prompt(self) -> Optional[str]: diff --git a/src/pytest_celery/vendors/rabbitmq/defaults.py b/src/pytest_celery/vendors/rabbitmq/defaults.py new file mode 100644 index 00000000..b1b5b508 --- /dev/null +++ b/src/pytest_celery/vendors/rabbitmq/defaults.py @@ -0,0 +1,6 @@ +CELERY_RABBITMQ_BROKER = "celery_rabbitmq_broker" +DEFAULT_RABBITMQ_BROKER = "default_rabbitmq_broker" +RABBITMQ_IMAGE = "rabbitmq:latest" +RABBITMQ_PORTS = {"5672/tcp": None} +RABBITMQ_ENV: dict = {} +RABBITMQ_CONTAINER_TIMEOUT = 120 diff --git a/src/pytest_celery/vendors/rabbitmq/fixtures.py b/src/pytest_celery/vendors/rabbitmq/fixtures.py index 1f39ebb8..09aac204 100644 --- a/src/pytest_celery/vendors/rabbitmq/fixtures.py +++ b/src/pytest_celery/vendors/rabbitmq/fixtures.py @@ -6,9 +6,9 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr -from pytest_celery import defaults from pytest_celery.vendors.rabbitmq.api import RabbitMQTestBroker from pytest_celery.vendors.rabbitmq.container import RabbitMQContainer +from pytest_celery.vendors.rabbitmq.defaults import RABBITMQ_CONTAINER_TIMEOUT @pytest.fixture @@ -29,7 +29,7 @@ def default_rabbitmq_broker_cls() -> Type[RabbitMQContainer]: environment=fxtr("default_rabbitmq_broker_env"), network="{default_pytest_celery_network.name}", wrapper_class=RabbitMQContainer, - timeout=defaults.RABBITMQ_CONTAINER_TIMEOUT, + timeout=RABBITMQ_CONTAINER_TIMEOUT, ) diff --git a/src/pytest_celery/vendors/redis/backend/defaults.py b/src/pytest_celery/vendors/redis/backend/defaults.py new file mode 100644 index 00000000..ad8f7bcc --- /dev/null +++ b/src/pytest_celery/vendors/redis/backend/defaults.py @@ -0,0 +1,2 @@ +CELERY_REDIS_BACKEND = "celery_redis_backend" +DEFAULT_REDIS_BACKEND = "default_redis_backend" diff --git a/src/pytest_celery/vendors/redis/backend/fixtures.py b/src/pytest_celery/vendors/redis/backend/fixtures.py index 0d36737b..9634e103 100644 --- a/src/pytest_celery/vendors/redis/backend/fixtures.py +++ b/src/pytest_celery/vendors/redis/backend/fixtures.py @@ -6,9 +6,9 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr -from pytest_celery import defaults from pytest_celery.vendors.redis.backend.api import RedisTestBackend from pytest_celery.vendors.redis.container import RedisContainer +from pytest_celery.vendors.redis.defaults import REDIS_CONTAINER_TIMEOUT @pytest.fixture @@ -29,7 +29,7 @@ def default_redis_backend_cls() -> Type[RedisContainer]: environment=fxtr("default_redis_backend_env"), network="{default_pytest_celery_network.name}", wrapper_class=RedisContainer, - timeout=defaults.REDIS_CONTAINER_TIMEOUT, + timeout=REDIS_CONTAINER_TIMEOUT, ) diff --git a/src/pytest_celery/vendors/redis/broker/defaults.py b/src/pytest_celery/vendors/redis/broker/defaults.py new file mode 100644 index 00000000..abdc9bfc --- /dev/null +++ b/src/pytest_celery/vendors/redis/broker/defaults.py @@ -0,0 +1,2 @@ +CELERY_REDIS_BROKER = "celery_redis_broker" +DEFAULT_REDIS_BROKER = "default_redis_broker" diff --git a/src/pytest_celery/vendors/redis/broker/fixtures.py b/src/pytest_celery/vendors/redis/broker/fixtures.py index 928a6f46..f70e64bb 100644 --- a/src/pytest_celery/vendors/redis/broker/fixtures.py +++ b/src/pytest_celery/vendors/redis/broker/fixtures.py @@ -6,9 +6,9 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr -from pytest_celery import defaults from pytest_celery.vendors.redis.broker.api import RedisTestBroker from pytest_celery.vendors.redis.container import RedisContainer +from pytest_celery.vendors.redis.defaults import REDIS_CONTAINER_TIMEOUT @pytest.fixture @@ -29,7 +29,7 @@ def default_redis_broker_cls() -> Type[RedisContainer]: environment=fxtr("default_redis_broker_env"), network="{default_pytest_celery_network.name}", wrapper_class=RedisContainer, - timeout=defaults.REDIS_CONTAINER_TIMEOUT, + timeout=REDIS_CONTAINER_TIMEOUT, ) diff --git a/src/pytest_celery/vendors/redis/container.py b/src/pytest_celery/vendors/redis/container.py index 518fe636..6ce90ac5 100644 --- a/src/pytest_celery/vendors/redis/container.py +++ b/src/pytest_celery/vendors/redis/container.py @@ -2,8 +2,10 @@ from redis import StrictRedis as Redis -from pytest_celery import defaults from pytest_celery.api.container import CeleryTestContainer +from pytest_celery.vendors.redis.defaults import REDIS_ENV +from pytest_celery.vendors.redis.defaults import REDIS_IMAGE +from pytest_celery.vendors.redis.defaults import REDIS_PORTS class RedisContainer(CeleryTestContainer): @@ -51,15 +53,15 @@ def version(cls) -> str: @classmethod def env(cls) -> dict: - return defaults.DEFAULT_REDIS_BACKEND_ENV + return REDIS_ENV @classmethod def image(cls) -> str: - return defaults.DEFAULT_REDIS_BACKEND_IMAGE + return REDIS_IMAGE @classmethod def ports(cls) -> dict: - return defaults.DEFAULT_REDIS_BACKEND_PORTS + return REDIS_PORTS @property def ready_prompt(self) -> Optional[str]: diff --git a/src/pytest_celery/vendors/redis/defaults.py b/src/pytest_celery/vendors/redis/defaults.py new file mode 100644 index 00000000..55b36a7c --- /dev/null +++ b/src/pytest_celery/vendors/redis/defaults.py @@ -0,0 +1,4 @@ +REDIS_IMAGE = "redis:latest" +REDIS_PORTS = {"6379/tcp": None} +REDIS_ENV: dict = {} +REDIS_CONTAINER_TIMEOUT = 60 diff --git a/src/pytest_celery/vendors/worker/container.py b/src/pytest_celery/vendors/worker/container.py index 71996281..131fc590 100644 --- a/src/pytest_celery/vendors/worker/container.py +++ b/src/pytest_celery/vendors/worker/container.py @@ -1,26 +1,30 @@ import inspect from typing import Union -from pytest_celery import defaults from pytest_celery.api.container import CeleryTestContainer +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_ENV +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_LOG_LEVEL +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_NAME +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_QUEUE +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_VERSION class CeleryWorkerContainer(CeleryTestContainer): @classmethod def version(cls) -> str: - return defaults.DEFAULT_WORKER_VERSION + return DEFAULT_WORKER_VERSION @classmethod def log_level(cls) -> str: - return defaults.DEFAULT_WORKER_LOG_LEVEL + return DEFAULT_WORKER_LOG_LEVEL @classmethod def worker_name(cls) -> str: - return defaults.DEFAULT_WORKER_NAME + return DEFAULT_WORKER_NAME @classmethod def worker_queue(cls) -> str: - return defaults.DEFAULT_WORKER_QUEUE + return DEFAULT_WORKER_QUEUE @classmethod def buildargs(cls) -> dict: @@ -40,7 +44,7 @@ def env(cls, celery_worker_cluster_config: dict) -> dict: env["CELERY_BROKER_URL"] = ";".join(celery_broker_cluster_config["urls"]) if celery_backend_cluster_config: env["CELERY_RESULT_BACKEND"] = ";".join(celery_backend_cluster_config["urls"]) - return {**defaults.DEFAULT_WORKER_ENV, **env} + return {**DEFAULT_WORKER_ENV, **env} @classmethod def initial_content(cls, worker_tasks: set, worker_signals: Union[set, None] = None) -> dict: diff --git a/src/pytest_celery/vendors/worker/defaults.py b/src/pytest_celery/vendors/worker/defaults.py new file mode 100644 index 00000000..c3b4d542 --- /dev/null +++ b/src/pytest_celery/vendors/worker/defaults.py @@ -0,0 +1,26 @@ +CELERY_SETUP_WORKER = "celery_setup_worker" +DEFAULT_WORKER = "default_worker_container" +WORKER_DOCKERFILE_ROOTDIR = "src/pytest_celery/vendors/worker" +WORKER_CELERY_APP_NAME = "celery_test_app" +WORKER_CELERY_VERSION = "" # latest from pypi +WORKER_LOG_LEVEL = "INFO" +WORKER_NAME = CELERY_SETUP_WORKER +WORKER_QUEUE = "celery" +WORKER_ENV = { + "CELERY_BROKER_URL": "memory://", + "CELERY_RESULT_BACKEND": "cache+memory://", + "PYTHONUNBUFFERED": "1", + "PYTHONDONTWRITEBYTECODE": "1", +} +WORKER_VOLUME = { + "bind": "/app", + "mode": "rw", +} +DEFAULT_WORKER_APP_NAME = WORKER_CELERY_APP_NAME +DEFAULT_WORKER_VERSION = WORKER_CELERY_VERSION +DEFAULT_WORKER_LOG_LEVEL = WORKER_LOG_LEVEL +DEFAULT_WORKER_NAME = WORKER_NAME +DEFAULT_WORKER_ENV = WORKER_ENV +DEFAULT_WORKER_QUEUE = WORKER_QUEUE +DEFAULT_WORKER_CONTAINER_TIMEOUT = 60 +DEFAULT_WORKER_VOLUME = WORKER_VOLUME diff --git a/src/pytest_celery/vendors/worker/fixtures.py b/src/pytest_celery/vendors/worker/fixtures.py index 8abd901c..ae7bd8da 100644 --- a/src/pytest_celery/vendors/worker/fixtures.py +++ b/src/pytest_celery/vendors/worker/fixtures.py @@ -9,9 +9,11 @@ from pytest_docker_tools import fxtr from pytest_docker_tools import volume -from pytest_celery import defaults from pytest_celery.api.worker import CeleryTestWorker from pytest_celery.vendors.worker.container import CeleryWorkerContainer +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_CONTAINER_TIMEOUT +from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_VOLUME +from pytest_celery.vendors.worker.defaults import WORKER_DOCKERFILE_ROOTDIR @pytest.fixture @@ -47,13 +49,13 @@ def default_worker_container_session_cls() -> Type[CeleryWorkerContainer]: image="{celery_base_worker_image.id}", environment=fxtr("default_worker_env"), network="{default_pytest_celery_network.name}", - volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=CeleryWorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) celery_base_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:default", buildargs={ "CELERY_VERSION": fxtr("default_worker_celery_version"), diff --git a/tests/defaults.py b/tests/defaults.py new file mode 100644 index 00000000..528ed31f --- /dev/null +++ b/tests/defaults.py @@ -0,0 +1,41 @@ +from pytest_celery import CELERY_BACKEND +from pytest_celery import CELERY_BACKEND_CLUSTER +from pytest_celery import CELERY_BROKER +from pytest_celery import CELERY_BROKER_CLUSTER +from pytest_celery import CELERY_WORKER +from pytest_celery import CELERY_WORKER_CLUSTER +from pytest_celery import DEFAULT_RABBITMQ_BROKER +from pytest_celery import DEFAULT_REDIS_BACKEND +from pytest_celery import DEFAULT_REDIS_BROKER +from pytest_celery import DEFAULT_WORKER + +DEFAULT_WORKERS = (DEFAULT_WORKER,) +DEFAULT_BACKENDS = (DEFAULT_REDIS_BACKEND,) +DEFAULT_BROKERS = ( + DEFAULT_RABBITMQ_BROKER, + DEFAULT_REDIS_BROKER, +) + +ALL_REDIS_FIXTURES = ( + DEFAULT_REDIS_BACKEND, + DEFAULT_REDIS_BROKER, +) +ALL_RABBITMQ_FIXTURES = (DEFAULT_RABBITMQ_BROKER,) +ALL_WORKERS_FIXTURES = (*DEFAULT_WORKERS,) +ALL_BACKENDS_FIXTURES = (*DEFAULT_BACKENDS,) +ALL_BROKERS_FIXTURES = (*DEFAULT_BROKERS,) +ALL_COMPONENTS_FIXTURES = ( + *ALL_WORKERS_FIXTURES, + *ALL_BACKENDS_FIXTURES, + *ALL_BROKERS_FIXTURES, +) +ALL_NODES_FIXTURES = ( + CELERY_WORKER, + CELERY_BACKEND, + CELERY_BROKER, +) +ALL_CLUSTERS_FIXTURES = ( + CELERY_WORKER_CLUSTER, + CELERY_BACKEND_CLUSTER, + CELERY_BROKER_CLUSTER, +) diff --git a/tests/integration/api/custom_setup/conftest.py b/tests/integration/api/custom_setup/conftest.py index ea14723b..80673588 100644 --- a/tests/integration/api/custom_setup/conftest.py +++ b/tests/integration/api/custom_setup/conftest.py @@ -7,10 +7,12 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr +from pytest_celery import DEFAULT_WORKER_CONTAINER_TIMEOUT +from pytest_celery import DEFAULT_WORKER_VOLUME +from pytest_celery import WORKER_DOCKERFILE_ROOTDIR from pytest_celery import CeleryTestWorker from pytest_celery import CeleryWorkerCluster from pytest_celery import CeleryWorkerContainer -from pytest_celery import defaults class Celery4WorkerContainer(CeleryWorkerContainer): @@ -36,7 +38,7 @@ def worker_queue(cls) -> str: celery4_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:celery4", buildargs=Celery4WorkerContainer.buildargs(), ) @@ -46,9 +48,9 @@ def worker_queue(cls) -> str: image="{celery4_worker_image.id}", environment=fxtr("default_worker_env"), network="{default_pytest_celery_network.name}", - volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=Celery4WorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) @@ -84,7 +86,7 @@ def worker_queue(cls) -> str: celery5_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:celery5", buildargs=Celery5WorkerContainer.buildargs(), ) @@ -104,9 +106,9 @@ def default_worker_container_session_cls() -> Type[CeleryWorkerContainer]: image="{celery5_worker_image.id}", environment=fxtr("default_worker_env"), network="{default_pytest_celery_network.name}", - volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=Celery5WorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) diff --git a/tests/integration/api/custom_setup/test_custom_setup.py b/tests/integration/api/custom_setup/test_custom_setup.py index bcaa5432..c16bec56 100644 --- a/tests/integration/api/custom_setup/test_custom_setup.py +++ b/tests/integration/api/custom_setup/test_custom_setup.py @@ -1,9 +1,9 @@ from celery import Celery from pytest_docker_tools.wrappers.container import wait_for_callable +from pytest_celery import RESULT_TIMEOUT from pytest_celery import CeleryTestSetup from pytest_celery import CeleryTestWorker -from pytest_celery import defaults from tests.integration.api.custom_setup.conftest import Celery4WorkerContainer from tests.integration.api.custom_setup.conftest import Celery5WorkerContainer from tests.tasks import identity @@ -34,7 +34,7 @@ def test_log_level_custom_setup(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker.log_level in worker.logs()", lambda: worker.log_level in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_celery_setup_override(self, celery_setup: CeleryTestSetup): @@ -45,7 +45,7 @@ def test_celery_setup_override(self, celery_setup: CeleryTestSetup): queue = worker.worker_queue sig = identity.s(expected) res = sig.apply_async(queue=queue) - assert res.get(timeout=defaults.RESULT_TIMEOUT) == expected + assert res.get(timeout=RESULT_TIMEOUT) == expected def test_custom_cluster_version(self, celery_setup: CeleryTestSetup): assert len(celery_setup.worker_cluster) == 2 diff --git a/tests/integration/api/test_backend.py b/tests/integration/api/test_backend.py index 125c057f..676d1864 100644 --- a/tests/integration/api/test_backend.py +++ b/tests/integration/api/test_backend.py @@ -3,16 +3,17 @@ from pytest_celery import CeleryBackendCluster from pytest_celery import CeleryTestBackend -from pytest_celery import defaults +from tests.defaults import CELERY_BACKEND +from tests.defaults import CELERY_BACKEND_CLUSTER -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_BACKEND)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_BACKEND)]) class test_celey_test_backend: def test_ready(self, node: CeleryTestBackend): assert node.ready() -@pytest.mark.parametrize("cluster", [lazy_fixture(defaults.CELERY_BACKEND_CLUSTER)]) +@pytest.mark.parametrize("cluster", [lazy_fixture(CELERY_BACKEND_CLUSTER)]) class test_celery_backend_cluster: def test_ready(self, cluster: CeleryBackendCluster): assert cluster.ready() diff --git a/tests/integration/api/test_broker.py b/tests/integration/api/test_broker.py index 928836ae..085b9c22 100644 --- a/tests/integration/api/test_broker.py +++ b/tests/integration/api/test_broker.py @@ -3,16 +3,17 @@ from pytest_celery import CeleryBrokerCluster from pytest_celery import CeleryTestBroker -from pytest_celery import defaults +from tests.defaults import CELERY_BROKER +from tests.defaults import CELERY_BROKER_CLUSTER -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_BROKER)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_BROKER)]) class test_celery_test_broker: def test_ready(self, node: CeleryTestBroker): assert node.ready() -@pytest.mark.parametrize("cluster", [lazy_fixture(defaults.CELERY_BROKER_CLUSTER)]) +@pytest.mark.parametrize("cluster", [lazy_fixture(CELERY_BROKER_CLUSTER)]) class test_celery_broker_cluster: def test_ready(self, cluster: CeleryBrokerCluster): assert cluster.ready() diff --git a/tests/integration/api/test_cluster.py b/tests/integration/api/test_cluster.py index b820518f..e1211b7d 100644 --- a/tests/integration/api/test_cluster.py +++ b/tests/integration/api/test_cluster.py @@ -3,16 +3,17 @@ from pytest_celery import CeleryTestCluster from pytest_celery import CeleryTestNode -from pytest_celery import defaults +from tests.defaults import ALL_CLUSTERS_FIXTURES +from tests.defaults import ALL_NODES_FIXTURES -@pytest.mark.parametrize("node", lazy_fixture(defaults.ALL_NODES_FIXTURES)) +@pytest.mark.parametrize("node", lazy_fixture(ALL_NODES_FIXTURES)) class test_celery_test_node: def test_ready(self, node: CeleryTestNode): assert node.ready() -@pytest.mark.parametrize("cluster", lazy_fixture(defaults.ALL_CLUSTERS_FIXTURES)) +@pytest.mark.parametrize("cluster", lazy_fixture(ALL_CLUSTERS_FIXTURES)) class test_celery_test_cluster: def test_ready(self, cluster: CeleryTestCluster): assert cluster.ready() diff --git a/tests/integration/api/test_container.py b/tests/integration/api/test_container.py index 99f90a4c..7a1ef643 100644 --- a/tests/integration/api/test_container.py +++ b/tests/integration/api/test_container.py @@ -2,10 +2,10 @@ from pytest_lazyfixture import lazy_fixture from pytest_celery import CeleryTestContainer -from pytest_celery import defaults +from tests.defaults import ALL_COMPONENTS_FIXTURES -@pytest.mark.parametrize("container", lazy_fixture(defaults.ALL_COMPONENTS_FIXTURES)) +@pytest.mark.parametrize("container", lazy_fixture(ALL_COMPONENTS_FIXTURES)) class test_celery_test_container: def test_client(self, container: CeleryTestContainer): assert container.client diff --git a/tests/integration/api/test_setup.py b/tests/integration/api/test_setup.py index 1a93901f..f21d5050 100644 --- a/tests/integration/api/test_setup.py +++ b/tests/integration/api/test_setup.py @@ -1,9 +1,9 @@ from celery import Celery from pytest_docker_tools.wrappers.container import wait_for_callable +from pytest_celery import RESULT_TIMEOUT from pytest_celery import CeleryTestSetup from pytest_celery import CeleryTestWorker -from pytest_celery import defaults from tests.tasks import identity @@ -31,14 +31,14 @@ def test_log_level(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker.log_level in worker.logs()", lambda: worker.log_level in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_ready(self, celery_setup: CeleryTestSetup): for worker in celery_setup.worker_cluster: queue = worker.worker_queue r = identity.s("test_ready").apply_async(queue=queue) - assert r.get(timeout=defaults.RESULT_TIMEOUT) == "test_ready" + assert r.get(timeout=RESULT_TIMEOUT) == "test_ready" def test_celery_test_setup_ready_ping(self, celery_setup: CeleryTestSetup): assert celery_setup.ready(ping=True) diff --git a/tests/integration/api/test_worker.py b/tests/integration/api/test_worker.py index bf1eb235..b14d877d 100644 --- a/tests/integration/api/test_worker.py +++ b/tests/integration/api/test_worker.py @@ -2,13 +2,14 @@ from celery import Celery from pytest_lazyfixture import lazy_fixture +from pytest_celery import CELERY_WORKER +from pytest_celery import CELERY_WORKER_CLUSTER from pytest_celery import CeleryTestWorker from pytest_celery import CeleryWorkerCluster from pytest_celery import CeleryWorkerContainer -from pytest_celery import defaults -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_WORKER)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_WORKER)]) class test_celey_test_worker: def test_ready(self, node: CeleryTestWorker): assert node.ready() @@ -20,7 +21,7 @@ def test_version(self, node: CeleryTestWorker): assert node.version == CeleryWorkerContainer.version() -@pytest.mark.parametrize("cluster", [lazy_fixture(defaults.CELERY_WORKER_CLUSTER)]) +@pytest.mark.parametrize("cluster", [lazy_fixture(CELERY_WORKER_CLUSTER)]) class test_celery_worker_cluster: def test_ready(self, cluster: CeleryWorkerCluster): assert cluster.ready() diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 62f82577..c68944d7 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -6,8 +6,10 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr +from pytest_celery import DEFAULT_WORKER_CONTAINER_TIMEOUT +from pytest_celery import DEFAULT_WORKER_VOLUME +from pytest_celery import WORKER_DOCKERFILE_ROOTDIR from pytest_celery import CeleryWorkerContainer -from pytest_celery import defaults class IntegrationWorkerContainer(CeleryWorkerContainer): @@ -40,7 +42,7 @@ def default_worker_container_session_cls() -> Type[CeleryWorkerContainer]: integration_tests_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:integration", buildargs=IntegrationWorkerContainer.buildargs(), ) @@ -50,9 +52,9 @@ def default_worker_container_session_cls() -> Type[CeleryWorkerContainer]: image="{integration_tests_worker_image.id}", environment=fxtr("default_worker_env"), network="{default_pytest_celery_network.name}", - volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=IntegrationWorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) diff --git a/tests/integration/vendors/test_rabbitmq.py b/tests/integration/vendors/test_rabbitmq.py index 15e0e3ee..6321d717 100644 --- a/tests/integration/vendors/test_rabbitmq.py +++ b/tests/integration/vendors/test_rabbitmq.py @@ -2,12 +2,13 @@ from kombu import Connection from pytest_lazyfixture import lazy_fixture +from pytest_celery import CELERY_RABBITMQ_BROKER from pytest_celery import RabbitMQContainer from pytest_celery import RabbitMQTestBroker -from pytest_celery import defaults +from tests.defaults import ALL_RABBITMQ_FIXTURES -@pytest.mark.parametrize("container", lazy_fixture(defaults.ALL_RABBITMQ_FIXTURES)) +@pytest.mark.parametrize("container", lazy_fixture(ALL_RABBITMQ_FIXTURES)) class test_rabbitmq_container: def test_client(self, container: RabbitMQContainer): c: Connection = container.client @@ -18,7 +19,7 @@ def test_client(self, container: RabbitMQContainer): c.release() -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_RABBITMQ_BROKER)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_RABBITMQ_BROKER)]) class test_rabbitmq_test_broker: def test_ready(self, node: RabbitMQTestBroker): assert node.ready() diff --git a/tests/integration/vendors/test_redis.py b/tests/integration/vendors/test_redis.py index cd014166..dd5ff828 100644 --- a/tests/integration/vendors/test_redis.py +++ b/tests/integration/vendors/test_redis.py @@ -1,13 +1,15 @@ import pytest from pytest_lazyfixture import lazy_fixture +from pytest_celery import CELERY_REDIS_BACKEND +from pytest_celery import CELERY_REDIS_BROKER from pytest_celery import RedisContainer from pytest_celery import RedisTestBackend from pytest_celery import RedisTestBroker -from pytest_celery import defaults +from tests.defaults import ALL_REDIS_FIXTURES -@pytest.mark.parametrize("container", lazy_fixture(defaults.ALL_REDIS_FIXTURES)) +@pytest.mark.parametrize("container", lazy_fixture(ALL_REDIS_FIXTURES)) class test_redis_container: def test_client(self, container: RedisContainer): assert container.client @@ -17,13 +19,13 @@ def test_client(self, container: RedisContainer): assert container.client.delete("ready") -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_REDIS_BACKEND)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_REDIS_BACKEND)]) class test_redis_test_backend: def test_ready(self, node: RedisTestBackend): assert node.ready() -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_REDIS_BROKER)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_REDIS_BROKER)]) class test_redis_test_broker: def test_ready(self, node: RedisTestBroker): assert node.ready() diff --git a/tests/integration/vendors/test_worker.py b/tests/integration/vendors/test_worker.py index fb3a6532..26469934 100644 --- a/tests/integration/vendors/test_worker.py +++ b/tests/integration/vendors/test_worker.py @@ -2,19 +2,20 @@ from celery import Celery from pytest_lazyfixture import lazy_fixture +from pytest_celery import CELERY_SETUP_WORKER from pytest_celery import CeleryTestWorker from pytest_celery import CeleryWorkerContainer -from pytest_celery import defaults +from tests.defaults import ALL_WORKERS_FIXTURES -@pytest.mark.parametrize("container", lazy_fixture(defaults.ALL_WORKERS_FIXTURES)) +@pytest.mark.parametrize("container", lazy_fixture(ALL_WORKERS_FIXTURES)) class test_celery_worker_container: def test_client(self, container: CeleryWorkerContainer): assert container.client assert container.client == container, "Check tests/conftest.py/WorkerContainer.client" -@pytest.mark.parametrize("node", [lazy_fixture(defaults.CELERY_SETUP_WORKER)]) +@pytest.mark.parametrize("node", [lazy_fixture(CELERY_SETUP_WORKER)]) class test_base_test_worker: def test_ready(self, node: CeleryTestWorker): assert node.ready() diff --git a/tests/smoke/conftest.py b/tests/smoke/conftest.py index 3fd477fd..c855d721 100644 --- a/tests/smoke/conftest.py +++ b/tests/smoke/conftest.py @@ -8,10 +8,12 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr +from pytest_celery import DEFAULT_WORKER_CONTAINER_TIMEOUT +from pytest_celery import DEFAULT_WORKER_VOLUME +from pytest_celery import WORKER_DOCKERFILE_ROOTDIR from pytest_celery import CeleryTestWorker from pytest_celery import CeleryWorkerCluster from pytest_celery import CeleryWorkerContainer -from pytest_celery import defaults class CeleryLatestWorkerContainer(CeleryWorkerContainer): @@ -33,7 +35,7 @@ def worker_queue(cls) -> str: celery_latest_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:celery_latest", buildargs=CeleryLatestWorkerContainer.buildargs(), ) @@ -43,9 +45,9 @@ def worker_queue(cls) -> str: image="{celery_latest_worker_image.id}", environment=fxtr("default_worker_env"), network="{default_pytest_celery_network.name}", - volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=CeleryLatestWorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) @@ -87,7 +89,7 @@ def default_worker_container_session_cls() -> Type[CeleryWorkerContainer]: smoke_tests_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:smoke", buildargs=SmokeWorkerContainer.buildargs(), ) @@ -97,9 +99,9 @@ def default_worker_container_session_cls() -> Type[CeleryWorkerContainer]: image="{smoke_tests_worker_image.id}", # TODO: Use fixture to avoid defining default_worker_container again environment=fxtr("default_worker_env"), network="{default_pytest_celery_network.name}", - volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=SmokeWorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) diff --git a/tests/smoke/test_canvas.py b/tests/smoke/test_canvas.py index 6fa9c74f..59ac78b0 100644 --- a/tests/smoke/test_canvas.py +++ b/tests/smoke/test_canvas.py @@ -5,9 +5,9 @@ from celery.canvas import signature from pytest_docker_tools.wrappers.container import wait_for_callable +from pytest_celery import RESULT_TIMEOUT from pytest_celery import CeleryTestSetup from pytest_celery import CeleryTestWorker -from pytest_celery import defaults from tests.smoke.tasks import add from tests.tasks import identity @@ -19,19 +19,19 @@ def test_sanity(self, celery_setup: CeleryTestSetup): queue = worker.worker_queue expected = "test_sanity" res = identity.s(expected).apply_async(queue=queue) - assert res.get(timeout=defaults.RESULT_TIMEOUT) == expected + assert res.get(timeout=RESULT_TIMEOUT) == expected wait_for_callable( f"waiting for {expected} in worker.logs()", lambda: expected in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) if len(celery_setup.worker_cluster) > 1: queue = celery_setup.worker_cluster[1].worker_queue res = add.s(1, 2).apply_async(queue=queue) - assert res.get(timeout=defaults.RESULT_TIMEOUT) == 3 + assert res.get(timeout=RESULT_TIMEOUT) == 3 if len(celery_setup.worker_cluster) > 1: assert expected not in celery_setup.worker_cluster[1].logs() @@ -39,13 +39,13 @@ def test_sanity(self, celery_setup: CeleryTestSetup): wait_for_callable( f"waiting for {expected} in celery_setup.worker_cluster[1].logs()", lambda: expected in celery_setup.worker_cluster[1].logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) else: wait_for_callable( f"waiting for {expected} in worker.logs()", lambda: expected in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_signature(self, celery_setup: CeleryTestSetup): @@ -53,7 +53,7 @@ def test_signature(self, celery_setup: CeleryTestSetup): for worker in celery_setup.worker_cluster: queue = worker.worker_queue sig = signature(identity, args=("test_signature",), queue=queue) - assert sig.delay().get(timeout=defaults.RESULT_TIMEOUT) == "test_signature" + assert sig.delay().get(timeout=RESULT_TIMEOUT) == "test_signature" def test_group(self, celery_setup: CeleryTestSetup): worker: CeleryTestWorker @@ -65,7 +65,7 @@ def test_group(self, celery_setup: CeleryTestSetup): group(s for s in [add.si(1, 1), add.si(2, 2)]), ) res = sig.apply_async(queue=queue) - assert res.get(timeout=defaults.RESULT_TIMEOUT) == [2, 4, 2, 4, 2, 4] + assert res.get(timeout=RESULT_TIMEOUT) == [2, 4, 2, 4, 2, 4] def test_chain(self, celery_setup: CeleryTestSetup): worker: CeleryTestWorker @@ -76,7 +76,7 @@ def test_chain(self, celery_setup: CeleryTestSetup): identity.si("chain_task2").set(queue=queue), ) | identity.si("test_chain").set(queue=queue) res = sig.apply_async() - assert res.get(timeout=defaults.RESULT_TIMEOUT) == "test_chain" + assert res.get(timeout=RESULT_TIMEOUT) == "test_chain" def test_chord(self, celery_setup: CeleryTestSetup): worker: CeleryTestWorker @@ -118,4 +118,4 @@ def test_chord(self, celery_setup: CeleryTestSetup): ] ) res = sig.apply_async(queue=queue) - assert res.get(timeout=defaults.RESULT_TIMEOUT) == ["body_task"] * 3 + assert res.get(timeout=RESULT_TIMEOUT) == ["body_task"] * 3 diff --git a/tests/smoke/test_failover.py b/tests/smoke/test_failover.py index 26712bd3..28e0b738 100644 --- a/tests/smoke/test_failover.py +++ b/tests/smoke/test_failover.py @@ -4,11 +4,12 @@ from pytest_docker_tools import container from pytest_docker_tools import fxtr +from pytest_celery import RABBITMQ_CONTAINER_TIMEOUT +from pytest_celery import RESULT_TIMEOUT from pytest_celery import CeleryBrokerCluster from pytest_celery import CeleryTestSetup from pytest_celery import RabbitMQContainer from pytest_celery import RabbitMQTestBroker -from pytest_celery import defaults from tests.tasks import identity failover_broker = container( @@ -17,7 +18,7 @@ environment=fxtr("default_rabbitmq_broker_env"), network="{default_pytest_celery_network.name}", wrapper_class=RabbitMQContainer, - timeout=defaults.RABBITMQ_CONTAINER_TIMEOUT, + timeout=RABBITMQ_CONTAINER_TIMEOUT, ) @@ -46,4 +47,4 @@ def test_broker_failover(self, celery_setup: CeleryTestSetup): for worker in celery_setup.worker_cluster: expected = "test_broker_failover" res = identity.s(expected).apply_async(queue=worker.worker_queue) - assert res.get(timeout=defaults.RESULT_TIMEOUT) == expected + assert res.get(timeout=RESULT_TIMEOUT) == expected diff --git a/tests/smoke/test_signals.py b/tests/smoke/test_signals.py index 42542523..c729b771 100644 --- a/tests/smoke/test_signals.py +++ b/tests/smoke/test_signals.py @@ -3,9 +3,9 @@ from celery.signals import before_task_publish from pytest_docker_tools.wrappers.container import wait_for_callable +from pytest_celery import RESULT_TIMEOUT from pytest_celery import CeleryTestSetup from pytest_celery import CeleryTestWorker -from pytest_celery import defaults from tests.tasks import identity @@ -54,7 +54,7 @@ def test_worker_init(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker_init_handler in worker.logs()", lambda: "worker_init_handler" in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_worker_process_init(self, celery_setup: CeleryTestSetup): @@ -63,7 +63,7 @@ def test_worker_process_init(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker_process_init_handler in worker.logs()", lambda: "worker_process_init_handler" in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_worker_ready(self, celery_setup: CeleryTestSetup): @@ -72,7 +72,7 @@ def test_worker_ready(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker_ready_handler in worker.logs()", lambda: "worker_ready_handler" in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_worker_process_shutdown(self, celery_setup: CeleryTestSetup): @@ -82,7 +82,7 @@ def test_worker_process_shutdown(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker_process_shutdown_handler in worker.logs()", lambda: "worker_process_shutdown_handler" in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) def test_worker_shutdown(self, celery_setup: CeleryTestSetup): @@ -92,5 +92,5 @@ def test_worker_shutdown(self, celery_setup: CeleryTestSetup): wait_for_callable( "waiting for worker_shutdown_handler in worker.logs()", lambda: "worker_shutdown_handler" in worker.logs(), - timeout=defaults.RESULT_TIMEOUT, + timeout=RESULT_TIMEOUT, ) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index c0463c81..dfb4143a 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -10,6 +10,18 @@ from pytest_docker_tools import network from pytest_docker_tools import volume +from pytest_celery import DEFAULT_WORKER_CONTAINER_TIMEOUT +from pytest_celery import DEFAULT_WORKER_ENV +from pytest_celery import DEFAULT_WORKER_VOLUME +from pytest_celery import RABBITMQ_CONTAINER_TIMEOUT +from pytest_celery import RABBITMQ_ENV +from pytest_celery import RABBITMQ_IMAGE +from pytest_celery import RABBITMQ_PORTS +from pytest_celery import REDIS_CONTAINER_TIMEOUT +from pytest_celery import REDIS_ENV +from pytest_celery import REDIS_IMAGE +from pytest_celery import REDIS_PORTS +from pytest_celery import WORKER_DOCKERFILE_ROOTDIR from pytest_celery import CeleryTestWorker from pytest_celery import CeleryWorkerContainer from pytest_celery import RabbitMQContainer @@ -17,7 +29,6 @@ from pytest_celery import RedisContainer from pytest_celery import RedisTestBackend from pytest_celery import RedisTestBroker -from pytest_celery import defaults from tests.unit.docker.api import UnitTestContainer from tests.unit.docker.api import UnitWorkerContainer @@ -42,7 +53,7 @@ ) celery_unit_worker_image = build( - path=defaults.WORKER_DOCKERFILE_ROOTDIR, + path=WORKER_DOCKERFILE_ROOTDIR, tag="pytest-celery/components/worker:unit", buildargs=UnitWorkerContainer.buildargs(), ) @@ -74,11 +85,11 @@ def worker_test_container_tasks(default_worker_container_cls: Type[CeleryWorkerC worker_test_container = container( image="{celery_unit_worker_image.id}", scope="session", - environment=defaults.DEFAULT_WORKER_ENV, + environment=DEFAULT_WORKER_ENV, network="{unit_tests_network.name}", - volumes={"{worker_test_container_volume.name}": defaults.DEFAULT_WORKER_VOLUME}, + volumes={"{worker_test_container_volume.name}": DEFAULT_WORKER_VOLUME}, wrapper_class=UnitWorkerContainer, - timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT, + timeout=DEFAULT_WORKER_CONTAINER_TIMEOUT, ) @@ -95,33 +106,33 @@ def celery_setup_worker( worker.teardown() -redis_image = fetch(repository=defaults.REDIS_IMAGE) +redis_image = fetch(repository=REDIS_IMAGE) redis_test_container = container( image="{redis_image.id}", scope="session", - ports=defaults.REDIS_PORTS, - environment=defaults.REDIS_ENV, + ports=REDIS_PORTS, + environment=REDIS_ENV, network="{unit_tests_network.name}", wrapper_class=RedisContainer, - timeout=defaults.REDIS_CONTAINER_TIMEOUT, + timeout=REDIS_CONTAINER_TIMEOUT, ) redis_backend_container = container( image="{redis_image.id}", scope="session", - ports=defaults.REDIS_PORTS, - environment=defaults.REDIS_ENV, + ports=REDIS_PORTS, + environment=REDIS_ENV, network="{unit_tests_network.name}", wrapper_class=RedisContainer, - timeout=defaults.REDIS_CONTAINER_TIMEOUT, + timeout=REDIS_CONTAINER_TIMEOUT, ) redis_broker_container = container( image="{redis_image.id}", scope="session", - ports=defaults.REDIS_PORTS, - environment=defaults.REDIS_ENV, + ports=REDIS_PORTS, + environment=REDIS_ENV, network="{unit_tests_network.name}", wrapper_class=RedisContainer, - timeout=defaults.REDIS_CONTAINER_TIMEOUT, + timeout=REDIS_CONTAINER_TIMEOUT, ) @@ -139,15 +150,15 @@ def celery_redis_broker(redis_broker_container: RedisContainer) -> RedisTestBrok broker.teardown() -rabbitmq_image = fetch(repository=defaults.RABBITMQ_IMAGE) +rabbitmq_image = fetch(repository=RABBITMQ_IMAGE) rabbitmq_test_container = container( image="{rabbitmq_image.id}", scope="session", - ports=defaults.RABBITMQ_PORTS, - environment=defaults.RABBITMQ_ENV, + ports=RABBITMQ_PORTS, + environment=RABBITMQ_ENV, network="{unit_tests_network.name}", wrapper_class=RabbitMQContainer, - timeout=defaults.RABBITMQ_CONTAINER_TIMEOUT, + timeout=RABBITMQ_CONTAINER_TIMEOUT, ) diff --git a/tests/unit/vendors/test_rabbitmq.py b/tests/unit/vendors/test_rabbitmq.py index 809c5c46..13c58583 100644 --- a/tests/unit/vendors/test_rabbitmq.py +++ b/tests/unit/vendors/test_rabbitmq.py @@ -1,6 +1,7 @@ +from pytest_celery import RABBITMQ_ENV +from pytest_celery import RABBITMQ_IMAGE from pytest_celery import RabbitMQContainer from pytest_celery import RabbitMQTestBroker -from pytest_celery import defaults class test_rabbitmq_container: @@ -15,10 +16,10 @@ def test_version(self, rabbitmq_test_container: RabbitMQContainer): assert rabbitmq_test_container.version() == "latest" def test_env(self, rabbitmq_test_container: RabbitMQContainer): - assert rabbitmq_test_container.env() == defaults.RABBITMQ_ENV + assert rabbitmq_test_container.env() == RABBITMQ_ENV def test_image(self, rabbitmq_test_container: RabbitMQContainer): - assert rabbitmq_test_container.image() == defaults.RABBITMQ_IMAGE + assert rabbitmq_test_container.image() == RABBITMQ_IMAGE class test_rabbitmq_test_broker: diff --git a/tests/unit/vendors/test_redis.py b/tests/unit/vendors/test_redis.py index 53cd0e09..84c65acc 100644 --- a/tests/unit/vendors/test_redis.py +++ b/tests/unit/vendors/test_redis.py @@ -1,7 +1,8 @@ +from pytest_celery import REDIS_ENV +from pytest_celery import REDIS_IMAGE from pytest_celery import RedisContainer from pytest_celery import RedisTestBackend from pytest_celery import RedisTestBroker -from pytest_celery import defaults class test_redis_container: @@ -16,10 +17,10 @@ def test_version(self, redis_test_container: RedisContainer): assert redis_test_container.version() == "latest" def test_env(self, redis_test_container: RedisContainer): - assert redis_test_container.env() == defaults.REDIS_ENV + assert redis_test_container.env() == REDIS_ENV def test_image(self, redis_test_container: RedisContainer): - assert redis_test_container.image() == defaults.REDIS_IMAGE + assert redis_test_container.image() == REDIS_IMAGE class test_redis_test_backend: diff --git a/tests/unit/vendors/test_worker.py b/tests/unit/vendors/test_worker.py index 4055295b..0b3b6400 100644 --- a/tests/unit/vendors/test_worker.py +++ b/tests/unit/vendors/test_worker.py @@ -3,9 +3,10 @@ import pytest from celery import Celery +from pytest_celery import WORKER_CELERY_VERSION +from pytest_celery import WORKER_ENV from pytest_celery import CeleryTestWorker from pytest_celery import CeleryWorkerContainer -from pytest_celery import defaults class test_celery_worker_container: @@ -17,10 +18,10 @@ def test_celeryconfig(self, worker_test_container: CeleryWorkerContainer): worker_test_container.celeryconfig def test_version(self, worker_test_container: CeleryWorkerContainer): - assert worker_test_container.version() == defaults.WORKER_CELERY_VERSION + assert worker_test_container.version() == WORKER_CELERY_VERSION def test_env(self, worker_test_container: CeleryWorkerContainer, celery_worker_cluster_config: dict): - assert worker_test_container.env(celery_worker_cluster_config) == defaults.WORKER_ENV + assert worker_test_container.env(celery_worker_cluster_config) == WORKER_ENV def test_tasks_modules(self, worker_test_container: CeleryWorkerContainer): assert worker_test_container.tasks_modules() == set()