NIFI-2905: Log error stream of ExecuteProcess cmd

ExecuteProcess ignores error stream when Redirect Error Stream is
false, this commit let it to be logged instead so that user can see it on
bulletin.

This closes #1143.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
This commit is contained in:
Koji Kawamura 2016-10-17 17:07:32 +09:00 committed by Andy LoPresto
parent f287bc9119
commit 27dba60f27
No known key found for this signature in database
GPG Key ID: 3C6EF65B2F7DEF69
2 changed files with 47 additions and 2 deletions

View File

@ -316,8 +316,7 @@ public class ExecuteProcess extends AbstractProcessor {
@Override
public void run() {
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(externalProcess.getErrorStream()))) {
while (reader.read() >= 0) {
}
reader.lines().filter(line -> line != null && line.length() > 0).forEach(getLogger()::warn);
} catch (final IOException ioe) {
}
}

View File

@ -30,6 +30,7 @@ import java.util.concurrent.ExecutorService;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processors.standard.util.ArgumentUtils;
import org.apache.nifi.util.LogMessage;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
@ -205,4 +206,49 @@ public class TestExecuteProcess {
// assertEquals(inFile.length(), totalFlowFilesSize);
}
@Test
public void testNotRedirectErrorStream() {
final TestRunner runner = TestRunners.newTestRunner(ExecuteProcess.class);
runner.setProperty(ExecuteProcess.COMMAND, "cd");
runner.setProperty(ExecuteProcess.COMMAND_ARGUMENTS, "does-not-exist");
ProcessContext processContext = runner.getProcessContext();
ExecuteProcess processor = (ExecuteProcess) runner.getProcessor();
processor.updateScheduledTrue();
processor.setupExecutor(processContext);
processor.onTrigger(processContext, runner.getProcessSessionFactory());
final List<LogMessage> warnMessages = runner.getLogger().getWarnMessages();
assertEquals("If redirect error stream is false, " +
"the output should be logged as a warning so that user can notice on bulletin.", 1, warnMessages.size());
final List<MockFlowFile> succeeded = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS);
assertEquals(0, succeeded.size());
}
@Test
public void testRedirectErrorStream() {
final TestRunner runner = TestRunners.newTestRunner(ExecuteProcess.class);
runner.setProperty(ExecuteProcess.COMMAND, "cd");
runner.setProperty(ExecuteProcess.COMMAND_ARGUMENTS, "does-not-exist");
runner.setProperty(ExecuteProcess.REDIRECT_ERROR_STREAM, "true");
ProcessContext processContext = runner.getProcessContext();
ExecuteProcess processor = (ExecuteProcess) runner.getProcessor();
processor.updateScheduledTrue();
processor.setupExecutor(processContext);
processor.onTrigger(processContext, runner.getProcessSessionFactory());
final List<LogMessage> warnMessages = runner.getLogger().getWarnMessages();
assertEquals("If redirect error stream is true " +
"the output should be sent as a content of flow-file.", 0, warnMessages.size());
final List<MockFlowFile> succeeded = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS);
assertEquals(1, succeeded.size());
}
}