tmf: lttngControl: Separate STDERR from output and create errorOutput
authorJonathan Rajotte Julien <jonathan.rajotte-julien@ericsson.com>
Thu, 7 Aug 2014 18:16:04 +0000 (14:16 -0400)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Mon, 11 Aug 2014 14:58:40 +0000 (10:58 -0400)
Refactoring of current function to adjust to change.
Modifications to test scenarios to support new error output

Change-Id: I457aeed7ae6aba1ce880339c5a9b7f55c6a0e232
Signed-off-by: Jonathan Rajotte Julien <jonathan.rajotte-julien@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/31228
Tested-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
org.eclipse.linuxtools.lttng2.control.ui.tests/stubs/org/eclipse/linuxtools/internal/lttng2/control/stubs/shells/LTTngToolsFileShell.java
org.eclipse.linuxtools.lttng2.control.ui.tests/stubs/org/eclipse/linuxtools/internal/lttng2/control/stubs/shells/TestCommandShell.java
org.eclipse.linuxtools.lttng2.control.ui.tests/testfiles/CreateTreeTest2.cfg
org.eclipse.linuxtools.lttng2.control.ui.tests/testfiles/LTTngServiceTest.cfg
org.eclipse.linuxtools.lttng2.control.ui.tests/testfiles/ListInfoTest.cfg
org.eclipse.linuxtools.lttng2.control.ui/src/org/eclipse/linuxtools/internal/lttng2/control/ui/views/remote/CommandResult.java
org.eclipse.linuxtools.lttng2.control.ui/src/org/eclipse/linuxtools/internal/lttng2/control/ui/views/remote/CommandShell.java
org.eclipse.linuxtools.lttng2.control.ui/src/org/eclipse/linuxtools/internal/lttng2/control/ui/views/remote/ICommandResult.java
org.eclipse.linuxtools.lttng2.control.ui/src/org/eclipse/linuxtools/internal/lttng2/control/ui/views/service/LTTngControlService.java

index babebe29dad472daa4583043b3817c6d48394071..3f262b6c04e324cad5b96fafd551c5e4633ccb74 100644 (file)
@@ -42,6 +42,8 @@ public class LTTngToolsFileShell extends TestCommandShell {
     private final static String RESULT_KEY = "<COMMAND_RESULT>";
     private final static String OUTPUT_KEY = "<COMMAND_OUTPUT>";
     private final static String OUTPUT_END_KEY = "</COMMAND_OUTPUT>";
+    private final static String ERROR_OUTPUT_KEY = "<COMMAND_ERROR_OUTPUT>";
+    private final static String ERROR_OUTPUT_END_KEY = "</COMMAND_ERROR_OUTPUT>";
     private final static String COMMENT_KEY = "#.*";
 
     private final static Pattern LTTNG_LIST_SESSION_PATTERN =  Pattern.compile("lttng\\s+list\\s+(.+)");
@@ -58,32 +60,37 @@ public class LTTngToolsFileShell extends TestCommandShell {
 
     /**
      * Parse a scenario file with the format:
-     * <SCENARIO>
+     * <pre>
+     * &lt;SCENARIO&gt;
      * ScenarioName
      *
-     * <COMMAND_INPUT>
+     * &lt;COMMAND_INPUT&gt;
      * Command
-     * </COMAND_INPUT>
+     * &lt;/COMAND_INPUT&gt;
      *
-     * <COMMAND_RESULT>
+     * &lt;COMMAND_RESULT&gt;
      * CommandResult
-     * </COMMAND_RESULT>
+     * &lt;/COMMAND_RESULT&gt;
      *
-     * <COMMAND_OUTPUT>
+     * &lt;COMMAND_OUTPUT&gt;
      * CommandOutput
-     * </COMMAND_OUTPUT>
+     * &lt;COMMAND_ERROR_OUTPUT&gt;
+     * CommandErrorOutput
+     * &lt;/COMMAND_ERROR_OUTPUT&gt;
+     * &lt;/COMMAND_OUTPUT&gt;
      *
-     * </SCENARIO>
+     * &lt;/SCENARIO&gt;
      *
      * Where: ScenarioName - is the scenario name
      *        Command - the command line string
      *        CommandResult - the result integer of the command (0 for success, 1 for failure)
      *        ComandOutput - the command output string (multi-line possible)
+     *        ComandErrorOutput - the command error output string (multi-line possible)
      *
      * Note: 1) There can be many scenarios per file
      *       2) There can be many (Command-CommandResult-CommandOutput) triples per scenario
      *       3) Lines starting with # will be ignored (comments)
-     *
+     * <pre>
      * @param scenariofile - path to scenario file
      * @throws Exception
      */
@@ -132,8 +139,10 @@ public class LTTngToolsFileShell extends TestCommandShell {
                     Map<String, ICommandResult> commandMap = new HashMap<>();
                     fScenarioMap.put(scenario, commandMap);
                     List<String> output = null;
+                    List<String> errorOutput = null;
                     String input = null;
                     boolean inOutput = false;
+                    boolean inErrorOutput = false;
                     int result = 0;
                     tmpSessionNameMap.clear();
                     while ((strLine = br.readLine()) != null) {
@@ -172,6 +181,7 @@ public class LTTngToolsFileShell extends TestCommandShell {
                         } else if (INPUT_END_KEY.equals(strLine)) {
                             // Initialize output array
                             output = new ArrayList<>();
+                            errorOutput = new ArrayList<>();
                         } else if (RESULT_KEY.equals(strLine)) {
                             strLine = br.readLine();
                             // Ignore comments
@@ -182,25 +192,26 @@ public class LTTngToolsFileShell extends TestCommandShell {
                             result = Integer.parseInt(strLine);
                         } else if (OUTPUT_END_KEY.equals(strLine)) {
                             // Save output/result in command map
-                            if (output != null) {
-                                commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()])));
+                            if (output != null && errorOutput != null) {
+                                commandMap.put(input, new CommandResult(result, output.toArray(new String[output.size()]), errorOutput.toArray(new String[errorOutput.size()])));
                             }
                             inOutput = false;
                         } else if (OUTPUT_KEY.equals(strLine)) {
                             // first line of output
                             inOutput = true;
-                            strLine = br.readLine();
-
-                            // Ignore comments
+                        } else if (ERROR_OUTPUT_KEY.equals(strLine)) {
+                            // first line of output
+                            inErrorOutput = true;
+                        } else if (ERROR_OUTPUT_END_KEY.equals(strLine)) {
+                            inErrorOutput = false;
+                        } else if (inOutput) {
                             while (isComment(strLine)) {
                                 strLine = br.readLine();
                             }
-                            if (output != null) {
-                                output.add(strLine);
-                            }
-                        } else if (inOutput) {
-                            // subsequent lines of output
-                            if (output != null) {
+                            // lines of output/error output
+                            if (errorOutput != null && inErrorOutput) {
+                                errorOutput.add(strLine);
+                            } else if (output != null) {
                                 output.add(strLine);
                             }
                         }
@@ -247,9 +258,10 @@ public class LTTngToolsFileShell extends TestCommandShell {
 
         String[] output = new String[1];
         output[0] = String.valueOf("Command not found");
-        CommandResult result = new CommandResult(0, null);
+        CommandResult result = new CommandResult(0, null, null);
         // For verification of setters of class CommandResult
         result.setOutput(output);
+        result.setErrorOutput(output);
         result.setResult(1);
         return result;
    }
index 86b39e2dada2d503f59d3126204e1505854531e2..998e72eb300b37a8e9232f0f322666d33e273e55 100644 (file)
@@ -45,6 +45,6 @@ public class TestCommandShell implements ICommandShell {
         if (fIsConnected) {
 
         }
-        return new CommandResult(0, new String[0]);
+        return new CommandResult(0, new String[0], new String[0]);
     }
 }
index dde430a336ac8baecf5103e8c13404c4c0f77fbe..929dc44a25e1aead1d24313bdab00893d13606c8 100644 (file)
@@ -280,7 +280,9 @@ lttng enable-channel mychannel -u  -s mysession -C 1024 -W 10
 0
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Warning: Tracefile size rounded up from (1024) to subbuffer size (8388608)
+</COMMAND_ERROR_OUTPUT>
 UST channel mychannel enabled for session mysession
 </COMMAND_OUTPUT>
 
index 22a69e988c1b89b8014ba0fb22c34b2d19e52007..9d519f92eb213f8a5e05441554640ee094b39f93 100644 (file)
@@ -21,7 +21,9 @@ lttng list
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Command not found
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
 
@@ -151,7 +153,9 @@ lttng list test
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
 Session test not found
+<COMMAND_ERROR_OUTPUT>
 Error: Session name not found
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
 
@@ -169,8 +173,10 @@ lttng -vvv  list test
 <COMMAND_OUTPUT>
 DEBUG2: Session name: test [in cmd_list() at commands/list.c:618]
 DEBUG1: Session count 1 [in list_sessions() at commands/list.c:485]
+<COMMAND_ERROR_OUTPUT>
 Error: Session 'test' not found
 Error: Command error
+</COMMAND_ERROR_OUTPUT>
 DEBUG1: Clean exit [in clean_exit() at lttng.c:165]
 </COMMAND_OUTPUT>
 </SCENARIO>
@@ -319,7 +325,9 @@ lttng list -k
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
 Spawning session daemon
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list kernel events
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
 
@@ -335,7 +343,9 @@ lttng list -k
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list kernel events
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
 
@@ -353,7 +363,9 @@ lttng -vvv  list -k
 <COMMAND_OUTPUT>
 DEBUG2: Session name: (null) [in cmd_list() at commands/list.c:618]
 DEBUG1: Getting kernel tracing events [in list_kernel_events() at commands/list.c:309]
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list kernel events
+</COMMAND_ERROR_OUTPUT>
 DEBUG1: Clean exit [in clean_exit() at lttng.c:165]
 </COMMAND_OUTPUT>
 </SCENARIO>
@@ -435,8 +447,10 @@ lttng list -u -f
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
 Spawning a session daemon
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list UST events: Listing UST events failed
 Error: Command Error
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
 
@@ -452,8 +466,10 @@ lttng list -u -f
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list UST events: Listing UST events failed
 Error: Command Error
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
 
@@ -472,8 +488,10 @@ lttng -vvv  list -u -f
 DEBUG2: Session name: (null) [in cmd_list() at commands/list.c:618]
 DEBUG1: Getting kernel tracing events [in list_kernel_events() at commands/list.c:309]
 Spawning a session daemon
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list UST events: Listing UST events failed
 Error: Command Error
+</COMMAND_ERROR_OUTPUT>
 DEBUG1: Clean exit [in clean_exit() at lttng.c:165]
 </COMMAND_OUTPUT>
 </SCENARIO>
@@ -559,7 +577,9 @@ lttng create alreadyExist
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: Session name already exist
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 
 <COMMAND_INPUT>
@@ -1271,7 +1291,9 @@ lttng snapshot list-output  -s blabla
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: Session name not found
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 #------------------------------------------------------------------------------
 #next is not an error case but good to be tested
@@ -1293,7 +1315,9 @@ lttng snapshot record  -s blabla
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
     Error: Session name not found
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 #------------------------------------------------------------------------------
 <COMMAND_INPUT>
@@ -1303,7 +1327,9 @@ lttng snapshot record  -s mysession
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
     Error: Session needs to be started once
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 
 </SCENARIO>
@@ -1361,8 +1387,10 @@ lttng create mysession --live --snapshot
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: Snapshot and live modes are mutually exclusive.
 Error: Command error
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 #------------------------------------------------------------------------------
 <COMMAND_INPUT>
@@ -1372,9 +1400,11 @@ lttng create mysession --live -U blah
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: URI parse unknown protocol blah
 Error: Unable to parse the URL blah
 Error: Invalid parameter
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 #------------------------------------------------------------------------------
 <COMMAND_INPUT>
@@ -1384,7 +1414,9 @@ lttng create mysession --live -C net://127.0.0.1
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: You need both control and data URL.
 Error: Command error
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 </SCENARIO>
\ No newline at end of file
index e7abfcd91245d822602987d8b779128857d990d8..4329c6f5bc88a0d10a248646dd730b01b5a3234d 100644 (file)
@@ -215,7 +215,9 @@ lttng list -k
 1
 </COMMAND_RESULT>
 <COMMAND_OUTPUT>
+<COMMAND_ERROR_OUTPUT>
 Error: Unable to list kernel events
+</COMMAND_ERROR_OUTPUT>
 </COMMAND_OUTPUT>
 
 <COMMAND_INPUT>
index 3ae6cd0af411648dde11616c4d593b8cc5c704ac..9b7c0b545d8da94a623c8cd56c9738c8da00e16f 100644 (file)
@@ -34,6 +34,7 @@ public class CommandResult implements ICommandResult {
      * The output as String array.
      */
     private String[] fOutput = new String[0];
+    private String[] fErrorOutput = new String[0];
 
     // ------------------------------------------------------------------------
     // Constructor
@@ -46,12 +47,17 @@ public class CommandResult implements ICommandResult {
      *            The result of the command
      * @param output
      *            The output, as an array of strings
+     * @param errorOutput
+     *            THe error output as an array of strings
      */
-    public CommandResult(int result, String[] output) {
+    public CommandResult(int result, String[] output, String[] errorOutput) {
         fResult = result;
         if (output != null) {
             fOutput = Arrays.copyOf(output, output.length);
         }
+        if (errorOutput != null) {
+            fErrorOutput = Arrays.copyOf(errorOutput, errorOutput.length);
+        }
     }
 
     // ------------------------------------------------------------------------
@@ -80,4 +86,17 @@ public class CommandResult implements ICommandResult {
             fOutput = Arrays.copyOf(output, output.length);
         }
     }
+
+    @Override
+    public String[] getErrorOutput() {
+        return Arrays.copyOf(fErrorOutput, fErrorOutput.length);
+    }
+
+    @Override
+    public void setErrorOutput(String[] output) {
+        fErrorOutput = new String[0];
+        if (output != null) {
+            fErrorOutput = Arrays.copyOf(output, output.length);
+        }
+    }
 }
\ No newline at end of file
index 2a94a9c1f1c4dd7c90835c68156ac230481acd87..04d3938b3c2caf481e66a3acaa31a53aec369f68 100644 (file)
@@ -137,6 +137,7 @@ public class CommandShell implements ICommandShell {
                 @Override
                 public CommandResult call() throws IOException, CancellationException {
                     final ArrayList<String> result = new ArrayList<>();
+                    final ArrayList<String> errorResult = new ArrayList<>();
 
                     synchronized (fHostShell) {
                         // Initialize return value which will be updated in isAliasEchoResult()
@@ -189,12 +190,12 @@ public class CommandShell implements ICommandShell {
                         if (fReturnValue != 0) {
                             while(fErrorBufferReader.ready()) {
                                 if ((nextLine = fErrorBufferReader.readLine()) != null)  {
-                                    result.add(nextLine);
+                                    errorResult.add(nextLine);
                                 }
                             }
                         }
                     }
-                    return new CommandResult(fReturnValue, result.toArray(new String[result.size()]));
+                    return new CommandResult(fReturnValue, result.toArray(new String[result.size()]), errorResult.toArray(new String[errorResult.size()]));
                 }
             });
 
index 3b23d358b06d287e567565998bb202e33ebb684b..be1d2abe33539913d93750436ba207aa501ff795 100644 (file)
@@ -46,4 +46,19 @@ public interface ICommandResult {
      *            The output (as an array of Strings) to assign
      */
     void setOutput(String[] output);
+
+    /**
+     * The error output of the command.
+     *
+     * @return returns the command error output.
+     */
+    String[] getErrorOutput();
+
+    /**
+     * Sets the command output.
+     *
+     * @param output
+     *            The output (as an array of Strings) to assign
+     */
+    void setErrorOutput(String[] output);
 }
\ No newline at end of file
index 9af28be07a1bf8811adb45a4fdd75acfa1cec911..bcce1d163c6b0d0b5d1fdfb318b63cdffea93231 100644 (file)
@@ -296,7 +296,7 @@ public class LTTngControlService implements ILttngControlService {
 
         List<IBaseEventInfo> events = new ArrayList<>();
 
-        if (result.getOutput() != null) {
+        if (result.getErrorOutput() != null) {
             // Ignore the following 2 cases:
             // Spawning a session daemon
             // Error: Unable to list kernel events
@@ -304,8 +304,8 @@ public class LTTngControlService implements ILttngControlService {
             // Error: Unable to list kernel events
             //
             int index = 0;
-            while (index < result.getOutput().length) {
-                String line = result.getOutput()[index];
+            while (index < result.getErrorOutput().length) {
+                String line = result.getErrorOutput()[index];
                 Matcher matcher = LTTngControlServiceConstants.LIST_KERNEL_NO_KERNEL_PROVIDER_PATTERN.matcher(line);
                 if (matcher.matches()) {
                     return events;
@@ -347,7 +347,7 @@ public class LTTngControlService implements ILttngControlService {
             return allProviders;
         }
 
-        if (result.getOutput() != null) {
+        if (result.getErrorOutput() != null) {
             // Ignore the following 2 cases:
             // Spawning a session daemon
             // Error: Unable to list UST events: Listing UST events failed
@@ -355,8 +355,8 @@ public class LTTngControlService implements ILttngControlService {
             // Error: Unable to list UST events: Listing UST events failed
             //
             int index = 0;
-            while (index < result.getOutput().length) {
-                String line = result.getOutput()[index];
+            while (index < result.getErrorOutput().length) {
+                String line = result.getErrorOutput()[index];
                 Matcher matcher = LTTngControlServiceConstants.LIST_UST_NO_UST_PROVIDER_PATTERN.matcher(line);
                 if (matcher.matches()) {
                     return allProviders;
@@ -575,13 +575,13 @@ public class LTTngControlService implements ILttngControlService {
         StringBuffer command = createCommand(LTTngControlServiceConstants.COMMAND_DESTROY_SESSION, newName);
 
         ICommandResult result = executeCommand(command.toString(), monitor, false);
-        String[] output = result.getOutput();
+        String[] errorOutput = result.getErrorOutput();
 
         boolean isError = isError(result);
-        if (isError && (output != null)) {
+        if (isError && (errorOutput != null)) {
             int index = 0;
-            while (index < output.length) {
-                String line = output[index];
+            while (index < errorOutput.length) {
+                String line = errorOutput[index];
                 Matcher matcher = LTTngControlServiceConstants.SESSION_NOT_FOUND_ERROR_PATTERN.matcher(line);
                 if (matcher.matches()) {
                     // Don't treat this as an error
@@ -1032,14 +1032,15 @@ public class LTTngControlService implements ILttngControlService {
      */
     protected boolean isError(ICommandResult result) {
         // Check return code and length of returned strings
-        if ((result.getResult()) != 0 || (result.getOutput().length < 1)) {
+
+        if ((result.getResult()) != 0) {
             return true;
         }
 
         // Look for error pattern
         int index = 0;
-        while (index < result.getOutput().length) {
-            String line = result.getOutput()[index];
+        while (index < result.getErrorOutput().length) {
+            String line = result.getErrorOutput()[index];
             Matcher matcher = LTTngControlServiceConstants.ERROR_PATTERN.matcher(line);
             if (matcher.matches()) {
                 return true;
@@ -1058,10 +1059,11 @@ public class LTTngControlService implements ILttngControlService {
      * @return - the formatted output
      */
     public static String formatOutput(ICommandResult result) {
-        if ((result == null) || result.getOutput() == null || result.getOutput().length == 0) {
+        if ((result == null) || ((result.getOutput() == null || result.getOutput().length == 0) && (result.getErrorOutput() == null || result.getErrorOutput().length == 0))) {
             return ""; //$NON-NLS-1$
         }
         String[] output = result.getOutput();
+        String[] errorOutput = result.getErrorOutput();
         StringBuffer ret = new StringBuffer();
         ret.append("Return Value: "); //$NON-NLS-1$
         ret.append(result.getResult());
@@ -1069,6 +1071,10 @@ public class LTTngControlService implements ILttngControlService {
         for (int i = 0; i < output.length; i++) {
             ret.append(output[i]).append("\n"); //$NON-NLS-1$
         }
+        ret.append("Error stream:\n"); //$NON-NLS-1$
+        for (int i = 0; i < errorOutput.length; i++) {
+           ret.append(errorOutput[i]).append("\n"); //$NON-NLS-1$
+        }
         return ret.toString();
     }
 
This page took 0.036439 seconds and 5 git commands to generate.