Isolate the functions for executing shell commands in a new ShellUtils
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 29 Jul 2015 23:25:51 +0000 (19:25 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 29 Jul 2015 23:25:51 +0000 (19:25 -0400)
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
src/test/java/org/lttng/ust/agent/utils/LttngSession.java
src/test/java/org/lttng/ust/agent/utils/MiscTestUtils.java
src/test/java/org/lttng/ust/agent/utils/ShellUtils.java [new file with mode: 0644]

index 8869ef5da8c901acac2de140b3ee63772d394aa2..9d0b4ab52234aa94a19a31b3efce6e0212f0d924 100644 (file)
@@ -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<String> 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<String> command = Arrays.asList("ls", "-l");
-        executeCommand(command);
-    }
-
-    private static boolean executeCommand(List<String> 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;
-        }
-    }
 }
index fe717367e9996668f94e2a882a50334c2237bf91..cfd4a8e47c2f5de5796120c6324d188620d95e1f 100644 (file)
 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<String> command = Arrays.asList("ps", "-e", "u");
-        List<String> output = getOutputFromCommand(false, command);
+        List<String> 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<String> command = Arrays.asList("ps", "-e", "u");
-        List<String> output = getOutputFromCommand(false, command);
+        List<String> output = ShellUtils.getOutputFromCommand(false, command);
         return output.stream()
                 .filter(s -> s.contains("lttng-sessiond"))
                 .anyMatch(s -> s.startsWith("root"));
     }
 
-
-    static List<String> getOutputFromCommand(List<String> command) {
-        return MiscTestUtils.getOutputFromCommand(true, command);
-    }
-
-    static List<String> getOutputFromCommand(boolean print, List<String> 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<String> 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 (file)
index 0000000..48c7fd5
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
+ *
+ * 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<String> 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<String> 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<String> getOutputFromCommand(boolean print, List<String> 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<String> 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;
+        }
+    }
+}
This page took 0.027514 seconds and 4 git commands to generate.