|
1 | 1 | """Test docker login.""" |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | # pylint: disable=protected-access |
4 | 6 | from supervisor.coresys import CoreSys |
5 | | -from supervisor.docker.const import DOCKER_HUB |
| 7 | +from supervisor.docker.const import DOCKER_HUB, DOCKER_HUB_LEGACY |
6 | 8 | from supervisor.docker.interface import DockerInterface |
| 9 | +from supervisor.docker.utils import get_registry_from_image |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + ("image_ref", "expected_registry"), |
| 14 | + [ |
| 15 | + # No registry - Docker Hub images |
| 16 | + ("nginx", None), |
| 17 | + ("nginx:latest", None), |
| 18 | + ("library/nginx", None), |
| 19 | + ("library/nginx:latest", None), |
| 20 | + ("homeassistant/amd64-supervisor", None), |
| 21 | + ("homeassistant/amd64-supervisor:1.2.3", None), |
| 22 | + # Registry with dot |
| 23 | + ("ghcr.io/homeassistant/amd64-supervisor", "ghcr.io"), |
| 24 | + ("ghcr.io/homeassistant/amd64-supervisor:latest", "ghcr.io"), |
| 25 | + ("myregistry.com/nginx", "myregistry.com"), |
| 26 | + ("registry.example.com/org/image:v1", "registry.example.com"), |
| 27 | + ("127.0.0.1/myimage", "127.0.0.1"), |
| 28 | + # Registry with port |
| 29 | + ("myregistry:5000/myimage", "myregistry:5000"), |
| 30 | + ("localhost:5000/myimage", "localhost:5000"), |
| 31 | + ("registry.io:5000/org/app:v1", "registry.io:5000"), |
| 32 | + # localhost special case |
| 33 | + ("localhost/myimage", "localhost"), |
| 34 | + ("localhost/myimage:tag", "localhost"), |
| 35 | + # IPv6 |
| 36 | + ("[::1]:5000/myimage", "[::1]:5000"), |
| 37 | + ("[2001:db8::1]:5000/myimage:tag", "[2001:db8::1]:5000"), |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_get_registry_from_image(image_ref: str, expected_registry: str | None): |
| 41 | + """Test get_registry_from_image extracts registry from image reference. |
| 42 | +
|
| 43 | + Based on Docker's reference implementation: |
| 44 | + vendor/github.com/distribution/reference/normalize.go |
| 45 | + """ |
| 46 | + assert get_registry_from_image(image_ref) == expected_registry |
7 | 47 |
|
8 | 48 |
|
9 | 49 | def test_no_credentials(coresys: CoreSys, test_docker_interface: DockerInterface): |
@@ -47,3 +87,36 @@ def test_matching_credentials(coresys: CoreSys, test_docker_interface: DockerInt |
47 | 87 | ) |
48 | 88 | assert credentials["username"] == "Spongebob Squarepants" |
49 | 89 | assert "registry" not in credentials |
| 90 | + |
| 91 | + |
| 92 | +def test_legacy_docker_hub_credentials( |
| 93 | + coresys: CoreSys, test_docker_interface: DockerInterface |
| 94 | +): |
| 95 | + """Test legacy hub.docker.com credentials are used for Docker Hub images.""" |
| 96 | + coresys.docker.config._data["registries"] = { |
| 97 | + DOCKER_HUB_LEGACY: {"username": "LegacyUser", "password": "Password1!"}, |
| 98 | + } |
| 99 | + |
| 100 | + credentials = test_docker_interface._get_credentials( |
| 101 | + "homeassistant/amd64-supervisor" |
| 102 | + ) |
| 103 | + assert credentials["username"] == "LegacyUser" |
| 104 | + # No registry should be included for Docker Hub |
| 105 | + assert "registry" not in credentials |
| 106 | + |
| 107 | + |
| 108 | +def test_docker_hub_preferred_over_legacy( |
| 109 | + coresys: CoreSys, test_docker_interface: DockerInterface |
| 110 | +): |
| 111 | + """Test docker.io is preferred over legacy hub.docker.com when both exist.""" |
| 112 | + coresys.docker.config._data["registries"] = { |
| 113 | + DOCKER_HUB: {"username": "NewUser", "password": "Password1!"}, |
| 114 | + DOCKER_HUB_LEGACY: {"username": "LegacyUser", "password": "Password2!"}, |
| 115 | + } |
| 116 | + |
| 117 | + credentials = test_docker_interface._get_credentials( |
| 118 | + "homeassistant/amd64-supervisor" |
| 119 | + ) |
| 120 | + # docker.io should be preferred |
| 121 | + assert credentials["username"] == "NewUser" |
| 122 | + assert "registry" not in credentials |
0 commit comments