Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DockerCompose.get_service_info public #240

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 19 additions & 4 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,15 +204,30 @@ 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(":")
if len(result) != 2 or not all(result):
raise NoSuchPortExposed(f"port {port} is not exposed for service {service}")
return result
return result[0], result[1]
csikb marked this conversation as resolved.
Show resolved Hide resolved

def _call_command(self, cmd, filepath=None):
if filepath is None:
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
Collaborator

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