From eee520f4ba8da959a83ba16a66ca8948470dc0db Mon Sep 17 00:00:00 2001 From: Karl DeBisschop Date: Mon, 30 Nov 2020 11:15:19 -0500 Subject: [PATCH] Catch up with RunDeck 3.3.6 (#60) --- .../rancher/RancherWebSocketListener.java | 49 ++++++++++++------- .../rancher/RancherWebSocketListenerTest.java | 42 +++++++++++++--- 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/bioraft/rundeck/rancher/RancherWebSocketListener.java b/src/main/java/com/bioraft/rundeck/rancher/RancherWebSocketListener.java index aa46e8f..bf33173 100644 --- a/src/main/java/com/bioraft/rundeck/rancher/RancherWebSocketListener.java +++ b/src/main/java/com/bioraft/rundeck/rancher/RancherWebSocketListener.java @@ -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 }; } @@ -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()); @@ -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); } @@ -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(); + } } /** @@ -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(); } /** @@ -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); } } \ No newline at end of file diff --git a/src/test/java/com/bioraft/rundeck/rancher/RancherWebSocketListenerTest.java b/src/test/java/com/bioraft/rundeck/rancher/RancherWebSocketListenerTest.java index 747a6d6..0a97f2d 100644 --- a/src/test/java/com/bioraft/rundeck/rancher/RancherWebSocketListenerTest.java +++ b/src/test/java/com/bioraft/rundeck/rancher/RancherWebSocketListenerTest.java @@ -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