Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions testcontainers/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def get_service_port(self, service_name, port):
str:
The mapped port on the host
"""
return self._get_service_info(service_name, port)[1]
return self.get_service_info(service_name, port)[1]

def get_service_host(self, service_name, port):
"""
Expand All @@ -204,9 +204,24 @@ def get_service_host(self, service_name, port):
str:
The hostname for the service
"""
return self._get_service_info(service_name, port)[0]
return self.get_service_info(service_name, port)[0]

def _get_service_info(self, service, port):
def get_service_info(self, service, port):
"""
Returns the host and the port for one of the services.

Parameters
----------
service: str
Name of the docker compose service
port: int
The internal port to get the host for

Returns
-------
(str, str):
The hostname for the service, The port for the service
"""
port_cmd = self.docker_compose_command() + ["port", service, str(port)]
output = subprocess.check_output(port_cmd, cwd=self.filepath).decode("utf-8")
result = str(output).rstrip().split(":")
Expand Down
3 changes: 1 addition & 2 deletions tests/test_core/test_docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def test_can_parse_multiple_compose_files():
assert host == "0.0.0.0"
assert port == "3306"

host = compose.get_service_host("hub", 4444)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more small change: let's check both methods for getting the information here to ensure we main backwards compatibility.

port = compose.get_service_port("hub", 4444)
host, port = compose.get_service_info("hub", 4444)
assert host == "0.0.0.0"
assert port == "4444"

Expand Down