Skip to content
Closed
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from atexit import register
from contextlib import suppress
from platform import system
from signal import SIGINT, SIGTERM, signal
from socket import socket
from typing import TYPE_CHECKING, Optional

from docker.errors import NotFound

from testcontainers.core.config import RYUK_DISABLED, RYUK_DOCKER_SOCKET, RYUK_IMAGE, RYUK_PRIVILEGED
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.exceptions import ContainerStartException
Expand Down Expand Up @@ -39,7 +44,7 @@ def __init__(
self.volumes = {}
self.image = image
self._docker = DockerClient(**(docker_client_kw or {}))
self._container = None
self._container: Optional[Container] = None
self._command = None
self._name = None
self._kwargs = kwargs
Expand Down Expand Up @@ -178,7 +183,9 @@ def delete_instance(cls) -> None:
Reaper._socket = None

if Reaper._container is not None:
Reaper._container.stop()
if Reaper._container._container is not None:
with suppress(NotFound):
Reaper._container._container.stop()
Reaper._container = None

if Reaper._instance is not None:
Expand All @@ -193,9 +200,12 @@ def _create_instance(cls) -> "Reaper":
.with_name(f"testcontainers-ryuk-{SESSION_ID}")
.with_exposed_ports(8080)
.with_volume_mapping(RYUK_DOCKER_SOCKET, "/var/run/docker.sock", "rw")
.with_kwargs(privileged=RYUK_PRIVILEGED)
.with_kwargs(privileged=RYUK_PRIVILEGED, auto_remove=True)
.start()
)
register(lambda: Reaper.delete_instance())
signal(SIGINT, lambda _, __: Reaper.delete_instance())
signal(SIGTERM, lambda _, __: Reaper.delete_instance())
wait_for_logs(Reaper._container, r".* Started!")

container_host = Reaper._container.get_container_host_ip()
Expand Down
25 changes: 22 additions & 3 deletions core/tests/test_ryuk.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pytest

from contextlib import contextmanager

from testcontainers.core import container
from testcontainers.core.container import Reaper
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for_logs


@pytest.mark.skip("invalid test - ryuk logs 'Removed' right before exiting")
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we remove the test if it is invalid?
I think the test should check, that the Ryuk container and the Docker container are gone in this case?

def test_wait_for_reaper():
container = DockerContainer("hello-world").start()
wait_for_logs(container, "Hello from Docker!")
Expand All @@ -19,6 +24,20 @@ def test_wait_for_reaper():

def test_container_without_ryuk(monkeypatch):
monkeypatch.setattr(container, "RYUK_DISABLED", True)
with DockerContainer("hello-world") as cont:
wait_for_logs(cont, "Hello from Docker!")
assert Reaper._instance is None

@contextmanager
def reset_reaper_instance():
old_value = Reaper._instance
Reaper._instance = None
yield
Reaper._instance = old_value

with reset_reaper_instance():
with DockerContainer("hello-world") as cont:
wait_for_logs(cont, "Hello from Docker!")
assert Reaper._instance is None


def test_reaper_gets_deleted():
"""this is a manual check - run 'docker ps -a' after the test suite to 'assert'."""
DockerContainer("alpine").with_command("tail -f /dev/stdout").start()