Skip to content

Commit eb6e1e8

Browse files
authored
Add NetworkConnectivity and CorruptedResponses Tests using Wiremock (#295)
2 parents 24901a0 + 00aae58 commit eb6e1e8

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/test/java/land/oras/RegistryWireMockTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package land.oras;
2222

2323
import static com.github.tomakehurst.wiremock.client.WireMock.*;
24+
import static org.junit.Assert.assertTrue;
2425
import static org.junit.jupiter.api.Assertions.assertEquals;
2526
import static org.junit.jupiter.api.Assertions.assertNotNull;
2627
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -588,4 +589,50 @@ void shouldHandleConcurrentBlobPushes(WireMockRuntimeInfo wmRuntimeInfo) throws
588589
boolean completed = executor.awaitTermination(10, TimeUnit.SECONDS);
589590
assertEquals(true, completed, "Concurrent blob pushes did not complete within timeout");
590591
}
592+
593+
@Test
594+
void shouldHandleNetworkConnectivityLoss(WireMockRuntimeInfo wmRuntimeInfo) {
595+
WireMock wireMock = wmRuntimeInfo.getWireMock();
596+
String registryUrl = wmRuntimeInfo.getHttpBaseUrl().replace("http://", "");
597+
598+
// Setup WireMock to simulate a connection reset
599+
wireMock.register(get(urlEqualTo("/v2/library/network-loss/tags/list"))
600+
.willReturn(aResponse().withStatus(503).withBody("Service Unavailable")));
601+
602+
Registry registry = Registry.Builder.builder()
603+
.withAuthProvider(authProvider)
604+
.withInsecure(true)
605+
.build();
606+
607+
ContainerRef ref = ContainerRef.parse("%s/library/network-loss".formatted(registryUrl));
608+
609+
// Verify that a network connectivity loss results in an OrasException
610+
OrasException exception = assertThrows(OrasException.class, () -> registry.getTags(ref));
611+
assertEquals("Response code: 503", exception.getMessage());
612+
}
613+
614+
@Test
615+
void shouldHandleCorruptedResponse(WireMockRuntimeInfo wmRuntimeInfo) {
616+
WireMock wireMock = wmRuntimeInfo.getWireMock();
617+
String registryUrl = wmRuntimeInfo.getHttpBaseUrl().replace("http://", "");
618+
String digest = SupportedAlgorithm.SHA256.digest("blob-data".getBytes());
619+
620+
// Setup WireMock to return a corrupted blob response
621+
wireMock.register(head(urlEqualTo("/v2/library/corrupted-blob/blobs/%s".formatted(digest)))
622+
.willReturn(aResponse().withStatus(200)));
623+
wireMock.register(get(urlEqualTo("/v2/library/corrupted-blob/blobs/%s".formatted(digest)))
624+
.willReturn(aResponse().withStatus(200).withBody("corrupted-data")));
625+
626+
Registry registry = Registry.Builder.builder()
627+
.withAuthProvider(authProvider)
628+
.withInsecure(true)
629+
.build();
630+
631+
ContainerRef containerRef = ContainerRef.parse("%s/library/corrupted-blob".formatted(registryUrl));
632+
633+
// Expect digest mismatch exception
634+
OrasException exception =
635+
assertThrows(OrasException.class, () -> registry.getBlob(containerRef.withDigest(digest)));
636+
assertTrue(exception.getMessage().startsWith("Digest mismatch"));
637+
}
591638
}

0 commit comments

Comments
 (0)