testcontainers-python icon indicating copy to clipboard operation
testcontainers-python copied to clipboard

Make DockerCompose.get_service_info public

Open csikb opened this issue 2 years ago • 5 comments

Return a touple with the host and the 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[0], result[1]
# old approach
host = compose.get_service_host("hub", 4444)
port = compose.get_service_port("hub", 4444)
# new approach
host, port = compose.get_service_info("hub", 4444)

csikb avatar Aug 28 '22 09:08 csikb

Sounds good. Fancy sending a PR?

tillahoffmann avatar Aug 28 '22 19:08 tillahoffmann

Sure, I alredy did the coding part, let me look trough it if everything is OK with it.

csikb avatar Aug 28 '22 19:08 csikb

Should just require removing the underscore in compose.py, right?

tillahoffmann avatar Aug 28 '22 19:08 tillahoffmann

And adding a test case

csikb avatar Aug 28 '22 20:08 csikb

https://github.com/testcontainers/testcontainers-python/pull/240

csikb avatar Aug 28 '22 20:08 csikb

this is now obsolete! see the new docker compose v2 implementation!

totallyzen avatar Mar 19 '24 15:03 totallyzen