File: test_registries.py

package info (click to toggle)
gitlabracadabra 2.8.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,520 kB
  • sloc: python: 12,305; javascript: 663; makefile: 4
file content (144 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#
# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from typing import NamedTuple

from gitlabracadabra.containers.const import DOCKER_HOSTNAME, DOCKER_REGISTRY
from gitlabracadabra.containers.registries import Registries
from gitlabracadabra.tests.case import TestCase


class FixtureData(NamedTuple):
    input_name: str
    hostname: str
    manifest_name: str
    tag: str
    reference_tag: str | None
    digest: str | None
    full_reference: str
    short_reference: str


LATEST = "latest"
TEST_DATA = (
    FixtureData(
        "my/repo",
        DOCKER_HOSTNAME,
        "my/repo",
        LATEST,
        None,
        None,
        "docker.io/my/repo",
        "my/repo",
    ),
    FixtureData(
        "debian",
        DOCKER_HOSTNAME,
        "library/debian",
        LATEST,
        None,
        None,
        "docker.io/library/debian",
        "debian",
    ),
    FixtureData(
        "localhost/foo/bar:v1.0",
        "localhost",
        "foo/bar",
        "v1.0",
        "v1.0",
        None,
        "localhost/foo/bar:v1.0",
        "localhost/foo/bar:v1.0",
    ),
    FixtureData(
        "example.org/foo/baz:v2.3.4",
        "example.org",
        "foo/baz",
        "v2.3.4",
        "v2.3.4",
        None,
        "example.org/foo/baz:v2.3.4",
        "example.org/foo/baz:v2.3.4",
    ),
    FixtureData(
        "registry:5000/foo/bar:latest",
        "registry:5000",
        "foo/bar",
        LATEST,
        LATEST,
        None,
        "registry:5000/foo/bar:latest",
        "registry:5000/foo/bar",
    ),
    FixtureData(
        "busybox:latest@sha256:74e4a68dfba6f40b01787a3876cc1be0fb1d9025c3567cf8367c659f2187234f",
        DOCKER_HOSTNAME,
        "library/busybox",
        LATEST,
        LATEST,
        "sha256:74e4a68dfba6f40b01787a3876cc1be0fb1d9025c3567cf8367c659f2187234f",
        "docker.io/library/busybox:latest@sha256:74e4a68dfba6f40b01787a3876cc1be0fb1d9025c3567cf8367c659f2187234f",
        "busybox@sha256:74e4a68dfba6f40b01787a3876cc1be0fb1d9025c3567cf8367c659f2187234f",
    ),
)


class TestRegistries(TestCase):
    def test_singleton(self) -> None:
        registries1 = Registries()
        registries2 = Registries()
        assert id(registries1) == id(registries2)

    def test_get_registry(self) -> None:
        registries = Registries()
        docker_registry = registries.get_registry(DOCKER_REGISTRY)
        assert docker_registry.hostname == DOCKER_HOSTNAME
        docker_registry2 = registries.get_registry(DOCKER_HOSTNAME)
        assert docker_registry2.hostname == DOCKER_HOSTNAME
        docker_registry3 = registries.get_registry("example.org")
        assert docker_registry3.hostname == "example.org"

    def test_get_manifest(self) -> None:
        registries = Registries()
        for test_data in TEST_DATA:
            with self.subTest(input_name=test_data.input_name):
                manifest = registries.get_manifest(test_data.input_name)
                assert manifest.registry.hostname == test_data.hostname
                assert manifest.manifest_name == test_data.manifest_name
                assert manifest.tag == test_data.tag
                assert manifest._digest == test_data.digest

    def test_short_reference(self) -> None:
        for test_data in TEST_DATA:
            with self.subTest(input_name=test_data.input_name):
                reference = Registries.short_reference(test_data.input_name)
                assert reference == test_data.short_reference

    def test_full_reference(self) -> None:
        for test_data in TEST_DATA:
            with self.subTest(input_name=test_data.input_name):
                reference = Registries.full_reference(test_data.input_name)
                assert reference == test_data.full_reference

    def test_full_reference_parts(self) -> None:
        for test_data in TEST_DATA:
            with self.subTest(input_name=test_data.input_name):
                manifest = Registries.full_reference_parts(test_data.input_name)
                assert manifest.hostname == test_data.hostname
                assert manifest.manifest_name == test_data.manifest_name
                assert manifest.tag == test_data.reference_tag
                assert manifest.digest == test_data.digest