Search in sources :

Example 1 with WatchException

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchException in project jkube by eclipse.

the class PodExecutorTest method executeCommandInPodKubernetesError.

@Test
public void executeCommandInPodKubernetesError() {
    // Given
    // @formatter:off
    new Expectations() {

        {
            kubernetesClient.pods().inNamespace(anyString).withName(anyString);
            result = new KubernetesClientException("Mocked Error");
        }
    };
    // @formatter:on
    // When
    final WatchException result = assertThrows(WatchException.class, () -> podExecutor.executeCommandInPod(Collections.emptySet(), "sh"));
    // Then
    assertThat(result).hasMessage("Execution failed due to a KubernetesClient error: Mocked Error");
}
Also used : Expectations(mockit.Expectations) WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.junit.Test)

Example 2 with WatchException

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchException in project jkube by eclipse.

the class PodExecutor method executeCommandInPod.

void executeCommandInPod(Collection<HasMetadata> resources, String command) throws IOException, InterruptedException, WatchException {
    try (KubernetesClient client = clusterAccess.createDefaultClient();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ByteArrayOutputStream error = new ByteArrayOutputStream()) {
        String namespace = clusterAccess.getNamespace();
        final ExecListenerLatch latch = new ExecListenerLatch(onOpen);
        ExecWatch execWatch = client.pods().inNamespace(namespace).withName(KubernetesHelper.getNewestApplicationPodName(client, namespace, resources)).readingInput(readingInput).writingOutput(outputStream).writingError(outputStream).writingErrorChannel(error).usingListener(latch).exec("sh", "-c", command);
        final boolean completed = latch.await(waitTimeout.toMillis(), TimeUnit.MILLISECONDS);
        execWatch.close();
        output = outputStream.toString();
        if (!completed) {
            throw new WatchException("Command execution timed out");
        }
        if (latch.getCloseCode() != 1000) {
            throw new WatchException("Command execution socket closed unexpectedly " + latch.getCloseReason());
        }
        final Map<String, Object> status = objectMapper.readValue(error.toString(), Map.class);
        if (status.getOrDefault("status", "Failure").equals("Failure")) {
            throw new WatchException("Command execution failed: " + status.getOrDefault("message", ""));
        }
    } catch (KubernetesClientException e) {
        throw new WatchException("Execution failed due to a KubernetesClient error: " + e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 3 with WatchException

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchException in project jkube by eclipse.

the class PodExecutorTest method executeCommandInPodTimeout.

@Test
public void executeCommandInPodTimeout() {
    // When
    final WatchException result = assertThrows(WatchException.class, () -> podExecutor.executeCommandInPod(Collections.emptySet(), "sh"));
    // Then
    assertThat(result).hasMessage("Command execution timed out");
}
Also used : WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) Test(org.junit.Test)

Example 4 with WatchException

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchException in project jkube by eclipse.

the class PodExecutorTest method executeCommandInPodSocketError.

@Test
public void executeCommandInPodSocketError() {
    // Given
    listenableCloseWithCode(1337);
    // When
    final WatchException result = assertThrows(WatchException.class, () -> podExecutor.executeCommandInPod(Collections.emptySet(), "sh"));
    // Then
    assertThat(result).hasMessage("Command execution socket closed unexpectedly Closed by mock");
}
Also used : WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) Test(org.junit.Test)

Example 5 with WatchException

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchException in project jkube by eclipse.

the class PodExecutorTest method executeCommandInPodCommandFailure.

@Test
public void executeCommandInPodCommandFailure() {
    // Given
    listenableCloseWithCode(1000);
    withErrorChannelResponse("{\"message\": \"Deserialized JSON message\"}");
    // When
    final WatchException result = assertThrows(WatchException.class, () -> podExecutor.executeCommandInPod(Collections.emptySet(), "sh"));
    // Then
    assertThat(result).hasMessage("Command execution failed: Deserialized JSON message");
}
Also used : WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) Test(org.junit.Test)

Aggregations

WatchException (org.eclipse.jkube.kit.build.service.docker.watch.WatchException)5 Test (org.junit.Test)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 ExecWatch (io.fabric8.kubernetes.client.dsl.ExecWatch)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Expectations (mockit.Expectations)1