Skip to content

Commit 9c0528b

Browse files
authored
Added new localstack (SQS) broker (#374)
* Added new localstack (SQS) broker * Added documentation * Hotfix
1 parent 0affe13 commit 9c0528b

25 files changed

+566
-17
lines changed

docs/getting-started/first-steps.rst

+21
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,27 @@ Fixtures
683683

684684
A list of available fixtures for the broker can be found in the :mod:`pytest_celery.vendors.redis.broker.fixtures` module.
685685

686+
Localstack (SQS) Broker
687+
-----------------------
688+
689+
The Localstack broker uses the ``localstack/localstack`` version for the underlying container.
690+
691+
Container
692+
#########
693+
694+
The :class:`LocalstackContainer <pytest_celery.vendors.localstack.container.LocalstackContainer>` is used
695+
to describe the ``localstack/localstack`` docker image.
696+
697+
Node
698+
####
699+
700+
The :class:`LocalstackTestBroker <pytest_celery.vendors.localstack.api.LocalstackTestBroker>` is used to represent the broker node.
701+
702+
Fixtures
703+
########
704+
705+
A list of available fixtures for the broker can be found in the :mod:`pytest_celery.vendors.localstack.fixtures` module.
706+
686707
.. _redis-backend:
687708

688709
Redis Backend

docs/getting-started/introduction.rst

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ pytest-celery is…
153153

154154
- RabbitMQ.
155155
- Redis.
156+
- SQS (via Localstack)
156157
- Custom broker.
157158

158159
- **Backends**

docs/getting-started/vendors.rst

+66-9
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ the box. This page lists the supported vendors and their status.
2020
Brokers
2121
~~~~~~~
2222

23-
+---------------+--------------+--------------+
24-
| **Name** | **Status** | **Enabled** |
25-
+---------------+--------------+--------------+
26-
| *RabbitMQ* | Stable | Yes |
27-
+---------------+--------------+--------------+
28-
| *Redis* | Stable | Yes |
29-
+---------------+--------------+--------------+
23+
+-----------------------+--------------+--------------+
24+
| **Name** | **Status** | **Enabled** |
25+
+-----------------------+--------------+--------------+
26+
| *RabbitMQ* | Stable | Yes |
27+
+-----------------------+--------------+--------------+
28+
| *Redis* | Stable | Yes |
29+
+-----------------------+--------------+--------------+
30+
| *Localstack (SQS)* | Beta | No |
31+
+-----------------------+--------------+--------------+
3032

3133
Backends
3234
~~~~~~~~
@@ -39,8 +41,7 @@ Backends
3941
| *Memcache* | Experimental | No |
4042
+---------------+--------------+--------------+
4143

42-
Experimental brokers may be functional but are not confirmed to be
43-
production ready.
44+
Experimental or Beta status means it may be functional but are not confirmed to be production ready.
4445

4546
Enabled means that it is automatically added to the test :ref:`setup-matrix`
4647
when running the test suite :ref:`if the vendor dependencies are installed <installation>`.
@@ -67,6 +68,61 @@ The Dockerfile is published with the source code and can be found using
6768
:language: docker
6869
:caption: pytest_celery.vendors.worker.Dockerfile
6970

71+
.. _localstack-broker:
72+
73+
Localstack (SQS) Broker
74+
=======================
75+
76+
To use the Localstack broker, you will need add additional configuration to the test setup.
77+
78+
You may add this to ``conftest.py`` to configure the Localstack broker.
79+
80+
.. code-block:: python
81+
82+
import os
83+
84+
import pytest
85+
from celery import Celery
86+
87+
from pytest_celery import LOCALSTACK_CREDS
88+
89+
@pytest.fixture
90+
def default_worker_env(default_worker_env: dict) -> dict:
91+
default_worker_env.update(LOCALSTACK_CREDS)
92+
return default_worker_env
93+
94+
95+
@pytest.fixture(scope="session", autouse=True)
96+
def set_aws_credentials():
97+
os.environ.update(LOCALSTACK_CREDS)
98+
99+
100+
@pytest.fixture
101+
def default_worker_app(default_worker_app: Celery) -> Celery:
102+
app = default_worker_app
103+
if app.conf.broker_url and app.conf.broker_url.startswith("sqs"):
104+
app.conf.broker_transport_options["region"] = LOCALSTACK_CREDS["AWS_DEFAULT_REGION"]
105+
return app
106+
107+
And to enable the Localstack broker in the default :ref:`setup-matrix`, add the following configuration to ``conftest.py``.
108+
109+
.. code-block:: python
110+
111+
from pytest_celery import ALL_CELERY_BROKERS
112+
from pytest_celery import CELERY_LOCALSTACK_BROKER
113+
from pytest_celery import CeleryTestBroker
114+
from pytest_celery import _is_vendor_installed
115+
116+
if _is_vendor_installed("localstack"):
117+
ALL_CELERY_BROKERS.add(CELERY_LOCALSTACK_BROKER)
118+
119+
120+
@pytest.fixture(params=ALL_CELERY_BROKERS)
121+
def celery_broker(request: pytest.FixtureRequest) -> CeleryTestBroker: # type: ignore
122+
broker: CeleryTestBroker = request.getfixturevalue(request.param)
123+
yield broker
124+
broker.teardown()
125+
70126
.. _custom-vendors:
71127

72128
Custom Vendors
@@ -210,6 +266,7 @@ There's no ``session`` vendor class for other vendors.
210266

211267
- For RabbitMQ Broker use :func:`default_rabbitmq_broker_cls <pytest_celery.vendors.rabbitmq.fixtures.default_rabbitmq_broker_cls>`.
212268
- For Redis Broker use :func:`default_redis_broker_cls <pytest_celery.vendors.redis.broker.fixtures.default_redis_broker_cls>`.
269+
- For SQS Broker use :func:`default_localstack_broker_cls <pytest_celery.vendors.localstack.fixtures.default_localstack_broker_cls>`.
213270
- For Redis Backend use :func:`default_redis_backend_cls <pytest_celery.vendors.redis.backend.fixtures.default_redis_backend_cls>`.
214271
- For Memcache Backend use :func:`default_memcached_backend_cls <pytest_celery.vendors.memcached.fixtures.default_memcached_backend_cls>`.
215272

docs/includes/installation.txt

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ To install the vendors, you may either install all of the dependencies manually,
3636
- ``all``: Installs all vendors.
3737
- ``redis``: Installs Redis vendor, providing **broker** and **result backend** components.
3838
- ``memcached``: Installs Memcached vendor, providing a **result backend** component.
39+
- ``sqs``: Installs Localstack vendor, providing an **SQS broker** component.
3940

4041
The following extra is installed by default:
4142

@@ -56,6 +57,15 @@ RabbitMQ & Redis combo
5657

5758
This will configure the plugin to generate all possible setups using only RabbitMQ and Redis vendors.
5859

60+
SQS & Redis combo
61+
-----------------
62+
63+
.. code-block:: console
64+
65+
pip install -U "pytest-celery[redis,sqs]"
66+
67+
This will configure the plugin to generate all possible setups using only Localstack and Redis vendors.
68+
5969
All vendors
6070
-----------
6171

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
pytest\_celery.vendors.localstack package
2+
=========================================
3+
4+
Submodules
5+
----------
6+
7+
pytest\_celery.vendors.localstack.api module
8+
--------------------------------------------
9+
10+
.. automodule:: pytest_celery.vendors.localstack.api
11+
:members:
12+
:undoc-members:
13+
:show-inheritance:
14+
15+
pytest\_celery.vendors.localstack.container module
16+
--------------------------------------------------
17+
18+
.. automodule:: pytest_celery.vendors.localstack.container
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
22+
23+
pytest\_celery.vendors.localstack.defaults module
24+
-------------------------------------------------
25+
26+
.. automodule:: pytest_celery.vendors.localstack.defaults
27+
:members:
28+
:undoc-members:
29+
:show-inheritance:
30+
31+
pytest\_celery.vendors.localstack.fixtures module
32+
-------------------------------------------------
33+
34+
.. automodule:: pytest_celery.vendors.localstack.fixtures
35+
:members:
36+
:undoc-members:
37+
:show-inheritance:
38+
39+
Module contents
40+
---------------
41+
42+
.. automodule:: pytest_celery.vendors.localstack
43+
:members:
44+
:undoc-members:
45+
:show-inheritance:

docs/reference/pytest_celery.vendors.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Subpackages
77
.. toctree::
88
:maxdepth: 4
99

10+
pytest_celery.vendors.localstack
1011
pytest_celery.vendors.memcached
1112
pytest_celery.vendors.rabbitmq
1213
pytest_celery.vendors.redis

docs/userguide/advanced-installation.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ For example, let's assume you want to use the RabbitMQ/Redis combination.
4141

4242
1. For RabbitMQ we will need :pypi:`kombu <kombu>`.
4343
2. For Redis we will need :pypi:`redis <redis>`.
44+
3. For SQS we will need :pypi:`kombu <kombu>` with the sqs extra.
4445

4546
To install the plugin with the RabbitMQ/Redis combination, you will need to install the following dependencies:
4647

@@ -53,8 +54,8 @@ Let's break down the command:
5354
- The ``pytest-celery`` is the plugin package, it will install the plugin alongside Celery and its dependencies,including **Kombu** (if not installed already).
5455
- The ``[redis]`` is the feature flag for the Redis vendor, it will install the :pypi:`redis <redis>` package and configure the plugin to use it which will add the Redis backend and Redis broker components to the default setup matrix.
5556

56-
Experimental Vendors
57-
~~~~~~~~~~~~~~~~~~~~
57+
Experimental or Beta Vendors
58+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5859

5960
:ref:`vendors` that are in not stable, will not be added to the default setup matrix.
6061
To use the experimental vendors, you will need to configure the setup matrix manually.

docs/userguide/setup-matrix.rst

+44
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,47 @@ Redis Broker and Redis Backend
128128
cluster = CeleryBackendCluster(celery_redis_backend)
129129
yield cluster
130130
cluster.teardown()
131+
132+
SQS Broker and No Backend
133+
~~~~~~~~~~~~~~~~~~~~~~~~~
134+
135+
1. Use only Localstack as the broker.
136+
137+
.. code-block:: python
138+
139+
@pytest.fixture
140+
def celery_broker_cluster(celery_localstack_broker: LocalstackTestBroker) -> CeleryBrokerCluster:
141+
cluster = CeleryBrokerCluster(celery_localstack_broker)
142+
yield cluster
143+
cluster.teardown()
144+
145+
2. :ref:`Disable the backend <disable_backend>`.
146+
147+
.. code-block:: python
148+
149+
@pytest.fixture
150+
def celery_backend_cluster():
151+
return None
152+
153+
SQS Broker and Redis Backend
154+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155+
156+
1. Use only Localstack as the broker.
157+
158+
.. code-block:: python
159+
160+
@pytest.fixture
161+
def celery_broker_cluster(celery_localstack_broker: LocalstackTestBroker) -> CeleryBrokerCluster:
162+
cluster = CeleryBrokerCluster(celery_localstack_broker)
163+
yield cluster
164+
cluster.teardown()
165+
166+
2. Use Redis as the backend.
167+
168+
.. code-block:: python
169+
170+
@pytest.fixture
171+
def celery_backend_cluster(celery_redis_backend: RedisTestBackend) -> CeleryBackendCluster:
172+
cluster = CeleryBackendCluster(celery_redis_backend)
173+
yield cluster
174+
cluster.teardown()

src/pytest_celery/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
from pytest_celery.fixtures.worker import celery_worker_cluster_config
4141
from pytest_celery.vendors import _is_vendor_installed
4242

43+
if _is_vendor_installed("localstack"):
44+
from pytest_celery.vendors.localstack.api import LocalstackTestBroker
45+
from pytest_celery.vendors.localstack.container import LocalstackContainer
46+
from pytest_celery.vendors.localstack.defaults import *
47+
from pytest_celery.vendors.localstack.fixtures import celery_localstack_broker
48+
from pytest_celery.vendors.localstack.fixtures import default_localstack_broker
49+
from pytest_celery.vendors.localstack.fixtures import default_localstack_broker_cls
50+
from pytest_celery.vendors.localstack.fixtures import default_localstack_broker_env
51+
from pytest_celery.vendors.localstack.fixtures import default_localstack_broker_image
52+
from pytest_celery.vendors.localstack.fixtures import default_localstack_broker_ports
53+
4354
if _is_vendor_installed("memcached"):
4455
from pytest_celery.vendors.memcached.api import MemcachedTestBackend
4556
from pytest_celery.vendors.memcached.container import MemcachedContainer

src/pytest_celery/defaults.py

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from pytest_docker_tools import network
1414

1515
from pytest_celery.vendors import _is_vendor_installed
16+
from pytest_celery.vendors.localstack.defaults import CELERY_LOCALSTACK_BROKER
17+
from pytest_celery.vendors.localstack.defaults import *
1618
from pytest_celery.vendors.memcached.defaults import CELERY_MEMCACHED_BACKEND
1719
from pytest_celery.vendors.memcached.defaults import *
1820
from pytest_celery.vendors.rabbitmq.defaults import CELERY_RABBITMQ_BROKER
@@ -53,6 +55,11 @@
5355
if _is_vendor_installed("memcached") and False:
5456
ALL_CELERY_BACKENDS.add(CELERY_MEMCACHED_BACKEND)
5557

58+
# Localstack is disabled by default regardless of its availability due to its beta status.
59+
if _is_vendor_installed("localstack") and False:
60+
# Uses Kombu
61+
ALL_CELERY_BROKERS.add(CELERY_LOCALSTACK_BROKER)
62+
5663
# Worker setup is assumed to be always available.
5764
ALL_CELERY_WORKERS = (CELERY_SETUP_WORKER,)
5865

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""The pytest-celery plugin provides a set of built-in components called
2+
:ref:`vendors`.
3+
4+
This module is part of the Localstack Broker vendor.
5+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""The pytest-celery plugin provides a set of built-in components called
2+
:ref:`vendors`.
3+
4+
This module is part of the Localstack Broker vendor.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from pytest_celery.api.broker import CeleryTestBroker
10+
11+
12+
class LocalstackTestBroker(CeleryTestBroker):
13+
pass

0 commit comments

Comments
 (0)