-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pytest>=7.4.4 | ||
# pytest-celery>=1.0.0 | ||
git+https://github.com/celery/pytest-celery.git | ||
pytest-xdist>=3.5.0 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
|
||
from pytest_celery import RABBITMQ_PORTS | ||
from pytest_celery import CeleryBrokerCluster | ||
from pytest_celery import RabbitMQContainer | ||
from pytest_celery import RabbitMQTestBroker | ||
|
||
|
||
@pytest.fixture | ||
def default_rabbitmq_broker_image() -> str: | ||
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 | ||
|
||
|
||
class RabbitMQManagementTestBroker(RabbitMQTestBroker): | ||
def get_management_url(self) -> str: | ||
ip = self.container.attrs["NetworkSettings"]["Ports"]["15672/tcp"][0]["HostIp"] | ||
port = self.container.attrs["NetworkSettings"]["Ports"]["15672/tcp"][0]["HostPort"] | ||
# Opening this link during debugging allows you to see the RabbitMQ management UI | ||
# in your browser | ||
return f"http://{ip}:{port}" | ||
|
||
|
||
@pytest.fixture | ||
def celery_rabbitmq_broker(default_rabbitmq_broker: RabbitMQContainer) -> RabbitMQTestBroker: | ||
broker = RabbitMQManagementTestBroker(default_rabbitmq_broker) | ||
yield broker | ||
broker.teardown() | ||
|
||
|
||
@pytest.fixture | ||
def celery_broker_cluster(celery_rabbitmq_broker: RabbitMQTestBroker) -> CeleryBrokerCluster: # type: ignore | ||
cluster = CeleryBrokerCluster(celery_rabbitmq_broker) # type: ignore | ||
yield cluster | ||
cluster.teardown() |
23 changes: 23 additions & 0 deletions
23
examples/rabbitmq_management/tests/test_management_broker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import requests | ||
from requests.auth import HTTPBasicAuth | ||
|
||
from pytest_celery import CeleryTestSetup | ||
from tests.conftest import RabbitMQManagementTestBroker | ||
|
||
|
||
def test_login_to_broker_alone(celery_rabbitmq_broker: RabbitMQManagementTestBroker): | ||
api = celery_rabbitmq_broker.get_management_url() + "/api/whoami" | ||
response = requests.get(api, auth=HTTPBasicAuth("guest", "guest")) | ||
assert response.status_code == 200 | ||
assert response.json()["name"] == "guest" | ||
assert response.json()["tags"] == ["administrator"] | ||
|
||
|
||
def test_broker_in_setup(celery_setup: CeleryTestSetup): | ||
assert isinstance(celery_setup.broker, RabbitMQManagementTestBroker) | ||
api = celery_setup.broker.get_management_url() + "/api/queues" | ||
response = requests.get(api, auth=HTTPBasicAuth("guest", "guest")) | ||
assert response.status_code == 200 | ||
res = response.json() | ||
assert isinstance(res, list) | ||
assert len(list(filter(lambda queues: celery_setup.worker.hostname() in queues["name"], res))) == 1 |