use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class VersionTest method testFailure.
@Test
public void testFailure() throws InterruptedException, IOException, ApiException {
wireMockRule.stubFor(get(urlPathEqualTo("/version/")).willReturn(aResponse().withStatus(401).withHeader("Content-Type", "application/json").withBody("{}")));
Version versionUtil = new Version(client);
boolean thrown = false;
try {
VersionInfo versionInfo = versionUtil.getVersion();
} catch (ApiException ex) {
assertEquals(401, ex.getCode());
thrown = true;
}
assertEquals(thrown, true);
verify(getRequestedFor(urlPathEqualTo("/version/")).withHeader("Content-Type", equalTo("application/json")).withHeader("Accept", equalTo("application/json")));
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class VersionTest method testUrl.
@Test
public void testUrl() throws InterruptedException, IOException, ApiException {
wireMockRule.stubFor(get(urlPathEqualTo("/version/")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{}")));
Version versionUtil = new Version(client);
try {
VersionInfo versionInfo = versionUtil.getVersion();
} catch (ApiException ex) {
}
verify(getRequestedFor(urlPathEqualTo("/version/")).withHeader("Content-Type", equalTo("application/json")).withHeader("Accept", equalTo("application/json")));
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class DefaultSharedIndexInformerWireMockTest method testAllNamespacedPodInformerNormalBehavior.
@Test
public void testAllNamespacedPodInformerNormalBehavior() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
String startRV = "1000";
String endRV = "1001";
V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
stubFor(get(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
Watch.Response<V1Pod> watchResponse = new Watch.Response<>(EventType.ADDED.name(), new V1Pod().metadata(new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV).labels(Collections.singletonMap("foo", "bar")).annotations(Collections.singletonMap("foo", "bar"))));
stubFor(get(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(watchResponse))));
SharedInformerFactory factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
try {
return coreV1Api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
}, V1Pod.class, V1PodList.class);
podInformer.setTransform((obj) -> {
// deepcopy
String json = new JSON().serialize(obj);
V1Pod pod = new JSON().deserialize(json, V1Pod.class);
// remove pod annotations
pod.getMetadata().setAnnotations(null);
return pod;
});
AtomicBoolean foundExistingPod = new AtomicBoolean(false);
AtomicBoolean transformed = new AtomicBoolean(false);
AtomicBoolean setTransformAfterStarted = new AtomicBoolean(false);
podInformer.addEventHandler(new ResourceEventHandler<V1Pod>() {
@Override
public void onAdd(V1Pod obj) {
if (podName.equals(obj.getMetadata().getName()) && namespace.equals(obj.getMetadata().getNamespace())) {
foundExistingPod.set(true);
}
V1ObjectMeta metadata = obj.getMetadata();
// check if the object was transformed
if (metadata.getLabels().get("foo").equals("bar") && metadata.getAnnotations() == null) {
transformed.set(true);
}
}
@Override
public void onUpdate(V1Pod oldObj, V1Pod newObj) {
}
@Override
public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
}
});
factory.startAllRegisteredInformers();
Thread.sleep(1000);
// can not set transform func if the informer has started
try {
podInformer.setTransform((obj) -> new V1Pod());
setTransformAfterStarted.set(true);
} catch (IllegalStateException e) {
}
assertTrue(foundExistingPod.get());
assertTrue(transformed.get());
assertFalse(setTransformAfterStarted.get());
assertEquals(endRV, podInformer.lastSyncResourceVersion());
verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")));
factory.stopAllRegisteredInformers();
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class DefaultSharedIndexInformerWireMockTest method testInformerReListWatchOnWatchConflict.
@Test
public void testInformerReListWatchOnWatchConflict() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
String startRV = "1000";
V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
Watch.Response<V1Pod> watchResponse = new Watch.Response<>(EventType.ERROR.name(), new V1Status().apiVersion("v1").kind("Status").code(409));
stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")).withQueryParam("resourceVersion", equalTo(startRV)).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(watchResponse))));
SharedInformerFactory factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
try {
return coreV1Api.listNamespacedPodCall(namespace, null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
}, V1Pod.class, V1PodList.class);
factory.startAllRegisteredInformers();
// Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
Thread.sleep(3000);
verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")));
verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")));
factory.stopAllRegisteredInformers();
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class Version method getVersion.
public VersionInfo getVersion() throws ApiException, IOException {
Call call = versionApi.getCodeCall(null);
Response response = null;
try {
response = call.execute();
} catch (IOException e) {
throw new ApiException(e);
}
if (!response.isSuccessful()) {
throw new ApiException(response.code(), "Version request failed: " + response.code());
}
return this.versionApi.getApiClient().getJSON().deserialize(response.body().string(), VersionInfo.class);
}
Aggregations