|
60 | 60 | import org.junit.jupiter.api.io.TempDir; |
61 | 61 | import org.junit.jupiter.api.parallel.Execution; |
62 | 62 | import org.junit.jupiter.api.parallel.ExecutionMode; |
63 | | -import org.slf4j.Logger; |
64 | | -import org.slf4j.LoggerFactory; |
65 | 63 |
|
66 | 64 | @WireMockTest |
67 | 65 | @Execution(ExecutionMode.SAME_THREAD) |
68 | 66 | class RegistryWireMockTest { |
69 | 67 |
|
70 | | - private static final Logger LOG = LoggerFactory.getLogger(RegistryWireMockTest.class); |
71 | | - |
72 | 68 | private final UsernamePasswordProvider authProvider = new UsernamePasswordProvider("myuser", "mypass"); |
73 | 69 |
|
74 | 70 | @TempDir |
@@ -366,46 +362,23 @@ void shouldRetryBlobUpload(WireMockRuntimeInfo wmRuntimeInfo) throws IOException |
366 | 362 |
|
367 | 363 | @Test |
368 | 364 | void shouldGetToken(WireMockRuntimeInfo wmRuntimeInfo) { |
| 365 | + byte[] blob = tokenScenario(wmRuntimeInfo, "get-token", "token", null); |
| 366 | + assertEquals("blob-data", new String(blob)); |
| 367 | + } |
369 | 368 |
|
370 | | - String digest = SupportedAlgorithm.SHA256.digest("blob-data".getBytes()); |
371 | | - |
372 | | - // Return data from wiremock |
373 | | - WireMock wireMock = wmRuntimeInfo.getWireMock(); |
374 | | - wireMock.register(WireMock.any(WireMock.urlEqualTo("/v2/library/get-token/blobs/%s".formatted(digest))) |
375 | | - .inScenario("get token") |
376 | | - .willReturn(WireMock.unauthorized() |
377 | | - .withHeader( |
378 | | - Const.WWW_AUTHENTICATE_HEADER, |
379 | | - "Bearer realm=\"http://localhost:%d/token\",service=\"localhost\",scope=\"repository:library/get-token:pull\"" |
380 | | - .formatted(wmRuntimeInfo.getHttpPort())))); |
381 | | - |
382 | | - // Return token |
383 | | - wireMock.register( |
384 | | - WireMock.any(WireMock.urlEqualTo("/token?scope=repository:library/get-token:pull&service=localhost")) |
385 | | - .inScenario("get token") |
386 | | - .willSetStateTo("get") |
387 | | - .willReturn(WireMock.okJson(JsonUtils.toJson(new HttpClient.TokenResponse( |
388 | | - "fake-token", "access-token", 300, ZonedDateTime.now()))))); |
389 | | - |
390 | | - // On the second call we return ok |
391 | | - wireMock.register(WireMock.any(WireMock.urlEqualTo("/v2/library/get-token/blobs/%s".formatted(digest))) |
392 | | - .inScenario("get token") |
393 | | - .whenScenarioStateIs("get") |
394 | | - .willReturn( |
395 | | - WireMock.ok().withBody("blob-data").withHeader(Const.DOCKER_CONTENT_DIGEST_HEADER, digest))); |
396 | | - |
397 | | - // Insecure registry |
398 | | - Registry registry = Registry.Builder.builder() |
399 | | - .withAuthProvider(authProvider) |
400 | | - .withInsecure(true) |
401 | | - .build(); |
402 | | - |
403 | | - ContainerRef containerRef = |
404 | | - ContainerRef.parse("localhost:%d/library/get-token".formatted(wmRuntimeInfo.getHttpPort())); |
405 | | - byte[] blob = registry.getBlob(containerRef.withDigest(digest)); |
| 369 | + @Test |
| 370 | + void shouldGetAuthToken(WireMockRuntimeInfo wmRuntimeInfo) { |
| 371 | + byte[] blob = tokenScenario(wmRuntimeInfo, "get-auth-token", null, "access-token"); |
406 | 372 | assertEquals("blob-data", new String(blob)); |
407 | 373 | } |
408 | 374 |
|
| 375 | + @Test |
| 376 | + void shouldThrowIfNoTokenFound(WireMockRuntimeInfo wmRuntimeInfo) { |
| 377 | + assertThrows(OrasException.class, () -> { |
| 378 | + tokenScenario(wmRuntimeInfo, "get-auth-token", null, null); |
| 379 | + }); |
| 380 | + } |
| 381 | + |
409 | 382 | @Test |
410 | 383 | void shouldRefreshExpiredToken(WireMockRuntimeInfo wmRuntimeInfo) { |
411 | 384 |
|
@@ -810,4 +783,44 @@ void shouldHandleCorruptedResponse(WireMockRuntimeInfo wmRuntimeInfo) { |
810 | 783 | "Digest mismatch: sha256:c2752ad96ee652e4d37fd3852de632c50f193490d132f27a1794c986e1f112ef != sha256:2be4e14a6587ab9b637afb553f0654c70e80fa14bd0b8fbf9fa09079f55a2ace", |
811 | 784 | exception.getMessage()); |
812 | 785 | } |
| 786 | + |
| 787 | + private byte[] tokenScenario( |
| 788 | + WireMockRuntimeInfo wmRuntimeInfo, String registryName, String token, String accessToken) { |
| 789 | + String digest = SupportedAlgorithm.SHA256.digest("blob-data".getBytes()); |
| 790 | + |
| 791 | + // Return data from wiremock |
| 792 | + WireMock wireMock = wmRuntimeInfo.getWireMock(); |
| 793 | + wireMock.register(WireMock.any(WireMock.urlEqualTo("/v2/library/%s/blobs/%s".formatted(registryName, digest))) |
| 794 | + .inScenario(registryName) |
| 795 | + .willReturn(WireMock.unauthorized() |
| 796 | + .withHeader( |
| 797 | + Const.WWW_AUTHENTICATE_HEADER, |
| 798 | + "Bearer realm=\"http://localhost:%d/token\",service=\"localhost\",scope=\"repository:library/%s:pull\"" |
| 799 | + .formatted(wmRuntimeInfo.getHttpPort(), registryName)))); |
| 800 | + |
| 801 | + // Return token |
| 802 | + wireMock.register(WireMock.any(WireMock.urlEqualTo( |
| 803 | + "/token?scope=repository:library/%s:pull&service=localhost".formatted(registryName))) |
| 804 | + .inScenario(registryName) |
| 805 | + .willSetStateTo("get") |
| 806 | + .willReturn(WireMock.okJson( |
| 807 | + JsonUtils.toJson(new HttpClient.TokenResponse(token, accessToken, 300, ZonedDateTime.now()))))); |
| 808 | + |
| 809 | + // On the second call we return ok |
| 810 | + wireMock.register(WireMock.any(WireMock.urlEqualTo("/v2/library/%s/blobs/%s".formatted(registryName, digest))) |
| 811 | + .inScenario(registryName) |
| 812 | + .whenScenarioStateIs("get") |
| 813 | + .willReturn( |
| 814 | + WireMock.ok().withBody("blob-data").withHeader(Const.DOCKER_CONTENT_DIGEST_HEADER, digest))); |
| 815 | + |
| 816 | + // Insecure registry |
| 817 | + Registry registry = Registry.Builder.builder() |
| 818 | + .withAuthProvider(authProvider) |
| 819 | + .withInsecure(true) |
| 820 | + .build(); |
| 821 | + |
| 822 | + ContainerRef containerRef = |
| 823 | + ContainerRef.parse("localhost:%d/library/%s".formatted(wmRuntimeInfo.getHttpPort(), registryName)); |
| 824 | + return registry.getBlob(containerRef.withDigest(digest)); |
| 825 | + } |
813 | 826 | } |
0 commit comments