forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistryTest.java
More file actions
240 lines (197 loc) · 9.45 KB
/
RegistryTest.java
File metadata and controls
240 lines (197 loc) · 9.45 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
package land.oras;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import land.oras.utils.Const;
import land.oras.utils.JsonUtils;
import land.oras.utils.RegistryContainer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@WireMockTest
public class RegistryTest {
private static final Logger LOG = LoggerFactory.getLogger(RegistryTest.class);
@Container
private final RegistryContainer registry = new RegistryContainer().withStartupAttempts(3);
/**
* Blob temporary dir
*/
@TempDir
private Path blobDir;
@TempDir
private Path artifactDir;
@BeforeEach
void before() {
registry.withFollowOutput();
}
@Test
void shouldListTags(WireMockRuntimeInfo wmRuntimeInfo) {
// Return data from wiremock
WireMock wireMock = wmRuntimeInfo.getWireMock();
wireMock.register(WireMock.get(WireMock.urlEqualTo("/v2/library/artifact-text/tags/list"))
.willReturn(WireMock.okJson(JsonUtils.toJson(new Tags("artifact-text", List.of("latest", "0.1.1"))))));
// Insecure registry
Registry registry = Registry.Builder.builder().withInsecure(true).build();
// Test
List<String> tags = registry.getTags(ContainerRef.parse("%s/library/artifact-text"
.formatted(wmRuntimeInfo.getHttpBaseUrl().replace("http://", ""))));
// Assert
assertEquals(2, tags.size());
assertEquals("latest", tags.get(0));
assertEquals("0.1.1", tags.get(1));
}
@Test
void shouldPushAndGetBlobThenDelete() {
Registry registry = Registry.Builder.builder()
.withInsecure(true)
.withSkipTlsVerify(true)
.build();
ContainerRef containerRef =
ContainerRef.parse("%s/library/artifact-text".formatted(this.registry.getRegistry()));
Layer layer = registry.pushBlob(containerRef, "hello".getBytes());
assertEquals("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", layer.getDigest());
byte[] blob = registry.getBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
assertEquals("hello", new String(blob));
registry.pushBlob(containerRef, "hello".getBytes());
registry.deleteBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
// Ensure the blob is deleted
assertThrows(OrasException.class, () -> {
registry.getBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
});
}
@Test
void shouldUploadAndFetchBlobThenDelete() throws IOException {
Registry registry = Registry.Builder.builder()
.withInsecure(true)
.withSkipTlsVerify(true)
.build();
ContainerRef containerRef =
ContainerRef.parse("%s/library/artifact-text".formatted(this.registry.getRegistry()));
Files.createFile(blobDir.resolve("temp.txt"));
Files.writeString(blobDir.resolve("temp.txt"), "hello");
Layer layer = registry.uploadBlob(containerRef, blobDir.resolve("temp.txt"));
assertEquals("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", layer.getDigest());
registry.fetchBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"),
blobDir.resolve("temp.txt"));
try (InputStream is = registry.fetchBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"))) {
assertEquals("hello", new String(is.readAllBytes()));
}
assertEquals("hello", Files.readString(blobDir.resolve("temp.txt")));
registry.deleteBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
// Ensure the blob is deleted
assertThrows(OrasException.class, () -> {
registry.getBlob(
containerRef.withDigest("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
});
}
@Test
void shouldPushAndGetManifestThenDelete() {
Registry registry = Registry.Builder.builder()
.withInsecure(true)
.withSkipTlsVerify(true)
.build();
// Empty manifest
ContainerRef containerRef =
ContainerRef.parse("%s/library/empty-manifest".formatted(this.registry.getRegistry()));
Layer emptyLayer = registry.pushBlob(containerRef, Layer.empty().getDataBytes());
Manifest emptyManifest = Manifest.empty().withLayers(List.of(Layer.fromDigest(emptyLayer.getDigest(), 2)));
String location = registry.pushManifest(containerRef, emptyManifest);
assertEquals(
"http://%s/v2/library/empty-manifest/manifests/sha256:f570eb29564f04e73d15cc2a2bb4153d488b9e8428c7f5108b895baa379750bd"
.formatted(this.registry.getRegistry()),
location);
Manifest manifest = registry.getManifest(containerRef);
// Assert
assertEquals(2, manifest.getSchemaVersion());
assertEquals(Const.DEFAULT_MANIFEST_MEDIA_TYPE, manifest.getMediaType());
assertEquals(Config.empty().getDigest(), manifest.getConfig().getDigest());
assertEquals(1, manifest.getLayers().size()); // One empty layer
Layer layer = manifest.getLayers().get(0);
// An empty layer
assertEquals(2, layer.getSize());
assertEquals(Const.DEFAULT_EMPTY_MEDIA_TYPE, layer.getMediaType());
assertNull(manifest.getArtifactType());
assertTrue(manifest.getAnnotations().isEmpty());
// Push again
registry.pushManifest(containerRef, manifest);
// Delete manifest
registry.deleteManifest(containerRef);
// Ensure the blob is deleted
assertThrows(OrasException.class, () -> {
registry.getManifest(containerRef);
});
}
@Test
void testShouldPushAndPullMinimalArtifact() throws IOException {
Registry registry = Registry.Builder.builder()
.withInsecure(true)
.withSkipTlsVerify(true)
.build();
ContainerRef containerRef =
ContainerRef.parse("%s/library/artifact-full".formatted(this.registry.getRegistry()));
Path file1 = blobDir.resolve("file1.txt");
Files.writeString(file1, "foobar");
// Upload
Manifest manifest = registry.pushArtifact(containerRef, file1);
assertEquals(1, manifest.getLayers().size());
Layer layer = manifest.getLayers().get(0);
// A test file layer
assertEquals(6, layer.getSize());
assertEquals("text/plain", layer.getMediaType());
assertEquals("sha256:c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", layer.getDigest());
Map<String, String> annotations = layer.getAnnotations();
// Assert annotations of the layer
assertEquals(1, annotations.size());
assertEquals("file1.txt", annotations.get(Const.ANNOTATION_TITLE));
// Pull
registry.pullArtifact(containerRef, artifactDir, true);
assertEquals("foobar", Files.readString(artifactDir.resolve("file1.txt")));
}
@Test
void testShouldPushCompressedDirectory() throws IOException {
Registry registry = Registry.Builder.builder()
.withInsecure(true)
.withSkipTlsVerify(true)
.build();
ContainerRef containerRef =
ContainerRef.parse("%s/library/artifact-full".formatted(this.registry.getRegistry()));
Path file1 = blobDir.resolve("file1.txt");
Path file2 = blobDir.resolve("file2.txt");
Path file3 = blobDir.resolve("file3.txt");
Files.writeString(file1, "foobar");
Files.writeString(file2, "test1234");
Files.writeString(file3, "barfoo");
// Upload blob dir
Manifest manifest = registry.pushArtifact(containerRef, blobDir);
assertEquals(1, manifest.getLayers().size());
Layer layer = manifest.getLayers().get(0);
// A compressed directory file
assertEquals(Const.DEFAULT_BLOB_DIR_MEDIA_TYPE, layer.getMediaType());
Map<String, String> annotations = layer.getAnnotations();
// Assert annotations of the layer
assertEquals(2, annotations.size());
assertEquals(blobDir.getFileName().toString(), annotations.get(Const.ANNOTATION_TITLE));
assertEquals("true", annotations.get(Const.ANNOTATION_ORAS_UNPACK));
}
}