From: Alexandre Montplaisir Date: Wed, 29 Jul 2015 23:25:51 +0000 (-0400) Subject: Isolate the functions for executing shell commands in a new ShellUtils X-Git-Url: http://git.liburcu.org/?a=commitdiff_plain;h=7dfd1adfc507b63e62004efd3a9a74ae7d1f0d3e;p=lttng-ust-java-tests.git Isolate the functions for executing shell commands in a new ShellUtils Signed-off-by: Alexandre Montplaisir --- diff --git a/src/test/java/org/lttng/ust/agent/utils/LttngSession.java b/src/test/java/org/lttng/ust/agent/utils/LttngSession.java index 8869ef5..9d0b4ab 100644 --- a/src/test/java/org/lttng/ust/agent/utils/LttngSession.java +++ b/src/test/java/org/lttng/ust/agent/utils/LttngSession.java @@ -18,8 +18,9 @@ package org.lttng.ust.agent.utils; +import static org.lttng.ust.agent.utils.ShellUtils.executeCommand; + import java.io.IOException; -import java.lang.ProcessBuilder.Redirect; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -28,7 +29,6 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.List; -import java.util.StringJoiner; import java.util.UUID; import java.util.stream.Collectors; @@ -182,7 +182,7 @@ public class LttngSession implements AutoCloseable { * @return The output of Babeltrace on the session's current trace */ public List view() { - return MiscTestUtils.getOutputFromCommand(Arrays.asList("lttng", "view", sessionName)); + return ShellUtils.getOutputFromCommand(true, Arrays.asList("lttng", "view", sessionName)); } /** @@ -232,38 +232,4 @@ public class LttngSession implements AutoCloseable { return true; } - /** - * Simple command to test that the environment / stdout are working - * correctly. - * - * @param args - * Command-line arguments - */ - public static void main(String[] args) { - List command = Arrays.asList("ls", "-l"); - executeCommand(command); - } - - private static boolean executeCommand(List command) { - try { - /* "echo" the command to stdout */ - StringJoiner sj = new StringJoiner(" ", "$ ", ""); - command.stream().forEach(sj::add); - System.out.println(sj.toString()); - - ProcessBuilder builder = new ProcessBuilder(command); - builder.redirectErrorStream(true); - builder.redirectOutput(Redirect.INHERIT); - - Process p = builder.start(); - int ret = p.waitFor(); - - System.out.println("(returned from command)"); - - return (ret == 0); - - } catch (IOException | InterruptedException e) { - return false; - } - } } diff --git a/src/test/java/org/lttng/ust/agent/utils/MiscTestUtils.java b/src/test/java/org/lttng/ust/agent/utils/MiscTestUtils.java index fe71736..cfd4a8e 100644 --- a/src/test/java/org/lttng/ust/agent/utils/MiscTestUtils.java +++ b/src/test/java/org/lttng/ust/agent/utils/MiscTestUtils.java @@ -19,12 +19,8 @@ package org.lttng.ust.agent.utils; import java.io.IOException; -import java.lang.ProcessBuilder.Redirect; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import java.util.List; -import java.util.StringJoiner; import org.lttng.ust.agent.jul.LttngLogHandler; import org.lttng.ust.agent.log4j.LttngLogAppender; @@ -105,7 +101,7 @@ public final class MiscTestUtils { String shortUserName = userName.substring(0, Math.min(userName.length(), 7)); List command = Arrays.asList("ps", "-e", "u"); - List output = getOutputFromCommand(false, command); + List output = ShellUtils.getOutputFromCommand(false, command); return output.stream() .filter(s -> s.contains("lttng-sessiond")) .anyMatch(s -> s.startsWith(shortUserName)); @@ -119,48 +115,10 @@ public final class MiscTestUtils { */ public static boolean checkForRootSessiond() { List command = Arrays.asList("ps", "-e", "u"); - List output = getOutputFromCommand(false, command); + List output = ShellUtils.getOutputFromCommand(false, command); return output.stream() .filter(s -> s.contains("lttng-sessiond")) .anyMatch(s -> s.startsWith("root")); } - - static List getOutputFromCommand(List command) { - return MiscTestUtils.getOutputFromCommand(true, command); - } - - static List getOutputFromCommand(boolean print, List command) { - try { - /* "echo" the command to stdout */ - StringJoiner sj = new StringJoiner(" ", "$ ", ""); - command.stream().forEach(sj::add); - System.out.println(sj.toString()); - - Path tempFile = Files.createTempFile("test-output", null); - - ProcessBuilder builder = new ProcessBuilder(command); - builder.redirectErrorStream(true); - builder.redirectOutput(Redirect.to(tempFile.toFile())); - - Process p = builder.start(); - p.waitFor(); - - List lines = Files.readAllLines(tempFile); - Files.delete(tempFile); - - if (print) { - /* Also print the output to the console */ - lines.stream().forEach(System.out::println); - } else { - System.out.println("(output silenced)"); - } - - System.out.println("(returned from command)"); - return lines; - - } catch (IOException | InterruptedException e) { - return null; - } - } } diff --git a/src/test/java/org/lttng/ust/agent/utils/ShellUtils.java b/src/test/java/org/lttng/ust/agent/utils/ShellUtils.java new file mode 100644 index 0000000..48c7fd5 --- /dev/null +++ b/src/test/java/org/lttng/ust/agent/utils/ShellUtils.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.lttng.ust.agent.utils; + +import java.io.IOException; +import java.lang.ProcessBuilder.Redirect; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.StringJoiner; + +/** + * Utility methods to execute commands on the command line. + * + * @author Alexandre Montplaisir + */ +public final class ShellUtils { + + private ShellUtils() {} + + /** + * Simple command to test that the environment / stdout are working + * correctly. + * + * @param args + * Command-line arguments + */ + public static void main(String[] args) { + List command = Arrays.asList("ls", "-l"); + executeCommand(command); + } + + /** + * Execute a shell command and retrieve its return value. + * + * @param command + * The command to execute, as a list of individual arguments (do + * not use spaces) + * @return If the command returned successfully (ret code = 0) + */ + public static boolean executeCommand(List command) { + try { + /* "echo" the command to stdout */ + StringJoiner sj = new StringJoiner(" ", "$ ", ""); + command.stream().forEach(sj::add); + System.out.println(sj.toString()); + + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectErrorStream(true); + builder.redirectOutput(Redirect.INHERIT); + + Process p = builder.start(); + int ret = p.waitFor(); + + System.out.println("(returned from command)"); + + return (ret == 0); + + } catch (IOException | InterruptedException e) { + return false; + } + } + + /** + * Execute a shell command and retrieve its output. + * + * @param print + * Should the output also be printed to stdout as usual + * @param command + * The command to execute, as a list of individual arguments (do + * not use spaces) + * @return The output of the command, as one list element per line + */ + public static List getOutputFromCommand(boolean print, List command) { + try { + /* "echo" the command to stdout */ + StringJoiner sj = new StringJoiner(" ", "$ ", ""); + command.stream().forEach(sj::add); + System.out.println(sj.toString()); + + Path tempFile = Files.createTempFile("test-output", null); + + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectErrorStream(true); + builder.redirectOutput(Redirect.to(tempFile.toFile())); + + Process p = builder.start(); + p.waitFor(); + + List lines = Files.readAllLines(tempFile); + Files.delete(tempFile); + + if (print) { + /* Also print the output to the console */ + lines.stream().forEach(System.out::println); + } else { + System.out.println("(output silenced)"); + } + + System.out.println("(returned from command)"); + return lines; + + } catch (IOException | InterruptedException e) { + return null; + } + } +}