|
| 1 | +import pathlib |
| 2 | +import textwrap |
| 3 | + |
1 | 4 | import pytest |
2 | 5 |
|
3 | | -from testcontainers.core.container import DockerContainer |
| 6 | +from testcontainers.core.container import DockerContainer, NotHealthy |
4 | 7 | from testcontainers.core.docker_client import DockerClient |
5 | 8 | from testcontainers.core.config import ConnectionMode |
| 9 | +from testcontainers.core.image import DockerImage |
6 | 10 |
|
7 | 11 | FAKE_ID = "ABC123" |
8 | 12 |
|
@@ -96,3 +100,66 @@ def test_attribute(init_attr, init_value, class_attr, stored_value): |
96 | 100 | """Test that the attributes set through the __init__ function are properly stored.""" |
97 | 101 | with DockerContainer("ubuntu", **{init_attr: init_value}) as container: |
98 | 102 | assert getattr(container, class_attr) == stored_value |
| 103 | + |
| 104 | + |
| 105 | +@pytest.fixture(name="with_never_healthy_image") |
| 106 | +def with_never_health_image_fixture(tmp_path): |
| 107 | + DOCKERFILE = """FROM alpine:latest |
| 108 | + HEALTHCHECK --interval=1s CMD test -e /testfile |
| 109 | + CMD sleep infinity |
| 110 | + """ |
| 111 | + dockerfile = tmp_path / "Dockerfile" |
| 112 | + dockerfile.write_text(textwrap.dedent(DOCKERFILE)) |
| 113 | + with DockerImage(tmp_path) as image: |
| 114 | + yield image |
| 115 | + |
| 116 | + |
| 117 | +def test_wait_for_healthcheck_never_healthy(with_never_healthy_image: DockerImage): |
| 118 | + # Given |
| 119 | + with DockerContainer(image=str(with_never_healthy_image)) as container: |
| 120 | + # Expect |
| 121 | + with pytest.raises(NotHealthy): |
| 122 | + # When |
| 123 | + container.wait_for_healthcheck() |
| 124 | + |
| 125 | + |
| 126 | +@pytest.fixture(name="with_immediately_healthy_image") |
| 127 | +def with_immediately_healthy_image_fixture(tmp_path): |
| 128 | + DOCKERFILE = """FROM alpine:latest |
| 129 | + RUN touch /testfile |
| 130 | +
|
| 131 | + HEALTHCHECK --interval=1s CMD test -e /testfile |
| 132 | + CMD sleep infinity |
| 133 | + """ |
| 134 | + dockerfile = tmp_path / "Dockerfile" |
| 135 | + dockerfile.write_text(textwrap.dedent(DOCKERFILE)) |
| 136 | + with DockerImage(tmp_path) as image: |
| 137 | + yield image |
| 138 | + |
| 139 | + |
| 140 | +def test_wait_for_healthcheck_immediate_healthy(with_immediately_healthy_image: DockerImage): |
| 141 | + # Given |
| 142 | + with DockerContainer(image=str(with_immediately_healthy_image)) as container: |
| 143 | + # When |
| 144 | + container.wait_for_healthcheck() |
| 145 | + |
| 146 | + |
| 147 | +@pytest.fixture(name="with_eventually_healthy_image") |
| 148 | +def with_eventually_healthy_image_fixture(tmp_path): |
| 149 | + DOCKERFILE = """FROM alpine:latest |
| 150 | + RUN touch /testfile |
| 151 | +
|
| 152 | + HEALTHCHECK --interval=1s CMD test -e /testfile |
| 153 | + CMD sleep 4 && touch /testfile |
| 154 | + """ |
| 155 | + dockerfile = tmp_path / "Dockerfile" |
| 156 | + dockerfile.write_text(textwrap.dedent(DOCKERFILE)) |
| 157 | + with DockerImage(tmp_path) as image: |
| 158 | + yield image |
| 159 | + |
| 160 | + |
| 161 | +def test_wait_for_healthcheck_eventually_healthy(with_eventually_healthy_image: DockerImage): |
| 162 | + # Given |
| 163 | + with DockerContainer(str(with_eventually_healthy_image)) as container: |
| 164 | + # When |
| 165 | + container.wait_for_healthcheck() |
0 commit comments