io.fabric8.kubernetes.client.Watcher.Action Java Examples

The following examples show how to use io.fabric8.kubernetes.client.Watcher.Action. 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: KubernetesNamespaceTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStopsWaitingServiceAccountEventJustAfterEventReceived() throws Exception {
  prepareCreateNamespaceRequest();
  final Resource resource = prepareNamespaceResource(NAMESPACE);
  doThrow(new KubernetesClientException("error", 403, null)).when(resource).get();
  when(serviceAccountResource.get()).thenReturn(null);
  doAnswer(
          invocation -> {
            final Watcher<ServiceAccount> watcher = invocation.getArgument(0);
            watcher.eventReceived(Action.ADDED, mock(ServiceAccount.class));
            return mock(Watch.class);
          })
      .when(serviceAccountResource)
      .watch(any());

  new KubernetesNamespace(clientFactory, executor, NAMESPACE, WORKSPACE_ID).prepare(true);

  verify(serviceAccountResource).get();
  verify(serviceAccountResource).watch(any());
}
 
Example #2
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldGenerateStartedAndRecordSubmitToRunningTimeWhenContainerIsReady() throws Exception {
  when(time.nanoTime()).thenReturn(TimeUnit.SECONDS.toNanos(17));
  kdr.start(RUN_STATE, RunSpec.simple(POD_NAME, "busybox"));
  verify(stats).recordSubmission(POD_NAME);

  when(time.nanoTime()).thenReturn(TimeUnit.SECONDS.toNanos(18));

  when(time.nanoTime()).thenReturn(TimeUnit.SECONDS.toNanos(19));
  setRunning(createdPod, /* ready= */ false);
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);
  verify(stateManager, never()).receive(Event.started(WORKFLOW_INSTANCE), -1);

  when(time.nanoTime()).thenReturn(TimeUnit.SECONDS.toNanos(4711));
  setRunning(createdPod, /* ready= */ true);
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);
  verify(stateManager).receive(Event.started(WORKFLOW_INSTANCE), -1);

  verify(stats).recordRunning(POD_NAME);
}
 
Example #3
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itCompletesExceptionallyOnError() throws Exception {
  TrackingPredicate condition = condition(ss -> true);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.ERROR, configMap);
  assertTrue(watcher.getFuture().isDone());
  try {
    watcher.getFuture().get();
    fail("should have thrown exception");
  } catch (ExecutionException e) {
    assertEquals(e.getCause().getClass(), WatchException.class);
    assertEquals(e.getCause().getMessage(), "Action.ERROR received");
  }
  assertFalse(condition.isCalled());
}
 
Example #4
Source File: ConfigMapSupplierTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@Test
public void added() throws Exception {
  when(resource.get()).thenReturn(configMap1);
  try (ConfigMapSupplier underTest = new ConfigMapSupplier(client, name)) {
    assertThat(underTest.get(), is(configMap1));
    underTest.eventReceived(Action.ADDED, configMap2);
    assertThat(underTest.get(), is(configMap2));
  }
}
 
Example #5
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itDoesNotCompleteOnNoMatchDeleted() {
  TrackingPredicate condition = condition(Objects::nonNull);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.DELETED, configMap);
  assertFalse(watcher.getFuture().isDone());
  condition.isCalledWith(null);
}
 
Example #6
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itDoesNotCompleteOnNoMatchModified() {
  TrackingPredicate condition = condition(ss -> false);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.MODIFIED, configMap);
  assertFalse(watcher.getFuture().isDone());
  condition.isCalledWith(configMap);
}
 
Example #7
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itDoesNotCompleteOnNoMatchAdded() {
  TrackingPredicate condition = condition(ss -> false);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.ADDED, configMap);
  assertFalse(watcher.getFuture().isDone());
  condition.isCalledWith(configMap);
}
 
Example #8
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itCompletesOnMatchDeleted() throws Exception {
  TrackingPredicate condition = condition(Objects::isNull);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.DELETED, configMap);
  assertTrue(watcher.getFuture().isDone());
  assertNull(watcher.getFuture().get());
  condition.isCalledWith(null);
}
 
Example #9
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itCompletesOnMatchModified() throws Exception {
  TrackingPredicate condition = condition(ss -> true);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.MODIFIED, configMap);
  assertTrue(watcher.getFuture().isDone());
  assertEquals(watcher.getFuture().get(), configMap);
  condition.isCalledWith(configMap);
}
 
Example #10
Source File: WaitForConditionWatcherTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void itCompletesOnMatchAdded() throws Exception {
  TrackingPredicate condition = condition(ss -> true);
  WaitForConditionWatcher<ConfigMap> watcher = new WaitForConditionWatcher<>(condition);
  watcher.eventReceived(Action.ADDED, configMap);
  assertTrue(watcher.getFuture().isDone());
  assertEquals(watcher.getFuture().get(), configMap);
  condition.isCalledWith(configMap);
}
 
Example #11
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandlePodWithoutAnnotationsWhenWatching()  {
  // Create a pod instance with null `annotations` field - not possible through builder
  final Pod pod = Json.OBJECT_MAPPER.convertValue(
      Map.of("metadata",
          Map.of("name", "foobar")), Pod.class);

  assertThat(Try.run(() -> podWatcher.eventReceived(Action.MODIFIED, pod)).isSuccess(), is(true));
}
 
Example #12
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDiscardChangesForOldExecutions() throws Exception {
  kdr.start(RUN_STATE, RUN_SPEC);

  // simulate event from different pod, but still with the same workflow instance annotation
  createdPod.getMetadata().setName(POD_NAME + "-other");
  setTerminated(createdPod, "Succeeded", 20, null);

  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verify(stateManager, never()).receive(any(), anyLong());
}
 
Example #13
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailOnUnexpectedTerminatedStatus() throws Exception {
  setWaiting(createdPod, "Failed", "");
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verify(stateManager).receive(
      Event.runError(WORKFLOW_INSTANCE, "Unexpected null terminated status"),
      -1);
}
 
Example #14
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailOnMissingContainer() throws Exception {
  createdPod.setStatus(podStatusNoContainer("Succeeded"));
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verify(stateManager).receive(
      Event.runError(WORKFLOW_INSTANCE, "Could not find our container in pod"),
      -1);
}
 
Example #15
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldIgnoreDeletedEvents() throws Exception {
  createdPod.setStatus(podStatusNoContainer("Succeeded"));
  receiveAndProcessEvent(Watcher.Action.DELETED, createdPod);

  verify(stateManager, never()).receive(any(), anyLong());
}
 
Example #16
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailOnUnknownPhaseEntered() throws Exception {
  createdPod.setStatus(podStatusNoContainer("Unknown"));
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);
  verify(stateManager).receive(Event.runError(WORKFLOW_INSTANCE, "Pod entered Unknown phase"),
      -1);
}
 
Example #17
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotSendStatsOnOtherError() {
  createdPod.setStatus(podStatusNoContainer("Succeeded"));
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verifyNoMoreInteractions(stats);
}
 
Example #18
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSendStatsOnErrImagePull() throws Exception {
  setWaiting(createdPod, "Pending", "ErrImagePull", "foobar");
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verify(stats).recordPullImageError();
  verify(stateManager).receive(
      Event.runError(WORKFLOW_INSTANCE,
          "Failed to pull image busybox:latest of container styx-run, reason: ErrImagePull, message: foobar"),
      -1);
}
 
Example #19
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailOnErrImagePull() throws Exception {
  setWaiting(createdPod, "Pending", "ErrImagePull", "foobar");
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verify(stateManager).receive(
      Event.runError(WORKFLOW_INSTANCE,
          "Failed to pull image busybox:latest of container styx-run, reason: ErrImagePull, message: foobar"),
      -1);
}
 
Example #20
Source File: ConfigMapSupplierTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
public void error() throws Exception {
  when(resource.get()).thenReturn(configMap1);
  try (ConfigMapSupplier underTest = new ConfigMapSupplier(client, name)) {
    underTest.eventReceived(Action.ERROR, null);
  }
}
 
Example #21
Source File: ConfigMapSupplierTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void deleted() throws Exception {
  when(resource.get()).thenReturn(configMap1);
  try (ConfigMapSupplier underTest = new ConfigMapSupplier(client, name)) {
    underTest.eventReceived(Action.DELETED, null);
    underTest.get();
  }
}
 
Example #22
Source File: ConfigMapSupplierTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@Test
public void modified() throws Exception {
  when(resource.get()).thenReturn(configMap1);
  try (ConfigMapSupplier underTest = new ConfigMapSupplier(client, name)) {
    assertThat(underTest.get(), is(configMap1));
    underTest.eventReceived(Action.MODIFIED, configMap2);
    assertThat(underTest.get(), is(configMap2));
  }
}
 
Example #23
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 4 votes vote down vote up
/**
 * Helper to deal with asynchronous pod even handling
 */
private void receiveAndProcessEvent(Action action, Pod pod) {
  when(k8sClient.getPod(pod.getMetadata().getName())).thenReturn(Optional.of(pod));
  podWatcher.eventReceived(action, pod);
  executor.tick(10, TimeUnit.SECONDS);
}
 
Example #24
Source File: PodActionHandler.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
/** Handles the pod action events. */
void handle(Action action, Pod pod);
 
Example #25
Source File: KubernetesDockerRunnerTest.java    From styx with Apache License 2.0 4 votes vote down vote up
@Parameters({
    "Running,   20, true",
    "Running,   1,  true",
    "Running,   0,  true",
    "Succeeded, 20, true",
    "Succeeded, 1,  true",
    "Succeeded, 0,  true",
    "Failed,    20, true",
    "Failed,    1,  true",
    "Running,   20, false",
    "Running,   1,  false",
    "Running,   0,  false",
    "Succeeded, 20, false",
    "Succeeded, 1,  false",
    "Succeeded, 0,  false",
    "Failed,    20, false",
    "Failed,    1,  false"
})
@Test
public void shouldCompleteWithStatusCodeOnMainContainerTerminated(String phase, int code,
    boolean withKeepaliveContainer) throws Exception {

  final PodStatus podStatus = podStatusNoContainer(phase);

  podStatus.getContainerStatuses()
      .add(new ContainerStatusBuilder()
          .withState(terminatedContainerState(code, ""))
          .withName(MAIN_CONTAINER_NAME)
          .build());

  // Verify that old pods without a keepalive container are also correctly handled
  if (withKeepaliveContainer) {
    podStatus.getContainerStatuses()
        .add(new ContainerStatusBuilder()
            .withName(KEEPALIVE_CONTAINER_NAME)
            .withNewState().withNewRunning().endRunning().endState()
            .build());
  }

  createdPod.setStatus(podStatus);
  receiveAndProcessEvent(Watcher.Action.MODIFIED, createdPod);

  verify(stateManager).receive(Event.started(WORKFLOW_INSTANCE), -1);
  verify(stateManager).receive(Event.terminate(WORKFLOW_INSTANCE, Optional.of(code)), 0);
}