Skip to content

Commit ce76c6f

Browse files
committed
fix(core): preserve string commands in ExecWaitStrategy
1 parent 346caf0 commit ce76c6f

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎src/testcontainers/core/wait_strategies.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ class ExecWaitStrategy(WaitStrategy):
800800
command: Command to execute (list of strings or single string)
801801
expected_exit_code: Expected exit code for success (default: 0)
802802
803+
String commands follow Docker's argument parsing, without an implicit shell.
804+
To use shell expansion or operators, invoke a shell explicitly.
805+
803806
Example:
804807
# Wait for Postgres readiness
805808
strategy = ExecWaitStrategy(
@@ -820,7 +823,7 @@ def __init__(
820823
expected_exit_code: int = 0,
821824
) -> None:
822825
super().__init__()
823-
self._command = command if isinstance(command, list) else [command]
826+
self._command = command
824827
self._expected_exit_code = expected_exit_code
825828

826829
def wait_until_ready(self, container: WaitStrategyTarget) -> None:
@@ -848,7 +851,7 @@ def wait_until_ready(self, container: WaitStrategyTarget) -> None:
848851
while True:
849852
duration = time.time() - start_time
850853
if duration > self._startup_timeout:
851-
command_str = " ".join(self._command)
854+
command_str = self._command if isinstance(self._command, str) else " ".join(self._command)
852855
raise TimeoutError(
853856
f"Command execution did not succeed within {self._startup_timeout:.3f} seconds. "
854857
f"Command: {command_str}. "

‎tests/core/test_wait_strategies_integration.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import re
12
from pathlib import Path
23

34
import pytest
45

56
from testcontainers.core.container import DockerContainer
6-
from testcontainers.core.wait_strategies import LogMessageWaitStrategy
7+
from testcontainers.core.wait_strategies import ExecWaitStrategy, LogMessageWaitStrategy
78

89

910
class 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

3066
class TestDockerComposeIntegration:
3167
"""Integration tests for wait strategies with Docker Compose."""

0 commit comments

Comments
 (0)