From 581e186bd4599a620370d4d6c6bdb1920a82c31d Mon Sep 17 00:00:00 2001 From: Michal Zeman Date: Wed, 18 May 2022 10:37:35 +0200 Subject: [PATCH] Fixed cleaning of messy output for port number. --- src/pytest_docker/plugin.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pytest_docker/plugin.py b/src/pytest_docker/plugin.py index 99be325..f675b9b 100644 --- a/src/pytest_docker/plugin.py +++ b/src/pytest_docker/plugin.py @@ -90,8 +90,14 @@ def port_for(self, service, container_port): ) # This handles messy output that might contain warnings or other text - if len(endpoint.split("\n")) > 1: - endpoint = endpoint.split("\n")[-1] + ips = re.findall(r'\d{1,3}(?:\.\d{1,3}){3}:\d{1,5}', endpoint) + if len(ips) == 0: + raise ValueError(f'Could not found any IP in endpoint {endpoint} for "{service}:{container_port}"') + if len(ips) > 1: + raise ValueError( + f'Found more IPs ({",".join(ips)}) in endpoint {endpoint} for "{service}:{container_port}". ' + f'Could not decided which port to use. ') + endpoint = ips[0] # Usually, the IP address here is 0.0.0.0, so we don't use it. match = int(endpoint.split(":", 1)[-1])