1+ import re
12from pathlib import Path
23
34import pytest
45
56from testcontainers .core .container import DockerContainer
6- from testcontainers .core .wait_strategies import LogMessageWaitStrategy
7+ from testcontainers .core .wait_strategies import ExecWaitStrategy , LogMessageWaitStrategy
78
89
910class TestRealDockerIntegration :
@@ -26,6 +27,41 @@ def test_wait_strategy_timeout_with_real_container(self):
2627 with DockerContainer ("alpine:latest" ).with_command ("sleep 10" ).waiting_for (strategy ):
2728 pass # Should not reach here
2829
30+ @pytest .mark .parametrize (
31+ "command,expected_exit_code" ,
32+ [
33+ ("test -d /tmp" , 0 ),
34+ (["test" , "-d" , "/tmp" ], 0 ),
35+ ("test 'one two' = 'one two'" , 0 ),
36+ (["test" , "one two" , "=" , "one two" ], 0 ),
37+ ("sh -c 'exit 7'" , 7 ),
38+ ],
39+ )
40+ def test_exec_wait_strategy_with_real_container (self , command , expected_exit_code ):
41+ strategy = ExecWaitStrategy (command , expected_exit_code ).with_startup_timeout (2 ).with_poll_interval (0.1 )
42+
43+ with DockerContainer ("alpine:latest" ).with_command ("sleep 30" ).waiting_for (strategy ) as container :
44+ assert container .exec (command ).exit_code == expected_exit_code
45+
46+ def test_exec_wait_strategy_retries_string_command (self ):
47+ strategy = ExecWaitStrategy ("test -f /tmp/ready" ).with_startup_timeout (5 ).with_poll_interval (0.1 )
48+
49+ with (
50+ DockerContainer ("alpine:latest" )
51+ .with_command (["sh" , "-c" , "sleep 1; touch /tmp/ready; sleep 30" ])
52+ .waiting_for (strategy )
53+ ) as container :
54+ assert container .exec (["test" , "-f" , "/tmp/ready" ]).exit_code == 0
55+
56+ def test_exec_wait_strategy_does_not_implicitly_use_shell (self ):
57+ command = "test $HOME = /root"
58+ strategy = ExecWaitStrategy (command ).with_startup_timeout (1 ).with_poll_interval (0.1 )
59+
60+ with DockerContainer ("alpine:latest" ).with_command ("sleep 30" ) as container :
61+ assert container .exec (["sh" , "-c" , command ]).exit_code == 0
62+ with pytest .raises (TimeoutError , match = re .escape (f"Command: { command } ." )):
63+ strategy .wait_until_ready (container )
64+
2965
3066class TestDockerComposeIntegration :
3167 """Integration tests for wait strategies with Docker Compose."""
0 commit comments