Skip to content

Commit 68363d3

Browse files
committed
test: Add unit tests for Unix socket discovery logic
Add UnixSocketDiscoveryTest class with tests that mock os.path.exists to verify socket selection behavior: - test_find_socket_prefers_traditional_location: /var/run/docker.sock preferred - test_find_socket_falls_back_to_docker_desktop: ~/.docker/run/docker.sock used - test_find_socket_falls_back_to_older_docker_desktop: ~/.docker/desktop/docker.sock used - test_find_socket_fallback_when_none_exist: Falls back to traditional location - test_find_socket_preference_order: Verifies v4 > older desktop ordering - test_unix_socket_paths_order: Verifies UNIX_SOCKET_PATHS constant
1 parent b9ec333 commit 68363d3

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

tests/unit/utils_test.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,89 @@ def test_compare_versions():
634634
assert compare_version("1", "1.0") == 0
635635
assert compare_version("1.10", "1.10.1") == 1
636636
assert compare_version("1.10.0", "1.10") == 0
637+
638+
639+
class UnixSocketDiscoveryTest(unittest.TestCase):
640+
"""Tests for the Unix socket discovery logic in constants.py."""
641+
642+
def test_find_socket_prefers_traditional_location(self):
643+
"""When /var/run/docker.sock exists, it should be preferred."""
644+
from unittest import mock
645+
from docker.constants import UNIX_SOCKET_PATHS, _find_available_unix_socket
646+
647+
def mock_exists(path):
648+
# All sockets exist - should prefer the first one
649+
return path in UNIX_SOCKET_PATHS
650+
651+
with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists):
652+
result = _find_available_unix_socket()
653+
assert result == "http+unix:///var/run/docker.sock"
654+
655+
def test_find_socket_falls_back_to_docker_desktop(self):
656+
"""When only ~/.docker/run/docker.sock exists, it should be used."""
657+
from unittest import mock
658+
from docker.constants import UNIX_SOCKET_PATHS, _find_available_unix_socket
659+
660+
docker_desktop_socket = os.path.expanduser('~/.docker/run/docker.sock')
661+
662+
def mock_exists(path):
663+
# Only Docker Desktop v4.x+ socket exists
664+
return path == docker_desktop_socket
665+
666+
with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists):
667+
result = _find_available_unix_socket()
668+
assert result == f"http+unix://{docker_desktop_socket}"
669+
670+
def test_find_socket_falls_back_to_older_docker_desktop(self):
671+
"""When only ~/.docker/desktop/docker.sock exists, it should be used."""
672+
from unittest import mock
673+
from docker.constants import UNIX_SOCKET_PATHS, _find_available_unix_socket
674+
675+
older_desktop_socket = os.path.expanduser('~/.docker/desktop/docker.sock')
676+
677+
def mock_exists(path):
678+
# Only older Docker Desktop socket exists
679+
return path == older_desktop_socket
680+
681+
with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists):
682+
result = _find_available_unix_socket()
683+
assert result == f"http+unix://{older_desktop_socket}"
684+
685+
def test_find_socket_fallback_when_none_exist(self):
686+
"""When no socket exists, should fall back to traditional location."""
687+
from unittest import mock
688+
from docker.constants import _find_available_unix_socket
689+
690+
def mock_exists(path):
691+
# No sockets exist
692+
return False
693+
694+
with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists):
695+
result = _find_available_unix_socket()
696+
# Should fall back to traditional location for consistent error messages
697+
assert result == "http+unix:///var/run/docker.sock"
698+
699+
def test_find_socket_preference_order(self):
700+
"""Verify the preference order: traditional > docker desktop v4 > older desktop."""
701+
from unittest import mock
702+
from docker.constants import UNIX_SOCKET_PATHS, _find_available_unix_socket
703+
704+
docker_desktop_socket = os.path.expanduser('~/.docker/run/docker.sock')
705+
older_desktop_socket = os.path.expanduser('~/.docker/desktop/docker.sock')
706+
707+
# Test: when docker desktop v4 and older both exist, v4 should win
708+
def mock_exists_v4_and_older(path):
709+
return path in [docker_desktop_socket, older_desktop_socket]
710+
711+
with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists_v4_and_older):
712+
result = _find_available_unix_socket()
713+
assert result == f"http+unix://{docker_desktop_socket}"
714+
715+
def test_unix_socket_paths_order(self):
716+
"""Verify UNIX_SOCKET_PATHS contains expected paths in correct order."""
717+
from docker.constants import UNIX_SOCKET_PATHS
718+
719+
assert len(UNIX_SOCKET_PATHS) == 3
720+
assert UNIX_SOCKET_PATHS[0] == '/var/run/docker.sock'
721+
assert UNIX_SOCKET_PATHS[1] == os.path.expanduser('~/.docker/run/docker.sock')
722+
assert UNIX_SOCKET_PATHS[2] == os.path.expanduser('~/.docker/desktop/docker.sock')

0 commit comments

Comments
 (0)