|
18 | 18 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
|
19 | 19 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
20 | 20 | import static org.junit.Assert.assertEquals;
|
| 21 | +import static org.junit.Assert.fail; |
21 | 22 |
|
22 | 23 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
| 24 | +import io.kubernetes.client.Discovery.APIResource; |
23 | 25 | import io.kubernetes.client.openapi.ApiClient;
|
24 | 26 | import io.kubernetes.client.openapi.ApiException;
|
| 27 | +import io.kubernetes.client.openapi.models.V1APIGroup; |
| 28 | +import io.kubernetes.client.openapi.models.V1APIGroupList; |
25 | 29 | import io.kubernetes.client.openapi.models.V1APIResource;
|
26 | 30 | import io.kubernetes.client.openapi.models.V1APIResourceList;
|
27 | 31 | import io.kubernetes.client.openapi.models.V1APIVersions;
|
| 32 | +import io.kubernetes.client.openapi.models.V1GroupVersionForDiscovery; |
28 | 33 | import io.kubernetes.client.util.ClientBuilder;
|
| 34 | +import io.kubernetes.client.util.exception.IncompleteDiscoveryException; |
29 | 35 | import java.io.IOException;
|
| 36 | +import java.util.ArrayList; |
30 | 37 | import java.util.Arrays;
|
| 38 | +import java.util.List; |
31 | 39 | import java.util.Set;
|
32 | 40 | import org.junit.Before;
|
33 | 41 | import org.junit.Rule;
|
@@ -101,4 +109,143 @@ public void testGroupResourcesByName() {
|
101 | 109 | assertEquals(true, meow.getNamespaced());
|
102 | 110 | assertEquals("mouse", meow.getSubResources().get(0));
|
103 | 111 | }
|
| 112 | + |
| 113 | + @Test |
| 114 | + public void findAllShouldReturnAllApiResourceWhenAllResourceDiscoveryApiResponseAreSuccess() throws ApiException { |
| 115 | + Discovery discovery = new Discovery(apiClient); |
| 116 | + |
| 117 | + wireMockRule.stubFor( |
| 118 | + get("/api") |
| 119 | + .willReturn( |
| 120 | + aResponse() |
| 121 | + .withStatus(200) |
| 122 | + .withHeader("Content-Type", "application/json") |
| 123 | + .withBody( |
| 124 | + apiClient |
| 125 | + .getJSON() |
| 126 | + .serialize(new V1APIVersions())))); |
| 127 | + |
| 128 | + String group = "test.com"; |
| 129 | + String version = "v1"; |
| 130 | + String path="/apis/"+group+'/'+version; |
| 131 | + |
| 132 | + wireMockRule.stubFor( |
| 133 | + get("/apis") |
| 134 | + .willReturn( |
| 135 | + aResponse() |
| 136 | + .withStatus(200) |
| 137 | + .withHeader("Content-Type", "application/json") |
| 138 | + .withBody( |
| 139 | + apiClient |
| 140 | + .getJSON() |
| 141 | + .serialize(new V1APIGroupList() |
| 142 | + .addGroupsItem(new V1APIGroup() |
| 143 | + .name(group) |
| 144 | + .preferredVersion(new V1GroupVersionForDiscovery().version(version)) |
| 145 | + .versions(Arrays.asList( |
| 146 | + new V1GroupVersionForDiscovery().version(version)))))))); |
| 147 | + |
| 148 | + wireMockRule.stubFor( |
| 149 | + get(urlPathEqualTo(path)) |
| 150 | + .willReturn( |
| 151 | + aResponse() |
| 152 | + .withStatus(200) |
| 153 | + .withHeader("Content-Type", "application/json") |
| 154 | + .withBody( |
| 155 | + apiClient |
| 156 | + .getJSON() |
| 157 | + .serialize(new V1APIResourceList() |
| 158 | + .resources( |
| 159 | + Arrays.asList( |
| 160 | + new V1APIResource() |
| 161 | + .name("first"), |
| 162 | + new V1APIResource() |
| 163 | + .name("second"))))))); |
| 164 | + |
| 165 | + List<String> versions = new ArrayList<>(); |
| 166 | + versions.add(version); |
| 167 | + Set<APIResource> apiResources = discovery.findAll(); |
| 168 | + wireMockRule.verify(1, getRequestedFor(urlPathEqualTo(path))); |
| 169 | + assertEquals(2, apiResources.size()); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void findAllShouldThrowImcompleteDiscoveryExceptionWhenAtLeastOneResourceDiscoveryApiResponseIsNotSuccess() throws ApiException { |
| 174 | + Discovery discovery = new Discovery(apiClient); |
| 175 | + |
| 176 | + wireMockRule.stubFor( |
| 177 | + get("/api") |
| 178 | + .willReturn( |
| 179 | + aResponse() |
| 180 | + .withStatus(200) |
| 181 | + .withHeader("Content-Type", "application/json") |
| 182 | + .withBody( |
| 183 | + apiClient |
| 184 | + .getJSON() |
| 185 | + .serialize(new V1APIVersions())))); |
| 186 | + |
| 187 | + String groupSuccess = "test.com"; |
| 188 | + String version = "v1"; |
| 189 | + String pathSuccess="/apis/"+groupSuccess+'/'+version; |
| 190 | + |
| 191 | + String groupError = "testError.com"; |
| 192 | + String pathError="/apis/"+groupError+'/'+version; |
| 193 | + |
| 194 | + wireMockRule.stubFor( |
| 195 | + get("/apis") |
| 196 | + .willReturn( |
| 197 | + aResponse() |
| 198 | + .withStatus(200) |
| 199 | + .withHeader("Content-Type", "application/json") |
| 200 | + .withBody( |
| 201 | + apiClient |
| 202 | + .getJSON() |
| 203 | + .serialize(new V1APIGroupList() |
| 204 | + .addGroupsItem(new V1APIGroup() |
| 205 | + .name(groupError) |
| 206 | + .preferredVersion(new V1GroupVersionForDiscovery().version(version)) |
| 207 | + .versions(Arrays.asList( |
| 208 | + new V1GroupVersionForDiscovery().version(version)))) |
| 209 | + .addGroupsItem(new V1APIGroup() |
| 210 | + .name(groupSuccess) |
| 211 | + .preferredVersion(new V1GroupVersionForDiscovery().version(version)) |
| 212 | + .versions(Arrays.asList( |
| 213 | + new V1GroupVersionForDiscovery().version(version)))))))); |
| 214 | + |
| 215 | + wireMockRule.stubFor( |
| 216 | + get(urlPathEqualTo(pathError)) |
| 217 | + .willReturn( |
| 218 | + aResponse() |
| 219 | + .withStatus(503))); |
| 220 | + |
| 221 | + wireMockRule.stubFor( |
| 222 | + get(urlPathEqualTo(pathSuccess)) |
| 223 | + .willReturn( |
| 224 | + aResponse() |
| 225 | + .withStatus(200) |
| 226 | + .withHeader("Content-Type", "application/json") |
| 227 | + .withBody( |
| 228 | + apiClient |
| 229 | + .getJSON() |
| 230 | + .serialize(new V1APIResourceList() |
| 231 | + .resources( |
| 232 | + Arrays.asList( |
| 233 | + new V1APIResource() |
| 234 | + .name("first"), |
| 235 | + new V1APIResource() |
| 236 | + .name("second"))))))); |
| 237 | + |
| 238 | + List<String> versions = new ArrayList<>(); |
| 239 | + versions.add(version); |
| 240 | + Set<APIResource> apiResources = null; |
| 241 | + try{ |
| 242 | + discovery.findAll(); |
| 243 | + fail("Should have throw ImcompleteDiscoveryException"); |
| 244 | + } catch (IncompleteDiscoveryException e) { |
| 245 | + apiResources = e.getDiscoveredResources(); |
| 246 | + } |
| 247 | + wireMockRule.verify(1, getRequestedFor(urlPathEqualTo(pathError))); |
| 248 | + wireMockRule.verify(1, getRequestedFor(urlPathEqualTo(pathSuccess))); |
| 249 | + assertEquals(2, apiResources.size()); |
| 250 | + } |
104 | 251 | }
|
0 commit comments