Skip to content

Commit

Permalink
Catch up with RunDeck 3.3.6 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdebisschop authored Nov 30, 2020
1 parent f5aec19 commit eee520f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static String[] remoteCommand(String[] command, String temp) {
String file = temp + ".pid; ";
// Prefix STDERR lines with STDERR_TOKEN to decode in logging step.
String cmd = String.join(" ", command);
String job = "( " + cmd + " ) 2> >(while read line;do echo \"" + STDERR_TOKEN + "$line\";done)";
String job = "( " + cmd + " ) 2> >(sed 's/^/" + STDERR_TOKEN + "/')";
// Note that bash is required to support adding a prefix token to STDERR.
return new String[]{ "bash", "-c", "printf $$ >>" + file + job + ";printf ' %s' $? >>" + file };
}
Expand All @@ -145,6 +145,11 @@ public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(code, reason);
}

@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
this.log(Constants.VERBOSE_LEVEL, reason);
}

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
this.log(Constants.ERR_LEVEL, t.getMessage());
Expand Down Expand Up @@ -397,9 +402,10 @@ public void logDockerStream(byte[] bytes) {
// If logging to RunDeck, we send lines beginning with STDERR_TOK to ERR_LEVEL.
// To do that, we make a BufferedReader and process it line-by-line in log function.
String nextMessage = new String(message.content.array(), StandardCharsets.UTF_8);
if (listener != null) {
if (null != listener) {
stringReader = new BufferedReader(new StringReader(nextMessage));
log(stringReader);
stringReader.close();
} else {
output.append(nextMessage);
}
Expand All @@ -408,6 +414,10 @@ public void logDockerStream(byte[] bytes) {
} catch (IOException e) {
log(ERR_LEVEL, e.getMessage());
}
if (output.length() > 0 && null != listener) {
log(currentOutputChannel, output.toString());
output = new StringBuilder();
}
}

/**
Expand All @@ -420,16 +430,19 @@ public void logDockerStream(byte[] bytes) {
private void log(BufferedReader stringReader) throws IOException {
String line;
while ((line = stringReader.readLine()) != null) {
if (output.length() > 0) {
log(currentOutputChannel, output.toString());
output = new StringBuilder();
}
if (line.startsWith(STDERR_TOKEN)) {
this.log(Constants.WARN_LEVEL, line.substring(STDERR_TOKEN_LENGTH) + "\n");
log(Constants.WARN_LEVEL, line.substring(STDERR_TOKEN_LENGTH));
} else if (line.contains(STDERR_TOKEN)) {
log(Constants.INFO_LEVEL, line.substring(0, line.indexOf(STDERR_TOKEN)));
log(Constants.WARN_LEVEL, line.substring(line.indexOf(STDERR_TOKEN) + STDERR_TOKEN_LENGTH));
} else {
this.log(Constants.INFO_LEVEL, line + "\n");
log(Constants.INFO_LEVEL, line);
}
}
if (output.length() > 0) {
listener.log(currentOutputChannel, output.toString());
}
output = new StringBuilder();
}

/**
Expand All @@ -440,18 +453,16 @@ private void log(BufferedReader stringReader) throws IOException {
* @param message The message to log.
*/
private void log(int level, String message) {
if (listener != null) {
if (currentOutputChannel == -1) {
currentOutputChannel = level;
} else if (currentOutputChannel != level) {
if (output.length() > 0) {
listener.log(currentOutputChannel, output.toString());
}
currentOutputChannel = level;
output = new StringBuilder();
}
if (null != listener && output.length() > 0) {
listener.log(currentOutputChannel, output.toString());
output = new StringBuilder();
}
currentOutputChannel = level;
if (null != listener) {
listener.log(level, message);
} else {
output.append(message);
}
output.append(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,65 @@ public void testLogDockerStream() {
doNothing().when(listener).log(anyInt(), anyString());
subject.logDockerStream(bytes);
// Buffer is designed to add a line feed at end of message.
verify(listener, times(1)).log(2, "6chars\n");
verify(listener, times(1)).log(2, "6chars");
}

@Test
public void testLogDockerStreamStderr() {
RancherWebSocketListener subject = new RancherWebSocketListener(listener, new StringBuilder());
byte[] bytes = (STDERR_TOKEN + "chars\n").getBytes();
byte[] bytes = (STDERR_TOKEN + "string1\n" + STDERR_TOKEN + "string2\n" + STDERR_TOKEN + "string3\n").getBytes();
doNothing().when(listener).log(anyInt(), anyString());
subject.logDockerStream(bytes);
// Buffer is designed to add a line feed at end of message.
verify(listener, times(1)).log(1, "chars\n");
verify(listener, times(1)).log(1, "string1");
verify(listener, times(1)).log(1, "string2");
verify(listener, times(1)).log(1, "string3");
}

@Test
public void testLogDockerStreamMixed() {
RancherWebSocketListener subject = new RancherWebSocketListener(listener, new StringBuilder());
byte[] bytes = (STDERR_TOKEN + "chars\nchars\n").getBytes();
byte[] bytes = (STDERR_TOKEN + "string1\nstring2\nstring3\n").getBytes();
doNothing().when(listener).log(anyInt(), anyString());
subject.logDockerStream(bytes);
// Buffer is designed to add a line feed at end of message.
verify(listener, times(1)).log(2, "chars\n");
verify(listener, times(1)).log(1, "string1");
verify(listener, times(1)).log(2, "string2");
verify(listener, times(1)).log(2, "string3");
}

@Test
public void testLogDockerStreamMixed2() {
RancherWebSocketListener subject = new RancherWebSocketListener(listener, new StringBuilder());
byte[] bytes = ("chars\n" + STDERR_TOKEN + "chars\n").getBytes();
byte[] bytes = ("string1\n" + STDERR_TOKEN + "string2\n").getBytes();
doNothing().when(listener).log(anyInt(), anyString());
subject.logDockerStream(bytes);
// Buffer is designed to add a line feed at end of message.
verify(listener, times(1)).log(2, "chars\n");
verify(listener, times(1)).log(2, "string1");
verify(listener, times(1)).log(1, "string2");
}

@Test
public void testLogDockerStreamMixed3() {
RancherWebSocketListener subject = new RancherWebSocketListener(listener, new StringBuilder());
byte[] bytes = ("string1" + STDERR_TOKEN + "string2\n").getBytes();
doNothing().when(listener).log(anyInt(), anyString());
subject.logDockerStream(bytes);
// Buffer is designed to add a line feed at end of message.
verify(listener, times(1)).log(2, "string1");
verify(listener, times(1)).log(1, "string2");
}

@Test
public void testLogDockerStreamMixed4() {
RancherWebSocketListener subject = new RancherWebSocketListener(listener, new StringBuilder());
byte[] bytes = ("string1" + STDERR_TOKEN + "string2\n" + STDERR_TOKEN + "string3").getBytes();
doNothing().when(listener).log(anyInt(), anyString());
subject.logDockerStream(bytes);
// Buffer is designed to add a line feed at end of message.
verify(listener, times(1)).log(2, "string1");
verify(listener, times(1)).log(1, "string2");
verify(listener, times(1)).log(1, "string3");
}

@Test
Expand Down

0 comments on commit eee520f

Please sign in to comment.