Skip to content

Commit

Permalink
fix(core): use auto_remove=True with reaper instance (testcontainers#499
Browse files Browse the repository at this point in the history
)

fix testcontainers#489 , supercede testcontainers#491, testcontainers#498

Co-authored-by: Stefan Hoffmeister <stefan.hoffmeister@econos.de>
  • Loading branch information
alexanderankin and shoffmeister committed Mar 24, 2024
1 parent cd72f68 commit 274a400
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ 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()
)
wait_for_logs(Reaper._container, r".* Started!")
Expand Down
15 changes: 14 additions & 1 deletion core/tests/test_ryuk.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from contextlib import contextmanager

import pytest

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")
def test_wait_for_reaper():
container = DockerContainer("hello-world").start()
wait_for_logs(container, "Hello from Docker!")
Expand All @@ -17,8 +22,16 @@ def test_wait_for_reaper():
Reaper.delete_instance()


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


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

0 comments on commit 274a400

Please sign in to comment.