@Test public void runCommandInterruptException () throws Exception { String expectedOutput = Strings.EMPTY; Process process = mock(Process.class); String command = randomUUID().toString(); Throwable exception = new InterruptedException(); InputStream inputStream = mock(InputStream.class); doReturn(process).when(exec) .exec(NAMESPACE, POD_NAME, new String[]{ "sh", "-c", command }, CONTAINER_NAME, false, false); doReturn(inputStream).when(process).getInputStream(); doThrow(exception).when(process).waitFor(); String output = kubernetesShellClient.runCommand(command, true).getStdOut(); assertEquals(expectedOutput, output); verify(exec, times(1)).exec(NAMESPACE, POD_NAME, new String[]{ "sh", "-c", command }, CONTAINER_NAME, false, false); verify(process, times(1)).destroy(); assertTrue(Thread.interrupted()); }
ShellOutput runCommand (String[] command, boolean getOutput) { log.debug("Running command {}, waiting for output = {}", v("command", command), getOutput); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, false, false); if (getOutput) { Future<String> output = Executors.newSingleThreadExecutor() .submit(getFutureOutputFromInputStream(proc.getInputStream())); int exitCode = proc.waitFor(); proc.destroy(); proc = null; String stdOut = getOutputFromFutureString(output); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(stdOut).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } else { proc.waitFor(); return ShellOutput.EMPTY_SHELL_OUTPUT; } } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return ShellOutput.EMPTY_SHELL_OUTPUT; } catch (IOException | ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_CLIENT_ERROR, e); } finally { if (proc != null) proc.destroy(); } }
KubernetesShellClient implements ShellClient { ShellOutput runCommand (String[] command, boolean getOutput) { log.debug("Running command {}, waiting for output = {}", v("command", command), getOutput); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, false, false); if (getOutput) { Future<String> output = Executors.newSingleThreadExecutor() .submit(getFutureOutputFromInputStream(proc.getInputStream())); int exitCode = proc.waitFor(); proc.destroy(); proc = null; String stdOut = getOutputFromFutureString(output); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(stdOut).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } else { proc.waitFor(); return ShellOutput.EMPTY_SHELL_OUTPUT; } } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return ShellOutput.EMPTY_SHELL_OUTPUT; } catch (IOException | ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_CLIENT_ERROR, e); } finally { if (proc != null) proc.destroy(); } } }
KubernetesShellClient implements ShellClient { ShellOutput runCommand (String[] command, boolean getOutput) { log.debug("Running command {}, waiting for output = {}", v("command", command), getOutput); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, false, false); if (getOutput) { Future<String> output = Executors.newSingleThreadExecutor() .submit(getFutureOutputFromInputStream(proc.getInputStream())); int exitCode = proc.waitFor(); proc.destroy(); proc = null; String stdOut = getOutputFromFutureString(output); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(stdOut).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } else { proc.waitFor(); return ShellOutput.EMPTY_SHELL_OUTPUT; } } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return ShellOutput.EMPTY_SHELL_OUTPUT; } catch (IOException | ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_CLIENT_ERROR, e); } finally { if (proc != null) proc.destroy(); } }  }
KubernetesShellClient implements ShellClient { ShellOutput runCommand (String[] command, boolean getOutput) { log.debug("Running command {}, waiting for output = {}", v("command", command), getOutput); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, false, false); if (getOutput) { Future<String> output = Executors.newSingleThreadExecutor() .submit(getFutureOutputFromInputStream(proc.getInputStream())); int exitCode = proc.waitFor(); proc.destroy(); proc = null; String stdOut = getOutputFromFutureString(output); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(stdOut).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } else { proc.waitFor(); return ShellOutput.EMPTY_SHELL_OUTPUT; } } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return ShellOutput.EMPTY_SHELL_OUTPUT; } catch (IOException | ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_CLIENT_ERROR, e); } finally { if (proc != null) proc.destroy(); } }  static KubernetesShellClientBuilder builder(); @Override String runResource(Resource resource); @Override ShellOutput runCommand(String command); @Override void close(); }
KubernetesShellClient implements ShellClient { ShellOutput runCommand (String[] command, boolean getOutput) { log.debug("Running command {}, waiting for output = {}", v("command", command), getOutput); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, false, false); if (getOutput) { Future<String> output = Executors.newSingleThreadExecutor() .submit(getFutureOutputFromInputStream(proc.getInputStream())); int exitCode = proc.waitFor(); proc.destroy(); proc = null; String stdOut = getOutputFromFutureString(output); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(stdOut).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } else { proc.waitFor(); return ShellOutput.EMPTY_SHELL_OUTPUT; } } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return ShellOutput.EMPTY_SHELL_OUTPUT; } catch (IOException | ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_CLIENT_ERROR, e); } finally { if (proc != null) proc.destroy(); } }  static KubernetesShellClientBuilder builder(); @Override String runResource(Resource resource); @Override ShellOutput runCommand(String command); @Override void close();  }