Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completely refactored the defaults #54

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/pytest_celery/api/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 8 in src/pytest_celery/api/backend.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/api/backend.py#L8

Added line #L8 was not covered by tests


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"],
}


Expand All @@ -31,6 +31,6 @@
@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"]],
}
10 changes: 5 additions & 5 deletions src/pytest_celery/api/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 8 in src/pytest_celery/api/broker.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/api/broker.py#L8

Added line #L8 was not covered by tests


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"],
}


Expand All @@ -31,6 +31,6 @@
@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"]],
}
7 changes: 4 additions & 3 deletions src/pytest_celery/api/setup.py
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 8 in src/pytest_celery/api/setup.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/api/setup.py#L7-L8

Added lines #L7 - L8 were not covered by tests
from pytest_celery.vendors.worker.tasks import ping


Expand Down Expand Up @@ -56,13 +57,13 @@
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"

Check warning on line 60 in src/pytest_celery/api/setup.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/api/setup.py#L60

Added line #L60 was not covered by tests

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:
Expand Down
160 changes: 17 additions & 143 deletions src/pytest_celery/defaults.py
Original file line number Diff line number Diff line change
@@ -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 *

Check warning on line 13 in src/pytest_celery/defaults.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/defaults.py#L5-L13

Added lines #L5 - L13 were not covered by tests

##########
# 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"
Expand All @@ -36,137 +24,23 @@
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 = (
CELERY_REDIS_BROKER,
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

Check warning on line 43 in src/pytest_celery/defaults.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/defaults.py#L42-L43

Added lines #L42 - L43 were not covered by tests

# 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()

Check warning on line 46 in src/pytest_celery/defaults.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/defaults.py#L46

Added line #L46 was not covered by tests
7 changes: 4 additions & 3 deletions src/pytest_celery/fixtures/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 8 in src/pytest_celery/fixtures/backend.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/fixtures/backend.py#L7-L8

Added lines #L7 - L8 were not covered by tests


@pytest.fixture(params=defaults.ALL_CELERY_BACKENDS)
@pytest.fixture(params=ALL_CELERY_BACKENDS)

Check warning on line 11 in src/pytest_celery/fixtures/backend.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/fixtures/backend.py#L11

Added line #L11 was not covered by tests
def celery_backend(request: pytest.FixtureRequest) -> CeleryTestBackend: # type: ignore
backend: CeleryTestBackend = request.getfixturevalue(request.param)
yield backend
Expand All @@ -25,7 +26,7 @@
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()
7 changes: 4 additions & 3 deletions src/pytest_celery/fixtures/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 8 in src/pytest_celery/fixtures/broker.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/fixtures/broker.py#L7-L8

Added lines #L7 - L8 were not covered by tests


@pytest.fixture(params=defaults.ALL_CELERY_BROKERS)
@pytest.fixture(params=ALL_CELERY_BROKERS)

Check warning on line 11 in src/pytest_celery/fixtures/broker.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/fixtures/broker.py#L11

Added line #L11 was not covered by tests
def celery_broker(request: pytest.FixtureRequest) -> CeleryTestBroker: # type: ignore
broker: CeleryTestBroker = request.getfixturevalue(request.param)
yield broker
Expand All @@ -25,7 +26,7 @@
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()
4 changes: 2 additions & 2 deletions src/pytest_celery/fixtures/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 7 in src/pytest_celery/fixtures/worker.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/fixtures/worker.py#L7

Added line #L7 was not covered by tests


@pytest.fixture(params=defaults.ALL_CELERY_WORKERS)
@pytest.fixture(params=ALL_CELERY_WORKERS)

Check warning on line 10 in src/pytest_celery/fixtures/worker.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/fixtures/worker.py#L10

Added line #L10 was not covered by tests
def celery_worker(request: pytest.FixtureRequest) -> CeleryTestWorker: # type: ignore
worker: CeleryTestWorker = request.getfixturevalue(request.param)
yield worker
Expand Down
10 changes: 6 additions & 4 deletions src/pytest_celery/vendors/rabbitmq/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 8 in src/pytest_celery/vendors/rabbitmq/container.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/rabbitmq/container.py#L6-L8

Added lines #L6 - L8 were not covered by tests


class RabbitMQContainer(CeleryTestContainer):
Expand Down Expand Up @@ -51,15 +53,15 @@

@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

Check warning on line 64 in src/pytest_celery/vendors/rabbitmq/container.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/rabbitmq/container.py#L64

Added line #L64 was not covered by tests

@property
def ready_prompt(self) -> Optional[str]:
Expand Down
6 changes: 6 additions & 0 deletions src/pytest_celery/vendors/rabbitmq/defaults.py
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 6 in src/pytest_celery/vendors/rabbitmq/defaults.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/rabbitmq/defaults.py#L1-L6

Added lines #L1 - L6 were not covered by tests
Loading