Skip to content

Commit

Permalink
Cleanups and hotfixes (#171)
Browse files Browse the repository at this point in the history
* Cleanups and hotfixes

* Renamed "local_url" -> "host_url"
  • Loading branch information
Nusnus authored Jan 21, 2024
1 parent 56f2929 commit 2f7517c
Show file tree
Hide file tree
Showing 27 changed files with 154 additions and 181 deletions.
1 change: 0 additions & 1 deletion examples/django/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
sqlalchemy>=1.2.18
django>=2.2.1
pytest-django>=4.7.0
# pytest-celery>=1.0.0
git+https://github.com/celery/pytest-celery.git
pytest-xdist>=3.5.0
2 changes: 1 addition & 1 deletion examples/django/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def worker_queue(cls) -> str:
worker_image = build(
path=".",
dockerfile="tests/DjangoWorker.Dockerfile",
tag="pytest-celery/django:example",
tag="pytest-celery/examples/django:example",
buildargs=DjangoWorkerContainer.buildargs(),
)

Expand Down
1 change: 0 additions & 1 deletion examples/myworker/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pytest>=7.4.4
# pytest-celery>=1.0.0
git+https://github.com/celery/pytest-celery.git
pytest-xdist>=3.5.0
17 changes: 0 additions & 17 deletions examples/myworker/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
import pytest

from pytest_celery.vendors.rabbitmq.defaults import RABBITMQ_PORTS
from tests.myworker.myworker import myworker_container # noqa
from tests.myworker.myworker import myworker_image # noqa
from tests.myworker.myworker import myworker_worker # noqa


@pytest.fixture
def default_rabbitmq_broker_image() -> str:
# Useful for debugging
return "rabbitmq:management"


@pytest.fixture
def default_rabbitmq_broker_ports() -> dict:
# Expose the management UI port
ports = RABBITMQ_PORTS.copy()
ports.update({"15672/tcp": None})
return ports
3 changes: 3 additions & 0 deletions examples/myworker/tests/myworker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ WORKDIR /src/celery

RUN pip install -e .

# The workdir must be /app
WORKDIR /app

# Switch to the test_user
USER test_user

Expand Down
1 change: 0 additions & 1 deletion examples/rabbitmq_management/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pytest>=7.4.4
# pytest-celery>=1.0.0
git+https://github.com/celery/pytest-celery.git
pytest-xdist>=3.5.0
1 change: 0 additions & 1 deletion examples/range/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pytest>=7.4.4
# pytest-celery>=1.0.0
git+https://github.com/celery/pytest-celery.git
pytest-xdist>=3.5.0
pytest-subtests>=0.11.0
213 changes: 101 additions & 112 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/pytest_celery/api/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CeleryTestBackend(CeleryTestNode):
def default_config(cls) -> dict:
return {
"url": DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"],
"local_url": DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"],
"host_url": DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"],
}

def restart(self, reload_container: bool = True, force: bool = False) -> None:
Expand All @@ -26,7 +26,7 @@ def restart(self, reload_container: bool = True, force: bool = False) -> None:
super().restart(reload_container, force)
if self.app:
self.app.conf.update(
result_backend=self.config()["local_url"],
result_backend=self.config()["host_url"],
)


Expand All @@ -42,5 +42,5 @@ class CeleryBackendCluster(CeleryTestCluster):
def default_config(cls) -> dict:
return {
"urls": [DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]],
"local_urls": [DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]],
"host_urls": [DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]],
}
2 changes: 1 addition & 1 deletion src/pytest_celery/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def config(self, *args: tuple, **kwargs: dict) -> dict:
config = [node.container.celeryconfig for node in self]
return {
"urls": [c["url"] for c in config],
"local_urls": [c["local_url"] for c in config],
"host_urls": [c["host_url"] for c in config],
}

def teardown(self) -> None:
Expand Down
6 changes: 3 additions & 3 deletions src/pytest_celery/api/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CeleryTestBroker(CeleryTestNode):
def default_config(cls) -> dict:
return {
"url": DEFAULT_WORKER_ENV["CELERY_BROKER_URL"],
"local_url": DEFAULT_WORKER_ENV["CELERY_BROKER_URL"],
"host_url": DEFAULT_WORKER_ENV["CELERY_BROKER_URL"],
}

def restart(self, reload_container: bool = True, force: bool = False) -> None:
Expand All @@ -26,7 +26,7 @@ def restart(self, reload_container: bool = True, force: bool = False) -> None:
super().restart(reload_container, force)
if self.app:
self.app.conf.update(
broker_url=self.config()["local_url"],
broker_url=self.config()["host_url"],
)


Expand All @@ -42,5 +42,5 @@ class CeleryBrokerCluster(CeleryTestCluster):
def default_config(cls) -> dict:
return {
"urls": [DEFAULT_WORKER_ENV["CELERY_BROKER_URL"]],
"local_urls": [DEFAULT_WORKER_ENV["CELERY_BROKER_URL"]],
"host_urls": [DEFAULT_WORKER_ENV["CELERY_BROKER_URL"]],
}
4 changes: 2 additions & 2 deletions src/pytest_celery/api/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def config(cls, celery_worker_cluster_config: dict) -> dict:
celery_backend_cluster_config: dict = celery_worker_cluster_config.get("celery_backend_cluster_config", {})
config = {}
if celery_broker_cluster_config:
config["broker_url"] = ";".join(celery_broker_cluster_config["local_urls"])
config["broker_url"] = ";".join(celery_broker_cluster_config["host_urls"])
if celery_backend_cluster_config:
config["result_backend"] = ";".join(celery_backend_cluster_config["local_urls"])
config["result_backend"] = ";".join(celery_backend_cluster_config["host_urls"])
return config

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions src/pytest_celery/vendors/memcached/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class MemcachedContainer(CeleryTestContainer):
@property
def client(self) -> memcache.Client:
conf = self.celeryconfig
servers = [f"{conf['local_url'][:-1].split('://')[-1]}"]
servers = [f"{conf['host_url'][:-1].split('://')[-1]}"]
client = memcache.Client(servers)
return client

@property
def celeryconfig(self) -> dict:
return {
"url": self.url,
"local_url": self.local_url,
"host_url": self.host_url,
"hostname": self.hostname,
"port": self.port,
}
Expand All @@ -31,7 +31,7 @@ def url(self) -> str:
return f"{self.prefix()}{self.hostname}/"

@property
def local_url(self) -> str:
def host_url(self) -> str:
return f"{self.prefix()}localhost:{self.port}/"

@property
Expand Down
6 changes: 3 additions & 3 deletions src/pytest_celery/vendors/rabbitmq/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RabbitMQContainer(CeleryTestContainer):
@property
def client(self) -> Connection:
client = Connection(
self.celeryconfig["local_url"],
self.celeryconfig["host_url"],
port=self.celeryconfig["port"],
)
return client
Expand All @@ -22,7 +22,7 @@ def client(self) -> Connection:
def celeryconfig(self) -> dict:
return {
"url": self.url,
"local_url": self.local_url,
"host_url": self.host_url,
"hostname": self.hostname,
"port": self.port,
"vhost": self.vhost,
Expand All @@ -33,7 +33,7 @@ def url(self) -> str:
return f"{self.prefix()}{self.hostname}/{self.vhost}"

@property
def local_url(self) -> str:
def host_url(self) -> str:
return f"{self.prefix()}localhost:{self.port}/{self.vhost}"

@property
Expand Down
6 changes: 3 additions & 3 deletions src/pytest_celery/vendors/redis/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RedisContainer(CeleryTestContainer):
@property
def client(self) -> Redis | None:
client = Redis.from_url(
self.celeryconfig["local_url"],
self.celeryconfig["host_url"],
decode_responses=True,
)
return client
Expand All @@ -22,7 +22,7 @@ def client(self) -> Redis | None:
def celeryconfig(self) -> dict:
return {
"url": self.url,
"local_url": self.local_url,
"host_url": self.host_url,
"hostname": self.hostname,
"port": self.port,
"vhost": self.vhost,
Expand All @@ -37,7 +37,7 @@ def url(self) -> str:
return f"{self.prefix()}{self.hostname}/{self.vhost}"

@property
def local_url(self) -> str:
def host_url(self) -> str:
return f"{self.prefix()}localhost:{self.port}/{self.vhost}"

@property
Expand Down
3 changes: 2 additions & 1 deletion src/pytest_celery/vendors/worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ARG CELERY_VERSION=""
ARG CELERY_LOG_LEVEL=INFO
ARG CELERY_WORKER_NAME=celery_test_worker
ARG CELERY_WORKER_QUEUE=celery
ENV WORKER_VERSION=$CELERY_VERSION
ENV LOG_LEVEL=$CELERY_LOG_LEVEL
ENV WORKER_NAME=$CELERY_WORKER_NAME
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
Expand All @@ -21,7 +22,7 @@ ENV PYTHONDONTWRITEBYTECODE=1
# Install Python dependencies
RUN pip install --no-cache-dir --upgrade \
pip \
celery[redis,memcache,pymemcache]${CELERY_VERSION:+==$CELERY_VERSION} \
celery[redis,memcache,pymemcache]${WORKER_VERSION:+==$WORKER_VERSION} \
psutil

# The workdir must be /app
Expand Down
5 changes: 4 additions & 1 deletion src/pytest_celery/vendors/worker/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def env(cls, celery_worker_cluster_config: dict, initial: dict | None = None) ->
dict: Environment variables set for the worker container from the test case.
"""
env = initial or {}
env = {**DEFAULT_WORKER_ENV.copy(), **env}
env = {
**DEFAULT_WORKER_ENV.copy(),
**env,
}

config_mappings = [
("celery_broker_cluster_config", "CELERY_BROKER_URL"),
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/api/custom_setup/test_custom_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ def test_ready(self, celery_setup: CeleryTestSetup):

def test_worker_is_connected_to_backend(self, celery_setup: CeleryTestSetup):
backend_urls = [
backend.container.celeryconfig["local_url"].replace("cache+", "")
for backend in celery_setup.backend_cluster
backend.container.celeryconfig["host_url"].replace("cache+", "") for backend in celery_setup.backend_cluster
]
worker: CeleryTestWorker
for worker in celery_setup.worker_cluster:
app: Celery = worker.app
assert app.backend.as_uri() in backend_urls

def test_worker_is_connected_to_broker(self, celery_setup: CeleryTestSetup):
broker_urls = [broker.container.celeryconfig["local_url"] for broker in celery_setup.broker_cluster]
broker_urls = [broker.container.celeryconfig["host_url"] for broker in celery_setup.broker_cluster]
worker: CeleryTestWorker
for worker in celery_setup.worker_cluster:
app: Celery = worker.app
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/api/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ def test_app(self, cluster: CeleryBackendCluster):
assert backend.app is None

def test_config(self, cluster: CeleryBackendCluster):
expected_keys = {"urls", "local_urls"}
expected_keys = {"urls", "host_urls"}
assert set(cluster.config().keys()) == expected_keys
2 changes: 1 addition & 1 deletion tests/integration/api/test_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ def test_app(self, cluster: CeleryBrokerCluster):
assert broker.app is None

def test_config(self, cluster: CeleryBrokerCluster):
expected_keys = {"urls", "local_urls"}
expected_keys = {"urls", "host_urls"}
assert set(cluster.config().keys()) == expected_keys
5 changes: 2 additions & 3 deletions tests/integration/api/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ def test_ready(self, celery_setup: CeleryTestSetup, confirmation: dict):

def test_worker_is_connected_to_backend(self, celery_setup: CeleryTestSetup):
backend_urls = [
backend.container.celeryconfig["local_url"].replace("cache+", "")
for backend in celery_setup.backend_cluster
backend.container.celeryconfig["host_url"].replace("cache+", "") for backend in celery_setup.backend_cluster
]
worker: CeleryTestWorker
for worker in celery_setup.worker_cluster:
app: Celery = worker.app
assert app.backend.as_uri() in backend_urls

def test_worker_is_connected_to_broker(self, celery_setup: CeleryTestSetup):
broker_urls = [broker.container.celeryconfig["local_url"] for broker in celery_setup.broker_cluster]
broker_urls = [broker.container.celeryconfig["host_url"] for broker in celery_setup.broker_cluster]
worker: CeleryTestWorker
for worker in celery_setup.worker_cluster:
app: Celery = worker.app
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/vendors/test_memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ def test_client(self, container: MemcachedContainer):
assert container.client.delete("ready")

def test_celeryconfig(self, container: MemcachedContainer):
expected_keys = {"url", "local_url", "hostname", "port"}
expected_keys = {"url", "host_url", "hostname", "port"}
config = container.celeryconfig
assert set(config.keys()) == expected_keys
assert container.prefix() in config["url"]
assert container.prefix() in config["local_url"]
assert container.prefix() in config["host_url"]


@pytest.mark.parametrize("backend", [lazy_fixture(CELERY_MEMCACHED_BACKEND)])
class test_memcached_test_backend:
def test_config(self, backend: MemcachedTestBackend):
expected_keys = {"url", "local_url", "hostname", "port"}
expected_keys = {"url", "host_url", "hostname", "port"}
assert set(backend.config().keys()) == expected_keys
assert backend.container.prefix() in backend.config()["url"]
assert backend.container.prefix() in backend.config()["local_url"]
assert backend.container.prefix() in backend.config()["host_url"]
8 changes: 4 additions & 4 deletions tests/integration/vendors/test_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def test_client(self, container: RabbitMQContainer):
c.release()

def test_celeryconfig(self, container: RabbitMQContainer):
expected_keys = {"url", "local_url", "hostname", "port", "vhost"}
expected_keys = {"url", "host_url", "hostname", "port", "vhost"}
config = container.celeryconfig
assert set(config.keys()) == expected_keys
assert container.prefix() in config["url"]
assert container.prefix() in config["local_url"]
assert container.prefix() in config["host_url"]


@pytest.mark.parametrize("broker", [lazy_fixture(CELERY_RABBITMQ_BROKER)])
class test_rabbitmq_test_broker:
def test_config(self, broker: RabbitMQTestBroker):
expected_keys = {"url", "local_url", "hostname", "port", "vhost"}
expected_keys = {"url", "host_url", "hostname", "port", "vhost"}
assert set(broker.config().keys()) == expected_keys
assert broker.container.prefix() in broker.config()["url"]
assert broker.container.prefix() in broker.config()["local_url"]
assert broker.container.prefix() in broker.config()["host_url"]
12 changes: 6 additions & 6 deletions tests/integration/vendors/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ def test_client(self, container: RedisContainer):
assert container.client.delete("ready")

def test_celeryconfig(self, container: RedisContainer):
expected_keys = {"url", "local_url", "hostname", "port", "vhost"}
expected_keys = {"url", "host_url", "hostname", "port", "vhost"}
config = container.celeryconfig
assert set(config.keys()) == expected_keys
assert container.prefix() in config["url"]
assert container.prefix() in config["local_url"]
assert container.prefix() in config["host_url"]


@pytest.mark.parametrize("backend", [lazy_fixture(CELERY_REDIS_BACKEND)])
class test_redis_test_backend:
def test_config(self, backend: RedisTestBackend):
expected_keys = {"url", "local_url", "hostname", "port", "vhost"}
expected_keys = {"url", "host_url", "hostname", "port", "vhost"}
assert set(backend.config().keys()) == expected_keys
assert backend.container.prefix() in backend.config()["url"]
assert backend.container.prefix() in backend.config()["local_url"]
assert backend.container.prefix() in backend.config()["host_url"]


@pytest.mark.parametrize("broker", [lazy_fixture(CELERY_REDIS_BROKER)])
class test_redis_test_broker:
def test_config(self, broker: RedisTestBroker):
expected_keys = {"url", "local_url", "hostname", "port", "vhost"}
expected_keys = {"url", "host_url", "hostname", "port", "vhost"}
assert set(broker.config().keys()) == expected_keys
assert broker.container.prefix() in broker.config()["url"]
assert broker.container.prefix() in broker.config()["local_url"]
assert broker.container.prefix() in broker.config()["host_url"]
4 changes: 2 additions & 2 deletions tests/unit/api/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class test_celey_test_backend:
def test_default_config_format(self, backend: CeleryTestBackend):
assert backend.default_config()["url"] == DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]
assert backend.default_config()["local_url"] == DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]
assert backend.default_config()["host_url"] == DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]

def test_restart_no_app(self, backend: CeleryTestBackend):
assert backend.app is None
Expand All @@ -32,7 +32,7 @@ def test_restart_with_app(self, backend: CeleryTestBackend, celery_setup_app: Ce
class test_celery_backend_cluster:
def test_default_config_format(self, cluster: CeleryBackendCluster):
assert cluster.default_config()["urls"] == [DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]]
assert cluster.default_config()["local_urls"] == [DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]]
assert cluster.default_config()["host_urls"] == [DEFAULT_WORKER_ENV["CELERY_RESULT_BACKEND"]]

class test_disabling_cluster:
@pytest.fixture
Expand Down
Loading

0 comments on commit 2f7517c

Please sign in to comment.