-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filter exit code from command output
filter exit code from stdout on join removed lefover public sleep 5 sleep on ssh-add and ssh-agent
- Loading branch information
Showing
5 changed files
with
259 additions
and
22 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...n/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecCutExitCodeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.pipeline; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import org.apache.commons.lang.ArrayUtils; | ||
|
||
public class ContainerExecCutExitCodeUtil { | ||
private static byte SPACE = " ".getBytes(StandardCharsets.UTF_8)[0]; | ||
|
||
public static byte[] getPartToWriteOut(byte[] q, byte[] exitCommandTxt) { | ||
int exitCodeStartIndex = getExitCodeIndex(q, exitCommandTxt); | ||
int endIndex = exitCodeStartIndex != -1 ? exitCodeStartIndex : q.length; | ||
return partToWriteOut(q, endIndex); | ||
} | ||
|
||
private static byte[] partToWriteOut(byte[] q, int endIndex) { | ||
return ArrayUtils.subarray(q, 0, endIndex); | ||
} | ||
|
||
private static int getExitCodeIndex(byte[] q, byte[] exitCommandTxt) { | ||
if (!endsWithNumber(q)) { | ||
return -1; | ||
} else { | ||
int numbersStartIndex = getNumbersStartIndex(q); | ||
return getExitCodeStartIndex(q, numbersStartIndex, exitCommandTxt); | ||
} | ||
} | ||
|
||
private static int getExitCodeStartIndex(byte[] q, int numbersStartIndex, byte[] exitCommandTxt) { | ||
int possibleStartIndex = getPossibleStartIndex(q, numbersStartIndex, exitCommandTxt); | ||
byte[] exitCodeTxtSubarray = getExitCodeTxtSubarray(q, possibleStartIndex, numbersStartIndex); | ||
if (Arrays.equals(exitCodeTxtSubarray, exitCommandTxt)) { | ||
return possibleStartIndex; | ||
} | ||
return -1; | ||
} | ||
|
||
private static int getPossibleStartIndex(byte[] q, int numbersStartIndex, byte[] exitCommandTxt) { | ||
return q.length - (q.length - numbersStartIndex) - exitCommandTxt.length; | ||
} | ||
|
||
private static byte[] getExitCodeTxtSubarray(byte[] q, int possibleStartIndex, int numbersStartIndex) { | ||
return ArrayUtils.subarray(q, possibleStartIndex, numbersStartIndex); | ||
} | ||
|
||
private static int getNumbersStartIndex(byte[] ba) { | ||
int i = ba.length - 1; | ||
for (; isNumber(ba[i]); i--) | ||
; | ||
for (; ba[i] == SPACE; i--) | ||
; | ||
return ++i; | ||
} | ||
|
||
private static boolean endsWithNumber(byte[] ba) { | ||
return isNumber(ba[ba.length - 1]); | ||
} | ||
|
||
private static boolean isNumber(byte b) { | ||
String s = new String(new byte[] { (byte) b }, StandardCharsets.UTF_8); | ||
try { | ||
Integer.parseInt(s); | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...va/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecCutExitCodeUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.pipeline; | ||
|
||
import static org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecCutExitCodeUtil.getPartToWriteOut; | ||
import static org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator.FilterOutExitCodeOutputStream.EXIT_COMMAND_TXT_BYTES; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.junit.Test; | ||
|
||
public class ContainerExecCutExitCodeUtilTest { | ||
|
||
final static String EXITCODE_GOOD = "\nEXITCODE 0"; | ||
final static String EXITCODE_ERR = "\nEXITCODE 127"; | ||
final static String EXITCODE_BAD_NONUM = "\nEXITCODE "; | ||
final static String LONG_STR = "this is a very long string. followd by something. or not. who knows?"; | ||
final static String SHORT_STR = "a"; | ||
final static byte[] EMPTY = "".getBytes(StandardCharsets.UTF_8); | ||
final static byte[] GOOD_LONG = (LONG_STR + EXITCODE_GOOD).getBytes(StandardCharsets.UTF_8); | ||
final static byte[] GOOD_SHORT = (SHORT_STR + EXITCODE_GOOD).getBytes(StandardCharsets.UTF_8); | ||
final static byte[] BAD_LONG = (LONG_STR + EXITCODE_BAD_NONUM).getBytes(StandardCharsets.UTF_8); | ||
final static byte[] BAD_SHORT = (SHORT_STR + EXITCODE_BAD_NONUM).getBytes(StandardCharsets.UTF_8); | ||
final static byte[] BAD_LONG_NOEXIT = LONG_STR.getBytes(StandardCharsets.UTF_8); | ||
final static byte[] BAD_SHORT_NOEXIT = SHORT_STR.getBytes(StandardCharsets.UTF_8); | ||
final static byte[] ERR_LONG = (LONG_STR + EXITCODE_ERR).getBytes(StandardCharsets.UTF_8); | ||
final static byte[] ERR_SHORT = (SHORT_STR + EXITCODE_ERR).getBytes(StandardCharsets.UTF_8); | ||
|
||
@Test | ||
public void testGoodLong() { | ||
byte[] p = getPartToWriteOut(GOOD_LONG, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(LONG_STR, s); | ||
} | ||
|
||
@Test | ||
public void testGoodShort() { | ||
byte[] p = getPartToWriteOut(GOOD_SHORT, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(SHORT_STR, s); | ||
} | ||
|
||
@Test | ||
public void testErrLong() { | ||
byte[] p = getPartToWriteOut(ERR_LONG, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(LONG_STR, s); | ||
} | ||
|
||
@Test | ||
public void testErrShort() { | ||
byte[] p = getPartToWriteOut(ERR_SHORT, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(SHORT_STR, s); | ||
} | ||
|
||
@Test | ||
public void testBadShort() { | ||
byte[] p = getPartToWriteOut(BAD_SHORT, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(new String(BAD_SHORT, StandardCharsets.UTF_8), s); | ||
} | ||
|
||
@Test | ||
public void testBadLong() { | ||
byte[] p = getPartToWriteOut(BAD_LONG, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(new String(BAD_LONG, StandardCharsets.UTF_8), s); | ||
} | ||
|
||
@Test | ||
public void testBadShortNoExit() { | ||
byte[] p = getPartToWriteOut(BAD_SHORT_NOEXIT, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(new String(BAD_SHORT_NOEXIT, StandardCharsets.UTF_8), s); | ||
} | ||
|
||
@Test | ||
public void testBadLongNoExit() { | ||
byte[] p = getPartToWriteOut(BAD_LONG_NOEXIT, EXIT_COMMAND_TXT_BYTES); | ||
String s = new String(p, StandardCharsets.UTF_8); | ||
assertEquals(new String(BAD_LONG_NOEXIT, StandardCharsets.UTF_8), s); | ||
} | ||
} |
Oops, something went wrong.