private void command(List<ProcReturn> results, int i) { ProcReturn r; try { r = execCommand(false, "sh", "-c", "cd /tmp; echo pid is $$$$ > test; cat /tmp/test"); } catch (Exception e) { throw new RuntimeException(e); } results.add(r); }
/** * Test that multiple command execution in parallel works * @throws Exception */ @Test(timeout = 10000) public void testCommandExecution() throws Exception { Thread[] t = new Thread[10]; List<ProcReturn> results = Collections.synchronizedList(new ArrayList<>(t.length)); for (int i = 0; i < t.length; i++) { t[i] = newThread(i, results); } for (int i = 0; i < t.length; i++) { t[i].start(); } for (int i = 0; i < t.length; i++) { t[i].join(); } assertEquals("Not all threads finished successfully", t.length, results.size()); for (ProcReturn r : results) { assertTrue("Output should contain pid: " + r.output, PID_PATTERN.matcher(r.output).find()); assertEquals(0, r.exitCode); assertFalse(r.proc.isAlive()); } }
@Test public void testCommandExecutionWithEscaping() throws Exception { ProcReturn r = execCommand(false, "sh", "-c", "cd /tmp; false; echo result is $$? > test; cat /tmp/test"); assertTrue("Output should contain result: " + r.output, Pattern.compile("^(result is 1)$", Pattern.MULTILINE).matcher(r.output).find()); assertEquals(0, r.exitCode); assertFalse(r.proc.isAlive()); }
@Test public void testQuietCommandExecution() throws Exception { ProcReturn r = execCommand(true, "echo", "pid is 9999"); assertFalse("Output should not contain command: " + r.output, PID_PATTERN.matcher(r.output).find()); assertEquals(0, r.exitCode); assertFalse(r.proc.isAlive()); }
@Test public void testCommandExecutionWithNohup() throws Exception { ProcReturn r = execCommand(false, "nohup", "sh", "-c", "sleep 5; cd /tmp; echo pid is $$$$ > test; cat /tmp/test"); assertTrue("Output should contain pid: " + r.output, PID_PATTERN.matcher(r.output).find()); assertEquals(0, r.exitCode); assertFalse(r.proc.isAlive()); }
@Test public void testCommandExecutionFailureHighError() throws Exception { ProcReturn r = execCommand(false, "sh", "-c", "return 127"); assertEquals(127, r.exitCode); assertFalse(r.proc.isAlive()); }
@Test public void testCommandExecutionFailure() throws Exception { ProcReturn r = execCommand(false, "false"); assertEquals(1, r.exitCode); assertFalse(r.proc.isAlive()); }
@Test public void testCommandExecutionWithNohupAndError() throws Exception { ProcReturn r = execCommand(false, "nohup", "sh", "-c", "sleep 5; return 127"); assertEquals(127, r.exitCode); assertFalse(r.proc.isAlive()); }
@Test @Issue("JENKINS-46719") public void testContainerDoesNotExist() throws Exception { decorator = new ContainerExecDecorator(client, pod.getMetadata().getName(), "doesNotExist", client.getNamespace()); exception.expect(IOException.class); exception.expectMessage(containsString("container [doesNotExist] does not exist in pod [")); execCommand(false, "nohup", "sh", "-c", "sleep 5; return 127"); }