|
| 1 | +/* |
| 2 | + * Copyright 2025 OpsMx, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client; |
| 18 | + |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; |
| 21 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertIterableEquals; |
| 24 | +import static org.mockito.ArgumentMatchers.anyString; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 27 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 29 | +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; |
| 30 | +import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerToken; |
| 31 | +import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerTokenService; |
| 32 | +import com.netflix.spinnaker.config.DefaultServiceClientProvider; |
| 33 | +import com.netflix.spinnaker.config.okhttp3.DefaultOkHttpClientBuilderProvider; |
| 34 | +import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider; |
| 35 | +import com.netflix.spinnaker.kork.client.ServiceClientProvider; |
| 36 | +import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory; |
| 37 | +import com.netflix.spinnaker.kork.retrofit.Retrofit2ServiceFactory; |
| 38 | +import com.netflix.spinnaker.kork.retrofit.Retrofit2ServiceFactoryAutoConfiguration; |
| 39 | +import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.Map; |
| 42 | +import okhttp3.OkHttpClient; |
| 43 | +import org.junit.jupiter.api.BeforeEach; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 46 | +import org.mockito.Mockito; |
| 47 | +import org.springframework.beans.factory.annotation.Autowired; |
| 48 | +import org.springframework.boot.test.context.SpringBootTest; |
| 49 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 50 | +import org.springframework.http.HttpStatus; |
| 51 | +import retrofit2.Retrofit; |
| 52 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 53 | + |
| 54 | +@SpringBootTest( |
| 55 | + classes = { |
| 56 | + OkHttpClientConfigurationProperties.class, |
| 57 | + Retrofit2ServiceFactory.class, |
| 58 | + ServiceClientProvider.class, |
| 59 | + OkHttpClientProvider.class, |
| 60 | + OkHttpClient.class, |
| 61 | + DefaultServiceClientProvider.class, |
| 62 | + DefaultOkHttpClientBuilderProvider.class, |
| 63 | + Retrofit2ServiceFactoryAutoConfiguration.class, |
| 64 | + ObjectMapper.class |
| 65 | + }, |
| 66 | + webEnvironment = SpringBootTest.WebEnvironment.NONE) |
| 67 | +public class DockerRegistryClientTest { |
| 68 | + |
| 69 | + @RegisterExtension |
| 70 | + static WireMockExtension wmDockerRegistry = |
| 71 | + WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build(); |
| 72 | + |
| 73 | + static DockerRegistryClient.DockerRegistryService dockerRegistryService; |
| 74 | + @MockBean DockerBearerTokenService dockerBearerTokenService; |
| 75 | + static DockerRegistryClient dockerRegistryClient; |
| 76 | + @Autowired ServiceClientProvider serviceClientProvider; |
| 77 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 78 | + Map<String, Object> tagsResponse; |
| 79 | + String tagsResponseString; |
| 80 | + String tagsSecondResponseString; |
| 81 | + String tagsThirdResponseString; |
| 82 | + Map<String, Object> catalogResponse; |
| 83 | + String catalogResponseString; |
| 84 | + String catalogSecondResponseString; |
| 85 | + String catalogThirdResponseString; |
| 86 | + |
| 87 | + @BeforeEach |
| 88 | + public void init() throws JsonProcessingException { |
| 89 | + tagsResponse = |
| 90 | + Map.of( |
| 91 | + "name", |
| 92 | + "library/nginx", |
| 93 | + "tags", |
| 94 | + new String[] {"1", "1-alpine", "1-alpine-otel", "1-alpine-perl", "1-alpine-slim"}); |
| 95 | + catalogResponse = |
| 96 | + Map.of( |
| 97 | + "repositories", |
| 98 | + new String[] { |
| 99 | + "library/repo-a-1", |
| 100 | + "library/repo-b-1", |
| 101 | + "library/repo-c-1", |
| 102 | + "library/repo-d-1", |
| 103 | + "library/repo-e-1" |
| 104 | + }); |
| 105 | + tagsResponseString = objectMapper.writeValueAsString(tagsResponse); |
| 106 | + tagsSecondResponseString = tagsResponseString.replaceAll("1", "2"); |
| 107 | + tagsThirdResponseString = tagsResponseString.replaceAll("1", "3"); |
| 108 | + |
| 109 | + catalogResponseString = objectMapper.writeValueAsString(catalogResponse); |
| 110 | + catalogSecondResponseString = catalogResponseString.replaceAll("1", "2"); |
| 111 | + catalogThirdResponseString = catalogResponseString.replaceAll("1", "3"); |
| 112 | + |
| 113 | + DockerBearerToken bearerToken = new DockerBearerToken(); |
| 114 | + bearerToken.setToken("someToken"); |
| 115 | + bearerToken.setAccess_token("someToken"); |
| 116 | + Mockito.when(dockerBearerTokenService.getToken(anyString())).thenReturn(bearerToken); |
| 117 | + dockerRegistryService = |
| 118 | + buildService(DockerRegistryClient.DockerRegistryService.class, wmDockerRegistry.baseUrl()); |
| 119 | + dockerRegistryClient = |
| 120 | + new DockerRegistryClient( |
| 121 | + wmDockerRegistry.baseUrl(), 5, "", "", dockerRegistryService, dockerBearerTokenService); |
| 122 | + } |
| 123 | + |
| 124 | + private static <T> T buildService(Class<T> type, String baseUrl) { |
| 125 | + return new Retrofit.Builder() |
| 126 | + .baseUrl(baseUrl) |
| 127 | + .client(new OkHttpClient()) |
| 128 | + .addCallAdapterFactory(ErrorHandlingExecutorCallAdapterFactory.getInstance()) |
| 129 | + .addConverterFactory(JacksonConverterFactory.create()) |
| 130 | + .build() |
| 131 | + .create(type); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void getTagsWithoutNextLink() { |
| 136 | + wmDockerRegistry.stubFor( |
| 137 | + WireMock.get(urlMatching("/v2/library/nginx/tags/list")) |
| 138 | + .willReturn( |
| 139 | + aResponse().withStatus(HttpStatus.OK.value()).withBody(tagsResponseString))); |
| 140 | + |
| 141 | + DockerRegistryTags dockerRegistryTags = dockerRegistryClient.getTags("library/nginx"); |
| 142 | + String[] tags = (String[]) tagsResponse.get("tags"); |
| 143 | + assertIterableEquals(Arrays.asList(tags), dockerRegistryTags.getTags()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void getTagsWithNextLink() { |
| 148 | + wmDockerRegistry.stubFor( |
| 149 | + WireMock.get(urlMatching("/v2/library/nginx/tags/list")) |
| 150 | + .willReturn( |
| 151 | + aResponse() |
| 152 | + .withStatus(HttpStatus.OK.value()) |
| 153 | + .withHeader( |
| 154 | + "link", |
| 155 | + "</v2/library/nginx/tags/list?last=1-alpine-slim&n=5>; rel=\"next\"") |
| 156 | + .withBody(tagsResponseString))); |
| 157 | + wmDockerRegistry.stubFor( |
| 158 | + WireMock.get(urlMatching("/v2/library/nginx/tags/list\\?last=1-alpine-slim&n=5")) |
| 159 | + .willReturn( |
| 160 | + aResponse() |
| 161 | + .withStatus(HttpStatus.OK.value()) |
| 162 | + .withHeader( |
| 163 | + "link", |
| 164 | + // to test the logic when `?` is not present in the link header |
| 165 | + "</v2/library/nginx/tags/list1>; rel=\"next\"") |
| 166 | + .withBody(tagsSecondResponseString))); |
| 167 | + wmDockerRegistry.stubFor( |
| 168 | + WireMock.get(urlMatching("/v2/library/nginx/tags/list1")) |
| 169 | + .willReturn( |
| 170 | + aResponse().withStatus(HttpStatus.OK.value()).withBody(tagsThirdResponseString))); |
| 171 | + |
| 172 | + DockerRegistryTags dockerRegistryTags = dockerRegistryClient.getTags("library/nginx"); |
| 173 | + assertEquals(15, dockerRegistryTags.getTags().size()); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void getCatalogWithNextLink() { |
| 178 | + wmDockerRegistry.stubFor( |
| 179 | + WireMock.get(urlMatching("/v2/_catalog\\?n=5")) |
| 180 | + .willReturn( |
| 181 | + aResponse() |
| 182 | + .withStatus(HttpStatus.OK.value()) |
| 183 | + .withHeader("link", "</v2/_catalog?last=repo1&n=5>; rel=\"next\"") |
| 184 | + .withBody(catalogResponseString))); |
| 185 | + wmDockerRegistry.stubFor( |
| 186 | + WireMock.get(urlMatching("/v2/_catalog\\?last=repo1&n=5")) |
| 187 | + .willReturn( |
| 188 | + aResponse() |
| 189 | + .withStatus(HttpStatus.OK.value()) |
| 190 | + .withHeader( |
| 191 | + "link", |
| 192 | + // to test the logic when `?` is not present in the link header |
| 193 | + "</v2/_catalog1>; rel=\"next\"") |
| 194 | + .withBody(catalogSecondResponseString))); |
| 195 | + wmDockerRegistry.stubFor( |
| 196 | + WireMock.get(urlMatching("/v2/_catalog1\\?n=5")) |
| 197 | + .willReturn( |
| 198 | + aResponse() |
| 199 | + .withStatus(HttpStatus.OK.value()) |
| 200 | + .withBody(catalogThirdResponseString))); |
| 201 | + |
| 202 | + DockerRegistryCatalog dockerRegistryCatalog = dockerRegistryClient.getCatalog(); |
| 203 | + assertEquals(15, dockerRegistryCatalog.getRepositories().size()); |
| 204 | + } |
| 205 | +} |
0 commit comments