Skip to content

Commit f20cd46

Browse files
authored
Hotfix: Added celery bug report snippet to CI (#260)
* Hotfix: Improved celery bug report documentation * Hotfix: Added celery bug report to CI
1 parent a5d633e commit f20cd46

File tree

5 files changed

+164
-114
lines changed

5 files changed

+164
-114
lines changed

.github/workflows/examples.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ permissions:
3030
contents: read # to fetch code (actions/checkout)
3131

3232
jobs:
33+
celery_bug_report:
34+
runs-on: ${{ matrix.os }}
35+
36+
strategy:
37+
fail-fast: false
38+
matrix:
39+
python-version: ["3.12"]
40+
os: ["ubuntu-latest"]
41+
42+
steps:
43+
- name: Install apt packages
44+
if: startsWith(matrix.os, 'ubuntu-')
45+
run: |
46+
sudo apt update
47+
- uses: actions/checkout@v4
48+
- name: Set up Python ${{ matrix.python-version }}
49+
uses: actions/setup-python@v5
50+
with:
51+
python-version: ${{ matrix.python-version }}
52+
cache: 'pip'
53+
cache-dependency-path: '**/setup.py'
54+
- name: Install dependencies
55+
working-directory: examples
56+
run: |
57+
python -m pip install --upgrade pip pytest-cov
58+
pip install -U "pytest-celery[all]@git+https://github.com/celery/pytest-celery.git"
59+
60+
- name: Run tests
61+
working-directory: examples
62+
timeout-minutes: 5
63+
run: |
64+
pytest -xsv celery_bug_report.py --no-cov
65+
3366
myworker:
3467
runs-on: ${{ matrix.os }}
3568

docs/userguide/celery-bug-report.rst

Lines changed: 11 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ Inline handlers can be used like this:
272272
Injecting signal handlers is using a similar pattern to adding tasks and can be done according
273273
to the :ref:`signal-handlers-modules-injection` section.
274274

275+
Tasks
276+
~~~~~
277+
278+
The :ref:`default-tasks` can be used out-of-the-box, but you can also add new tasks to the worker by creating a new module
279+
and injecting it into the environment. See :ref:`injecting-tasks` for more information.
280+
275281
Templates
276282
=========
277283

@@ -283,6 +289,8 @@ Standalone Test Snippet
283289
The following snippet can be used as a starting point for a bug report script. To use it, just copy and paste it into a
284290
new file and run it with pytest.
285291

292+
The snippet is also part of the `CI system <https://github.com/celery/pytest-celery/actions/workflows/examples.yml>`_.
293+
286294
RabbitMQ Management Broker
287295
--------------------------
288296

@@ -298,117 +306,9 @@ Built-in Worker
298306

299307
We'll use the :ref:`built-in-worker` to use a specific Celery release.
300308

301-
.. code-block:: python
302-
303-
# flake8: noqa
304-
305-
from __future__ import annotations
306-
307-
import pytest
308-
from celery import Celery
309-
from celery.canvas import Signature
310-
from celery.result import AsyncResult
311-
312-
from pytest_celery import RABBITMQ_PORTS
313-
from pytest_celery import CeleryBackendCluster
314-
from pytest_celery import CeleryBrokerCluster
315-
from pytest_celery import CeleryTestSetup
316-
from pytest_celery import RabbitMQContainer
317-
from pytest_celery import RabbitMQTestBroker
318-
from pytest_celery import RedisTestBackend
319-
from pytest_celery import ping
320-
321-
###############################################################################
322-
# RabbitMQ Management Broker
323-
###############################################################################
324-
325-
326-
class RabbitMQManagementTestBroker(RabbitMQTestBroker):
327-
def get_management_url(self) -> str:
328-
"""Opening this link during debugging allows you to see the
329-
RabbitMQ management UI in your browser.
330-
"""
331-
ports = self.container.attrs["NetworkSettings"]["Ports"]
332-
ip = ports["15672/tcp"][0]["HostIp"]
333-
port = ports["15672/tcp"][0]["HostPort"]
334-
return f"http://{ip}:{port}"
335-
336-
337-
@pytest.fixture
338-
def default_rabbitmq_broker_image() -> str:
339-
return "rabbitmq:management"
340-
341-
342-
@pytest.fixture
343-
def default_rabbitmq_broker_ports() -> dict:
344-
# Expose the management UI port
345-
ports = RABBITMQ_PORTS.copy()
346-
ports.update({"15672/tcp": None})
347-
return ports
348-
349-
350-
@pytest.fixture
351-
def celery_rabbitmq_broker(default_rabbitmq_broker: RabbitMQContainer) -> RabbitMQTestBroker:
352-
broker = RabbitMQManagementTestBroker(default_rabbitmq_broker)
353-
yield broker
354-
broker.teardown()
355-
356-
357-
@pytest.fixture
358-
def celery_broker_cluster(celery_rabbitmq_broker: RabbitMQTestBroker) -> CeleryBrokerCluster:
359-
cluster = CeleryBrokerCluster(celery_rabbitmq_broker)
360-
yield cluster
361-
cluster.teardown()
362-
363-
364-
###############################################################################
365-
# Redis Result Backend
366-
###############################################################################
367-
368-
369-
@pytest.fixture
370-
def celery_backend_cluster(celery_redis_backend: RedisTestBackend) -> CeleryBackendCluster:
371-
cluster = CeleryBackendCluster(celery_redis_backend)
372-
yield cluster
373-
cluster.teardown()
374-
375-
376-
@pytest.fixture
377-
def default_redis_backend_image() -> str:
378-
return "redis:latest"
379-
380-
381-
###############################################################################
382-
# Worker Configuration
383-
###############################################################################
384-
385-
386-
@pytest.fixture(scope="session")
387-
def default_worker_celery_log_level() -> str:
388-
return "INFO"
389-
390-
391-
@pytest.fixture(scope="session")
392-
def default_worker_celery_version() -> str:
393-
return "5.2.7"
394-
395-
396-
@pytest.fixture
397-
def default_worker_app(default_worker_app: Celery) -> Celery:
398-
app = default_worker_app
399-
# app.conf... # Add any additional configuration here
400-
return app
401-
402-
403-
###############################################################################
404-
# Bug Reproduction
405-
###############################################################################
406-
407-
408-
def test_issue_1234(celery_setup: CeleryTestSetup):
409-
sig: Signature = ping.s()
410-
res: AsyncResult = sig.delay()
411-
assert res.get() == "pong"
309+
.. literalinclude:: ../../examples/celery_bug_report.py
310+
:language: python
311+
:caption: examples.celery_bug_report.py
412312

413313
Execute with Pytest
414314
###################

docs/userguide/default-tasks.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@
1010
The plugin provides a list of built-in celery tasks that can be used out of the box. This page will
1111
list all the available tasks.
1212

13+
To import the tasks, you can use the following code:
14+
15+
.. code-block:: python
16+
17+
from pytest_celery import the, tasks, you, want
18+
19+
or
20+
21+
.. code-block:: python
22+
23+
from pytest_celery.vendors.worker import tasks
24+
1325
.. tip::
1426

15-
The tasks injected into the workers that use the default volume with:
27+
The tasks are injected into the workers that use the default volume with:
1628

1729
.. code-block:: python
1830

examples/celery_bug_report.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
from celery import Celery
5+
from celery.canvas import Signature
6+
from celery.result import AsyncResult
7+
8+
from pytest_celery import RABBITMQ_PORTS
9+
from pytest_celery import CeleryBackendCluster
10+
from pytest_celery import CeleryBrokerCluster
11+
from pytest_celery import CeleryTestSetup
12+
from pytest_celery import RabbitMQContainer
13+
from pytest_celery import RabbitMQTestBroker
14+
from pytest_celery import RedisTestBackend
15+
from pytest_celery import ping
16+
17+
###############################################################################
18+
# RabbitMQ Management Broker
19+
###############################################################################
20+
21+
22+
class RabbitMQManagementTestBroker(RabbitMQTestBroker):
23+
def get_management_url(self) -> str:
24+
"""Opening this link during debugging allows you to see the RabbitMQ
25+
management UI in your browser."""
26+
ports = self.container.attrs["NetworkSettings"]["Ports"]
27+
ip = ports["15672/tcp"][0]["HostIp"]
28+
port = ports["15672/tcp"][0]["HostPort"]
29+
return f"http://{ip}:{port}"
30+
31+
32+
@pytest.fixture
33+
def default_rabbitmq_broker_image() -> str:
34+
return "rabbitmq:management"
35+
36+
37+
@pytest.fixture
38+
def default_rabbitmq_broker_ports() -> dict:
39+
# Expose the management UI port
40+
ports = RABBITMQ_PORTS.copy()
41+
ports.update({"15672/tcp": None})
42+
return ports
43+
44+
45+
@pytest.fixture
46+
def celery_rabbitmq_broker(default_rabbitmq_broker: RabbitMQContainer) -> RabbitMQTestBroker:
47+
broker = RabbitMQManagementTestBroker(default_rabbitmq_broker)
48+
yield broker
49+
broker.teardown()
50+
51+
52+
@pytest.fixture
53+
def celery_broker_cluster(celery_rabbitmq_broker: RabbitMQTestBroker) -> CeleryBrokerCluster:
54+
cluster = CeleryBrokerCluster(celery_rabbitmq_broker)
55+
yield cluster
56+
cluster.teardown()
57+
58+
59+
###############################################################################
60+
# Redis Result Backend
61+
###############################################################################
62+
63+
64+
@pytest.fixture
65+
def celery_backend_cluster(celery_redis_backend: RedisTestBackend) -> CeleryBackendCluster:
66+
cluster = CeleryBackendCluster(celery_redis_backend)
67+
yield cluster
68+
cluster.teardown()
69+
70+
71+
@pytest.fixture
72+
def default_redis_backend_image() -> str:
73+
return "redis:latest"
74+
75+
76+
###############################################################################
77+
# Worker Configuration
78+
###############################################################################
79+
80+
81+
@pytest.fixture(scope="session")
82+
def default_worker_celery_log_level() -> str:
83+
return "INFO"
84+
85+
86+
@pytest.fixture(scope="session")
87+
def default_worker_celery_version() -> str:
88+
return "5.2.7"
89+
90+
91+
@pytest.fixture
92+
def default_worker_app(default_worker_app: Celery) -> Celery:
93+
app = default_worker_app
94+
# app.conf... # Add any additional configuration here
95+
return app
96+
97+
98+
###############################################################################
99+
# Bug Reproduction
100+
###############################################################################
101+
102+
103+
def test_issue_1234(celery_setup: CeleryTestSetup):
104+
sig: Signature = ping.s()
105+
res: AsyncResult = sig.delay()
106+
assert res.get() == "pong"

src/pytest_celery/vendors/worker/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ ENV PYTHONDONTWRITEBYTECODE=1
2323
RUN pip install --no-cache-dir --upgrade \
2424
pip \
2525
celery[redis,pymemcache]${WORKER_VERSION:+==$WORKER_VERSION} \
26-
pytest-celery@git+https://github.com/Katz-Consulting-Group/pytest-celery.git@hotfix
27-
# pytest-celery@git+https://github.com/celery/pytest-celery.git@hotfix
26+
pytest-celery@git+https://github.com/celery/pytest-celery.git
2827

2928
# The workdir must be /app
3029
WORKDIR /app

0 commit comments

Comments
 (0)