|
| 1 | +from time import sleep |
| 2 | + |
| 3 | +from docker.models.containers import Container |
| 4 | + |
| 5 | +from testcontainers.core.config import testcontainers_config |
| 6 | +from testcontainers.core.container import DockerContainer |
| 7 | +from testcontainers.core.docker_client import DockerClient |
| 8 | +from testcontainers.core.waiting_utils import wait_for_logs |
| 9 | +from testcontainers.core.container import Reaper |
| 10 | + |
| 11 | + |
| 12 | +def test_docker_container_reuse_default(): |
| 13 | + with DockerContainer("hello-world") as container: |
| 14 | + assert container._reuse == False |
| 15 | + id = container._container.id |
| 16 | + wait_for_logs(container, "Hello from Docker!") |
| 17 | + containers = DockerClient().client.containers.list(all=True) |
| 18 | + assert id not in [container.id for container in containers] |
| 19 | + |
| 20 | + |
| 21 | +def test_docker_container_with_reuse_reuse_disabled(): |
| 22 | + with DockerContainer("hello-world").with_reuse() as container: |
| 23 | + assert container._reuse == True |
| 24 | + id = container._container.id |
| 25 | + wait_for_logs(container, "Hello from Docker!") |
| 26 | + containers = DockerClient().client.containers.list(all=True) |
| 27 | + assert id not in [container.id for container in containers] |
| 28 | + |
| 29 | + |
| 30 | +def test_docker_container_with_reuse_reuse_enabled_ryuk_enabled(monkeypatch): |
| 31 | + # Make sure Ryuk cleanup is not active from previous test runs |
| 32 | + Reaper.delete_instance() |
| 33 | + tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} |
| 34 | + monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) |
| 35 | + monkeypatch.setattr(testcontainers_config, "ryuk_reconnection_timeout", "0.1s") |
| 36 | + |
| 37 | + with DockerContainer("hello-world").with_reuse() as container: |
| 38 | + id = container._container.id |
| 39 | + wait_for_logs(container, "Hello from Docker!") |
| 40 | + |
| 41 | + Reaper._socket.close() |
| 42 | + # Sleep until Ryuk reaps all dangling containers |
| 43 | + sleep(0.6) |
| 44 | + |
| 45 | + containers = DockerClient().client.containers.list(all=True) |
| 46 | + assert id not in [container.id for container in containers] |
| 47 | + |
| 48 | + # Cleanup Ryuk class fields after manual Ryuk shutdown |
| 49 | + Reaper.delete_instance() |
| 50 | + |
| 51 | + |
| 52 | +def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled(monkeypatch): |
| 53 | + # Make sure Ryuk cleanup is not active from previous test runs |
| 54 | + Reaper.delete_instance() |
| 55 | + tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} |
| 56 | + monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) |
| 57 | + monkeypatch.setattr(testcontainers_config, "ryuk_disabled", True) |
| 58 | + with DockerContainer("hello-world").with_reuse() as container: |
| 59 | + assert container._reuse == True |
| 60 | + id = container._container.id |
| 61 | + wait_for_logs(container, "Hello from Docker!") |
| 62 | + containers = DockerClient().client.containers.list(all=True) |
| 63 | + assert id in [container.id for container in containers] |
| 64 | + # Cleanup after keeping container alive (with_reuse) |
| 65 | + container._container.remove(force=True) |
| 66 | + |
| 67 | + |
| 68 | +def test_docker_container_labels_hash(): |
| 69 | + expected_hash = "91fde3c09244e1d3ec6f18a225b9261396b9a1cb0f6365b39b9795782817c128" |
| 70 | + with DockerContainer("hello-world").with_reuse() as container: |
| 71 | + assert container._container.labels["hash"] == expected_hash |
| 72 | + |
| 73 | + |
| 74 | +def test_docker_client_find_container_by_hash_not_existing(): |
| 75 | + with DockerContainer("hello-world"): |
| 76 | + assert DockerClient().find_container_by_hash("foo") == None |
| 77 | + |
| 78 | + |
| 79 | +def test_docker_client_find_container_by_hash_existing(): |
| 80 | + with DockerContainer("hello-world").with_reuse() as container: |
| 81 | + hash_ = container._container.labels["hash"] |
| 82 | + found_container = DockerClient().find_container_by_hash(hash_) |
| 83 | + assert isinstance(found_container, Container) |
0 commit comments