use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.
the class DefaultSharedIndexInformerTest method testHasSynced.
@Test
@DisplayName("PodInformer's hasSynced() method should return false when it's not able to resync")
void testHasSynced() {
// Given
String startResourceVersion = "1000", endResourceVersion = "1001";
server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(startResourceVersion).endMetadata().withItems(Collections.emptyList()).build()).once();
server.expect().withPath("/api/v1/namespaces/test/pods?resourceVersion=" + startResourceVersion + "&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(WATCH_EVENT_EMIT_TIME).andEmit(new WatchEvent(new PodBuilder().withNewMetadata().withNamespace("test").withName("pod1").withResourceVersion(endResourceVersion).endMetadata().build(), "ADDED")).waitFor(OUTDATED_WATCH_EVENT_EMIT_TIME).andEmit(outdatedEvent).done().once();
// When
SharedIndexInformer<Pod> podInformer = factory.sharedIndexInformerFor(Pod.class, 2000L);
podInformer.addEventHandler(new ResourceEventHandler<Pod>() {
@Override
public void onAdd(Pod obj) {
}
@Override
public void onUpdate(Pod oldObj, Pod newObj) {
}
@Override
public void onDelete(Pod oldObj, boolean deletedFinalStateUnknown) {
}
});
factory.startAllRegisteredInformers();
await().atMost(1, TimeUnit.SECONDS).until(() -> !podInformer.hasSynced());
// Then
assertFalse(podInformer.hasSynced());
}
use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.
the class DefaultSharedIndexInformerTest method shouldDeleteIfMissingOnResync.
@Test
@DisplayName("Pod Informer should delete the entry from the index")
void shouldDeleteIfMissingOnResync() throws InterruptedException {
// Given
String startResourceVersion = "1000", midResourceVersion = "1001", mid2ResourceVersion = "1002";
server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(startResourceVersion).endMetadata().withItems(Collections.emptyList()).build()).once();
server.expect().withPath("/api/v1/pods?resourceVersion=" + startResourceVersion + "&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(WATCH_EVENT_EMIT_TIME).andEmit(new WatchEvent(new PodBuilder().withNewMetadata().withNamespace("test").withName("pod1").withResourceVersion(midResourceVersion).endMetadata().build(), "ADDED")).waitFor(OUTDATED_WATCH_EVENT_EMIT_TIME).andEmit(outdatedEvent).done().always();
server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(mid2ResourceVersion).endMetadata().withItems(Collections.emptyList()).build()).times(2);
// When
SharedIndexInformer<Pod> podInformer = factory.sharedIndexInformerFor(Pod.class, 10000L);
CountDownLatch delete = new CountDownLatch(1);
podInformer.addEventHandler(new ResourceEventHandler<Pod>() {
@Override
public void onAdd(Pod obj) {
}
@Override
public void onUpdate(Pod oldObj, Pod newObj) {
}
@Override
public void onDelete(Pod oldObj, boolean deletedFinalStateUnknown) {
if (deletedFinalStateUnknown) {
delete.countDown();
}
}
});
factory.startAllRegisteredInformers();
delete.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
// Then
assertEquals(0, delete.getCount());
assertEquals(0, podInformer.getIndexer().list().size());
}
use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.
the class DefaultSharedIndexInformerTest method testPodSetCustomResourceInformerShouldWatchInAllNamespaces.
@Test
@DisplayName("PodSet Informer should watch in all namespaces")
void testPodSetCustomResourceInformerShouldWatchInAllNamespaces() throws InterruptedException {
// Given
setupMockServerExpectations(PodSet.class, null, this::getList, r -> new WatchEvent(getPodSet("podset1", r), "ADDED"), null, null);
// When
SharedIndexInformer<PodSet> podSetSharedIndexInformer = factory.sharedIndexInformerForCustomResource(PodSet.class, 60 * WATCH_EVENT_EMIT_TIME);
CountDownLatch foundExistingPodSet = new CountDownLatch(1);
podSetSharedIndexInformer.addEventHandler(new TestResourceHandler<>(foundExistingPodSet, "podset1"));
factory.startAllRegisteredInformers();
foundExistingPodSet.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
// Namespace set in Client's Configuration from MockWebServer
assertEquals("test", client.getConfiguration().getNamespace());
assertEquals(0, foundExistingPodSet.getCount());
}
use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.
the class DefaultSharedIndexInformerTest method testWithNamespaceInformer.
@Test
@DisplayName("Should create Informer for Namespace resource")
void testWithNamespaceInformer() throws InterruptedException {
// Given
setupMockServerExpectations(Namespace.class, null, this::getList, r -> new WatchEvent(new NamespaceBuilder().withNewMetadata().withName("ns1").withResourceVersion(r).endMetadata().build(), "ADDED"), null, null);
// When
SharedIndexInformer<Namespace> namespaceSharedIndexInformer = factory.sharedIndexInformerFor(Namespace.class, RESYNC_PERIOD);
CountDownLatch foundExistingNamespace = new CountDownLatch(1);
namespaceSharedIndexInformer.addEventHandler(new TestResourceHandler<>(foundExistingNamespace, "ns1"));
factory.startAllRegisteredInformers();
foundExistingNamespace.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
// Then
assertEquals(0, foundExistingNamespace.getCount());
}
use of io.fabric8.kubernetes.api.model.WatchEvent in project kubernetes-client by fabric8io.
the class DefaultSharedIndexInformerTest method testWithClusterBindingInformer.
@Test
@DisplayName("Should create informer for ClusterRoleBinding resource")
void testWithClusterBindingInformer() throws InterruptedException {
// Given
setupMockServerExpectations(ClusterRoleBinding.class, null, this::getList, r -> new WatchEvent(new ClusterRoleBindingBuilder().withNewMetadata().withName("crb1").withResourceVersion(r).endMetadata().build(), "ADDED"), null, null);
// Given
SharedIndexInformer<ClusterRoleBinding> clusterRoleBindingSharedIndexInformer = factory.sharedIndexInformerFor(ClusterRoleBinding.class, RESYNC_PERIOD);
CountDownLatch foundExistingClusterRoleBinding = new CountDownLatch(1);
clusterRoleBindingSharedIndexInformer.addEventHandler(new TestResourceHandler<>(foundExistingClusterRoleBinding, "crb1"));
factory.startAllRegisteredInformers();
foundExistingClusterRoleBinding.await(LATCH_AWAIT_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
// Then
assertEquals(0, foundExistingClusterRoleBinding.getCount());
}
Aggregations