forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexTest.java
More file actions
290 lines (257 loc) · 14.2 KB
/
IndexTest.java
File metadata and controls
290 lines (257 loc) · 14.2 KB
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
/*-
* =LICENSE=
* ORAS Java SDK
* ===
* Copyright (C) 2024 - 2026 ORAS
* ===
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =LICENSEEND=
*/
package land.oras;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.Map;
import land.oras.utils.Const;
import land.oras.utils.SupportedAlgorithm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@Execution(ExecutionMode.CONCURRENT)
class IndexTest {
@Test
void shouldReadAndWriteIndex() {
String json =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}";
Index index = Index.fromJson(json);
assertNull(index.getMediaType());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertNull(index.getArtifactType());
assertNull(index.getAnnotations());
assertNull(index.getDescriptor());
assertEquals(
"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27",
index.getManifests().get(0).getDigest());
assertEquals(559, index.getManifests().get(0).getSize());
assertEquals(json, index.getJson());
String result = index.toJson();
assertEquals(
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}",
result);
}
@Test
void shouldAddArtifactType() {
Index index = Index.fromManifests(List.of());
index = index.withArtifactType("application/vnd.opentofu.provider");
assertNotNull(index.getArtifactType());
assertEquals(
"application/vnd.opentofu.provider", index.getArtifactType().getMediaType());
assertEquals(
"{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.index.v1+json\",\"artifactType\":\"application/vnd.opentofu.provider\",\"manifests\":[]}",
index.toJson());
}
@Test
void shouldReadAndWriteIndexWithAnnotations() {
String json =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559,\"annotations\":{\"foo\":\"bar\"}}]}";
Index index = Index.fromJson(json);
assertNull(index.getMediaType());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertNull(index.getArtifactType());
assertNull(index.getAnnotations());
assertNull(index.getDescriptor());
assertEquals(
"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27",
index.getManifests().get(0).getDigest());
assertEquals(559, index.getManifests().get(0).getSize());
assertEquals(json, index.getJson());
String result = index.toJson();
assertEquals(
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559,\"annotations\":{\"foo\":\"bar\"}}]}",
result);
}
@Test
void shouldNotWriteEmptyAnnotations() {
String json =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559,\"annotations\":{}}]}";
Index index = Index.fromJson(json);
assertNull(index.getAnnotations());
}
@Test
void shouldReadAndWriteIndexWithArtifactType() {
String json =
"{\"schemaVersion\":2,\"artifactType\":\"foo/bar\",\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}";
Index index = Index.fromJson(json);
assertNull(index.getMediaType());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertNull(index.getAnnotations());
assertNotNull(index.getArtifactType());
assertEquals("foo/bar", index.getArtifactType().getMediaType());
assertNull(index.getDescriptor());
assertEquals(
"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27",
index.getManifests().get(0).getDigest());
assertEquals(559, index.getManifests().get(0).getSize());
assertEquals(json, index.toJson());
}
@Test
void shouldAddManifests() {
Index index = Index.fromManifests(List.of());
index = index.withNewManifests(Manifest.empty().getDescriptor());
assertEquals(1, index.getManifests().size());
Manifest newManifest = Manifest.empty().withAnnotations(Map.of("foo", "bar"));
String digest =
SupportedAlgorithm.getDefault().digest(newManifest.toJson().getBytes());
int size = newManifest.toJson().getBytes().length;
ManifestDescriptor descriptor = ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, digest, size)
.withPlatform(Platform.unknown())
.withAnnotations(newManifest.getAnnotations());
newManifest.withDescriptor(descriptor);
index = index.withNewManifests(descriptor);
// Add new manifest with linux platform
Manifest newManifestWithPlatform = Manifest.empty();
String digest2 = SupportedAlgorithm.getDefault()
.digest(newManifestWithPlatform.toJson().getBytes());
int size2 = newManifestWithPlatform.toJson().getBytes().length;
ManifestDescriptor descriptor2 = ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, digest2, size2)
.withPlatform(Platform.linuxAmd64());
newManifestWithPlatform.withDescriptor(descriptor2);
index = index.withNewManifests(descriptor2);
assertEquals(3, index.getManifests().size());
assertNotNull(index.getManifests().get(1).getAnnotations());
assertEquals(1, index.getManifests().get(1).getAnnotations().size());
// Filter unspecified platforms
List<ManifestDescriptor> filtered = index.unspecifiedPlatforms();
assertEquals(2, filtered.size());
// Filter by platform
ManifestDescriptor linuxManifest = index.findUnique(Platform.linuxAmd64());
assertNotNull(linuxManifest);
// Not found
ManifestDescriptor notFound = index.findUnique(Platform.of("darwin", "arm64"));
assertNull(notFound);
}
@Test
void shouldAddSubject() {
Index index = Index.fromManifests(List.of());
index = index.withSubject(Subject.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, "sha256:123", 123));
Subject subject = index.getSubject();
assertNotNull(subject);
assertEquals(Const.DEFAULT_MANIFEST_MEDIA_TYPE, subject.getMediaType());
assertEquals("sha256:123", subject.getDigest());
assertEquals(123, subject.getSize());
assertEquals(
"{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.index.v1+json\",\"subject\":{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:123\",\"size\":123},\"manifests\":[]}",
index.toJson());
}
@Test
void shouldNotAddIfSameDigest() {
Index index = Index.fromManifests(List.of());
index = index.withNewManifests(Manifest.empty().getDescriptor());
assertEquals(1, index.getManifests().size());
index = index.withNewManifests(Manifest.empty().getDescriptor());
assertEquals(1, index.getManifests().size());
index = index.withNewManifests(Manifest.empty().getDescriptor());
assertEquals(1, index.getManifests().size());
}
@Test
void shouldMoveRefAndSetNullAnnotations() {
Index index = Index.fromManifests(List.of());
index = index.withNewManifests(
Manifest.empty().getDescriptor().withAnnotations(Map.of(Const.ANNOTATION_REF, "latest")));
assertEquals(1, index.getManifests().size());
index = index.withNewManifests(ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, "sha256:123", 123)
.withAnnotations(Map.of(Const.ANNOTATION_REF, "latest")));
assertEquals(2, index.getManifests().size());
// Ensure 1st descriptor has null annotations
assertNull(index.getManifests().get(0).getAnnotations());
// Ensure 2nd descriptor has ref
assertNotNull(index.getManifests().get(1).getAnnotations());
assertTrue(index.getManifests().get(1).getAnnotations().containsKey(Const.ANNOTATION_REF));
assertEquals("latest", index.getManifests().get(1).getAnnotations().get(Const.ANNOTATION_REF));
}
@Test
void shouldNotMoveRefIfDifferent() {
Index index = Index.fromManifests(List.of());
index = index.withNewManifests(
Manifest.empty().getDescriptor().withAnnotations(Map.of(Const.ANNOTATION_REF, "latest")));
assertEquals(1, index.getManifests().size());
index = index.withNewManifests(ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, "sha256:123", 123)
.withAnnotations(Map.of(Const.ANNOTATION_REF, "stable")));
assertEquals(2, index.getManifests().size());
// No change
assertEquals("latest", index.getManifests().get(0).getAnnotations().get(Const.ANNOTATION_REF));
// Added ref
assertEquals("stable", index.getManifests().get(1).getAnnotations().get(Const.ANNOTATION_REF));
}
@Test
void shouldKeepExistingAnnotation() {
Index index = Index.fromManifests(List.of());
index = index.withNewManifests(
Manifest.empty().getDescriptor().withAnnotations(Map.of(Const.ANNOTATION_REF, "latest", "foo", "bar")));
assertEquals(1, index.getManifests().size());
index = index.withNewManifests(ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, "sha256:123", 123)
.withAnnotations(Map.of(Const.ANNOTATION_REF, "latest")));
assertEquals(2, index.getManifests().size());
// One annotation
assertEquals(1, index.getManifests().get(0).getAnnotations().size());
assertEquals("bar", index.getManifests().get(0).getAnnotations().get("foo"));
// Added ref
assertEquals("latest", index.getManifests().get(1).getAnnotations().get(Const.ANNOTATION_REF));
// Add one more
index = index.withNewManifests(ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, "sha256:532", 123)
.withAnnotations(Map.of("test", "hello")));
assertEquals(3, index.getManifests().size());
assertEquals(1, index.getManifests().get(0).getAnnotations().size());
assertEquals("bar", index.getManifests().get(0).getAnnotations().get("foo"));
assertEquals(1, index.getManifests().get(1).getAnnotations().size());
assertEquals("latest", index.getManifests().get(1).getAnnotations().get(Const.ANNOTATION_REF));
assertEquals(1, index.getManifests().get(2).getAnnotations().size());
assertEquals("hello", index.getManifests().get(2).getAnnotations().get("test"));
// With null annotations
index = index.withNewManifests(ManifestDescriptor.of(Const.DEFAULT_MANIFEST_MEDIA_TYPE, "sha256:789", 123)
.withAnnotations(null));
assertEquals(4, index.getManifests().size());
assertEquals(1, index.getManifests().get(0).getAnnotations().size());
assertEquals("bar", index.getManifests().get(0).getAnnotations().get("foo"));
assertEquals(1, index.getManifests().get(1).getAnnotations().size());
assertEquals("latest", index.getManifests().get(1).getAnnotations().get(Const.ANNOTATION_REF));
assertEquals(1, index.getManifests().get(2).getAnnotations().size());
assertEquals("hello", index.getManifests().get(2).getAnnotations().get("test"));
assertNull(index.getManifests().get(3).getAnnotations());
}
@Test
void testEquals() {
String json1 =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}";
String json2 =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}";
// Data
Index object1 = Index.fromJson(json1);
Index object2 = Index.fromJson(json2);
assertEquals(object1, object2);
assertEquals(object1.hashCode(), object2.hashCode());
}
@Test
void testToString() {
String json =
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}";
Index index = Index.fromJson(json);
String json1 = index.toString();
assertNotNull(json);
assertEquals(
"{\"schemaVersion\":2,\"manifests\":[{\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"digest\":\"sha256:f381775b1f558b02165b5dfe1b2f973387d995e18302c4039daabd32f938cb27\",\"size\":559}]}",
json1);
}
}