File: test_registry.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 (296 lines) | stat: -rw-r--r-- 11,370 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#
# 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 __future__ import annotations

from contextlib import contextmanager
from os.path import isfile
from typing import TYPE_CHECKING
from unittest import skipIf
from unittest.mock import call, patch

from vcr import __version__ as vcr_version

from gitlabracadabra.containers.blob import Blob
from gitlabracadabra.containers.const import DOCKER_HOSTNAME, DOCKER_MANIFEST_SCHEMA2, DOCKER_MANIFEST_SCHEMA2_LIST
from gitlabracadabra.containers.manifest import Manifest
from gitlabracadabra.containers.registry import Registry
from gitlabracadabra.containers.with_digest import WithDigest
from gitlabracadabra.tests import my_vcr
from gitlabracadabra.tests.case import TestCase

if TYPE_CHECKING:
    from collections.abc import Generator
    from logging import Logger


VERIFY_DIGEST_METHOD = "_verify_digest"
REGISTRY_IMPORTER_LOGGER = "gitlabracadabra.containers.registry_importer.logger"
REGISTRY_IMPORTER_MESSAGE = "%s%s %simported as %s:%s (%s, %s, %s)"
BLOBS2_0_0_MESSAGE = "2+0+0=2 uploaded+mounted+existing blobs"

DEBIAN_IMAGE = "library/debian"
BUSYBOX_IMAGE = "library/busybox"
BUSYBOX_IMAGE_FULL = "docker.io/library/busybox:latest@{0}"
LATEST = "latest"


class TestRegistry(TestCase):
    def destination_registry(self):
        registry = Registry("localhost:5000")
        registry._session.scheme = "http"
        return registry

    def assert_with_digest(self, registry, cacheable, digest, size, mime_type):
        assert isinstance(cacheable, WithDigest)
        assert cacheable.registry == registry
        assert cacheable.digest == digest
        assert cacheable.size == size
        assert cacheable.mime_type == mime_type

    def assert_manifest(self, registry, manifest, digest, size, mime_type):
        self.assert_with_digest(registry, manifest, digest, size, mime_type)
        assert isinstance(manifest, Manifest)
        assert isinstance(manifest.json, dict)
        assert isfile(manifest.cache_path)
        assert manifest.json["mediaType"] == mime_type
        assert manifest.json["schemaVersion"] == 2

    @contextmanager
    def mocked_objects(self) -> Generator[Logger, None, None]:
        with patch.object(WithDigest, VERIFY_DIGEST_METHOD) as verify_digest_mock:
            verify_digest_mock.return_value = None
            with patch(REGISTRY_IMPORTER_LOGGER, autospec=True) as logger:
                yield logger

    @my_vcr.use_cassette
    def test_manifest(self, cass):
        digest = "sha256:102ab2db1ad671545c0ace25463c4e3c45f9b15e319d3a00a1b2b085293c27fb"
        registry = Registry(DOCKER_HOSTNAME)
        manifest = registry.manifest(DEBIAN_IMAGE, digest)
        self.assert_manifest(
            registry,
            manifest,
            digest,
            529,
            DOCKER_MANIFEST_SCHEMA2,
        )
        assert isinstance(manifest.json["config"], dict)
        assert isinstance(manifest.json["layers"], list)
        assert cass.all_played

    @my_vcr.use_cassette
    def test_manifest_list(self, cass):
        registry = Registry(DOCKER_HOSTNAME)
        manifest_list = registry.manifest(DEBIAN_IMAGE)
        self.assert_manifest(
            registry,
            manifest_list,
            "sha256:1092695e843ad975267131f27a2b523128c4e03d2d96574bbdd7cf949ed51475",
            1854,
            DOCKER_MANIFEST_SCHEMA2_LIST,
        )
        assert isinstance(manifest_list.json["manifests"], list)

        manifests = manifest_list.manifests()
        assert isinstance(manifests, list)
        first = manifests[0]
        assert not isfile(first.cache_path)
        self.assert_manifest(
            registry,
            first,
            "sha256:102ab2db1ad671545c0ace25463c4e3c45f9b15e319d3a00a1b2b085293c27fb",
            529,
            DOCKER_MANIFEST_SCHEMA2,
        )
        assert isinstance(first, Manifest)
        assert first.platform == {"architecture": "amd64", "os": "linux"}
        assert cass.all_played

    @my_vcr.use_cassette
    def test_blob(self, cass):
        manifest_name = DEBIAN_IMAGE
        digest = "sha256:5890f8ba95f680c87fcf89e51190098641b4f646102ce7ca906e7f83c84874dc"
        registry = Registry(DOCKER_HOSTNAME)
        blob = registry.blob(manifest_name, digest)
        assert isinstance(blob, Blob)
        assert blob.registry == registry
        assert blob.manifest_name == manifest_name
        assert blob.digest == digest
        assert not isfile(blob.cache_path)
        assert blob.size == 1459
        assert not isfile(blob.cache_path)
        assert cass.all_played

    @skipIf(vcr_version == "6.0.1", "vcrpy without iterator support https://github.com/kevin1024/vcrpy/pull/851")
    @my_vcr.use_cassette
    def test_import_manifest(self, cass):
        digest = "sha256:74e4a68dfba6f40b01787a3876cc1be0fb1d9025c3567cf8367c659f2187234f"
        registry = Registry(DOCKER_HOSTNAME)
        registry2 = self.destination_registry()
        manifest = Manifest(
            registry,
            BUSYBOX_IMAGE,
            digest,
        )
        with self.mocked_objects() as logger:
            registry2.import_manifest(manifest)
            logger.info.assert_has_calls(
                [
                    call(
                        REGISTRY_IMPORTER_MESSAGE,
                        "",
                        BUSYBOX_IMAGE_FULL.format(digest),
                        "",
                        BUSYBOX_IMAGE,
                        LATEST,
                        "1+0=1 uploaded+existing manifests",
                        BLOBS2_0_0_MESSAGE,
                        "766112+0+0=766112 uploaded+mounted+existing blobs size",
                    )
                ]
            )
        assert cass.all_played

    @my_vcr.use_cassette
    def test_import_manifest_already_exists(self, cass):
        digest = "sha256:74e4a68dfba6f40b01787a3876cc1be0fb1d9025c3567cf8367c659f2187234f"
        registry = Registry(DOCKER_HOSTNAME)
        registry2 = self.destination_registry()
        manifest = Manifest(
            registry,
            BUSYBOX_IMAGE,
            digest,
        )
        with self.mocked_objects() as logger:
            registry2.import_manifest(manifest)
            logger.info.assert_has_calls([])
        assert cass.all_played

    @skipIf(vcr_version == "6.0.1", "vcrpy without iterator support https://github.com/kevin1024/vcrpy/pull/851")
    @my_vcr.use_cassette
    def test_import_manifest_list(self, cass):
        digest = "sha256:c6b45a95f932202dbb27c31333c4789f45184a744060f6e569cc9d2bf1b9ad6f"
        registry = Registry(DOCKER_HOSTNAME)
        registry2 = self.destination_registry()
        manifest = Manifest(
            registry,
            BUSYBOX_IMAGE,
            tag=LATEST,
        )
        with self.mocked_objects() as logger:
            registry2.import_manifest(manifest)
            logger.info.assert_has_calls(
                [
                    call(
                        REGISTRY_IMPORTER_MESSAGE,
                        "",
                        BUSYBOX_IMAGE_FULL.format(digest),
                        "",
                        BUSYBOX_IMAGE,
                        LATEST,
                        "1+0=1 uploaded+existing manifests",
                        BLOBS2_0_0_MESSAGE,
                        "766112+0+0=766112 uploaded+mounted+existing blobs size",
                    )
                ]
            )
        assert cass.all_played

    @my_vcr.use_cassette
    def test_import_manifest_list_dry_run(self, cass):
        digest = "sha256:c6b45a95f932202dbb27c31333c4789f45184a744060f6e569cc9d2bf1b9ad6f"
        registry = Registry(DOCKER_HOSTNAME)
        registry2 = self.destination_registry()
        manifest = Manifest(
            registry,
            BUSYBOX_IMAGE,
            tag=LATEST,
        )
        with self.mocked_objects() as logger:
            registry2.import_manifest(manifest, dry_run=True)
            logger.info.assert_has_calls(
                [
                    call(
                        REGISTRY_IMPORTER_MESSAGE,
                        "",
                        BUSYBOX_IMAGE_FULL.format(digest),
                        "NOT ",
                        BUSYBOX_IMAGE,
                        LATEST,
                        "1+0=1 uploaded+existing manifests",
                        BLOBS2_0_0_MESSAGE,
                        "766112+0+0=766112 uploaded+mounted+existing blobs size",
                    )
                ]
            )
        assert cass.all_played

    @skipIf(vcr_version == "6.0.1", "vcrpy without iterator support https://github.com/kevin1024/vcrpy/pull/851")
    @my_vcr.use_cassette
    def test_import_manifest_list_all(self, cass):
        digest = "sha256:c6b45a95f932202dbb27c31333c4789f45184a744060f6e569cc9d2bf1b9ad6f"
        registry = Registry(DOCKER_HOSTNAME)
        registry2 = self.destination_registry()
        manifest = Manifest(
            registry,
            BUSYBOX_IMAGE,
            tag=LATEST,
        )
        with self.mocked_objects() as logger:
            registry2.import_manifest(manifest, "newname", "newtag", platform="all")
            logger.info.assert_has_calls(
                [
                    call(
                        REGISTRY_IMPORTER_MESSAGE,
                        "",
                        BUSYBOX_IMAGE_FULL.format(digest),
                        "",
                        "newname",
                        "newtag",
                        "2+8=10 uploaded+existing manifests",
                        BLOBS2_0_0_MESSAGE,
                        "2140766+0+0=2140766 uploaded+mounted+existing blobs size",
                    )
                ]
            )
        assert cass.all_played

    @my_vcr.use_cassette
    def test_import_manifest_not_found(self, cass):
        registry = Registry(DOCKER_HOSTNAME)
        registry2 = self.destination_registry()
        manifest = Manifest(
            registry,
            BUSYBOX_IMAGE,
            tag="not_found",
        )
        url = "https://registry-1.docker.io/v2/library/busybox/manifests/not_found"
        with self.mocked_objects() as logger:
            registry2.import_manifest(manifest)
            logger.warning.assert_has_calls(
                [
                    call(
                        "%s%s NOT imported as %s:%s: %s",
                        "",
                        "docker.io/library/busybox:not_found",
                        "library/busybox",
                        "not_found",
                        f"404 Client Error: Not Found for url: {url}",
                    )
                ]
            )
        assert cass.all_played