use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.
the class DefaultSharedIndexInformerWireMockTest method testNamespacedPodInformerNormalBehavior.
@Test
public void testNamespacedPodInformerNormalBehavior() 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/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.ADDED.name(), new V1Pod().metadata(new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV)));
stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/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.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);
AtomicBoolean foundExistingPod = 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);
}
}
@Override
public void onUpdate(V1Pod oldObj, V1Pod newObj) {
}
@Override
public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
}
});
factory.startAllRegisteredInformers();
Thread.sleep(1000);
assertEquals(true, foundExistingPod.get());
assertEquals(endRV, podInformer.lastSyncResourceVersion());
verify(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.apis.CoreV1Api in project java by kubernetes-client.
the class DefaultSharedIndexInformerWireMockTest method testInformerReListingOnListForbidden.
@Test
public void testInformerReListingOnListForbidden() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(403).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(new V1Status().apiVersion("v1").kind("Status").code(403).reason("RBAC forbidden")))));
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")));
factory.stopAllRegisteredInformers();
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.
the class PatchUtilsTest method testStrategicMergePatchPod.
@Test
public void testStrategicMergePatchPod() throws ApiException {
CoreV1Api coreV1Api = new CoreV1Api(client);
stubFor(patch(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")).withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{}")));
PatchUtils.patch(V1Pod.class, () -> coreV1Api.patchNamespacedPodCall("foo", "default", new V1Patch("[]"), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, client);
verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.
the class TokenFileAuthenticationTest method testTokenProvided.
@Test
public void testTokenProvided() throws IOException, ApiException {
stubFor(get(urlPathEqualTo("/api/v1/pods")).willReturn(okForContentType("application/json", "{}")));
CoreV1Api api = new CoreV1Api();
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
WireMock.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withHeader("Authorization", equalTo("Bearer token1")));
this.auth.setFile(SERVICEACCOUNT_TOKEN2_PATH);
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
WireMock.verify(2, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withHeader("Authorization", equalTo("Bearer token1")));
this.auth.setExpiry(Instant.now().minusSeconds(1));
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
WireMock.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withHeader("Authorization", equalTo("Bearer token2")));
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.
the class PagerTest method testPaginationForNamespaceListWithFieldSelectorFailure.
@Test
public void testPaginationForNamespaceListWithFieldSelectorFailure() throws IOException {
String status400Str = new String(Files.readAllBytes(Paths.get(LIST_STATUS_FILE_PATH)));
CoreV1Api api = new CoreV1Api(client);
stubFor(get(urlPathEqualTo("/api/v1/namespaces")).withQueryParam("fieldSelector", equalTo("metadata.name=default")).withQueryParam("limit", equalTo("1")).willReturn(aResponse().withStatus(400).withHeader("Content-Type", "application/json").withBody(status400Str)));
Pager<V1Namespace, V1NamespaceList> pager = new Pager<V1Namespace, V1NamespaceList>((Pager.PagerParams param) -> {
try {
return api.listNamespaceCall(null, null, param.getContinueToken(), "metadata.name=default", null, param.getLimit(), null, null, null, null, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}, client, 1, V1NamespaceList.class);
int count = 0;
try {
for (V1Namespace namespace : pager) {
count++;
assertEquals("default", namespace.getMetadata().getName());
}
} catch (Exception e) {
assertEquals(status400Str, e.getMessage());
}
verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces")).withQueryParam("fieldSelector", equalTo("metadata.name=default")).withQueryParam("limit", equalTo("1")));
}
Aggregations