Java Code Examples for io.fabric8.kubernetes.client.Watcher#eventReceived()

The following examples show how to use io.fabric8.kubernetes.client.Watcher#eventReceived() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldCompleteExceptionallyFutureForWaitingPodIfStatusIsSucceeded() throws Exception {
  // given
  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_SUCCEEDED);
  CompletableFuture future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isCompletedExceptionally());

  try {
    future.get();
  } catch (ExecutionException e) {
    assertEquals(
        e.getCause().getMessage(),
        "Pod container has been terminated. Container must be configured to use a non-terminating command.");
  }
}
 
Example 2
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldCompleteExceptionallyFutureForWaitingPodIfStatusIsFailed() throws Exception {
  // given
  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_FAILED);
  when(pod.getStatus().getReason()).thenReturn("Evicted");
  CompletableFuture future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isCompletedExceptionally());

  try {
    future.get();
  } catch (ExecutionException e) {
    assertEquals(e.getCause().getMessage(), "Pod 'podName' failed to start. Reason: Evicted");
  }
}
 
Example 3
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void
    shouldCompleteExceptionallyFutureForWaitingPodIfStatusIsFailedAndReasonIsNotAvailable()
        throws Exception {
  // given
  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_FAILED);
  when(podResource.getLog()).thenReturn("Pod fail log");
  CompletableFuture future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isCompletedExceptionally());
  try {
    future.get();
  } catch (ExecutionException e) {
    assertEquals(
        e.getCause().getMessage(), "Pod 'podName' failed to start. Pod logs: Pod fail log");
  }
}
 
Example 4
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldCompleteFutureForWaitingPodIfStatusIsRunning() {
  // given
  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_RUNNING);
  CompletableFuture<?> future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isDone());
}
 
Example 5
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void
    shouldCompleteExceptionallyFutureForWaitingPodIfStatusIsRunningButSomeContainersAreTerminated() {
  // given
  ContainerStatus containerStatus = mock(ContainerStatus.class);
  when(containerStatus.getName()).thenReturn("FailingContainer");
  when(containerStatus.getState())
      .thenReturn(
          new ContainerStateBuilder()
              .withNewTerminated()
              .withReason("Completed")
              .endTerminated()
              .build());

  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_RUNNING);
  when(status.getContainerStatuses()).thenReturn(singletonList(containerStatus));
  CompletableFuture<?> future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isDone());
  assertTrue(future.isCompletedExceptionally());
}
 
Example 6
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void
    shouldCompleteExceptionallyFutureForWaitingPodIfStatusIsRunningButSomeContainersAreWaitingAndTerminatedBefore() {
  // given
  ContainerStatus containerStatus = mock(ContainerStatus.class);
  when(containerStatus.getName()).thenReturn("FailingContainer");
  when(containerStatus.getState())
      .thenReturn(
          new ContainerStateBuilder().withNewWaiting().withMessage("bah").endWaiting().build());
  when(containerStatus.getLastState())
      .thenReturn(
          new ContainerStateBuilder()
              .withNewTerminated()
              .withReason("Completed")
              .endTerminated()
              .build());

  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_RUNNING);
  when(status.getContainerStatuses()).thenReturn(singletonList(containerStatus));
  CompletableFuture<?> future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isDone());
  assertTrue(future.isCompletedExceptionally());
}
 
Example 7
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void
    shouldCompleteExceptionallyFutureForWaitingPodIfStatusIsFailedAndReasonNorLogsAreNotAvailable()
        throws Exception {
  // given
  CompletableFuture future = kubernetesDeployments.waitRunningAsync(POD_NAME);

  when(status.getPhase()).thenReturn(POD_STATUS_PHASE_FAILED);
  doThrow(new InfrastructureException("Unable to create client"))
      .when(clientFactory)
      .create(anyString());

  // when
  verify(podResource).watch(watcherCaptor.capture());
  Watcher<Pod> watcher = watcherCaptor.getValue();
  watcher.eventReceived(Watcher.Action.MODIFIED, pod);

  // then
  assertTrue(future.isCompletedExceptionally());
  try {
    future.get();
  } catch (ExecutionException e) {
    assertEquals(
        e.getCause().getMessage(),
        "Pod 'podName' failed to start. Error occurred while fetching pod logs: Unable to create client");
  }
}
 
Example 8
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldCallHandlerForEventsOnPods() throws Exception {
  // Given
  when(objectReference.getKind()).thenReturn(POD_OBJECT_KIND);
  kubernetesDeployments.watchEvents(podEventHandler);
  verify(eventNamespaceMixedOperation).watch(eventWatcherCaptor.capture());
  Watcher<Event> watcher = eventWatcherCaptor.getValue();

  // When
  watcher.eventReceived(Watcher.Action.ADDED, event);

  // Then
  verify(podEventHandler).handle(any());
}
 
Example 9
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldCallHandlerForEventsOnReplicaSets() throws Exception {
  // Given
  when(objectReference.getKind()).thenReturn(REPLICASET_OBJECT_KIND);
  kubernetesDeployments.watchEvents(podEventHandler);
  verify(eventNamespaceMixedOperation).watch(eventWatcherCaptor.capture());
  Watcher<Event> watcher = eventWatcherCaptor.getValue();

  // When
  watcher.eventReceived(Watcher.Action.ADDED, event);

  // Then
  verify(podEventHandler).handle(any());
}
 
Example 10
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldCallHandlerForEventsOnDeployments() throws Exception {
  // Given
  when(objectReference.getKind()).thenReturn(DEPLOYMENT_OBJECT_KIND);
  kubernetesDeployments.watchEvents(podEventHandler);
  verify(eventNamespaceMixedOperation).watch(eventWatcherCaptor.capture());
  Watcher<Event> watcher = eventWatcherCaptor.getValue();

  // When
  watcher.eventReceived(Watcher.Action.ADDED, event);

  // Then
  verify(podEventHandler).handle(any());
}
 
Example 11
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldFallbackToFirstTimeStampIfLastTimeStampIsNull() throws InfrastructureException {
  // Given
  when(objectReference.getKind()).thenReturn(POD_OBJECT_KIND);
  kubernetesDeployments.watchEvents(podEventHandler);
  verify(eventNamespaceMixedOperation).watch(eventWatcherCaptor.capture());
  Watcher<Event> watcher = eventWatcherCaptor.getValue();
  Event event = mock(Event.class);
  when(event.getInvolvedObject()).thenReturn(objectReference);
  when(event.getMetadata()).thenReturn(new ObjectMeta());
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.YEAR, 1);
  Date nextYear = cal.getTime();
  when(event.getFirstTimestamp()).thenReturn(PodEvents.convertDateToEventTimestamp(nextYear));
  when(event.getLastTimestamp()).thenReturn(null);

  // When
  watcher.eventReceived(Watcher.Action.ADDED, event);

  // Then
  verify(event, times(1)).getLastTimestamp();
  verify(event, times(1)).getFirstTimestamp();
  ArgumentCaptor<PodEvent> captor = ArgumentCaptor.forClass(PodEvent.class);
  verify(podEventHandler).handle(captor.capture());
  PodEvent podEvent = captor.getValue();
  assertEquals(podEvent.getLastTimestamp(), PodEvents.convertDateToEventTimestamp(nextYear));
}
 
Example 12
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldUseLastTimestampIfAvailable() throws InfrastructureException {
  // Given
  when(objectReference.getKind()).thenReturn(POD_OBJECT_KIND);
  kubernetesDeployments.watchEvents(podEventHandler);
  verify(eventNamespaceMixedOperation).watch(eventWatcherCaptor.capture());
  Watcher<Event> watcher = eventWatcherCaptor.getValue();
  Event event = mock(Event.class);
  when(event.getInvolvedObject()).thenReturn(objectReference);
  when(event.getMetadata()).thenReturn(new ObjectMeta());
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.YEAR, 2);
  Date nextYear = cal.getTime();
  when(event.getLastTimestamp()).thenReturn(PodEvents.convertDateToEventTimestamp(nextYear));
  when(event.getFirstTimestamp()).thenReturn(PodEvents.convertDateToEventTimestamp(new Date()));

  // When
  watcher.eventReceived(Watcher.Action.ADDED, event);

  // Then
  verify(event, times(1)).getLastTimestamp();
  verify(event, never()).getFirstTimestamp();
  ArgumentCaptor<PodEvent> captor = ArgumentCaptor.forClass(PodEvent.class);
  verify(podEventHandler).handle(captor.capture());
  PodEvent podEvent = captor.getValue();
  assertEquals(podEvent.getLastTimestamp(), PodEvents.convertDateToEventTimestamp(nextYear));
}
 
Example 13
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldHandleEventWithEmptyLastTimestampAndFirstTimestamp() throws Exception {
  // Given
  when(objectReference.getKind()).thenReturn(POD_OBJECT_KIND);
  kubernetesDeployments.watchEvents(podEventHandler);
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.MINUTE, -1);
  Date minuteAgo = cal.getTime();

  Field f = KubernetesDeployments.class.getDeclaredField("watcherInitializationDate");
  f.setAccessible(true);
  f.set(kubernetesDeployments, minuteAgo);

  verify(eventNamespaceMixedOperation).watch(eventWatcherCaptor.capture());
  Watcher<Event> watcher = eventWatcherCaptor.getValue();
  Event event = mock(Event.class);
  when(event.getInvolvedObject()).thenReturn(objectReference);
  when(event.getMetadata()).thenReturn(new ObjectMeta());
  when(event.getLastTimestamp()).thenReturn(null);
  when(event.getFirstTimestamp()).thenReturn(null);

  // When
  watcher.eventReceived(Watcher.Action.ADDED, event);

  // Then
  verify(event, times(1)).getLastTimestamp();
  verify(event, times(1)).getFirstTimestamp();
  ArgumentCaptor<PodEvent> captor = ArgumentCaptor.forClass(PodEvent.class);
  verify(podEventHandler).handle(captor.capture());
  PodEvent podEvent = captor.getValue();
  assertNotNull(podEvent.getLastTimestamp());
}