-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_imageindex.py
195 lines (166 loc) · 5.65 KB
/
test_imageindex.py
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
#!/usr/bin/python
# Copyright (C) 2019-2022 Vanessa Sochat.
# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
from opencontainers.image.v1 import Index
import os
import pytest
mediatype_invalid_pattern = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "invalid",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
"platform": {"architecture": "ppc64le", "os": "linux"},
}
],
}
manifest_invalid_string = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": "7682",
"digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
"platform": {"architecture": "amd64", "os": "linux"},
}
],
}
digest_missing = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7682,
"platform": {"architecture": "amd64", "os": "linux"},
}
],
}
platform_arch_missing = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7682,
"digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
"platform": {"os": "linux"},
}
],
}
invalid_manifest_mediatype = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "invalid",
"size": 7682,
"digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
"platform": {"architecture": "amd64", "os": "linux"},
}
],
}
empty_manifest_mediatype = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "",
"size": 7682,
"digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
"platform": {"architecture": "amd64", "os": "linux"},
}
],
}
index_with_optional = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
"platform": {"architecture": "ppc64le", "os": "linux"},
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7682,
"digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
"platform": {"architecture": "amd64", "os": "linux"},
},
],
"annotations": {"com.example.key1": "value1", "com.example.key2": "value2"},
}
index_with_required = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
}
],
}
index_with_custom = {
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/customized.manifest+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
"platform": {"architecture": "ppc64le", "os": "linux"},
}
],
}
root_mediatype_valid = {
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
}
],
}
root_mediatype_invalid = {
"schemaVersion": 2,
"mediaType": "something/else",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
}
],
}
def test_imageindex(tmp_path):
"""test creation of an opencontainers Index"""
index = Index()
# expected failure: mediaType does not match pattern
with pytest.raises(SystemExit):
index.load(mediatype_invalid_pattern)
# expected failure: manifest.size is string, expected integer
with pytest.raises(SystemExit):
index.load(manifest_invalid_string)
# expected failure: manifest.digest is missing, expected required
with pytest.raises(SystemExit):
index.load(digest_missing)
# expected failure: in the optional field platform platform.architecture is missing, expected required
with pytest.raises(SystemExit):
index.load(platform_arch_missing)
# expected failure: invalid referenced manifest media type
with pytest.raises(SystemExit):
index.load(invalid_manifest_mediatype)
# expected failure: empty referenced manifest media type
with pytest.raises(SystemExit):
index.load(empty_manifest_mediatype)
# valid image index, with optional fields
index.load(index_with_optional)
# valid image index, with required fields only
index.load(index_with_required)
# valid image index, with customized media type of referenced manifest
index.load(index_with_custom)
# expected failure: invalid root media type
with pytest.raises(SystemExit):
index.load(root_mediatype_invalid)
# valid root media type
index.load(root_mediatype_valid)