Skip to content

Commit b843b44

Browse files
committed
improve dind tests
Make it more clear why a test is skipped or not
1 parent bd1dbda commit b843b44

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

core/tests/test_docker_in_docker.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
_DIND_PYTHON_VERSION = (3, 13)
2424

2525

26-
def _should_run_dind() -> bool:
27-
# todo refine macos check -> run in ci but not locally
28-
return not is_mac() and tuple([*sys.version_info][:2]) == _DIND_PYTHON_VERSION
26+
RUN_ONCE_IN_CI = pytest.mark.skipif(
27+
bool(os.environ.get("CI")) and tuple([*sys.version_info][:2]) != _DIND_PYTHON_VERSION,
28+
reason=(
29+
f"To reduce load the DinD test are only executed "
30+
f"in Python {'.'.join(map(str, _DIND_PYTHON_VERSION))} in the Pipeline."
31+
),
32+
)
2933

3034

3135
def _wait_for_dind_return_ip(client: DockerClient, dind: Container):
@@ -46,10 +50,8 @@ def _wait_for_dind_return_ip(client: DockerClient, dind: Container):
4650
return docker_host_ip
4751

4852

49-
@pytest.mark.skipif(_should_run_dind(), reason="Docker socket forwarding (socat) is unsupported on Docker Desktop for macOS")
50-
@pytest.mark.skipif(
51-
_should_skip_dind(), reason="Docker socket forwarding (socat) is unsupported on Docker Desktop for macOS"
52-
)
53+
@pytest.mark.skipif(is_mac(), reason="Docker socket forwarding (socat) is unsupported on Docker Desktop for macOS")
54+
@RUN_ONCE_IN_CI
5355
def test_wait_for_logs_docker_in_docker():
5456
# real dind isn't possible (AFAIK) in CI
5557
# forwarding the socket to a container port is at least somewhat the same
@@ -76,26 +78,13 @@ def test_wait_for_logs_docker_in_docker():
7678

7779
not_really_dind.stop()
7880
not_really_dind.remove()
79-
try:
80-
with DockerContainer(
81-
image="hello-world",
82-
docker_client_kw={
83-
"environment": {"DOCKER_HOST": docker_host, "DOCKER_CERT_PATH": "", "DOCKER_TLS_VERIFY": ""}
84-
},
85-
) as container:
86-
logger.info("started hello-world container")
87-
assert container.get_container_host_ip() == docker_host_ip
88-
wait_for_logs(container, "Hello from Docker!")
89-
stdout, stderr = container.get_logs()
90-
assert stdout, "There should be something on stdout"
91-
finally:
92-
not_really_dind.stop()
93-
not_really_dind.remove()
9481

9582

9683
@pytest.mark.skipif(
97-
_should_run_dind(), reason="Bridge networking and Docker socket forwarding are not supported on Docker Desktop for macOS"
84+
is_mac(),
85+
reason="Bridge networking and Docker socket forwarding are not supported on Docker Desktop for macOS",
9886
)
87+
@RUN_ONCE_IN_CI
9988
def test_dind_inherits_network():
10089
client = DockerClient()
10190
try:
@@ -176,9 +165,9 @@ def get_docker_info() -> dict[str, Any]:
176165

177166

178167
# see https://forums.docker.com/t/get-a-containers-full-id-from-inside-of-itself
179-
@pytest.mark.xfail(reason="Does not work in rootles docker i.e. github actions")
168+
@pytest.mark.xfail(reason="Does not work in rootless docker i.e. github actions")
180169
@pytest.mark.inside_docker_check
181-
@pytest.mark.skipif(_should_run_dind() or not os.environ.get(EXPECTED_NETWORK_VAR), reason="No expected network given")
170+
@pytest.mark.skipif(not os.environ.get(EXPECTED_NETWORK_VAR), reason="No expected network given")
182171
def test_find_host_network_in_dood() -> None:
183172
"""
184173
Check that the correct host network is found for DooD
@@ -191,9 +180,11 @@ def test_find_host_network_in_dood() -> None:
191180

192181

193182
@pytest.mark.skipif(
194-
_should_run_dind(), reason="Docker socket mounting and container networking do not work reliably on Docker Desktop for macOS"
183+
is_mac(),
184+
reason="Docker socket mounting and container networking do not work reliably on Docker Desktop for macOS",
195185
)
196186
@pytest.mark.skipif(not Path(tcc.ryuk_docker_socket).exists(), reason="No docker socket available")
187+
@RUN_ONCE_IN_CI
197188
def test_dood(python_testcontainer_image: str) -> None:
198189
"""
199190
Run tests marked as inside_docker_check inside docker out of docker
@@ -229,8 +220,10 @@ def test_dood(python_testcontainer_image: str) -> None:
229220

230221

231222
@pytest.mark.skipif(
232-
_should_run_dind(), reason="Docker socket mounting and container networking do not work reliably on Docker Desktop for macOS"
223+
is_mac(),
224+
reason="Docker socket mounting and container networking do not work reliably on Docker Desktop for macOS",
233225
)
226+
@RUN_ONCE_IN_CI
234227
def test_dind(python_testcontainer_image: str, tmp_path: Path) -> None:
235228
"""
236229
Run selected tests in Docker in Docker

0 commit comments

Comments
 (0)