From 587a5d3146165beedf043ebe054d67749ce40f53 Mon Sep 17 00:00:00 2001 From: Bence Csik <22349790+csikb@users.noreply.github.com> Date: Sun, 28 Aug 2022 21:01:31 +0100 Subject: [PATCH 1/2] make get_service_info public --- testcontainers/compose.py | 23 +++++++++++++++++++---- tests/test_core/test_docker_compose.py | 3 +-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/testcontainers/compose.py b/testcontainers/compose.py index b5961aa8..148c86b2 100644 --- a/testcontainers/compose.py +++ b/testcontainers/compose.py @@ -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): """ @@ -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] def _call_command(self, cmd, filepath=None): if filepath is None: diff --git a/tests/test_core/test_docker_compose.py b/tests/test_core/test_docker_compose.py index ce8ec02c..e93dcac7 100644 --- a/tests/test_core/test_docker_compose.py +++ b/tests/test_core/test_docker_compose.py @@ -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) - port = compose.get_service_port("hub", 4444) + host, port = compose.get_service_info("hub", 4444) assert host == "0.0.0.0" assert port == "4444" From 8921757fd272153ae5cc4dab3ecb55c3dde75a89 Mon Sep 17 00:00:00 2001 From: Bence Csik <22349790+csikb@users.noreply.github.com> Date: Tue, 30 Aug 2022 16:05:57 +0100 Subject: [PATCH 2/2] Update testcontainers/compose.py Co-authored-by: Till Hoffmann --- testcontainers/compose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcontainers/compose.py b/testcontainers/compose.py index 148c86b2..0284634c 100644 --- a/testcontainers/compose.py +++ b/testcontainers/compose.py @@ -227,7 +227,7 @@ def get_service_info(self, service, port): 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] + return result def _call_command(self, cmd, filepath=None): if filepath is None: