@Test public void copyResourceToPathTimeout () throws Exception { String filename = randomUUID().toString(); Resource resource = testResource(filename); Process process = mock(Process.class); ChaosException expectedChaosException = new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_TIMEOUT); OutputStream outputStream = mock(OutputStream.class); doReturn(process).when(exec) .exec(NAMESPACE, POD_NAME, ("dd if=/dev/stdin of=/tmp/" + filename + " bs=" + resource.contentLength() + " count=1") .split(" "), CONTAINER_NAME, true, false); doReturn(outputStream).when(process).getOutputStream(); try { kubernetesShellClient.copyResourceToPath(resource, "/tmp/"); fail("Expected timeout exception"); } catch (ChaosException ex) { assertEquals(expectedChaosException.getMessage(), ex.getMessage()); } verify(outputStream, atLeastOnce()).flush(); verify(process, times(1)).destroy(); verify(process, times(1)).waitFor(anyLong(), any()); }
String copyResourceToPath (Resource resource, String path) throws IOException { long contentLength = resource.contentLength(); String finalPath = path + (path.endsWith("/") ? Strings.EMPTY : "/") + resource.getFilename(); String[] command = new String[]{ "dd", "if=/dev/stdin", "of=" + finalPath, "bs=" + contentLength, "count=1" }; log.debug("Transferring {} to {}/{} in path {}", resource, namespace, podName, path); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, true, false); try (OutputStream os = proc.getOutputStream()) { try (InputStream is = resource.getInputStream()) { StreamUtils.copy(is, os); } } if (!proc.waitFor(5000, TimeUnit.MILLISECONDS)) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_TIMEOUT); } int i = proc.exitValue(); if (i == 0) { String command1 = "chmod 755 " + finalPath; runCommand(command1); return finalPath; } throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL); } catch (ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL, e); } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return Strings.EMPTY; } finally { if (proc != null) proc.destroy(); } }
KubernetesShellClient implements ShellClient { String copyResourceToPath (Resource resource, String path) throws IOException { long contentLength = resource.contentLength(); String finalPath = path + (path.endsWith("/") ? Strings.EMPTY : "/") + resource.getFilename(); String[] command = new String[]{ "dd", "if=/dev/stdin", "of=" + finalPath, "bs=" + contentLength, "count=1" }; log.debug("Transferring {} to {}/{} in path {}", resource, namespace, podName, path); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, true, false); try (OutputStream os = proc.getOutputStream()) { try (InputStream is = resource.getInputStream()) { StreamUtils.copy(is, os); } } if (!proc.waitFor(5000, TimeUnit.MILLISECONDS)) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_TIMEOUT); } int i = proc.exitValue(); if (i == 0) { String command1 = "chmod 755 " + finalPath; runCommand(command1); return finalPath; } throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL); } catch (ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL, e); } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return Strings.EMPTY; } finally { if (proc != null) proc.destroy(); } } }
KubernetesShellClient implements ShellClient { String copyResourceToPath (Resource resource, String path) throws IOException { long contentLength = resource.contentLength(); String finalPath = path + (path.endsWith("/") ? Strings.EMPTY : "/") + resource.getFilename(); String[] command = new String[]{ "dd", "if=/dev/stdin", "of=" + finalPath, "bs=" + contentLength, "count=1" }; log.debug("Transferring {} to {}/{} in path {}", resource, namespace, podName, path); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, true, false); try (OutputStream os = proc.getOutputStream()) { try (InputStream is = resource.getInputStream()) { StreamUtils.copy(is, os); } } if (!proc.waitFor(5000, TimeUnit.MILLISECONDS)) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_TIMEOUT); } int i = proc.exitValue(); if (i == 0) { String command1 = "chmod 755 " + finalPath; runCommand(command1); return finalPath; } throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL); } catch (ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL, e); } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return Strings.EMPTY; } finally { if (proc != null) proc.destroy(); } }  }
KubernetesShellClient implements ShellClient { String copyResourceToPath (Resource resource, String path) throws IOException { long contentLength = resource.contentLength(); String finalPath = path + (path.endsWith("/") ? Strings.EMPTY : "/") + resource.getFilename(); String[] command = new String[]{ "dd", "if=/dev/stdin", "of=" + finalPath, "bs=" + contentLength, "count=1" }; log.debug("Transferring {} to {}/{} in path {}", resource, namespace, podName, path); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, true, false); try (OutputStream os = proc.getOutputStream()) { try (InputStream is = resource.getInputStream()) { StreamUtils.copy(is, os); } } if (!proc.waitFor(5000, TimeUnit.MILLISECONDS)) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_TIMEOUT); } int i = proc.exitValue(); if (i == 0) { String command1 = "chmod 755 " + finalPath; runCommand(command1); return finalPath; } throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL); } catch (ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL, e); } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return Strings.EMPTY; } 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 { String copyResourceToPath (Resource resource, String path) throws IOException { long contentLength = resource.contentLength(); String finalPath = path + (path.endsWith("/") ? Strings.EMPTY : "/") + resource.getFilename(); String[] command = new String[]{ "dd", "if=/dev/stdin", "of=" + finalPath, "bs=" + contentLength, "count=1" }; log.debug("Transferring {} to {}/{} in path {}", resource, namespace, podName, path); Process proc = null; try { proc = getExec().exec(namespace, podName, command, containerName, true, false); try (OutputStream os = proc.getOutputStream()) { try (InputStream is = resource.getInputStream()) { StreamUtils.copy(is, os); } } if (!proc.waitFor(5000, TimeUnit.MILLISECONDS)) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_TIMEOUT); } int i = proc.exitValue(); if (i == 0) { String command1 = "chmod 755 " + finalPath; runCommand(command1); return finalPath; } throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL); } catch (ApiException e) { throw new ChaosException(KubernetesChaosErrorCode.K8S_SHELL_TRANSFER_FAIL, e); } catch (InterruptedException e) { log.error("Interrupted while processing, throwing Interrupt up thread", e); Thread.currentThread().interrupt(); return Strings.EMPTY; } finally { if (proc != null) proc.destroy(); } }  static KubernetesShellClientBuilder builder(); @Override String runResource(Resource resource); @Override ShellOutput runCommand(String command); @Override void close();  }