|
| 1 | +.. _examples_vhost: |
| 2 | + |
| 3 | +======= |
| 4 | + vhost |
| 5 | +======= |
| 6 | + |
| 7 | +:Release: |version| |
| 8 | +:Date: |today| |
| 9 | + |
| 10 | +.. contents:: |
| 11 | + :local: |
| 12 | + :depth: 2 |
| 13 | + |
| 14 | +Description |
| 15 | +=========== |
| 16 | + |
| 17 | +This example project demonstrates how to use a single Redis container as both a broker and a result backend, |
| 18 | +using different vhosts for each purpose. |
| 19 | + |
| 20 | +Breakdown |
| 21 | +========= |
| 22 | + |
| 23 | +File Structure |
| 24 | +~~~~~~~~~~~~~~ |
| 25 | + |
| 26 | +The following diagram lists the relevant files in the project. |
| 27 | + |
| 28 | +.. code-block:: text |
| 29 | +
|
| 30 | + rabbitmq_management/ |
| 31 | + ├── tests/ |
| 32 | + │ ├── __init__.py |
| 33 | + │ ├── conftest.py |
| 34 | + │ └── test_vhost.py |
| 35 | + └── requirements.txt |
| 36 | +
|
| 37 | +conftest.py |
| 38 | +~~~~~~~~~~~ |
| 39 | + |
| 40 | +We create our own Redis container, and then use it as both a broker and a result backend. |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + redis_image = fetch(repository=REDIS_IMAGE) |
| 45 | + redis_test_container: RedisContainer = container( |
| 46 | + image="{redis_image.id}", |
| 47 | + ports=REDIS_PORTS, |
| 48 | + environment=REDIS_ENV, |
| 49 | + network="{default_pytest_celery_network.name}", |
| 50 | + wrapper_class=RedisContainer, |
| 51 | + timeout=REDIS_CONTAINER_TIMEOUT, |
| 52 | + ) |
| 53 | +
|
| 54 | +As the default vhost is "0", we can use it as the broker, and create a new vhost for the result backend. |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + @pytest.fixture |
| 59 | + def redis_broker(redis_test_container: RedisContainer) -> RedisTestBroker: |
| 60 | + broker = RedisTestBroker(redis_test_container) |
| 61 | + yield broker |
| 62 | + broker.teardown() |
| 63 | +
|
| 64 | +
|
| 65 | + @pytest.fixture |
| 66 | + def celery_broker_cluster(redis_broker: RedisTestBroker) -> CeleryBrokerCluster: |
| 67 | + cluster = CeleryBrokerCluster(redis_broker) |
| 68 | + yield cluster |
| 69 | + cluster.teardown() |
| 70 | +
|
| 71 | +For the backend, we need to change some settings to use a different vhost, so we create our own |
| 72 | +Redis backend and work it out there. |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + class MyRedisTestBackend(RedisTestBackend): |
| 77 | + def config(self, *args: tuple, **kwargs: dict) -> dict: |
| 78 | + return super().config(vhost=1, *args, **kwargs) |
| 79 | +
|
| 80 | +Lastly, we add our backend that uses the same Redis container as the broker and our |
| 81 | +``MyRedisTestBackend`` class. |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + @pytest.fixture |
| 86 | + def redis_backend(redis_test_container: RedisContainer) -> MyRedisTestBackend: |
| 87 | + backend = MyRedisTestBackend(redis_test_container) |
| 88 | + yield backend |
| 89 | + backend.teardown() |
| 90 | +
|
| 91 | +
|
| 92 | + @pytest.fixture |
| 93 | + def celery_backend_cluster(redis_backend: MyRedisTestBackend) -> CeleryBackendCluster: |
| 94 | + cluster = CeleryBackendCluster(redis_backend) |
| 95 | + yield cluster |
| 96 | + cluster.teardown() |
| 97 | +
|
| 98 | +test_vhost.py |
| 99 | +~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +We can now run tests that will share the same Redis container for both the broker and the result backend components. |
| 102 | + |
| 103 | +.. literalinclude:: ../../../examples/vhost/tests/test_vhost.py |
| 104 | + :language: python |
| 105 | + :caption: examples.vhost.tests.test_vhost.py |
0 commit comments