|
5 | 5 | import shutil |
6 | 6 | import tempfile |
7 | 7 | import unittest |
| 8 | +from unittest import mock |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 |
|
11 | 12 | from docker.api.client import APIClient |
12 | | -from docker.constants import DEFAULT_DOCKER_API_VERSION, IS_WINDOWS_PLATFORM |
| 13 | +from docker.constants import ( |
| 14 | + DEFAULT_DOCKER_API_VERSION, |
| 15 | + IS_WINDOWS_PLATFORM, |
| 16 | + UNIX_SOCKET_PATHS, |
| 17 | +) |
| 18 | +from docker.constants import _find_available_unix_socket |
13 | 19 | from docker.errors import DockerException |
14 | 20 | from docker.utils import ( |
15 | 21 | compare_version, |
@@ -641,82 +647,71 @@ class UnixSocketDiscoveryTest(unittest.TestCase): |
641 | 647 |
|
642 | 648 | def test_find_socket_prefers_traditional_location(self): |
643 | 649 | """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 | 650 |
|
647 | 651 | def mock_exists(path): |
648 | 652 | # All sockets exist - should prefer the first one |
649 | 653 | return path in UNIX_SOCKET_PATHS |
650 | 654 |
|
651 | | - with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists): |
| 655 | + with mock.patch("docker.constants.os.path.exists", side_effect=mock_exists): |
652 | 656 | result = _find_available_unix_socket() |
653 | 657 | assert result == "http+unix:///var/run/docker.sock" |
654 | 658 |
|
655 | 659 | def test_find_socket_falls_back_to_docker_desktop(self): |
656 | 660 | """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 | + docker_desktop_socket = os.path.expanduser("~/.docker/run/docker.sock") |
661 | 662 |
|
662 | 663 | def mock_exists(path): |
663 | 664 | # Only Docker Desktop v4.x+ socket exists |
664 | 665 | return path == docker_desktop_socket |
665 | 666 |
|
666 | | - with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists): |
| 667 | + with mock.patch("docker.constants.os.path.exists", side_effect=mock_exists): |
667 | 668 | result = _find_available_unix_socket() |
668 | 669 | assert result == f"http+unix://{docker_desktop_socket}" |
669 | 670 |
|
670 | 671 | def test_find_socket_falls_back_to_older_docker_desktop(self): |
671 | 672 | """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') |
| 673 | + older_desktop_socket = os.path.expanduser("~/.docker/desktop/docker.sock") |
676 | 674 |
|
677 | 675 | def mock_exists(path): |
678 | 676 | # Only older Docker Desktop socket exists |
679 | 677 | return path == older_desktop_socket |
680 | 678 |
|
681 | | - with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists): |
| 679 | + with mock.patch("docker.constants.os.path.exists", side_effect=mock_exists): |
682 | 680 | result = _find_available_unix_socket() |
683 | 681 | assert result == f"http+unix://{older_desktop_socket}" |
684 | 682 |
|
685 | 683 | def test_find_socket_fallback_when_none_exist(self): |
686 | 684 | """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 | 685 |
|
690 | 686 | def mock_exists(path): |
691 | 687 | # No sockets exist |
692 | 688 | return False |
693 | 689 |
|
694 | | - with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists): |
| 690 | + with mock.patch("docker.constants.os.path.exists", side_effect=mock_exists): |
695 | 691 | result = _find_available_unix_socket() |
696 | 692 | # Should fall back to traditional location for consistent error messages |
697 | 693 | assert result == "http+unix:///var/run/docker.sock" |
698 | 694 |
|
699 | 695 | def test_find_socket_preference_order(self): |
700 | 696 | """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') |
| 697 | + docker_desktop_socket = os.path.expanduser("~/.docker/run/docker.sock") |
| 698 | + older_desktop_socket = os.path.expanduser("~/.docker/desktop/docker.sock") |
706 | 699 |
|
707 | 700 | # Test: when docker desktop v4 and older both exist, v4 should win |
708 | 701 | def mock_exists_v4_and_older(path): |
709 | 702 | return path in [docker_desktop_socket, older_desktop_socket] |
710 | 703 |
|
711 | | - with mock.patch('docker.constants.os.path.exists', side_effect=mock_exists_v4_and_older): |
| 704 | + with mock.patch( |
| 705 | + "docker.constants.os.path.exists", side_effect=mock_exists_v4_and_older |
| 706 | + ): |
712 | 707 | result = _find_available_unix_socket() |
713 | 708 | assert result == f"http+unix://{docker_desktop_socket}" |
714 | 709 |
|
715 | 710 | def test_unix_socket_paths_order(self): |
716 | 711 | """Verify UNIX_SOCKET_PATHS contains expected paths in correct order.""" |
717 | | - from docker.constants import UNIX_SOCKET_PATHS |
718 | | - |
719 | 712 | 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') |
| 713 | + assert UNIX_SOCKET_PATHS[0] == "/var/run/docker.sock" |
| 714 | + assert UNIX_SOCKET_PATHS[1] == os.path.expanduser("~/.docker/run/docker.sock") |
| 715 | + assert UNIX_SOCKET_PATHS[2] == os.path.expanduser( |
| 716 | + "~/.docker/desktop/docker.sock" |
| 717 | + ) |
0 commit comments