From 5009f13a0a60f5963459124fcb56feb0b5c173b8 Mon Sep 17 00:00:00 2001 From: Abraham Chavez Date: Wed, 25 Sep 2024 14:20:06 -0700 Subject: [PATCH] fixup! Fix: Race condition in DockerContainerTestCase (#6587) --- test/docker_container_test_case.py | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/test/docker_container_test_case.py b/test/docker_container_test_case.py index d28baa16b..d62a9dc9f 100644 --- a/test/docker_container_test_case.py +++ b/test/docker_container_test_case.py @@ -87,34 +87,30 @@ def _create_container(cls, image: str, container_port: int, **kwargs) -> Netloc: try: time_it = datetime.now() container_info = cls._docker.api.inspect_container(container.id) - network_settings = container_info['NetworkSettings'] if is_sibling: # no coverage - container_ip = network_settings['IPAddress'] + container_ip = container_info['NetworkSettings']['IPAddress'] assert isinstance(container_ip, str) endpoint = (container_ip, container_port) log.info('Launched sibling container %s from image %s, listening on %s:%i', container.name, image, container_ip, container_port) else: - retries = 0 - ports = network_settings['Ports'] - while len(ports[f'{container_port}/tcp']) < 1: - if (datetime.now() - time_it).seconds > 3: - # Wait for the published ports of the container that's - # supposedly running, otherwise giveup (let it fail). - log.error('Unreachable TCP port %s for container %s', - container_port, container.name) + seconds = 0.0 + while True: + ports = container_info['NetworkSettings']['Ports'][f'{container_port}/tcp'] + if len(ports) > 0: break - time.sleep(.33) - container_info = cls._docker.api.inspect_container(container.id) - ports = container_info['NetworkSettings']['Ports'] - retries += 1 - milisecs = (datetime.now() - time_it).microseconds / 1000 - port = one(ports[f'{container_port}/tcp']) + elif seconds > 3: + raise RuntimeError('Unreachable TCP port', container_port, container.name) + else: + time.sleep(.33) + container_info = cls._docker.api.inspect_container(container.id) + seconds = (datetime.now() - time_it).total_seconds() + port = one(ports) host_ip = port['HostIp'] host_port = int(port['HostPort']) - log.info('Launched container %s from image %s after %dms (retries %d), ' + log.info('Launched container %s from image %s after %.3fs, ' 'with container port %s mapped to %s:%i on the host', - container.name, image, milisecs, retries, container_port, host_ip, host_port) + container.name, image, seconds, container_port, host_ip, host_port) endpoint = (host_ip, host_port) except BaseException: # no coverage container.kill()