Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added retries to the container state check in TestDocker #12640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions tests/integration/docker_utils/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def _is_podman_test() -> bool:
return os.getenv("DOCKER_CMD") == "podman"


def _assert_container_state(docker_client: ContainerClient, name: str, is_running: bool):
assert docker_client.is_container_running(name) == is_running


@pytest.fixture
def dummy_container(create_container):
"""Returns a container that is created but not started"""
Expand Down Expand Up @@ -1273,22 +1277,39 @@ def test_run_container_non_existent_image(self, docker_client: ContainerClient):
def test_running_container_names(self, docker_client: ContainerClient, dummy_container):
docker_client.start_container(dummy_container.container_id)
name = dummy_container.container_name
assert name in docker_client.get_running_container_names()
retry(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: This works because the retry catches the AssertionError, which doesn't happen in, e.g., poll_condition. Was a bit confused at first :D

lambda: _assert_container_state(docker_client, name, is_running=True),
sleep=2,
retries=5,
)
docker_client.stop_container(name)
assert name not in docker_client.get_running_container_names()
retry(
lambda: _assert_container_state(docker_client, name, is_running=False),
sleep=2,
retries=5,
)

def test_is_container_running(self, docker_client: ContainerClient, dummy_container):
docker_client.start_container(dummy_container.container_id)
name = dummy_container.container_name

def _assert_container_state(is_running: bool):
assert docker_client.is_container_running(name) == is_running

retry(lambda: _assert_container_state(is_running=True), sleep=2, retries=5)
retry(
lambda: _assert_container_state(docker_client, name, is_running=True),
sleep=2,
retries=5,
)
docker_client.restart_container(name)
retry(lambda: _assert_container_state(is_running=True), sleep=2, retries=5)
retry(
lambda: _assert_container_state(docker_client, name, is_running=True),
sleep=2,
retries=5,
)
docker_client.stop_container(name)
retry(lambda: _assert_container_state(is_running=False), sleep=2, retries=5)
retry(
lambda: _assert_container_state(docker_client, name, is_running=False),
sleep=2,
retries=5,
)

@markers.skip_offline
def test_docker_image_names(self, docker_client: ContainerClient):
Expand Down
Loading