Skip to content

Commit

Permalink
fix amqps port
Browse files Browse the repository at this point in the history
  • Loading branch information
mosquito committed Mar 1, 2024
1 parent df7c855 commit a987279
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
37 changes: 26 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,30 @@ def payload(coroutine):


class RabbitmqContainer(DockerContainer): # type: ignore
SERVER_PORT: int = 5672
_amqp_port: int
_amqps_port: int

def get_url(self) -> URL:
def get_amqp_url(self) -> URL:
return URL.build(
scheme="amqp", user="guest", password="guest", path="//",
host=self.get_container_host_ip(),
port=int(self.get_exposed_port(self.SERVER_PORT)),
port=self._amqp_port,
)

def get_amqps_url(self) -> URL:
return URL.build(
scheme="amqps", user="guest", password="guest", path="//",
host=self.get_container_host_ip(),
port=self._amqps_port,
)

def readiness_probe(self) -> None:
url = self.get_url()
host = self.get_container_host_ip()
port = int(self.get_exposed_port(5672))
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
sock.connect((url.host, url.port))
sock.connect((host, port))
sock.send(b"AMQP\0x0\0x0\0x9\0x1")
data = sock.recv(4)
if len(data) != 4:
Expand All @@ -89,19 +98,25 @@ def readiness_probe(self) -> None:
return

def start(self) -> "RabbitmqContainer":
"""Start the test container."""
self.with_env("RABBITMQ_NODE_PORT", str(self.SERVER_PORT))
self.with_exposed_ports(self.SERVER_PORT)
self.with_exposed_ports(5672, 5671)
super().start()
self.readiness_probe()
self._amqp_port = int(self.get_exposed_port(5672))
self._amqps_port = int(self.get_exposed_port(5671))
return self


@pytest.fixture(scope="module")
def amqp_direct_url(request) -> Generator[URL, Any, Any]:
def rabbitmq_container() -> Generator[RabbitmqContainer, Any, Any]:
with RabbitmqContainer("mosquito/aiormq-rabbitmq") as container:
url = container.get_url()
yield url.update_query(name=request.node.nodeid)
yield container


@pytest.fixture(scope="module")
def amqp_direct_url(request, rabbitmq_container: RabbitmqContainer) -> URL:
return rabbitmq_container.get_amqp_url().update_query(
name=request.node.nodeid
)


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/test_amqps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def connection_fabric(request):


@pytest.fixture
def create_connection(connection_fabric, event_loop, amqp_url):
def create_connection(connection_fabric, event_loop, rabbitmq_container):
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.VerifyMode.CERT_NONE

return partial(
connection_fabric,
amqp_url.with_scheme("amqps").with_port(5671),
rabbitmq_container.get_amqps_url(),
loop=event_loop,
ssl_context=ssl_context,
)
Expand Down

0 comments on commit a987279

Please sign in to comment.