@Test public void runCommandNonZeroExitCode () throws Exception { String expectedOutput = randomUUID().toString(); String expectedLogMessage = "Command execution failed 1: " + expectedOutput; Process process = mock(Process.class); String command = randomUUID().toString(); doReturn(process).when(exec).exec(NAMESPACE, POD_NAME, new String[]{ "sh", "-c", command }, CONTAINER_NAME, false, false); doReturn(1).when(process).waitFor(); Logger logger = (Logger) LoggerFactory.getLogger(KubernetesShellClient.class); ArgumentCaptor<ILoggingEvent> iLoggingEventCaptor = ArgumentCaptor.forClass(ILoggingEvent.class); @SuppressWarnings("unchecked") final Appender<ILoggingEvent> appender = mock(Appender.class); logger.addAppender(appender); logger.setLevel(Level.DEBUG); doReturn(new ByteArrayInputStream(expectedOutput.getBytes())).when(process).getInputStream(); kubernetesShellClient.runCommand(command, true); Mockito.verify(appender, times(2)).doAppend(iLoggingEventCaptor.capture()); final ILoggingEvent loggingEvent = iLoggingEventCaptor.getValue(); assertEquals(Level.DEBUG, loggingEvent.getLevel()); assertEquals(expectedLogMessage, loggingEvent.getFormattedMessage()); }
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();  }