| 
5 | 5 |     TC_FILE,  | 
6 | 6 |     get_user_overwritten_connection_mode,  | 
7 | 7 |     ConnectionMode,  | 
 | 8 | +    get_docker_socket,  | 
8 | 9 | )  | 
9 | 10 | 
 
  | 
10 | 11 | from pytest import MonkeyPatch, mark, LogCaptureFixture  | 
11 | 12 | 
 
  | 
12 | 13 | import logging  | 
13 | 14 | import tempfile  | 
 | 15 | +from unittest.mock import Mock  | 
14 | 16 | 
 
  | 
15 | 17 | 
 
  | 
16 | 18 | def test_read_tc_properties(monkeypatch: MonkeyPatch) -> None:  | 
@@ -84,3 +86,61 @@ def test_valid_connection_mode(monkeypatch: pytest.MonkeyPatch, mode: str, use_m  | 
84 | 86 | def test_no_connection_mode_given(monkeypatch: pytest.MonkeyPatch) -> None:  | 
85 | 87 |     monkeypatch.delenv("TESTCONTAINERS_CONNECTION_MODE", raising=False)  | 
86 | 88 |     assert get_user_overwritten_connection_mode() is None  | 
 | 89 | + | 
 | 90 | + | 
 | 91 | +def test_get_docker_socket_uses_env(monkeypatch: pytest.MonkeyPatch) -> None:  | 
 | 92 | +    """  | 
 | 93 | +    If TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE env var is given prefer it  | 
 | 94 | +    """  | 
 | 95 | +    monkeypatch.setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/var/test.socket")  | 
 | 96 | +    assert get_docker_socket() == "/var/test.socket"  | 
 | 97 | + | 
 | 98 | + | 
 | 99 | +@pytest.fixture  | 
 | 100 | +def mock_docker_client_connections(monkeypatch: pytest.MonkeyPatch) -> None:  | 
 | 101 | +    """  | 
 | 102 | +    Ensure the docker client does not make any actual network calls  | 
 | 103 | +    """  | 
 | 104 | +    from docker.transport.sshconn import SSHHTTPAdapter  | 
 | 105 | +    from docker.api.client import APIClient  | 
 | 106 | + | 
 | 107 | +    # ensure that no actual connection is tried  | 
 | 108 | +    monkeypatch.setattr(SSHHTTPAdapter, "_connect", Mock())  | 
 | 109 | +    monkeypatch.setattr(SSHHTTPAdapter, "_create_paramiko_client", Mock())  | 
 | 110 | +    monkeypatch.setattr(APIClient, "_retrieve_server_version", Mock(return_value="1.47"))  | 
 | 111 | + | 
 | 112 | + | 
 | 113 | +@pytest.mark.usefixtures("mock_docker_client_connections")  | 
 | 114 | +def test_get_docker_host_default(monkeypatch: pytest.MonkeyPatch) -> None:  | 
 | 115 | +    """  | 
 | 116 | +    If non socket docker-host is given return default  | 
 | 117 | +
  | 
 | 118 | +    Still ryuk will properly still not work but this is the historical default  | 
 | 119 | +
  | 
 | 120 | +    """  | 
 | 121 | +    monkeypatch.delenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", raising=False)  | 
 | 122 | +    # Define Fake SSH Docker client  | 
 | 123 | +    monkeypatch.setenv("DOCKER_HOST", "ssh://remote_host")  | 
 | 124 | +    assert get_docker_socket() == "/var/run/docker.sock"  | 
 | 125 | + | 
 | 126 | + | 
 | 127 | +@pytest.mark.usefixtures("mock_docker_client_connections")  | 
 | 128 | +def test_get_docker_host_non_root(monkeypatch: pytest.MonkeyPatch) -> None:  | 
 | 129 | +    """  | 
 | 130 | +    Use the socket determined by the Docker API Adapter  | 
 | 131 | +    """  | 
 | 132 | +    monkeypatch.delenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", raising=False)  | 
 | 133 | +    # Define a Non-Root like Docker Client  | 
 | 134 | +    monkeypatch.setenv("DOCKER_HOST", "unix://var/run/user/1000/docker.sock")  | 
 | 135 | +    assert get_docker_socket() == "/var/run/user/1000/docker.sock"  | 
 | 136 | + | 
 | 137 | + | 
 | 138 | +@pytest.mark.usefixtures("mock_docker_client_connections")  | 
 | 139 | +def test_get_docker_host_root(monkeypatch: pytest.MonkeyPatch) -> None:  | 
 | 140 | +    """  | 
 | 141 | +    Use the socket determined by the Docker API Adapter  | 
 | 142 | +    """  | 
 | 143 | +    monkeypatch.delenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", raising=False)  | 
 | 144 | +    # Define a Root like Docker Client  | 
 | 145 | +    monkeypatch.setenv("DOCKER_HOST", "unix://")  | 
 | 146 | +    assert get_docker_socket() == "/var/run/docker.sock"  | 
0 commit comments