11import contextlib
22import json
3+ import logging
34import os
45import time
56import socket
67import sys
8+ from textwrap import indent
79from pathlib import Path
810from typing import Final , Any , Generator
911
2123from testcontainers .core .waiting_utils import wait_for_logs
2224
2325_DIND_PYTHON_VERSION = (3 , 13 )
26+ logger = logging .getLogger (__name__ )
2427
2528
2629def _should_skip_dind () -> bool :
30+ if os .getenv ("DIND" ):
31+ return False
2732 # todo refine macos check -> run in ci but not locally
2833 return is_mac () or tuple ([* sys .version_info ][:2 ]) != _DIND_PYTHON_VERSION
2934
@@ -50,6 +55,7 @@ def _wait_for_dind_return_ip(client: DockerClient, dind: Container):
5055def test_wait_for_logs_docker_in_docker ():
5156 # real dind isn't possible (AFAIK) in CI
5257 # forwarding the socket to a container port is at least somewhat the same
58+ logger .info ("starting test_wait_for_logs_docker_in_docker" )
5359 client = DockerClient ()
5460 not_really_dind = client .run (
5561 image = "alpine/socat" ,
@@ -58,21 +64,26 @@ def test_wait_for_logs_docker_in_docker():
5864 detach = True ,
5965 )
6066
67+ logger .info ("starting not_really_dind" )
6168 not_really_dind .start ()
69+ logger .info ("started not_really_dind" )
6270 docker_host_ip = _wait_for_dind_return_ip (client , not_really_dind )
71+ logger .info ("waited for not_really_dind: '_wait_for_dind_return_ip'" )
6372 docker_host = f"tcp://{ docker_host_ip } :2375"
6473
65- with DockerContainer (
66- image = "hello-world" ,
67- docker_client_kw = {"environment" : {"DOCKER_HOST" : docker_host , "DOCKER_CERT_PATH" : "" , "DOCKER_TLS_VERIFY" : "" }},
68- ) as container :
69- assert container .get_container_host_ip () == docker_host_ip
70- wait_for_logs (container , "Hello from Docker!" )
71- stdout , stderr = container .get_logs ()
72- assert stdout , "There should be something on stdout"
73-
74- not_really_dind .stop ()
75- not_really_dind .remove ()
74+ try :
75+ with DockerContainer (
76+ image = "hello-world" ,
77+ docker_client_kw = {"environment" : {"DOCKER_HOST" : docker_host , "DOCKER_CERT_PATH" : "" , "DOCKER_TLS_VERIFY" : "" }},
78+ ) as container :
79+ logger .info ("started hello-world container" )
80+ assert container .get_container_host_ip () == docker_host_ip
81+ wait_for_logs (container , "Hello from Docker!" )
82+ stdout , stderr = container .get_logs ()
83+ assert stdout , "There should be something on stdout"
84+ finally :
85+ not_really_dind .stop ()
86+ not_really_dind .remove ()
7687
7788
7889@pytest .mark .skipif (
@@ -122,19 +133,19 @@ def print_surround_header(what: str, header_len: int = 80) -> Generator[None, No
122133 start = f"# Beginning of { what } "
123134 end = f"# End of { what } "
124135
125- print ("\n " )
126- print ("#" * header_len )
127- print (start + " " * (header_len - len (start ) - 1 ) + "#" )
128- print ("#" * header_len )
129- print ("\n " )
136+ logger . info ("\n " )
137+ logger . info ("#" * header_len )
138+ logger . info (start + " " * (header_len - len (start ) - 1 ) + "#" )
139+ logger . info ("#" * header_len )
140+ logger . info ("\n " )
130141
131142 yield
132143
133- print ("\n " )
134- print ("#" * header_len )
135- print (end + " " * (header_len - len (end ) - 1 ) + "#" )
136- print ("#" * header_len )
137- print ("\n " )
144+ logger . info ("\n " )
145+ logger . info ("#" * header_len )
146+ logger . info (end + " " * (header_len - len (end ) - 1 ) + "#" )
147+ logger . info ("#" * header_len )
148+ logger . info ("\n " )
138149
139150
140151EXPECTED_NETWORK_VAR : Final [str ] = "TCC_EXPECTED_NETWORK"
@@ -165,10 +176,10 @@ def test_find_host_network_in_dood() -> None:
165176 """
166177 Check that the correct host network is found for DooD
167178 """
168- LOGGER .info (f"Running container id={ utils .get_running_in_container_id ()} " )
179+ logger .info (f"Running container id={ utils .get_running_in_container_id ()} " )
169180 # Get some debug information in the hope this helps to find
170- LOGGER .info (f"hostname: { socket .gethostname ()} " )
171- LOGGER .info (f"docker info: { json .dumps (get_docker_info (), indent = 2 )} " )
181+ logger .info (f"hostname: { socket .gethostname ()} " )
182+ logger .info (f"docker info: { json .dumps (get_docker_info (), indent = 2 )} " )
172183 assert DockerClient ().find_host_network () == os .environ [EXPECTED_NETWORK_VAR ]
173184
174185
@@ -183,6 +194,7 @@ def test_dood(python_testcontainer_image: str) -> None:
183194
184195 docker_sock = tcc .ryuk_docker_socket
185196 with Network () as network :
197+ logger .info ("test_dood - created network" )
186198 with (
187199 DockerContainer (
188200 image = python_testcontainer_image ,
@@ -196,7 +208,9 @@ def test_dood(python_testcontainer_image: str) -> None:
196208 .with_env ("RYUK_RECONNECTION_TIMEOUT" , "1s" )
197209 .with_network (network )
198210 ) as container :
211+ logger .info ("test_dood - created container" )
199212 status = container .get_wrapped_container ().wait ()
213+ logger .info ("test_dood - container returned status %s" , status )
200214 stdout , stderr = container .get_logs ()
201215 # ensure ryuk removed the containers created inside container
202216 # because they are bound our network the deletion of the network
@@ -205,8 +219,8 @@ def test_dood(python_testcontainer_image: str) -> None:
205219
206220 # Show what was done inside test
207221 with print_surround_header ("test_dood results" ):
208- print ( stdout .decode ("utf-8" , errors = "replace" ))
209- print ( stderr .decode ("utf-8" , errors = "replace" ))
222+ logger . info ( indent ( stdout .decode ("utf-8" , errors = "replace" ), prefix = ( " " * 4 ) + "container log: " ))
223+ logger . info ( indent ( stderr .decode ("utf-8" , errors = "replace" ), prefix = ( " " * 4 ) + "container log: " ))
210224 assert status ["StatusCode" ] == 0
211225
212226
@@ -220,6 +234,7 @@ def test_dind(python_testcontainer_image: str, tmp_path: Path) -> None:
220234 cert_dir = tmp_path / "certs"
221235 dind_name = f"docker_{ SESSION_ID } "
222236 with Network () as network :
237+ logger .info ("test_dind - created network" )
223238 with (
224239 DockerContainer (image = "docker:dind" , privileged = True )
225240 .with_name (dind_name )
@@ -229,7 +244,9 @@ def test_dind(python_testcontainer_image: str, tmp_path: Path) -> None:
229244 .with_network (network )
230245 .with_network_aliases ("docker" )
231246 ) as dind_container :
247+ logger .info ("test_dind - created docker:dind container" )
232248 wait_for_logs (dind_container , "API listen on" )
249+ logger .info ("test_dind - waited for ready message in logs" )
233250 client_dir = cert_dir / "docker" / "client"
234251 ca_file = client_dir / "ca.pem"
235252 assert ca_file .is_file ()
@@ -247,7 +264,9 @@ def test_dind(python_testcontainer_image: str, tmp_path: Path) -> None:
247264 .with_env ("DOCKER_HOST" , "tcp://docker:2376" )
248265 .with_network (network )
249266 ) as test_container :
267+ logger .info ("test_dind - created test suite container" )
250268 status = test_container .get_wrapped_container ().wait ()
269+ logger .info ("test_dind - test suite container returned status %s" , status )
251270 stdout , stderr = test_container .get_logs ()
252271 finally :
253272 # ensure the certs are deleted from inside the container
@@ -258,6 +277,6 @@ def test_dind(python_testcontainer_image: str, tmp_path: Path) -> None:
258277
259278 # Show what was done inside test
260279 with print_surround_header ("test_dood results" ):
261- print ( stdout .decode ("utf-8" , errors = "replace" ))
262- print ( stderr .decode ("utf-8" , errors = "replace" ))
280+ logger . info ( indent ( stdout .decode ("utf-8" , errors = "replace" ), prefix = ( " " * 4 ) + "container log: " ))
281+ logger . info ( indent ( stderr .decode ("utf-8" , errors = "replace" ), prefix = ( " " * 4 ) + "container log: " ))
263282 assert status ["StatusCode" ] == 0
0 commit comments