Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09b0555

Browse files
committed
Add support for integrationt tests and fix some issue with docker.io and GHCR
1 parent 1d5f85f commit 09b0555

File tree

6 files changed

+141
-14
lines changed

6 files changed

+141
-14
lines changed

pom.xml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<spotless.check.skip>false</spotless.check.skip>
5252
<enforcer.skip>false</enforcer.skip>
5353

54+
<!-- Skip unit tests -->
55+
<skipUnitTests>@{skipTests}</skipUnitTests>
56+
5457
<!-- Version -->
5558
<slf4j.version>2.0.17</slf4j.version>
5659
<jspecify.version>1.0.0</jspecify.version>
@@ -74,6 +77,7 @@
7477
<maven-clean-plugin.version>3.4.1</maven-clean-plugin.version>
7578
<maven-install-plugin.version>3.1.4</maven-install-plugin.version>
7679
<maven-surefire-plugin.version>3.5.3</maven-surefire-plugin.version>
80+
<maven-failsafe-plugin.version>3.5.3</maven-failsafe-plugin.version>
7781
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
7882
<maven-enforcer-plugin.version>3.5.0</maven-enforcer-plugin.version>
7983
<maven-jar-plugin.version>3.4.2</maven-jar-plugin.version>
@@ -339,6 +343,21 @@
339343
<groupId>org.apache.maven.plugins</groupId>
340344
<artifactId>maven-surefire-plugin</artifactId>
341345
<version>${maven-surefire-plugin.version}</version>
346+
<configuration>
347+
<skip>${skipUnitTests}</skip>
348+
</configuration>
349+
</plugin>
350+
<plugin>
351+
<groupId>org.apache.maven.plugins</groupId>
352+
<artifactId>maven-failsafe-plugin</artifactId>
353+
<version>${maven-failsafe-plugin.version}</version>
354+
<executions>
355+
<execution>
356+
<goals>
357+
<goal>integration-test</goal>
358+
</goals>
359+
</execution>
360+
</executions>
342361
</plugin>
343362
<plugin>
344363
<groupId>org.apache.maven.plugins</groupId>
@@ -505,6 +524,14 @@
505524
<groupId>org.jacoco</groupId>
506525
<artifactId>jacoco-maven-plugin</artifactId>
507526
</plugin>
527+
<plugin>
528+
<groupId>org.apache.maven.plugins</groupId>
529+
<artifactId>maven-surefire-plugin</artifactId>
530+
</plugin>
531+
<plugin>
532+
<groupId>org.apache.maven.plugins</groupId>
533+
<artifactId>maven-failsafe-plugin</artifactId>
534+
</plugin>
508535
<plugin>
509536
<groupId>org.codehaus.mojo</groupId>
510537
<artifactId>license-maven-plugin</artifactId>
@@ -537,7 +564,8 @@
537564
<profile>
538565
<id>quick-build</id>
539566
<properties>
540-
<skipTests>true</skipTests>
567+
<skipUnitTests>true</skipUnitTests>
568+
<skipITs>true</skipITs>
541569
<enforcer.skip>true</enforcer.skip>
542570
<license.skipCheckLicense>true</license.skipCheckLicense>
543571
<spdx.skip>true</spdx.skip>
@@ -547,7 +575,8 @@
547575
<profile>
548576
<id>release</id>
549577
<properties>
550-
<skipTests>true</skipTests>
578+
<skipUnitTests>true</skipUnitTests>
579+
<skipITs>true</skipITs>
551580
</properties>
552581
<build>
553582
<plugins>

src/main/java/land/oras/ContainerRef.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ public SupportedAlgorithm getAlgorithm() {
177177
* @return The API prefix
178178
*/
179179
private String getApiPrefix(@Nullable Registry target) {
180+
String namespace = getNamespace(target);
180181
if (namespace != null) {
181-
return "%s/v2/%s/%s".formatted(getApiRegistry(target), getNamespace(target), repository);
182+
return "%s/v2/%s/%s".formatted(getApiRegistry(target), namespace, repository);
182183
}
183184
return "%s/v2/%s".formatted(getApiRegistry(target), repository);
184185
}

src/main/java/land/oras/utils/HttpClient.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,13 @@ private <T> ResponseWrapper<T> redoRequest(
442442
ResponseWrapper<String> tokenResponse = refreshToken(toResponseWrapper(response), authProvider);
443443
HttpClient.TokenResponse token =
444444
JsonUtils.fromJson(tokenResponse.response(), HttpClient.TokenResponse.class);
445-
LOG.debug(
446-
"Found token issued_at {}, expire_id {} and expiring at {} ",
447-
token.issued_at(),
448-
token.expires_in(),
449-
token.issued_at().plusSeconds(token.expires_in()));
445+
if (token.issued_at() != null && token.expires_in() != null) {
446+
LOG.debug(
447+
"Found token issued_at {}, expire_id {} and expiring at {} ",
448+
token.issued_at(),
449+
token.expires_in(),
450+
token.issued_at().plusSeconds(token.expires_in()));
451+
}
450452
try {
451453
builder = builder.header(Const.AUTHORIZATION_HEADER, "Bearer " + token.token());
452454
return toResponseWrapper(client.send(builder.build(), handler));
@@ -532,7 +534,11 @@ public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngi
532534
* @param expires_in The expires in
533535
* @param issued_at The issued at
534536
*/
535-
public record TokenResponse(String token, String access_token, Integer expires_in, ZonedDateTime issued_at) {}
537+
public record TokenResponse(
538+
String token,
539+
@Nullable String access_token,
540+
@Nullable Integer expires_in,
541+
@Nullable ZonedDateTime issued_at) {}
536542

537543
/**
538544
* Builder for the HTTP client

src/test/java/land/oras/ContainerRefTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@ void shouldParseImageWithNoNamespace() {
8787
assertEquals("library", containerRef.getNamespace());
8888
assertEquals("library", containerRef.getNamespace(Registry.builder().build()));
8989
assertEquals("alpine", containerRef.getRepository());
90+
assertEquals(
91+
"registry-1.docker.io/v2/library/alpine/manifests/sha256:1234567890abcdef",
92+
containerRef.getManifestsPath());
9093
assertEquals("latest", containerRef.getTag());
9194
assertEquals(
92-
"registry-1.docker.io/v2/alpine/referrers/sha256:1234567890abcdef?artifactType=test%2Fbar",
95+
"registry-1.docker.io/v2/library/alpine/referrers/sha256:1234567890abcdef?artifactType=test%2Fbar",
9396
containerRef.getReferrersPath(ArtifactType.from("test/bar")));
9497
assertEquals("sha256:1234567890abcdef", containerRef.getDigest());
9598
assertEquals(
96-
"registry-1.docker.io/v2/alpine/manifests/sha256:1234567890abcdef", containerRef.getManifestsPath());
99+
"registry-1.docker.io/v2/library/alpine/manifests/sha256:1234567890abcdef",
100+
containerRef.getManifestsPath());
97101
containerRef = ContainerRef.parse("demo.goharbor.com/alpine:latest@sha256:1234567890abcdef");
98102
assertEquals("demo.goharbor.com", containerRef.getRegistry());
99103
assertEquals("demo.goharbor.com/v2/alpine/tags/list", containerRef.getTagsPath());
@@ -107,12 +111,14 @@ void shouldParseImageWithNoNamespace() {
107111
void shouldParseImageWithNoTag() {
108112
ContainerRef containerRef = ContainerRef.parse("docker.io/alpine@sha256:1234567890abcdef");
109113
assertEquals("docker.io", containerRef.getRegistry());
110-
assertEquals("registry-1.docker.io/v2/alpine/tags/list", containerRef.getTagsPath());
114+
assertEquals("registry-1.docker.io/v2/library/alpine/tags/list", containerRef.getTagsPath());
111115
assertEquals("library", containerRef.getNamespace());
112116
assertEquals("library", containerRef.getNamespace(Registry.builder().build()));
113117
assertEquals("alpine", containerRef.getRepository());
114118
assertEquals("latest", containerRef.getTag());
115-
assertEquals("registry-1.docker.io/v2/alpine/blobs/sha256:1234567890abcdef", containerRef.getBlobsPath(null));
119+
assertEquals(
120+
"registry-1.docker.io/v2/library/alpine/blobs/sha256:1234567890abcdef",
121+
containerRef.getBlobsPath(null));
116122
assertEquals(
117123
"foo.io/v2/alpine/blobs/sha256:1234567890abcdef",
118124
containerRef.getBlobsPath(
@@ -153,7 +159,7 @@ void shouldParseImageWithNoRegistry() {
153159
"foo.io",
154160
containerRef.getApiRegistry(
155161
Registry.builder().withRegistry("foo.io").build()));
156-
assertEquals("registry-1.docker.io/v2/alpine/tags/list", containerRef.getTagsPath());
162+
assertEquals("registry-1.docker.io/v2/library/alpine/tags/list", containerRef.getTagsPath());
157163
assertEquals("library", containerRef.getNamespace());
158164
assertEquals("alpine", containerRef.getRepository());
159165
assertEquals("latest", containerRef.getTag());
@@ -175,6 +181,7 @@ void shouldParseImageWithNoTagAndNoRegistry() {
175181
Registry.builder().withRegistry("test").build()),
176182
"Default library must be null when changing registry");
177183
assertEquals("alpine", containerRef.getRepository());
184+
assertEquals("library", containerRef.getNamespace());
178185
assertEquals("latest", containerRef.getTag());
179186
assertNull(containerRef.getDigest());
180187

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*-
2+
* =LICENSE=
3+
* ORAS Java SDK
4+
* ===
5+
* Copyright (C) 2024 - 2025 ORAS
6+
* ===
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =LICENSEEND=
19+
*/
20+
21+
package land.oras;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
public class DockerIoITCase {
28+
29+
@Test
30+
void shouldPullAnonymousIndex() {
31+
32+
// FQDN
33+
Registry registry = Registry.builder().build();
34+
// ContainerRef containerRef1 = ContainerRef.parse("docker.io/library/alpine");
35+
// Index index = registry.getIndex(containerRef1);
36+
// assertNotNull(index);
37+
//
38+
// // Default registry
39+
// ContainerRef containerRef2 = ContainerRef.parse("library/alpine");
40+
// Index index2 = registry.getIndex(containerRef2);
41+
// assertNotNull(index2);
42+
43+
// Simple name
44+
ContainerRef containerRef3 = ContainerRef.parse("alpine");
45+
Index index3 = registry.getIndex(containerRef3);
46+
assertNotNull(index3);
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*-
2+
* =LICENSE=
3+
* ORAS Java SDK
4+
* ===
5+
* Copyright (C) 2024 - 2025 ORAS
6+
* ===
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =LICENSEEND=
19+
*/
20+
21+
package land.oras;
22+
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
public class GitHubContainerRegistryITCase {
28+
29+
@Test
30+
void shouldPullIndex() {
31+
Registry registry = Registry.builder().build();
32+
ContainerRef containerRef1 = ContainerRef.parse("ghcr.io/oras-project/oras:main");
33+
Index index = registry.getIndex(containerRef1);
34+
assertNotNull(index);
35+
}
36+
}

0 commit comments

Comments
 (0)